隨機游走

Python 投資組合風險管理入門

Dakota Wixom

Quantitative Analyst | QuantCourse.com

隨機游走

在財務中,隨機游走通常比物理中的模型更簡單:

Python 投資組合風險管理入門

用 Python 進行隨機游走

假設你有一個物件 StockReturns,是股票報酬率的時間序列。

要模擬隨機游走:

mu = np.mean(StockReturns)
std = np.std(StockReturns)
T = 252
S0 = 10
rand_rets = np.random.normal(mu, std, T) + 1
forecasted_values = S0 * (rand_rets.cumprod())
forecasted_values
array([ 9.71274884,  9.72536923, 10.03605425 ... ])
Python 投資組合風險管理入門

Monte Carlo 模擬

單一資產的多次 Monte Carlo 模擬,初始股價為 $10(T0),預測 1 年(x 軸為 252 個交易日):

Python 投資組合風險管理入門

Python 中的 Monte Carlo VaR

計算 100 次 Monte Carlo 模擬的 VaR(95):

mu = 0.0005
vol = 0.001
 T = 252
sim_returns = []
for i in range(100):
    rand_rets = np.random.normal(mu, vol, T)
    sim_returns.append(rand_rets)
var_95 = np.percentile(sim_returns, 5)
var_95
-0.028
Python 投資組合風險管理入門

一起來練習吧!

Python 投資組合風險管理入門

Preparing Video For Download...