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

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)
数据量: 2422
ts_code trade_date open high low close pre_close change pct_chg vol amount
0 601012.SH 20220527 76.01 77.48 74.68 75.12 75.10 0.02 0.0266 328856.22 2500213.779
1 601012.SH 20220526 74.00 75.50 72.60 75.10 73.98 1.12 1.5139 439478.22 3272632.653
2 601012.SH 20220525 73.17 74.63 72.73 73.98 73.43 0.55 0.7490 340880.60 2514086.153
3 601012.SH 20220524 75.92 76.40 73.36 73.43 75.90 -2.47 -3.2543 409643.88 3063350.429
4 601012.SH 20220523 76.68 77.00 74.58 75.90 76.68 -0.78 -1.0172 422164.30 3195763.289

从上面可以看出,序号并不是以时间作为单位的。那么我们首先需要将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.09243727989936057

解释

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