gpt4 book ai didi

python - 计算风险值(value)或 "most probable loss",对于给定的返回分布

转载 作者:行者123 更新时间:2023-11-28 21:14:57 25 4
gpt4 key购买 nike

根据历史每日返回率,我如何根据 21 天内损失不超过起始投资组合值(value)的 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)

但是,以上仅告诉我 1 天的风险。我的问题是相反的;假设我不想在 21 天内损失超过 10%,我可以根据返回分配分配什么。

我更喜欢可以直接计算的答案,但蒙特卡洛答案是可以接受的。

非常感谢您的帮助。

最佳答案

import numpy as np

returns = np.random.randn(1000)

假设 yield 独立同分布 (i.i.d.),则 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

或者,您可以进行引导。那就是从历史数据集中随机选择21天,计算这随机抽取的21天的 yield 。绘制直方图并获得 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

enter image description here

关于python - 计算风险值(value)或 "most probable loss",对于给定的返回分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30878265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com