蒙特卡洛风险模拟 — 投资组合结果建模
蒙特卡洛模拟可让您建模10万种以上投资组合的未来潜在路径,量化风险指标如风险价值(VaR)和预期缺口。您将看到可能性的分布,而非单一结果假设。
示例: 对交易策略运行10万次模拟。您将看到:68%的结果盈利10-50美元,25%亏损10-30美元,5%亏损50-200美元,2%盈利100美元以上。现在您理解了风险概况。
基础几何布朗运动(GBM)
将未来价格建模为带漂移和波动率的随机游走:
Python — 蒙特卡洛价格路径
import numpy as np
def simulate_price_paths(S0, mu, sigma, T, dt, n_sims=10000):
# S0: 当前价格, mu: 漂移率, sigma: 波动率
# T: 时间周期(天), dt: 时间步长, n_sims: 模拟次数
steps = int(T / dt)
paths = np.zeros((n_sims, steps))
paths[:, 0] = S0
for i in range(1, steps):
# dS = mu*S*dt + sigma*S*sqrt(dt)*Z
Z = np.random.normal(0, 1, n_sims)
dS = mu * paths[:, i-1] * dt + sigma * paths[:, i-1] * np.sqrt(dt) * Z
paths[:, i] = paths[:, i-1] + dS
return paths
投资组合风险指标
风险价值(VaR)
仅在5%情景下会超出的损失(95%置信度):
Python — 计算VaR
def calculate_var(simulations, confidence=0.95):
pnl = simulations[:, -1] - simulations[:, 0]
var = np.percentile(pnl, (1 - confidence) * 100)
return var # 例如:-$2,340
预期缺口(CVaR)
最差5%情况下的平均损失:
Python — 计算CVaR
def calculate_cvar(simulations, confidence=0.95):
pnl = simulations[:, -1] - simulations[:, 0]
var_threshold = np.percentile(pnl, (1 - confidence) * 100)
worst_5_pct = pnl[pnl <= var_threshold]
cvar = np.mean(worst_5_pct)
return cvar # 例如:-$3,800(比VaR更糟糕)
策略特定模拟
模拟实际交易规则,而不仅是价格路径:
Python — 策略蒙特卡洛
def simulate_strategy(capital, win_rate, avg_win, avg_loss, trades_per_day=10, days=30, n_sims=10000):
results = np.zeros(n_sims)
for sim in range(n_sims):
equity = capital
n_trades = trades_per_day * days
for _ in range(n_trades):
if np.random.rand() < win_rate:
pnl = np.random.normal(avg_win, avg_win * 0.2)
else:
pnl = -np.random.normal(avg_loss, avg_loss * 0.2)
equity += pnl
if equity < capital * 0.5:
break # 亏损50%时止损
results[sim] = equity
return results
基于Smart Money信号的压力测试
当Smart Money信心下降时建模最坏情景:
Python — 压力测试情景
def stress_test_scenarios(base_win_rate):
# 情景1: 正常条件(高置信度)
scenario_normal = simulate_strategy(10000, win_rate=base_win_rate)
# 情景2: 信号恶化(中置信度)
scenario_medium = simulate_strategy(10000, win_rate=base_win_rate * 0.95)
# 情景3: 崩溃(VETO条件)
scenario_veto = simulate_strategy(10000, win_rate=0.45) # 负优势
return {
'normal': {'var': np.percentile(scenario_normal, 5), 'mean': np.mean(scenario_normal)},
'medium': {'var': np.percentile(scenario_medium, 5), 'mean': np.mean(scenario_medium)},
'veto': {'var': np.percentile(scenario_veto, 5), 'mean': np.mean(scenario_veto)}
}
结果解读
通过1万次模拟,您将了解:
- 中位结果(50百分位)
- 最佳情况(95百分位)
- 最差情况(5百分位)
- 破产概率(权益<0)
- 预期回撤时长