Campbell(2004)在《Bad beta,Good beta》一文中指出持有股票将面临两方面的风险,一方面来自于公司未来现金流的风险,另一方面来自于市场折现率变化的风险。因此传统的CAPM的beta应该拆解成两个beta,现金流beta与折现率beta。不同股票两种beta的大小决定了股票受现金流的冲击和折现率的冲击影响的大小。现金流反应了经济的景气程度,其代理变量可以是市盈率,市净率,PMI,工业增加值。折现率反映了市场的资金成本,其代理变量可以是Shibor,国债利率,CPI。
Campbell使用Two-beta模型对1963年7月至2001年12月的美股数据进行了实证分析。采用的代理变量有股票的超额收益率、期限利差、市盈率和价值利差。最终发现随着“市值大小”的递减,现金流beta基本没有什么变化,但是折现率beta增幅较大,故其可以解释小股票的风险溢价较高的原因;随着“账面价值/市值”的递增,折现率beta递减,但是现金流beta有较为显著的增幅,因此Two-beta模型可以很好地解释价值型股票风险溢价较高的原因。
首先进行收益率的聚类,数据来源于JQdata。这里采取申万一级行业分类2014年至今的数据,取行业内所有股票的月收益率均值作为该行业的月收益率。行业相关系数矩阵显示周期类、消费大类行业组的收益率内部相关性特别高,呈现出明显的行业收益集聚性。于是本文采取Kmeans方法对A股行业收益率进行聚类,大致可以将27个一级行业分为四大类:金融,周期,消费,成长,具体分类如下:
金融:['非银金融I', '银行I']
周期:['建筑装饰I', '建筑材料I', '房地产I', '交通运输I', '公用事业I', '有色金属I', '钢铁I', '采掘I']
消费:['汽车I', '传媒I', '电气设备I', '综合I', '休闲服务I', '商业贸易I', '医药生物I', '轻工制造I', '纺织服装I', '食品饮料I', '家用电器I', '化工I', '农林牧渔I']
成长:['通信I', '计算机I', '国防军工I', '电子I']
然后根据Campbell的方法构造Two-beta模型。由于聚宽不提供国债收益率数据,这里数据来源于wind,包括自2005年以来的上证综指收益率,pmi,cpi,十年期国债收益率,工业增加值,相对估值。其中上证综指相对估值指标采取市盈率指标在前360个交易日市盈率率序列中所处的分位数。各行业相对估值指标采取市净率指标在前360个交易日市净率序列中所处的分位数,这里的行业市净率取当日该行业所有股票市净率的中值。
由于VAR模型要求各时间序列平稳,首先对数据进行ADF检验,发现PMI和工业增加值为非平稳时间序列,故舍去。然后对CPI,十年国债收益,上证综指月收益,估值水平进行多重共线性检验,结果方差膨胀因子小于5,表明不存在强烈的多重共线性。
Campbell和Shiller(1988a)和Campbell(1991)从股利折现模型出发,先将股票收益率对数线性化,再利用VAR模型及一系列计算方法,将未预期到的超额收益率*成“现金流消息”和“折现率消息"两部分,现金流消息代表股票收益率中股息所反映的部分,折现率消息代表股票收益率中资本利得所反映的部分:
rt?1?Etrt 1=(Et 1?Et)∑j=0∞ρjΔdt 1 j?(Et 1?Et)∑j=0∞ρjrt 1 jrt?1?Etrt1=(Et1?Et)∑j=0∞ρjΔdt1j?(Et1?Et)∑j=0∞ρjrt1j
r{t-1}-E{t}r{t 1}=(E{t 1}-E{t})\sum{j=0}^{\infty}{\rho^{j}\Delta d{t 1 j}}-(E{t 1}-E{t})\sum{j=0}^{\infty}{\rho^{j}r{t 1 j}}
其中r表示股票收益率,Et表示期望,delta d表示股息增长率,该公式反映了未预期到的超额收益率同现金流和折现率的期望值的变化有关。
接着本文沿用Campbell(2004)的建模方法:
zt 1=Azt ut 1zt1=Aztut1
z{t 1}=Az{t} u{t 1}其中z表示由各时间序列数据组成的列向量,且首个元素为收益率,其余元素为预测变量,A为估计所得到系数矩阵。
定义e=[1,0......0]为N重列向量,I为单位阵,接下来就可以根据回归系数矩阵与得到的残差时间序列对市场的超额收益率进行拆解,获得现金流消息序列NCF与折现率消息序列NDR。
Ncf,t 1=(e eλ)ut 1Ncf,t1=(eeλ)ut1
N{cf,t 1}=(e e\lambda )u{t 1}Ndr,t 1=eλut 1Ndr,t1=eλut1
N{dr,t 1}=e\lambda u{t 1}
其中
λ=ρA(I?ρA)?1λ=ρA(I?ρA)?1
\lambda =\rho A(I-\rho A)^{-1}
根据不同行业的收益率数据与现金流消息和折现率消息的相关性,本文对四大板块的beta进行了拆解:
βi,cf=cov(ri,Ncf,t)var(r)βi,cf=cov(ri,Ncf,t)var(r)
\beta {i,cf}=\frac{cov(r{i},N{cf,t})}{var(r)}βi,dr=cov(ri,Ndr,t)var(r)βi,dr=cov(ri,Ndr,t)var(r)
\beta {i,dr}=\frac{cov(r{i},N{dr,t})}{var(r)}依照金融周期消费成长的顺序,现金流beta递减,折现率beta递增,可以得出以下结论:
金融与周期板块的现金流beta高于消费与成长,折现率流beta低于消费与成长,这就意味着在现金流上行,折现率下行阶段,也就是经济繁荣利率上升阶段,金融与周期板块表现较为出色;在现金流下行,折现率上行阶段,也就是经济萧条利率扩张阶段,消费与成长板块表现相对出色。
本文相关数据:https://pan.baidu.com/s/18wvYRACzHpFPGZ5umnSwIw
# -*- coding: utf-8 -*-#首先获取行业收益率from jqdata import *import pandas as pdimport datetime as dtimport numpy as npfrom sklearn.cluster import KMeans#获取时间轴与申万一级行业信息industries=get_industries(name='sw_l1')T=pd.date_range(start='3/1/2014', end='2/08/2019',freq='m')df2=pd.DataFrame()for month in T:#筛选每月的交易日lastmonth = month - dt.timedelta(days=month.day)d=get_trade_days(start_date=lastmonth,end_date=month)df1=pd.DataFrame()df=pd.DataFrame()for i in industries.index:#获得单个行业的所有个股rsum=np.array(0)list=get_industry_stocks(i, date=lastmonth)for stock in list:#获得个股收益率p1=np.array(get_price(stock, start_date=d[0], end_date=d[0])['close'])p2=np.array(get_price(stock, start_date=d[len(d)-1], end_date=d[len(d)-1])['close'])r=(p2-p1)/p1rsum=rsum+r#计算该月份单个行业所有个股的收益率均值ri=rsum/len(list)name=array(industries.loc[i,['name']])#合并所获得数据到一个dataframedf1=pd.concat([df, df1], axis=1)t=str(month)[:8]+'01'df = pd.DataFrame([ri], index=[t],columns=[name])df2=pd.concat([df2, df1], axis=0)print(df2)
汽车I 非银金融I 银行I 通信I 传媒I 计算机I \ 2014-03-01 -0.024358 -0.035115 0.008905 -0.056344 -0.069530 -0.080293 2014-04-01 0.003382 0.011625 0.012298 0.006063 0.003108 -0.026666 2014-05-01 0.031279 0.006578 -0.001877 0.066127 0.027238 0.126333 2014-06-01 0.059653 0.007908 0.016753 0.092177 0.064196 0.089783 2014-07-01 0.093976 0.135759 0.116666 0.029625 -0.025780 -0.000859 2014-08-01 0.067701 -0.003178 -0.040316 0.057023 0.096822 0.084139 2014-09-01 0.117639 0.098527 0.027848 0.140640 0.058282 0.132520 2014-10-01 0.027883 0.068575 0.062109 -0.004071 -0.010938 0.002233 2014-11-01 0.029846 0.332762 0.166963 0.061869 0.039337 0.094645 2014-12-01 -0.074693 0.370585 0.292792 -0.077929 -0.060365 -0.118208 2015-01-01 0.090910 NaN -0.097188 0.106773 0.148205 0.177109 2015-02-01 0.080555 0.098977 0.024491 0.125809 0.194130 0.173050 2015-03-01 0.179351 0.093457 0.099670 0.210321 0.196753 0.283671 2015-04-01 0.177934 0.145766 0.139083 0.143704 0.291905 0.163711 2015-05-01 0.290339 0.031141 -0.047200 0.466117 NaN 0.468377 2015-06-01 -0.131738 -0.160600 0.014282 -0.194357 -0.204636 -0.212061 2015-07-01 -0.156182 -0.182520 -0.116129 -0.155086 -0.130366 -0.170510 2015-08-01 -0.140730 -0.177396 -0.074081 -0.133432 -0.167625 -0.209164 2015-09-01 -0.022205 -0.046992 -0.022573 -0.019724 0.027949 0.059008 2015-10-01 0.196124 0.180500 0.057687 0.263159 0.209643 0.307950 2015-11-01 0.177423 0.165779 0.054450 0.200738 0.163140 0.175943 2015-12-01 0.084542 0.079622 0.036879 0.067298 0.028511 0.055420 2016-01-01 -0.297519 -0.283070 -0.155647 -0.280824 -0.284597 -0.315908 2016-02-01 0.013670 -0.041839 0.004349 -0.032006 -0.040101 -0.042065 2016-03-01 0.202444 0.213485 0.082602 0.194894 0.198100 0.236170 2016-04-01 0.011097 -0.044480 0.005176 -0.021150 -0.048095 -0.055870 2016-05-01 -0.037800 -0.022562 0.009847 -0.035491 -0.033698 -0.012505 2016-06-01 0.081083 0.020428 -0.007221 0.075060 0.008989 0.085059 2016-07-01 -0.018784 -0.012963 0.043683 -0.006559 -0.013566 -0.067149 2016-08-01 0.062655 0.075955 0.044042 0.059675 0.044152 0.055201 2016-09-01 -0.001031 -0.025710 -0.048909 -0.003778 -0.038917 -0.038821 2016-10-01 0.037921 0.038362 0.035142 0.019892 0.026905 0.054479 2016-11-01 0.030356 0.059718 0.037510 0.044562 0.029562 0.032006 2016-12-01 -0.025682 -0.076921 -0.061886 -0.061939 -0.107292 -0.112689 2017-01-01 -0.010282 -0.015798 -0.000365 -0.055307 -0.049532 NaN 2017-02-01 0.038725 0.038605 0.134434 0.049335 0.036958 0.038316 2017-03-01 0.001253 -0.052030 0.008319 -0.026073 -0.039307 -0.027084 2017-04-01 -0.067420 -0.039869 -0.036528 -0.087827 -0.042862 -0.070048 2017-05-01 -0.090795 -0.029615 0.000639 -0.075524 -0.058028 -0.069193 2017-06-01 0.053399 0.033622 0.014554 0.029029 0.024820 0.024342 2017-07-01 -0.028765 0.047665 0.027793 -0.057138 -0.044977 -0.064656 2017-08-01 0.025495 0.064432 0.025945 0.069797 0.035137 0.094308 2017-09-01 0.027798 -0.031515 -0.055855 0.112272 -0.009133 0.023557 2017-10-01 -0.037241 -0.040313 -0.023774 -0.100166 -0.019179 -0.069950 2017-11-01 -0.096998 -0.042139 -0.005699 -0.054569 -0.082327 -0.069211 2017-12-01 -0.020030 -0.060349 -0.023493 -0.015665 -0.024290 -0.025563 2018-01-01 -0.051961 0.013602 0.117031 -0.090841 -0.013792 -0.053608 2018-02-01 -0.058392 -0.095901 -0.078508 -0.040915 -0.030776 -0.012512 2018-03-01 0.040075 -0.013203 -0.066547 0.098691 0.049913 0.173281 2018-04-01 -0.056629 -0.039887 -0.013513 -0.043412 -0.062895 -0.014471 2018-05-01 0.014714 -0.052606 -0.021937 -0.035707 -0.029781 -0.044965 2018-06-01 -0.097934 -0.098278 -0.080477 -0.094044 -0.123583 -0.083953 2018-07-01 -0.000502 0.024479 0.088449 -0.018186 -0.016106 -0.017992 2018-08-01 -0.102234 -0.075152 -0.017191 -0.031255 -0.094923 -0.051481 2018-09-01 -0.011766 0.022275 0.041065 -0.040438 -0.009099 -0.028941 2018-10-01 -0.056653 0.044007 0.024612 -0.066185 -0.073948 -0.058176 2018-11-01 0.048292 0.044523 -0.038391 0.099758 0.087177 0.059003 2018-12-01 -0.034891 -0.067419 -0.049558 0.056794 -0.047650 -0.054820 2019-01-01 -0.031989 0.010179 0.072135 -0.082712 -0.065852 -0.028022 国防军工I 电气设备I 建筑装饰I 建筑材料I ... 轻工制造I \ 2014-03-01 -0.082440 -0.059799 -0.013191 0.031208 ... -0.029329 2014-04-01 -0.006551 -0.019057 0.000330 -0.011706 ... -0.006375 2014-05-01 0.061053 0.027859 0.020836 0.026476 ... 0.020820 2014-06-01 0.113674 0.061113 0.026130 0.031676 ... 0.061808 2014-07-01 0.097851 0.081110 0.090592 0.080530 ... 0.050522 2014-08-01 0.074168 0.065132 0.031008 0.051066 ... 0.063128 2014-09-01 0.153895 0.126586 0.110561 0.134362 ... 0.113638 2014-10-01 0.036101 0.001894 0.024941 0.024984 ... 0.013493 2014-11-01 -0.019290 0.039892 0.085773 0.044928 ... 0.048417 2014-12-01 0.035423 -0.055563 0.135669 0.024890 ... -0.070262 2015-01-01 0.069885 0.083611 0.040648 0.040084 ... 0.094135 2015-02-01 0.060462 0.083607 0.107346 0.062301 ... 0.091364 2015-03-01 0.139297 0.222283 0.209389 0.200155 ... 0.227046 2015-04-01 0.166767 0.181488 0.212579 0.136475 ... 0.178220 2015-05-01 0.511352 0.380356 0.195721 0.217221 ... 0.405431 2015-06-01 -0.128701 -0.163747 -0.146611 -0.132345 ... -0.117179 2015-07-01 -0.067821 -0.153401 -0.143074 -0.161781 ... -0.155373 2015-08-01 -0.135229 -0.051408 -0.132598 -0.069644 ... -0.102248 2015-09-01 -0.129986 -0.016954 -0.065202 -0.038868 ... -0.037862 2015-10-01 0.229928 0.236485 0.183512 0.151989 ... 0.169694 2015-11-01 0.042987 0.134544 0.085833 0.107907 ... 0.144958 2015-12-01 -0.038334 0.059122 0.043431 0.116781 ... 0.066727 2016-01-01 -0.249385 -0.282621 -0.235037 -0.232787 ... -0.280802 2016-02-01 -0.025112 -0.033339 -0.005410 0.020055 ... -0.001229 2016-03-01 0.198387 0.213515 0.178792 0.163627 ... 0.191975 2016-04-01 -0.065653 0.013519 -0.007425 0.003814 ... -0.003274 2016-05-01 -0.031842 -0.041586 -0.060849 -0.066975 ... -0.056673 2016-06-01 0.099134 0.094936 0.045491 0.044059 ... 0.065539 2016-07-01 0.014395 -0.031300 0.009602 0.015471 ... 0.014744 2016-08-01 0.062053 0.062838 0.128768 0.095765 ... 0.068221 2016-09-01 -0.064648 -0.015573 -0.014591 0.026764 ... -0.001802 2016-10-01 0.020760 0.035390 0.060293 0.058987 ... 0.037371 2016-11-01 0.007507 0.038114 0.061542 0.048536 ... 0.041380 2016-12-01 -0.013237 -0.044576 -0.064645 0.007573 ... -0.056150 2017-01-01 0.021833 -0.040133 -0.049663 -0.021369 ... -0.037607 2017-02-01 0.047795 0.049545 0.063957 0.067480 ... 0.058400 2017-03-01 0.003852 -0.012606 0.020156 -0.015943 ... -0.011161 2017-04-01 -0.079495 -0.075628 -0.035516 -0.024960 ... -0.079046 2017-05-01 -0.149077 -0.072965 -0.085747 -0.089540 ... -0.078773 2017-06-01 0.058923 0.024984 0.036457 0.044604 ... 0.027980 2017-07-01 -0.023526 -0.028507 -0.033684 0.013989 ... -0.024202 2017-08-01 0.039986 0.042594 0.085961 0.037311 ... 0.038786 2017-09-01 0.058221 0.040624 -0.008308 0.000416 ... 0.009157 2017-10-01 -0.066341 -0.037518 -0.006580 -0.030305 ... -0.013840 2017-11-01 -0.046959 -0.078730 -0.086612 -0.045739 ... -0.070311 2017-12-01 -0.028242 -0.026277 -0.025809 -0.035913 ... -0.028355 2018-01-01 -0.066024 -0.066351 -0.031342 -0.031934 ... -0.016288 2018-02-01 -0.046453 -0.053062 -0.072542 -0.063433 ... -0.060356 2018-03-01 0.121738 0.065722 0.037681 0.035780 ... 0.025164 2018-04-01 -0.045616 -0.062261 -0.054681 -0.065553 ... -0.054629 2018-05-01 -0.082524 -0.033538 -0.024176 -0.038721 ... 0.026286 2018-06-01 -0.086778 -0.096607 -0.112238 -0.098276 ... -0.120943 2018-07-01 0.046250 0.003274 0.047581 0.057859 ... 0.014529 2018-08-01 -0.093661 -0.096535 -0.076354 -0.083154 ... -0.083045 2018-09-01 0.033460 0.012044 -0.009860 -0.031464 ... -0.024640 2018-10-01 -0.103959 -0.064406 -0.057509 -0.057246 ... -0.060371 2018-11-01 0.043896 0.078876 0.041564 0.040901 ... 0.062086 2018-12-01 -0.055235 -0.018470 -0.021733 -0.042525 ... -0.028225 2019-01-01 -0.001136 0.003348 -0.030891 -0.015913 ... -0.017667 纺织服装I 食品饮料I 家用电器I 电子I 有色金属I 钢铁I \ 2014-03-01 -0.013698 -0.010650 -0.055575 -0.063955 -0.025179 -0.006589 2014-04-01 -0.019478 -0.016518 0.009816 0.005919 -0.002089 -0.031413 2014-05-01 0.009420 0.010335 0.022430 0.064763 0.043643 0.036127 2014-06-01 0.033877 0.011846 0.070304 0.075443 0.071700 0.038157 2014-07-01 0.081601 0.069230 0.077852 0.023196 0.164742 0.114057 2014-08-01 0.078754 0.050945 0.020334 0.104229 0.025365 0.066405 2014-09-01 0.139167 0.082333 0.103830 0.088207 0.067923 0.114102 2014-10-01 0.018616 0.000247 0.004972 0.005063 -0.007414 0.055136 2014-11-01 0.047852 0.043700 0.067657 0.027065 0.065646 0.101401 2014-12-01 -0.052442 0.006954 -0.052054 -0.115719 -0.014831 0.129365 2015-01-01 0.102230 0.026992 0.115360 0.103448 0.045004 0.018606 2015-02-01 0.076349 0.032956 0.080133 0.080497 0.065971 0.085120 2015-03-01 0.201286 0.151678 0.155452 0.187233 0.150138 0.192493 2015-04-01 0.238661 0.172612 0.175641 0.139881 0.153257 0.196981 2015-05-01 0.318168 0.214589 0.271931 0.387863 0.183457 0.075127 2015-06-01 -0.119323 -0.084532 -0.149273 -0.132081 -0.163353 -0.021230 2015-07-01 -0.138590 -0.160217 -0.094023 -0.151488 -0.194938 -0.183491 2015-08-01 -0.114649 -0.140988 -0.147005 -0.149782 -0.128518 -0.118761 2015-09-01 -0.051447 -0.042761 -0.040058 -0.038161 -0.050810 -0.113139 2015-10-01 0.216847 0.151734 0.206796 0.239312 0.182048 0.077367 2015-11-01 0.164956 0.088038 0.178107 0.216653 0.125212 0.034726 2015-12-01 0.086427 0.077539 0.065800 0.056578 0.066376 0.046631 2016-01-01 -0.268308 -0.249609 -0.253668 -0.297512 -0.284134 -0.225610 2016-02-01 0.002487 -0.015929 -0.032497 -0.016012 0.071168 0.052511 2016-03-01 0.185120 0.178777 0.188936 0.216185 0.182344 0.086176 2016-04-01 0.004550 0.024297 0.013679 -0.009157 0.015251 0.016054 2016-05-01 -0.057188 -0.044800 -0.018129 0.023788 -0.009928 -0.071856 2016-06-01 0.023748 0.077028 0.052256 0.115184 0.081450 0.003583 2016-07-01 -0.005671 0.023956 0.029198 -0.058813 0.010187 0.008882 2016-08-01 0.052197 0.061403 0.045523 0.060736 0.028500 0.105263 2016-09-01 0.010706 -0.005326 0.011751 -0.006226 -0.034582 0.001778 2016-10-01 0.041391 0.066342 0.025062 0.034815 0.054353 0.040939 2016-11-01 0.055262 0.016543 0.036028 0.018104 0.024273 0.095448 2016-12-01 -0.028297 -0.029609 -0.068984 -0.064547 -0.054943 -0.014174 2017-01-01 -0.049988 -0.041120 -0.036649 -0.047522 -0.003259 0.032167 2017-02-01 0.046854 0.049546 0.055631 0.051109 0.064280 NaN 2017-03-01 -0.029460 -0.011295 0.007555 0.008806 -0.027792 -0.035792 2017-04-01 -0.073232 -0.061410 -0.048991 -0.066246 -0.065689 -0.065124 2017-05-01 -0.073572 -0.072924 -0.048858 -0.060028 -0.084424 -0.064089 2017-06-01 0.018161 0.059055 0.054739 0.034490 0.072425 0.051719 2017-07-01 -0.040948 0.011276 -0.038746 -0.020153 0.142651 0.171535 2017-08-01 0.031616 0.019104 0.020440 0.062072 0.049479 0.099358 2017-09-01 -0.006828 0.024637 0.015903 0.059595 0.024217 -0.016579 2017-10-01 -0.007777 0.013088 -0.006531 -0.021702 -0.070439 -0.069134 2017-11-01 -0.089199 -0.078405 -0.076873 -0.046481 -0.056257 0.071073 2017-12-01 -0.022652 0.022017 -0.021980 -0.018233 0.009837 -0.073581 2018-01-01 -0.004664 -0.005002 -0.056740 -0.104005 -0.049955 -0.002841 2018-02-01 -0.068935 -0.078584 -0.039887 -0.012721 -0.037148 0.020235 2018-03-01 0.033640 0.015017 0.029221 0.088464 0.016558 -0.111377 2018-04-01 -0.042959 -0.021318 -0.045671 -0.054488 -0.047547 -0.016644 2018-05-01 0.049347 0.153142 0.015091 -0.030574 -0.013531 -0.059438 2018-06-01 -0.113256 -0.125773 -0.106194 -0.072214 -0.113741 0.008582 2018-07-01 -0.007177 0.017214 -0.001826 0.006281 0.021863 0.120293 2018-08-01 -0.098362 -0.111317 -0.106672 -0.082228 -0.096708 -0.057704 2018-09-01 -0.018219 0.032083 -0.007438 -0.041413 -0.015419 -0.007255 2018-10-01 -0.052545 -0.106158 -0.059856 -0.058289 -0.063352 -0.048513 2018-11-01 0.063345 0.045076 0.044952 0.052121 0.019721 -0.082722 2018-12-01 -0.035127 -0.024081 -0.037315 -0.042566 -0.045899 -0.046459 2019-01-01 -0.033149 0.020263 -0.004091 -0.020300 -0.016001 0.048932 化工I 采掘I 农林牧渔I 2014-03-01 -0.001605 -0.032822 -0.030162 2014-04-01 -0.025208 -0.004331 0.002488 2014-05-01 0.013728 0.031082 0.037403 2014-06-01 0.046025 0.011184 0.028650 2014-07-01 0.089470 0.147776 0.084824 2014-08-01 0.059026 0.000482 0.055533 2014-09-01 0.120486 0.109114 0.147857 2014-10-01 0.014241 -0.005523 -0.014990 2014-11-01 0.052927 0.049436 0.050293 2014-12-01 -0.047238 0.014210 -0.060737 2015-01-01 0.082703 -0.003303 0.095380 2015-02-01 0.069060 0.054455 0.036281 2015-03-01 0.178101 0.158348 0.201696 2015-04-01 0.179030 0.166422 0.135682 2015-05-01 0.295576 0.146735 0.316503 2015-06-01 -0.116665 -0.095186 -0.102517 2015-07-01 -0.165099 -0.231219 -0.109716 2015-08-01 -0.129609 -0.124301 -0.128561 2015-09-01 -0.047031 -0.062559 -0.037804 2015-10-01 0.215764 0.145432 0.180501 2015-11-01 0.168923 0.089004 0.141382 2015-12-01 0.077782 0.040434 0.071521 2016-01-01 -0.282180 -0.181097 -0.256223 2016-02-01 0.009658 0.043880 0.016326 2016-03-01 0.186324 0.075133 0.176402 2016-04-01 0.012327 0.039121 -0.009659 2016-05-01 -0.049544 -0.076783 -0.045427 2016-06-01 0.063691 0.035918 0.029368 2016-07-01 -0.005610 0.022397 0.006669 2016-08-01 0.070672 0.059382 0.065203 2016-09-01 0.008652 0.017716 -0.010291 2016-10-01 0.041747 0.074859 0.054169 2016-11-01 0.037185 0.000276 0.026284 2016-12-01 -0.027225 -0.046576 -0.017099 2017-01-01 -0.045426 -0.021425 -0.050369 2017-02-01 0.046101 0.040516 0.042665 2017-03-01 -0.020074 -0.029185 -0.036888 2017-04-01 -0.072807 -0.078226 -0.085553 2017-05-01 -0.084712 -0.061268 -0.090462 2017-06-01 0.041405 0.047762 0.034102 2017-07-01 0.024750 0.114924 -0.004266 2017-08-01 0.020929 0.042013 0.036750 2017-09-01 0.007850 -0.002114 0.001153 2017-10-01 -0.032706 -0.080446 -0.013694 2017-11-01 -0.066214 -0.026911 -0.065459 2017-12-01 -0.014430 -0.012923 -0.004292 2018-01-01 -0.038272 -0.023790 -0.030126 2018-02-01 -0.047993 -0.064520 -0.060231 2018-03-01 0.035393 -0.044881 0.037842 2018-04-01 -0.044571 -0.058926 -0.056725 2018-05-01 0.017494 0.000021 0.041820 2018-06-01 -0.091129 -0.065134 -0.133361 2018-07-01 0.023052 0.029104 0.013441 2018-08-01 -0.083127 -0.032568 -0.062143 2018-09-01 -0.007722 0.033832 -0.011275 2018-10-01 -0.061340 -0.060934 -0.067328 2018-11-01 0.039217 -0.026678 0.068020 2018-12-01 -0.040883 -0.069691 -0.038514 2019-01-01 -0.016267 0.020611 -0.000253 [59 rows x 27 columns]
#各行业收益率的相关系数矩阵CORR=df2.corr()print(CORR)
汽车I 非银金融I 银行I 通信I 传媒I 计算机I 国防军工I \ 汽车I 1.000000 0.638580 0.339475 0.935166 0.912380 0.921905 0.868667 非银金融I 0.638580 1.000000 0.832049 0.558881 0.669761 0.572780 0.532030 银行I 0.339475 0.832049 1.000000 0.202176 0.380360 0.216353 0.272663 通信I 0.935166 0.558881 0.202176 1.000000 0.905233 0.956442 0.886745 传媒I 0.912380 0.669761 0.380360 0.905233 1.000000 0.931132 0.803519 计算机I 0.921905 0.572780 0.216353 0.956442 0.931132 1.000000 0.864620 国防军工I 0.868667 0.532030 0.272663 0.886745 0.803519 0.864620 1.000000 电气设备I 0.968523 0.609170 0.307098 0.957567 0.913684 0.940044 0.906637 建筑装饰I 0.897639 0.809563 0.596203 0.837494 0.867011 0.828606 0.822782 建筑材料I 0.940538 0.710211 0.474166 0.869733 0.837610 0.844651 0.828792 综合I 0.956131 0.626500 0.316968 0.924289 0.871913 0.877548 0.845886 休闲服务I 0.960343 0.643625 0.354830 0.910141 0.905661 0.898855 0.825577 商业贸易I 0.945857 0.736436 0.499403 0.870405 0.881986 0.856582 0.829809 房地产I 0.896665 0.790375 0.602483 0.809665 0.835614 0.800245 0.774191 交通运输I 0.872027 0.780251 0.623277 0.764518 0.853694 0.771222 0.726409 公用事业I 0.919005 0.782333 0.554503 0.851288 0.873464 0.838384 0.832356 医药生物I 0.957160 0.582616 0.284425 0.921336 0.928069 0.940173 0.873926 轻工制造I 0.971488 0.596791 0.330896 0.946196 0.928148 0.928234 0.888678 纺织服装I 0.975131 0.642304 0.367369 0.917754 0.923142 0.903866 0.850389 食品饮料I 0.920595 0.657526 0.463908 0.814715 0.817891 0.799626 0.788715 家用电器I 0.973648 0.669538 0.358789 0.928983 0.928312 0.920098 0.860073 电子I 0.957208 0.551159 0.211110 0.958597 0.903067 0.957725 0.880862 有色金属I 0.918826 0.718211 0.485878 0.832216 0.819627 0.819752 0.797011 钢铁I 0.626288 0.709812 0.677456 0.512342 0.569860 0.519780 0.568661 化工I 0.986888 0.669292 0.388920 0.926758 0.900698 0.910898 0.868768 采掘I 0.836122 0.704915 0.575024 0.737020 0.717125 0.718278 0.720221 农林牧渔I 0.968976 0.620244 0.340608 0.925812 0.885105 0.911550 0.875215 电气设备I 建筑装饰I 建筑材料I ... 轻工制造I 纺织服装I 食品饮料I \ 汽车I 0.968523 0.897639 0.940538 ... 0.971488 0.975131 0.920595 非银金融I 0.609170 0.809563 0.710211 ... 0.596791 0.642304 0.657526 银行I 0.307098 0.596203 0.474166 ... 0.330896 0.367369 0.463908 通信I 0.957567 0.837494 0.869733 ... 0.946196 0.917754 0.814715 传媒I 0.913684 0.867011 0.837610 ... 0.928148 0.923142 0.817891 计算机I 0.940044 0.828606 0.844651 ... 0.928234 0.903866 0.799626 国防军工I 0.906637 0.822782 0.828792 ... 0.888678 0.850389 0.788715 电气设备I 1.000000 0.890991 0.926152 ... 0.974545 0.958630 0.884563 建筑装饰I 0.890991 1.000000 0.938144 ... 0.890683 0.907355 0.884915 建筑材料I 0.926152 0.938144 1.000000 ... 0.930408 0.934226 0.890218 综合I 0.950748 0.872628 0.941071 ... 0.951066 0.950536 0.868723 休闲服务I 0.932686 0.886209 0.923181 ... 0.952754 0.966855 0.911200 商业贸易I 0.931345 0.944006 0.958917 ... 0.940949 0.962985 0.943309 房地产I 0.873035 0.936577 0.942556 ... 0.884363 0.916718 0.880420 交通运输I 0.845790 0.928174 0.905104 ... 0.856560 0.891502 0.902481 公用事业I 0.906594 0.965143 0.934210 ... 0.902670 0.921405 0.901222 医药生物I 0.953988 0.882011 0.902314 ... 0.955542 0.956023 0.903491 轻工制造I 0.974545 0.890683 0.930408 ... 1.000000 0.976838 0.911255 纺织服装I 0.958630 0.907355 0.934226 ... 0.976838 1.000000 0.925861 食品饮料I 0.884563 0.884915 0.890218 ... 0.911255 0.925861 1.000000 家用电器I 0.955569 0.898536 0.906246 ... 0.958710 0.963928 0.906048 电子I 0.959125 0.828633 0.872178 ... 0.948520 0.922922 0.842059 有色金属I 0.883197 0.872573 0.908801 ... 0.875979 0.876775 0.881922 钢铁I 0.591992 0.739591 0.739153 ... 0.618061 0.626877 0.637431 化工I 0.966125 0.910873 0.955314 ... 0.974421 0.980814 0.932930 采掘I 0.805299 0.849542 0.874742 ... 0.814544 0.825237 0.847478 农林牧渔I 0.957374 0.887989 0.934041 ... 0.976979 0.974959 0.925611 家用电器I 电子I 有色金属I 钢铁I 化工I 采掘I 农林牧渔I 汽车I 0.973648 0.957208 0.918826 0.626288 0.986888 0.836122 0.968976 非银金融I 0.669538 0.551159 0.718211 0.709812 0.669292 0.704915 0.620244 银行I 0.358789 0.211110 0.485878 0.677456 0.388920 0.575024 0.340608 通信I 0.928983 0.958597 0.832216 0.512342 0.926758 0.737020 0.925812 传媒I 0.928312 0.903067 0.819627 0.569860 0.900698 0.717125 0.885105 计算机I 0.920098 0.957725 0.819752 0.519780 0.910898 0.718278 0.911550 国防军工I 0.860073 0.880862 0.797011 0.568661 0.868768 0.720221 0.875215 电气设备I 0.955569 0.959125 0.883197 0.591992 0.966125 0.805299 0.957374 建筑装饰I 0.898536 0.828633 0.872573 0.739591 0.910873 0.849542 0.887989 建筑材料I 0.906246 0.872178 0.908801 0.739153 0.955314 0.874742 0.934041 综合I 0.927614 0.923543 0.883274 0.609746 0.965219 0.824721 0.951197 休闲服务I 0.955831 0.913349 0.854975 0.595603 0.957919 0.791972 0.966722 商业贸易I 0.930801 0.870037 0.905136 0.708752 0.964867 0.872040 0.955688 房地产I 0.873509 0.807658 0.879751 0.755292 0.925002 0.870363 0.890591 交通运输I 0.856405 0.768262 0.865780 0.758095 0.888747 0.852046 0.861008 公用事业I 0.910493 0.854102 0.889567 0.730016 0.930195 0.855600 0.899644 医药生物I 0.953241 0.938010 0.862497 0.549331 0.949565 0.774890 0.947554 轻工制造I 0.958710 0.948520 0.875979 0.618061 0.974421 0.814544 0.976979 纺织服装I 0.963928 0.922922 0.876775 0.626877 0.980814 0.825237 0.974959 食品饮料I 0.906048 0.842059 0.881922 0.637431 0.932930 0.847478 0.925611 家用电器I 1.000000 0.945999 0.894228 0.592317 0.966928 0.805723 0.955929 电子I 0.945999 1.000000 0.862151 0.545080 0.946001 0.749112 0.932742 有色金属I 0.894228 0.862151 1.000000 0.751518 0.931755 0.918557 0.892360 钢铁I 0.592317 0.545080 0.751518 1.000000 0.683963 0.840170 0.618475 化工I 0.966928 0.946001 0.931755 0.683963 1.000000 0.875796 0.976845 采掘I 0.805723 0.749112 0.918557 0.840170 0.875796 1.000000 0.834896 农林牧渔I 0.955929 0.932742 0.892360 0.618475 0.976845 0.834896 1.000000 [27 rows x 27 columns]
#采用Kmeans算法对行业收益率进行聚类,基本可以分为四大类:金融,消费,成长,周期data=df2for i in data.columns:data[i]=data[i].fillna(data[i].mean())data=data.Tkm = KMeans(n_clusters=4,init='k-means++')label = km.fit_predict(data)Cluster = [[],[],[],[]]for i in range(len(label)):Cluster[label[i]].append(data.index[i])for i in range(len(Cluster)):print(Cluster[i])
['汽车I', '传媒I', '综合I', '休闲服务I', '商业贸易I', '医药生物I', '轻工制造I', '纺织服装I', '食品饮料I', '家用电器I', '化工I', '农林牧渔I'] ['建筑装饰I', '建筑材料I', '房地产I', '交通运输I', '公用事业I', '有色金属I', '钢铁I', '采掘I'] ['非银金融I', '银行I'] ['通信I', '计算机I', '国防军工I', '电气设备I', '电子I']
#该数据中pef为上证指数平均当日PE在前一年时间内所处的分位数,import pandas as pdimport matplotlib as pltdf=pd.read_excel('data.xls')df.index=df['date']print(df)
date pef 工业增加值 bond PMI CPI stock \ date 2005-01-31 2005-01-31 0.000000 20.900000 4.8982 54.7 1.900000 1191.8230 2005-02-28 2005-02-28 0.105314 7.600000 4.8334 54.5 3.900000 1306.0030 2005-03-31 2005-03-31 0.008142 15.100000 4.1203 57.9 2.700000 1181.2360 2005-04-30 2005-04-30 0.011855 16.000000 4.0441 56.7 1.800000 1159.1460 2005-05-31 2005-05-31 0.009479 16.600000 3.8469 52.9 1.800000 1060.7380 2005-06-30 2005-06-30 0.078101 16.800000 3.6530 51.7 1.600000 1080.9380 2005-07-31 2005-07-31 0.083461 16.100000 3.3874 51.1 1.800000 1083.0330 2005-08-31 2005-08-31 0.150077 16.000000 3.5608 52.6 1.300000 1162.7980 2005-09-30 2005-09-30 0.148088 16.500000 3.2404 55.1 0.900000 1155.6140 2005-10-31 2005-10-31 0.065972 16.100000 3.0613 54.1 1.200000 1092.8170 2005-11-30 2005-11-30 0.062734 16.600000 3.3255 54.1 1.300000 1099.2610 2005-12-31 2005-12-31 0.138267 16.500000 3.1216 54.3 1.600000 1161.0570 2006-01-31 2006-01-31 0.258033 12.600000 2.9795 52.1 1.900000 1258.0460 2006-02-28 2006-02-28 0.296008 20.100000 2.9266 52.1 0.900000 1299.0300 2006-03-31 2006-03-31 0.307692 17.800000 2.9400 55.3 0.800000 1298.2950 2006-04-30 2006-04-30 0.609290 16.600000 3.0000 58.1 1.200000 1440.2230 2006-05-31 2006-05-31 0.907869 17.900000 3.0200 54.8 1.400000 1641.3000 2006-06-30 2006-06-30 0.946463 19.500000 3.1650 54.1 1.500000 1672.2110 2006-07-31 2006-07-31 0.730949 16.700000 3.2600 52.4 1.000000 1612.7330 2006-08-31 2006-08-31 0.785381 15.700000 3.2380 53.1 1.300000 1658.6380 2006-09-30 2006-09-30 0.945568 16.100000 2.9990 57.0 1.500000 1752.4240 2006-10-31 2006-10-31 1.000000 14.700000 2.9512 54.7 1.400000 1837.9930 2006-11-30 2006-11-30 1.000000 14.900000 2.9781 55.3 1.900000 2099.2890 2006-12-31 2006-12-31 1.000000 14.700000 3.0269 54.8 2.800000 2675.4740 2007-01-31 2007-01-31 0.891733 24.710000 3.0671 55.1 2.200000 2786.3350 2007-02-28 2007-02-28 0.908394 12.600000 3.1466 53.1 2.700000 2881.0730 2007-03-31 2007-03-31 0.992075 17.600000 3.4338 56.1 3.300000 3183.9830 2007-04-30 2007-04-30 1.000000 17.400000 3.5916 58.6 3.000000 3841.2720 2007-05-31 2007-05-31 0.717503 18.100000 4.0600 55.7 3.400000 4109.6540 2007-06-30 2007-06-30 0.690260 19.400000 4.4500 54.5 4.400000 3820.7030 ... ... ... ... ... ... ... ... 2016-07-31 2016-07-31 0.231629 6.000000 2.7768 49.9 1.765113 2979.3390 2016-08-31 2016-08-31 0.435685 6.300000 2.7425 50.4 1.339773 3085.4910 2016-09-30 2016-09-30 0.367220 6.100000 2.7258 50.4 1.920226 3004.7030 2016-10-31 2016-10-31 0.500000 6.100000 2.7425 51.2 2.095947 3100.4920 2016-11-30 2016-11-30 0.672199 6.200000 2.9502 51.7 2.252258 3250.0340 2016-12-31 2016-12-31 0.752907 6.000000 3.0115 51.4 2.076545 3103.6373 2017-01-31 2017-01-31 0.875776 2.937250 3.3465 51.3 2.549055 3159.1660 2017-02-28 2017-02-28 0.980892 10.322581 3.2921 51.6 0.800000 3241.7331 2017-03-31 2017-03-31 0.942249 7.600000 3.2828 51.8 0.900000 3222.5142 2017-04-30 2017-04-30 0.839542 6.500000 3.4668 51.2 1.200000 3154.6584 2017-05-31 2017-05-31 0.773414 6.500000 3.6102 51.2 1.500000 3117.1778 2017-06-30 2017-06-30 0.899614 7.600000 3.5683 51.7 1.500000 3192.4269 2017-07-31 2017-07-31 1.000000 6.400000 3.6260 51.4 1.400000 3273.0283 2017-08-31 2017-08-31 0.990164 6.000000 3.6265 51.7 1.800000 3360.8103 2017-09-30 2017-09-30 0.957377 6.600000 3.6136 52.4 1.600000 3348.9431 2017-10-31 2017-10-31 0.957854 6.200000 3.8917 51.6 1.900000 3393.3417 2017-11-30 2017-11-30 0.777778 6.100000 3.8901 51.8 1.700000 3317.1884 2017-12-31 2017-12-31 0.783883 6.200000 3.8807 51.6 1.800000 3307.1721 2018-01-31 2018-01-31 0.879888 15.434501 3.9140 51.3 1.500000 3480.8334 2018-02-28 2018-02-28 0.611732 -2.119883 3.8202 50.3 2.900000 3259.4080 2018-03-31 2018-03-31 0.469274 6.000000 3.7407 51.5 2.100000 3168.8966 2018-04-30 2018-04-30 0.324022 7.000000 3.6217 51.4 1.800000 3082.2316 2018-05-31 2018-05-31 0.056604 6.800000 3.6129 51.9 1.800000 3095.4737 2018-06-30 2018-06-30 0.050847 6.000000 3.4756 51.5 1.900000 2847.4181 2018-07-31 2018-07-31 0.126829 6.000000 3.4812 51.2 2.100000 2876.4009 2018-08-31 2018-08-31 0.050000 6.100000 3.5751 51.3 2.300000 2725.2499 2018-09-30 2018-09-30 0.130638 5.800000 3.6103 50.8 2.500000 2821.3501 2018-10-31 2018-10-31 0.081267 5.900000 3.5086 50.2 2.500000 2602.7832 2018-11-30 2018-11-30 0.077135 5.400000 3.3550 50.0 2.200000 2588.1875 2018-12-31 2018-12-31 0.012397 5.700000 3.2265 49.4 1.900000 2493.8962 return date 2005-01-31 -0.058960 2005-02-28 0.095803 2005-03-31 -0.095533 2005-04-30 -0.018701 2005-05-31 -0.084897 2005-06-30 0.019043 2005-07-31 0.001938 2005-08-31 0.073650 2005-09-30 -0.006178 2005-10-31 -0.054341 2005-11-30 0.005897 2005-12-31 0.056216 2006-01-31 0.083535 2006-02-28 0.032578 2006-03-31 -0.000566 2006-04-30 0.109319 2006-05-31 0.139615 2006-06-30 0.018833 2006-07-31 -0.035568 2006-08-31 0.028464 2006-09-30 0.056544 2006-10-31 0.048829 2006-11-30 0.142164 2006-12-31 0.274467 2007-01-31 0.041436 2007-02-28 0.034001 2007-03-31 0.105138 2007-04-30 0.206436 2007-05-31 0.069868 2007-06-30 -0.070310 ... ... 2016-07-31 0.016976 2016-08-31 0.035629 2016-09-30 -0.026183 2016-10-31 0.031880 2016-11-30 0.048232 2016-12-31 -0.045045 2017-01-31 0.017891 2017-02-28 0.026136 2017-03-31 -0.005929 2017-04-30 -0.021057 2017-05-31 -0.011881 2017-06-30 0.024140 2017-07-31 0.025248 2017-08-31 0.026820 2017-09-30 -0.003531 2017-10-31 0.013257 2017-11-30 -0.022442 2017-12-31 -0.003020 2018-01-31 0.052511 2018-02-28 -0.063613 2018-03-31 -0.027769 2018-04-30 -0.027349 2018-05-31 0.004296 2018-06-30 -0.080135 2018-07-31 0.010179 2018-08-31 -0.052549 2018-09-30 0.035263 2018-10-31 -0.077469 2018-11-30 -0.005608 2018-12-31 -0.036431 [168 rows x 8 columns]
pe=df['pef']re=df['return']ind=df['工业增加值']bond=df['bond']pmi=df['PMI']cpi=df['CPI']list=[pe,re,ind,pmi,cpi,bond]
#工业增加值ind.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f18444b00b8>
#月收益率re.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f1844116b00>
#十年期国债bond.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f184418fe48>
#pe分位数pe.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f1844074320>
#cpicpi.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f183614bcc0>
#pmipmi.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f18360e44a8>
#对以上数据进行平稳性检验,发现工业增加值,PMI为不平稳时间序列,所以建模采用PE分位数,CPI,10年国债收益率from arch.unitroot import ADFfor i in list:adf=ADF(i)print(i.name)print(adf.summary().as_text)
/opt/conda/envs/python3new/lib/python3.6/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead. from pandas.core import datetools
pef <bound method Summary.as_text of <class 'statsmodels.iolib.summary.Summary'> """ Augmented Dickey-Fuller Results ===================================== Test Statistic -2.931 P-value 0.042 Lags 0 - Trend: Constant Critical Values: -3.47 (1%), -2.88 (5%), -2.58 (10%) Null Hypothesis: The process contains a unit root. Alternative Hypothesis: The process is weakly stationary. """> return <bound method Summary.as_text of <class 'statsmodels.iolib.summary.Summary'> """ Augmented Dickey-Fuller Results ===================================== Test Statistic -4.303 P-value 0.000 Lags 3 - Trend: Constant Critical Values: -3.47 (1%), -2.88 (5%), -2.58 (10%) Null Hypothesis: The process contains a unit root. Alternative Hypothesis: The process is weakly stationary. """> 工业增加值 <bound method Summary.as_text of <class 'statsmodels.iolib.summary.Summary'> """ Augmented Dickey-Fuller Results ===================================== Test Statistic -1.108 P-value 0.712 Lags 14 - Trend: Constant Critical Values: -3.47 (1%), -2.88 (5%), -2.58 (10%) Null Hypothesis: The process contains a unit root. Alternative Hypothesis: The process is weakly stationary. """> PMI <bound method Summary.as_text of <class 'statsmodels.iolib.summary.Summary'> """ Augmented Dickey-Fuller Results ===================================== Test Statistic -2.352 P-value 0.156 Lags 13 - Trend: Constant Critical Values: -3.47 (1%), -2.88 (5%), -2.58 (10%) Null Hypothesis: The process contains a unit root. Alternative Hypothesis: The process is weakly stationary. """> CPI <bound method Summary.as_text of <class 'statsmodels.iolib.summary.Summary'> """ Augmented Dickey-Fuller Results ===================================== Test Statistic -3.022 P-value 0.033 Lags 14 - Trend: Constant Critical Values: -3.47 (1%), -2.88 (5%), -2.58 (10%) Null Hypothesis: The process contains a unit root. Alternative Hypothesis: The process is weakly stationary. """> bond <bound method Summary.as_text of <class 'statsmodels.iolib.summary.Summary'> """ Augmented Dickey-Fuller Results ===================================== Test Statistic -3.316 P-value 0.014 Lags 2 - Trend: Constant Critical Values: -3.47 (1%), -2.88 (5%), -2.58 (10%) Null Hypothesis: The process contains a unit root. Alternative Hypothesis: The process is weakly stationary. """>
#相关系数图df1=df[['return','pef','bond','CPI']]import seaborn as snssns.heatmap(df1.corr(),annot=True, vmax=1,vmin = 0, xticklabels= True, yticklabels= True, square=True, cmap="YlGnBu")
<matplotlib.axes._subplots.AxesSubplot at 0x7f182801f518>
#方差膨胀因子小于5,证明不存在强的多重共线性from statsmodels.stats.outliers_influence import variance_inflation_factorimport numpy as npsample=np.array(df1)VIF=variance_inflation_factor(sample,0)print(VIF)
1.3029281508
#对数据应用VAR模型建模from statsmodels.tsa.api import VARmodel = VAR(df1)results = model.fit(1)print(results.summary())
Summary of Regression Results ================================== Model: VAR Method: OLS Date: Tue, 19, Feb, 2019 Time: 20:43:47 No. of Equations: 4.00000 BIC: -13.6335 Nobs: 167.000 HQIC: -13.8554 Log likelihood: 241.727 FPE: 8.25849e-07 AIC: -14.0069 Det(Omega_mle): 7.33926e-07 Results for equation return ============================================================================ coefficient std. error t-stat prob const 0.081983 0.050516 1.623 0.107 L1.return 0.020244 0.084856 0.239 0.812 L1.pef 0.033337 0.019868 1.678 0.095 L1.bond -0.019898 0.015366 -1.295 0.197 L1.CPI -0.006644 0.003834 -1.733 0.085 ============================================================================ Results for equation pef ============================================================================ coefficient std. error t-stat prob const 0.124521 0.096834 1.286 0.200 L1.return -0.103834 0.162659 -0.638 0.524 L1.pef 0.889549 0.038084 23.357 0.000 L1.bond -0.008864 0.029455 -0.301 0.764 L1.CPI -0.017156 0.007349 -2.334 0.021 ============================================================================ Results for equation bond ============================================================================ coefficient std. error t-stat prob const 0.314663 0.104293 3.017 0.003 L1.return 0.111964 0.175188 0.639 0.524 L1.pef 0.092503 0.041017 2.255 0.025 L1.bond 0.887768 0.031724 27.984 0.000 L1.CPI 0.014092 0.007915 1.780 0.077 ============================================================================ Results for equation CPI ============================================================================ coefficient std. error t-stat prob const -0.618700 0.373767 -1.655 0.100 L1.return -1.063986 0.627844 -1.695 0.092 L1.pef 0.431952 0.147000 2.938 0.004 L1.bond 0.167657 0.113693 1.475 0.142 L1.CPI 0.939286 0.028366 33.113 0.000 ============================================================================ Correlation matrix of residuals return pef bond CPI return 1.000000 0.645173 0.196821 0.083831 pef 0.645173 1.000000 0.141373 0.014309 bond 0.196821 0.141373 1.000000 0.163517 CPI 0.083831 0.014309 0.163517 1.000000
#一阶VAR的系数矩阵a=results.coefsprint(a)
[[[ 0.02024443 0.03333671 -0.0198981 -0.00664427] [-0.10383421 0.88954949 -0.00886375 -0.01715625] [ 0.11196379 0.09250276 0.88776807 0.01409223] [-1.06398557 0.43195201 0.16765682 0.93928586]]]
#残差的时间序列Resid=results.residprint(Resid)
return pef bond CPI date 2005-02-28 0.125103 0.050685 0.150098 1.850107 2005-03-31 -0.060878 -0.090362 -0.560729 -1.098425 2005-04-30 0.000905 -0.046984 0.043460 -0.913331 2005-05-31 -0.074467 -0.060803 -0.082355 0.024946 2005-06-30 0.026969 0.001313 -0.093555 -0.211397 2005-07-31 0.000285 -0.048728 -0.202184 0.289918 2005-08-31 0.068208 0.012421 0.205608 -0.373924 2005-09-30 -0.015164 -0.048421 -0.275876 -0.285828 2005-10-31 -0.070677 -0.146759 -0.155776 0.359527 2005-11-30 -0.008298 -0.078393 0.276184 0.191994 2005-12-31 0.046831 0.010333 -0.170119 0.419261 2006-01-31 0.068550 0.071473 -0.148052 0.492573 2006-02-28 0.012212 0.009634 -0.093165 -0.788055 2006-03-31 -0.028862 -0.035378 -0.016517 -0.010521 2006-04-30 0.080906 0.250787 0.035626 0.440850 2006-05-31 0.102775 0.299885 -0.043479 0.241716 2006-06-30 -0.026847 0.079631 0.049936 0.053769 2006-07-31 -0.076540 -0.179754 0.024754 -0.709651 2006-08-31 -0.005654 0.053004 -0.048511 0.079274 2006-09-30 0.020871 0.176371 -0.284413 0.045794 2006-10-31 0.003820 0.092538 -0.140816 -0.241310 2006-11-30 0.093881 0.041177 -0.074243 0.328912 2006-12-31 0.228152 0.059685 -0.066820 0.854066 2007-01-31 -0.000606 -0.018971 -0.097439 -0.458704 2007-02-28 -0.002901 0.059865 -0.009066 0.396952 2007-03-31 0.072735 0.137236 0.199801 0.498873 2007-04-30 0.179505 0.090948 0.078473 -0.373307 2007-05-31 0.041768 -0.191828 0.398936 0.386379 2007-06-30 -0.074250 0.029058 0.408891 0.908853 2007-07-31 0.184423 0.288949 -0.043115 0.966801 ... ... ... ... ... 2016-07-31 -0.001473 0.020989 -0.102061 0.076902 2016-08-31 0.012562 0.161776 -0.085519 -0.247013 2016-09-30 -0.059939 -0.093870 -0.086738 0.670412 2016-10-31 0.005182 0.103205 -0.050139 0.267527 2016-11-30 0.017432 0.166481 0.121476 0.260409 2016-12-31 -0.076745 0.100230 -0.021576 -0.153930 2017-01-31 -0.014558 0.139149 0.264458 0.339240 2017-02-28 -0.001879 0.152577 -0.112416 -1.895912 2017-03-31 -0.050318 -0.009204 -0.059419 -0.180562 2017-04-30 -0.063030 -0.079234 0.138592 0.009644 2017-05-31 -0.044469 -0.048791 0.125610 0.025278 2017-06-30 -0.001582 0.143604 -0.042734 -0.242222 2017-07-31 -0.006245 0.135098 0.036456 -0.351384 2017-08-31 -0.007558 0.034874 -0.022269 0.090687 2017-09-30 -0.034945 0.017867 -0.040516 -0.479189 2017-10-31 -0.018035 0.040813 0.258286 -0.007300 2017-11-30 -0.046563 -0.130333 0.003646 -0.518055 2017-12-31 -0.021775 0.028806 0.019140 -0.190128 2018-01-31 0.033635 0.123030 0.056636 -0.564453 2018-02-28 -0.088143 -0.229614 -0.077597 1.129363 2018-03-31 -0.033574 -0.122403 -0.055747 -0.977634 2018-04-30 -0.036027 -0.151639 -0.083731 -0.413204 2018-05-31 -0.003910 -0.296007 0.030731 -0.048278 2018-06-30 -0.080242 -0.060674 -0.077563 0.202379 2018-07-31 0.009905 0.012160 0.058504 0.244123 2018-08-31 -0.055743 -0.119400 0.127473 0.318598 2018-09-30 0.039097 0.027331 0.090624 0.281444 2018-10-31 -0.076072 -0.080909 -0.062435 0.146284 2018-11-30 -0.002306 -0.053731 -0.108560 -0.235285 2018-12-31 -0.039496 -0.113840 -0.104135 -0.149503 [167 rows x 4 columns]
#计算上证A股折现率消息与现金流消息的时间序列I = np.eye(4)A=0.95*ae=np.array([1,0,0,0])lam=np.matmul(A,(I-np.linalg.inv(A)))for i in Resid.index: u=np.array(Resid.loc[i])Ncf=np.matmul((e+np.matmul(e,lam)),u)Ndr=np.matmul((np.matmul(e,lam)),u)df.loc[i,'ncf']=Ncfdf.loc[i,'ndr']=Ndrprint(df['ncf'].describe(),df['ndr'].describe())df['ncf'].plot()df['ndr'].plot()
count 1.670000e+02 mean 5.699787e-17 std 7.300104e-03 min -2.154433e-02 25% -4.891029e-03 50% 4.542201e-04 75% 5.432716e-03 max 2.049594e-02 Name: ncf, dtype: float64 count 1.670000e+02 mean -1.620460e-17 std 7.501867e-02 min -2.260018e-01 25% -4.274980e-02 50% 2.567926e-03 75% 3.791603e-02 max 2.588274e-01 Name: ndr, dtype: float64
<matplotlib.axes._subplots.AxesSubplot at 0x7f17ef299048>
covCF=df['ncf'].cov(df['return'])covDR=-df['ndr'].cov(df['return'])Var=(df['return'].describe()['std'])**2print('现金流贝塔:',covCF/Var,'折现率贝塔:',covDR/Var)
现金流贝塔: 0.0424198211009 折现率贝塔: 0.863931686206
#获取不同板块的历史数据data=pd.read_excel('DATA!.xls')print(data)data.index=data['date']jr=data[['金融re','bond','CPI','金融pb']]zq=data[['周期re','bond','CPI','周期pb']]cz=data[['成长re','bond','CPI','成长pb']]xf=data[['消费re','bond','CPI','消费pb']]jr.rename(columns={'金融re':'re'}, inplace = True)zq.rename(columns={'周期re':'re'}, inplace = True)cz.rename(columns={'成长re':'re'}, inplace = True)xf.rename(columns={'消费re':'re'}, inplace = True)name=[jr,zq,xf,cz]
date bond PMI CPI 金融re 周期re 消费re \ 0 2005-01-31 4.8982 54.7 1.900000 -0.068274 -0.061308 -0.062734 1 2005-02-28 4.8334 54.5 3.900000 0.098269 0.098238 0.101943 2 2005-03-31 4.1203 57.9 2.700000 -0.119966 -0.103670 -0.114046 3 2005-04-30 4.0441 56.7 1.800000 0.015521 -0.044644 -0.045168 4 2005-05-31 3.8469 52.9 1.800000 -0.047885 -0.094199 -0.078986 5 2005-06-30 3.6530 51.7 1.600000 -0.015203 -0.006579 0.007896 6 2005-07-31 3.3874 51.1 1.800000 0.018944 -0.050600 -0.058751 7 2005-08-31 3.5608 52.6 1.300000 0.004354 0.069031 0.101977 8 2005-09-30 3.2404 55.1 0.900000 -0.016611 0.012824 0.029243 9 2005-10-31 3.0613 54.1 1.200000 -0.086445 -0.058770 -0.063804 10 2005-11-30 3.3255 54.1 1.300000 0.034017 -0.034664 -0.009441 11 2005-12-31 3.1216 54.3 1.600000 0.045143 0.042448 0.024308 12 2006-01-31 2.9795 52.1 1.900000 0.142597 0.085323 0.093265 13 2006-02-28 2.9266 52.1 0.900000 0.015200 0.021288 0.004504 14 2006-03-31 2.9400 55.3 0.800000 0.003036 0.009571 -0.024092 15 2006-04-30 3.0000 58.1 1.200000 0.115955 0.054848 0.099853 16 2006-05-31 3.0200 54.8 1.400000 0.163050 0.132024 0.165309 17 2006-06-30 3.1650 54.1 1.500000 0.041701 -0.016313 0.009046 18 2006-07-31 3.2600 52.4 1.000000 -0.285537 -0.071389 -0.061272 19 2006-08-31 3.2380 53.1 1.300000 0.025293 0.015776 0.022612 20 2006-09-30 2.9990 57.0 1.500000 0.070928 0.025514 0.098778 21 2006-10-31 2.9512 54.7 1.400000 -0.052592 0.022429 -0.012339 22 2006-11-30 2.9781 55.3 1.900000 0.169021 0.153532 0.086508 23 2006-12-31 3.0269 54.8 2.800000 0.414636 0.127685 0.080751 24 2007-01-31 3.0671 55.1 2.200000 0.762682 0.184001 0.217808 25 2007-02-28 3.1466 53.1 2.700000 -0.017515 0.128323 0.131881 26 2007-03-31 3.4338 56.1 3.300000 0.113502 0.121443 0.115085 27 2007-04-30 3.5916 58.6 3.000000 0.171164 0.323361 0.303623 28 2007-05-31 4.0600 55.7 3.400000 -0.002319 0.124386 0.119446 29 2007-06-30 4.4500 54.5 4.400000 -0.001679 -0.067533 -0.147785 .. ... ... ... ... ... ... ... 138 2016-07-31 2.7768 49.9 1.765113 -0.010535 0.004478 -0.015108 139 2016-08-31 2.7425 50.4 1.339773 0.056989 0.048771 0.033711 140 2016-09-30 2.7258 50.4 1.920226 -0.031774 -0.021781 -0.023695 141 2016-10-31 2.7425 51.2 2.095947 0.022095 0.042581 0.028202 142 2016-11-30 2.9502 51.7 2.252258 0.067608 0.060748 0.032601 143 2016-12-31 3.0115 51.4 2.076545 -0.058164 -0.038223 -0.038842 144 2017-01-31 3.3465 51.3 2.549055 0.026139 0.015431 -0.008682 145 2017-02-28 3.2921 51.6 0.800000 0.011873 0.040981 0.046099 146 2017-03-31 3.2828 51.8 0.900000 -0.016394 0.002169 -0.006814 147 2017-04-30 3.4668 51.2 1.200000 -0.010983 -0.016270 -0.049310 148 2017-05-31 3.6102 51.2 1.500000 0.040525 -0.056596 -0.064238 149 2017-06-30 3.5683 51.7 1.500000 0.014556 0.019901 0.005749 150 2017-07-31 3.6260 51.4 1.400000 0.037567 0.054518 -0.037070 151 2017-08-31 3.6265 51.7 1.800000 0.052223 0.026917 0.022471 152 2017-09-30 3.6136 52.4 1.600000 -0.021609 -0.006797 0.023296 153 2017-10-31 3.8917 51.6 1.900000 0.018933 0.000839 0.022689 154 2017-11-30 3.8901 51.8 1.700000 0.002236 -0.016110 -0.051743 155 2017-12-31 3.8807 51.6 1.800000 -0.010678 -0.018351 0.013356 156 2018-01-31 3.9140 51.3 1.500000 0.098295 0.046954 0.011636 157 2018-02-28 3.8202 50.3 2.900000 -0.092636 -0.053236 -0.043896 158 2018-03-31 3.7407 51.5 2.100000 -0.055581 -0.029373 0.008008 159 2018-04-30 3.6217 51.4 1.800000 -0.029747 -0.034390 -0.041056 160 2018-05-31 3.6129 51.9 1.800000 -0.032004 -0.022650 0.032715 161 2018-06-30 3.4756 51.5 1.900000 -0.076873 -0.097839 -0.096921 162 2018-07-31 3.4812 51.2 2.100000 0.026288 0.027894 -0.040345 163 2018-08-31 3.5751 51.3 2.300000 -0.028589 -0.062900 -0.092842 164 2018-09-30 3.6103 50.8 2.500000 0.048723 0.017632 0.012431 165 2018-10-31 3.5086 50.2 2.500000 -0.023848 -0.078156 -0.116540 166 2018-11-30 3.3550 50.0 2.200000 -0.051802 -0.008773 0.044212 167 2018-12-31 3.2265 49.4 1.900000 -0.058233 -0.033432 -0.032367 成长re 金融pb 消费pb 周期pb 成长pb 0 -0.107725 0.000000 0.000000 0.000000 0.000000 1 0.119380 0.120066 0.103448 0.130030 0.082651 2 -0.194296 0.000000 0.000000 0.000000 0.000000 3 -0.109691 0.048571 0.000000 0.000000 0.000000 4 -0.023197 0.000000 0.000000 0.000000 0.024014 5 -0.005164 0.153269 0.091501 0.142042 0.076674 6 -0.057326 0.112716 0.048015 0.048768 0.016811 7 0.165440 0.202038 0.236047 0.221293 0.202546 8 0.044009 0.166106 0.371571 0.260480 0.278787 9 -0.070360 0.064163 0.203828 0.101473 0.200054 10 -0.032596 0.264768 0.260957 0.107485 0.218358 11 0.016955 0.394564 0.265458 0.140851 0.264721 12 0.219638 0.486222 0.500067 0.311460 0.649964 13 -0.046700 1.000000 0.538717 0.377055 0.705210 14 -0.000631 0.708376 0.585609 0.485848 0.721814 15 0.071190 1.000000 1.000000 0.880840 1.000000 16 0.174524 1.000000 1.000000 0.897501 0.831189 17 -0.011463 1.000000 1.000000 0.960522 0.902142 18 -0.092472 0.664604 0.917072 0.854969 0.861064 19 0.054783 0.687714 0.765790 0.623194 0.650879 20 0.250801 0.889498 1.000000 0.821013 1.000000 21 -0.046336 0.995127 0.870506 0.762171 0.783947 22 0.023466 1.000000 0.816871 0.868297 0.696528 23 0.036313 1.000000 0.946621 0.992525 0.744652 24 0.294649 1.000000 0.992512 1.000000 0.966390 25 0.123665 0.862244 1.000000 1.000000 1.000000 26 0.093432 0.914498 0.979398 1.000000 1.000000 27 0.235259 1.000000 1.000000 1.000000 1.000000 28 0.091471 1.000000 1.000000 1.000000 1.000000 29 -0.125884 0.823667 0.576062 0.731394 0.569162 .. ... ... ... ... ... 138 -0.017845 0.172918 0.349258 0.253902 0.241916 139 0.046039 0.268404 0.449872 0.416609 0.325675 140 -0.036668 0.217775 0.405080 0.466546 0.273151 141 0.034048 0.257670 0.346998 0.528195 0.268960 142 0.029539 0.426818 0.413709 0.671293 0.225131 143 -0.063398 0.267073 0.251244 0.609876 0.092892 144 -0.014879 0.719962 0.256451 0.645570 0.157922 145 0.040121 0.866740 0.502437 0.934472 0.468504 146 0.006892 0.714221 0.089227 0.699890 0.207217 147 -0.049631 0.197466 0.000000 0.444715 0.000000 148 -0.103943 0.169564 0.005628 0.000000 0.000000 149 0.027049 0.364920 0.181965 0.276774 0.128716 150 -0.030939 0.549307 0.068184 0.319577 0.025094 151 0.062916 0.644767 0.108232 0.429130 0.110677 152 0.041491 0.553783 0.138610 0.591780 0.362432 153 -0.026969 0.426480 0.140393 0.481019 0.303851 154 -0.038878 0.258848 0.000000 0.000000 0.055349 155 -0.009751 0.098040 0.013486 0.015237 0.000000 156 -0.052685 0.325288 0.069960 0.134225 0.007167 157 -0.002199 0.114939 0.072435 0.142127 0.067280 158 0.087352 0.041433 0.220483 0.201967 0.336120 159 -0.050689 0.000000 0.092244 0.012968 0.289333 160 -0.072737 0.010428 0.182478 0.055790 0.306946 161 -0.096608 0.057790 0.040165 0.005209 0.113410 162 0.020738 0.180326 0.058649 0.102263 0.192108 163 -0.045815 0.030387 0.000000 0.000000 0.031916 164 -0.015874 0.128725 0.012216 0.039188 0.000000 165 -0.113605 0.260598 0.030067 0.038749 0.026269 166 0.042255 0.218817 0.117255 0.073031 0.111422 167 -0.037889 0.113844 0.048221 0.017674 0.075302 [168 rows x 12 columns]
/opt/conda/envs/python3new/lib/python3.6/site-packages/pandas/core/frame.py:2746: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the c*eats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy **kwargs)
#计算四大板块的折现率贝塔现金流贝塔jr=data[['金融re','bond','CPI','金融pb']]zq=data[['周期re','bond','CPI','周期pb']]cz=data[['成长re','bond','CPI','成长pb']]xf=data[['消费re','bond','CPI','消费pb']]jr.rename(columns={'金融re':'re'}, inplace = True)zq.rename(columns={'周期re':'re'}, inplace = True)cz.rename(columns={'成长re':'re'}, inplace = True)xf.rename(columns={'消费re':'re'}, inplace = True)name=[jr,zq,xf,cz]for j in name:model = VAR(j)results = model.fit(1)COR=results.resida=results.coefsI = np.eye(4)A=0.95*alam=np.matmul(A,(I-np.linalg.inv(A)))e=np.array([1,0,0,0])for i in COR.index: u=np.array(COR.loc[i])Ncf=np.matmul((e+np.matmul(e,lam)),u)Ndr=np.matmul((np.matmul(e,lam)),u)j.loc[i,'ncf']=Ncfj.loc[i,'ndr']=Ndrj['ndr']=-j['ndr']covCF=jr['ncf'].cov(j['re'])covDR=j['ndr'].cov(j['re'])Var=(j['re'].describe()['std'])**2sign=str(j.columns.values.tolist()[3])[:2]print(sign,'现金流beta:',covCF/Var,'折现率beta:',covDR/Var)
/opt/conda/envs/python3new/lib/python3.6/site-packages/pandas/core/frame.py:2746: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the c*eats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy **kwargs) /opt/conda/envs/python3new/lib/python3.6/site-packages/pandas/core/indexing.py:337: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the c*eats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self.obj[key] = _infer_fill_value(value) /opt/conda/envs/python3new/lib/python3.6/site-packages/pandas/core/indexing.py:341: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the c*eats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self._setitem_with_indexer(new_indexer, value) /opt/conda/envs/python3new/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the c*eats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self._setitem_with_indexer(indexer, value) /opt/conda/envs/python3new/lib/python3.6/site-packages/ipykernel_launcher.py:24: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the c*eats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy /opt/conda/envs/python3new/lib/python3.6/site-packages/ipykernel_launcher.py:25: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the c*eats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy /opt/conda/envs/python3new/lib/python3.6/site-packages/ipykernel_launcher.py:26: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the c*eats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
金融 现金流beta: 0.120946573195 折现率beta: 0.792699930258 周期 现金流beta: 0.119074437865 折现率beta: 0.828971959593 消费 现金流beta: 0.0935300598055 折现率beta: 0.870311393317 成长 现金流beta: 0.0661583347979 折现率beta: 0.899570493721
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程