VaR模型 - 用方差-协方差计算VaR#

用方差-协方差计算VaR

用方差-协方差计算VaR的思路跟历史模拟法来计算VaR是不一样的,前者是根据股票某时间段的均值(mean),标准差(standard deviation),根据这两个值再重新算出正态分布图,然后然后根据置信区间来确定在险价值;而后者是先计算出某只股票某段时间的整体回报率和波动, 然后根据置信区间的百分比,如10%、5%或 1% 来确定在险价值。

正如我刚刚所提到,用方差-协方差来计算VaR是基于正态分布作为前提,所以这也是缺点之一。

import tushare as ts
import pandas as pd
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
plt.rcParams['font.sans-serif'] = ['SimHei'] 
plt.rcParams['axes.unicode_minus'] = False  

pro = ts.pro_api('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') #这里需要填写你注册好的Tushare的TOKEN凭证

通过调用tushare获取股票601012(隆基股份)的股票数据,这里不设置日期,那么默认获取Tushare提供的历史数据。

ticker_data = pro.daily(ts_code='601012.SH')
print('数据量:',len(ticker_data))
ticker_data.head(5)
数据量: 2430
ts_code trade_date open high low close pre_close change pct_chg vol amount
0 601012.SH 20220609 60.21 61.59 59.90 60.98 60.21 0.77 1.2789 794660.60 4828508.525
1 601012.SH 20220608 61.08 61.30 59.01 60.21 60.63 -0.42 -0.6927 974318.52 5842896.395
2 601012.SH 20220607 62.70 63.56 60.60 60.63 60.62 0.01 0.0165 1467264.39 9075963.991
3 601012.SH 20220606 57.72 62.30 57.00 60.62 56.94 3.68 6.4629 1068502.03 6382247.895
4 601012.SH 20220602 79.00 80.17 78.00 79.98 78.70 1.28 1.6264 377225.29 3003191.420

从上面可以看出,序号并不是以时间作为单位的。那么我们首先需要将trade_date转为datetime格式,然后设置为序号以便于画图。

ticker_data['trade_date'] = pd.to_datetime(ticker_data['trade_date'],format='%Y%m%d')
ticker_data.set_index('trade_date', inplace=True)
returns = ticker_data["close"].pct_change().dropna()

plt.figure(figsize=(15, 5))
plt.title("股票代码:601012 - 隆基股份", weight='bold')
ticker_data['close'].plot()
<AxesSubplot:title={'center':'股票代码:601012 - 隆基股份'}, xlabel='trade_date'>
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 32929 (\N{CJK UNIFIED IDEOGRAPH-80A1}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 31080 (\N{CJK UNIFIED IDEOGRAPH-7968}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 20195 (\N{CJK UNIFIED IDEOGRAPH-4EE3}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 30721 (\N{CJK UNIFIED IDEOGRAPH-7801}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 38534 (\N{CJK UNIFIED IDEOGRAPH-9686}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 22522 (\N{CJK UNIFIED IDEOGRAPH-57FA}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 20221 (\N{CJK UNIFIED IDEOGRAPH-4EFD}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
../_images/variance-covariance-VaR_6_2.png

下面将画出每日收盘价的百分比变化图:

plt.figure(figsize=(15, 5))
ticker_data["close"].pct_change().plot()
plt.title("股票代码:601012 - 隆基股份", weight='bold');
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 32929 (\N{CJK UNIFIED IDEOGRAPH-80A1}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 31080 (\N{CJK UNIFIED IDEOGRAPH-7968}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 20195 (\N{CJK UNIFIED IDEOGRAPH-4EE3}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 30721 (\N{CJK UNIFIED IDEOGRAPH-7801}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 38534 (\N{CJK UNIFIED IDEOGRAPH-9686}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 22522 (\N{CJK UNIFIED IDEOGRAPH-57FA}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 20221 (\N{CJK UNIFIED IDEOGRAPH-4EFD}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
../_images/variance-covariance-VaR_8_1.png

方差-协方差计算VaR#

mean = returns.mean()
sigma = returns.std()
tdf, tmean, tsigma = scipy.stats.t.fit(returns)
support = np.linspace(returns.min(), returns.max(), 100)

plt.figure(figsize=(15, 5))
plt.hist(returns,bins=50, density=True)
plt.plot(support, scipy.stats.t.pdf(support, loc=tmean, scale=tsigma, df=tdf), "r-") #pdf(x, df, loc=0, scale=1)	Probability density function.
plt.title("回报率 (%)", weight='bold');
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 22238 (\N{CJK UNIFIED IDEOGRAPH-56DE}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 25253 (\N{CJK UNIFIED IDEOGRAPH-62A5}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
../_images/variance-covariance-VaR_10_1.png
#ppf(q, df, loc=0, scale=1)	Percent point function (inverse of cdf — percentiles).
scipy.stats.norm.ppf(0.05, mean, sigma)
-0.0928031580296715

解释

用股票601012的方差-协方差得出每日收益率为-9.34%, 这意味着95%的机率这只股票单日的损失不高于9.34%. 如果你投资100万来计算绝对金额,一日5% VaR为0.0934 * 100万 = 9.34万。