開始使用免費開始

隨機漫步模擬

隨機(stochastic)或隨機性移動在物理學中用來表示粒子與流體的運動,在數學中用來描述碎形行為,在金融學中則用來描述股市的走勢。

使用 np.random.normal() 函式,為 USO 原油 ETF 建立隨機漫步模型,假設整個 T 個交易日期間具有固定的日平均報酬(mu)與平均日波動率(vol)。

本練習屬於課程

Python 投資組合風險管理入門

檢視課程

練習說明

  • 將模擬天數(T)設為 252,初始股價(S0)設為 10。
  • 使用 np.random.normal() 計算 T 個常態隨機值,將 muvolT 作為參數,然後每個值加上 1,指定給 rand_rets
  • 計算隨機漫步:將 rand_rets.cumprod() 乘上初始股價,並指定給 forecasted_values

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Set the simulation parameters
mu = np.mean(StockReturns)
vol = np.std(StockReturns)
T = ____
S0 = ____

# Add one to the random returns
rand_rets = ____ + 1

# Forecasted random walk
forecasted_values = ____

# Plot the random walk
plt.plot(range(0, T), forecasted_values)
plt.show()
編輯並執行程式碼