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

标签 python pandas statistics scipy quantitative-finance

根据历史每日返回率,我如何根据 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/

相关文章:

python - 如何按顺序合并多个数据帧?

Pandas 数据帧到键值对

python - 计算 pandas 数据帧的总和

Python 使用相同的键汇总 Dataframe 中的行

python - ValueError : Error when checking input: expected dense_26_input to have shape (45781, )但得到形状为(2,)的数组

python - 在 matplotlib 的子图中使用 pdf 图像

statistics - 以编程方式创建生存曲线

r - 在 R 中加速 wilcox.test

python - 导入pynotify时出现GTK错误

python - OrderedDict 中第一个超过阈值的键