Jus*_*bat 2 python statistics scipy quantitative-finance pandas
鉴于历史日收益率,我如何计算单个股票头寸的投资组合分配,基于21天内不会损失超过起始投资组合价值的10%?(95%的信心.)
基于例如的一些起始代码
import numpy as np
from scipy.stats import norm
returns = [-0.01, -0.02, -0.01, 0.04, 0.02, 0.01, -0.03]
mu = np.mean(returns)
std = np.std(returns)
valueAtRisk = norm.ppf(0.05, mu, sigma)
Run Code Online (Sandbox Code Playgroud)
但是,以上仅告诉我1天的风险.我的问题是另一个方向; 假设我不想在21天内损失超过10%,我可以根据回报的分配分配什么.
我更喜欢可以直接计算的答案,但蒙特卡罗的答案是可以接受的.
感谢您的帮助.
import numpy as np
returns = np.random.randn(1000)
Run Code Online (Sandbox Code Playgroud)
假设回报是独立且相同的分布(iid),那么T天的波动率等于sqrt(T)乘以一天波动率的乘积.
# one-way 5% quantile, critical value is 1.64
VaR_21 = returns.std() * np.sqrt(21) * 1.645
VaR_21
Out[72]: 7.4161618430166989
Run Code Online (Sandbox Code Playgroud)
或者,你可以做bootstraps.这是从历史数据集中随机选择21天,计算这个随机抽取的21天的回报.绘制直方图并获得5%分位数.
def generate_random_index(n=21):
# could set replace to False as well
return np.random.choice(np.arange(1000), size=n, replace=False)
VaR_simulated_21 = []
n_bootstrap = 10000
for _ in range(n_bootstrap):
VaR = returns[generate_random_index(21)].sum()
VaR_simulated_21.append(VaR)
plt.hist(VaR_simulated_21)
np.percentile(VaR_simulated_21, q=5)
Out[108]: -8.0686958215041216
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5869 次 |
最近记录: |