#%config InlineBackend.figure_format = 'retina' #可提高图形显示的清晰度
import pandas as pd #加载数据分析包pandas
pd.set_option('display.precision',4) #设置pandas输出精度
pd.set_option('display.max_rows', 10)
YQdata=pd.read_excel('DaPy_data.xlsx','YQdata',index_col=0)
YQdata
Q1 | Q2 | Q3 | Q4 | |
---|---|---|---|---|
Year | ||||
2001 | 2.330 | 2.565 | 2.687 | 3.384 |
2002 | 2.536 | 2.797 | 2.972 | 3.728 |
2003 | 2.886 | 3.101 | 3.346 | 4.249 |
2004 | 3.342 | 3.699 | 3.956 | 4.991 |
2005 | 3.912 | 4.280 | 4.474 | 5.828 |
... | ... | ... | ... | ... |
2011 | 9.748 | 10.901 | 11.586 | 15.076 |
2012 | 10.837 | 11.963 | 12.574 | 16.573 |
2013 | 11.886 | 12.916 | 13.908 | 20.092 |
2014 | 12.821 | 14.083 | 15.086 | 21.656 |
2015 | 14.067 | 17.351 | 17.316 | 18.937 |
15 rows × 4 columns
QTdata=pd.read_excel('DaPy_data.xlsx','QTdata',index_col=0)
QTdata
GDP | |
---|---|
YQ | |
2001Q1 | 2.330 |
2001Q2 | 2.565 |
2001Q3 | 2.687 |
2001Q4 | 3.384 |
2002Q1 | 2.536 |
... | ... |
2014Q4 | 21.656 |
2015Q1 | 14.067 |
2015Q2 | 17.351 |
2015Q3 | 17.316 |
2015Q4 | 18.937 |
60 rows × 1 columns
QTdata.plot(grid=True);
#生成年度变量
QTdata['Year']=QTdata.index.str[:4];QTdata
GDP | Year | |
---|---|---|
YQ | ||
2001Q1 | 2.330 | 2001 |
2001Q2 | 2.565 | 2001 |
2001Q3 | 2.687 | 2001 |
2001Q4 | 3.384 | 2001 |
2002Q1 | 2.536 | 2002 |
... | ... | ... |
2014Q4 | 21.656 | 2014 |
2015Q1 | 14.067 | 2015 |
2015Q2 | 17.351 | 2015 |
2015Q3 | 17.316 | 2015 |
2015Q4 | 18.937 | 2015 |
60 rows × 2 columns
#形成年度时序数据
YGDP=QTdata.groupby(['Year'])['GDP'].sum();YGDP
Year 2001 10.966 2002 12.033 2003 13.582 2004 15.988 2005 18.494 ... 2011 47.311 2012 51.947 2013 58.802 2014 63.646 2015 67.671 Name: GDP, Length: 15, dtype: float64
YGDP.plot(grid=True);
YGDP
YGDPds=pd.DataFrame(YGDP); #构建年度动态序列框
YGDPds
GDP | |
---|---|
Year | |
2001 | 10.966 |
2002 | 12.033 |
2003 | 13.582 |
2004 | 15.988 |
2005 | 18.494 |
... | ... |
2011 | 47.311 |
2012 | 51.947 |
2013 | 58.802 |
2014 | 63.646 |
2015 | 67.671 |
15 rows × 1 columns
YGDPds['定基数']= YGDP-YGDP[:1].values; YGDPds
GDP | 定基数 | |
---|---|---|
Year | ||
2001 | 10.966 | 0.000 |
2002 | 12.033 | 1.067 |
2003 | 13.582 | 2.616 |
2004 | 15.988 | 5.022 |
2005 | 18.494 | 7.528 |
... | ... | ... |
2011 | 47.311 | 36.345 |
2012 | 51.947 | 40.981 |
2013 | 58.802 | 47.836 |
2014 | 63.646 | 52.680 |
2015 | 67.671 | 56.705 |
15 rows × 2 columns
YGDPds['环基数']= YGDP-YGDP.shift(1); YGDPds #shift(1)向下移动1个单位
GDP | 定基数 | 环基数 | |
---|---|---|---|
Year | |||
2001 | 10.966 | 0.000 | NaN |
2002 | 12.033 | 1.067 | 1.067 |
2003 | 13.582 | 2.616 | 1.549 |
2004 | 15.988 | 5.022 | 2.406 |
2005 | 18.494 | 7.528 | 2.506 |
... | ... | ... | ... |
2011 | 47.311 | 36.345 | 7.160 |
2012 | 51.947 | 40.981 | 4.636 |
2013 | 58.802 | 47.836 | 6.855 |
2014 | 63.646 | 52.680 | 4.844 |
2015 | 67.671 | 56.705 | 4.025 |
15 rows × 3 columns
YGDPds['定基比']=YGDP/YGDP[:1].values; YGDPds
GDP | 定基数 | 环基数 | 定基比 | |
---|---|---|---|---|
Year | ||||
2001 | 10.966 | 0.000 | NaN | 1.0000 |
2002 | 12.033 | 1.067 | 1.067 | 1.0973 |
2003 | 13.582 | 2.616 | 1.549 | 1.2386 |
2004 | 15.988 | 5.022 | 2.406 | 1.4580 |
2005 | 18.494 | 7.528 | 2.506 | 1.6865 |
... | ... | ... | ... | ... |
2011 | 47.311 | 36.345 | 7.160 | 4.3143 |
2012 | 51.947 | 40.981 | 4.636 | 4.7371 |
2013 | 58.802 | 47.836 | 6.855 | 5.3622 |
2014 | 63.646 | 52.680 | 4.844 | 5.8039 |
2015 | 67.671 | 56.705 | 4.025 | 6.1710 |
15 rows × 4 columns
YGDPds['环基比']=YGDP/YGDP.shift(1);YGDPds
GDP | 定基数 | 环基数 | 定基比 | 环基比 | |
---|---|---|---|---|---|
Year | |||||
2001 | 10.966 | 0.000 | NaN | 1.0000 | NaN |
2002 | 12.033 | 1.067 | 1.067 | 1.0973 | 1.0973 |
2003 | 13.582 | 2.616 | 1.549 | 1.2386 | 1.1287 |
2004 | 15.988 | 5.022 | 2.406 | 1.4580 | 1.1771 |
2005 | 18.494 | 7.528 | 2.506 | 1.6865 | 1.1567 |
... | ... | ... | ... | ... | ... |
2011 | 47.311 | 36.345 | 7.160 | 4.3143 | 1.1783 |
2012 | 51.947 | 40.981 | 4.636 | 4.7371 | 1.0980 |
2013 | 58.802 | 47.836 | 6.855 | 5.3622 | 1.1320 |
2014 | 63.646 | 52.680 | 4.844 | 5.8039 | 1.0824 |
2015 | 67.671 | 56.705 | 4.025 | 6.1710 | 1.0632 |
15 rows × 5 columns
n=len(YGDP);n
15
ADR=(YGDP[-1:].values/YGDP[:1].values)**(1/n)
print('\n 平均增长量 = %5.3f' % ADR)
平均增长量 = 1.129
import numpy as np
n=20
x=np.arange(n)+1 #生成1:n的等差数列
y=1+2*x
import matplotlib.pyplot as plt
plt.plot(x,y,'o-'); #点线图
y=1+0.2*np.log(x)
plt.plot(x,y,'o-');
(3)指数模型:y=ae^(bx)
y=0.2*np.exp(0.1*x)
plt.plot(x,y,'o-');
y=0.2*x**0.1
plt.plot(x,y,'o-');
import statsmodels.api as sm
Yt=YGDP #Yt=YGDP=QTdata.groupby(['Year'])['GDP'].sum()
plt.plot(Yt,'.')
[<matplotlib.lines.Line2D at 0x24a82cebc40>]
Xt=np.arange(len(Yt))+1; #自变量序列1:n,建模时最好不直接用年份
Xt
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
Yt_L1=sm.OLS(Yt,sm.add_constant(Xt)).fit();
Yt_L1.summary().tables[1]
C:\Users\Lenovo\anaconda3\lib\site-packages\scipy\stats\stats.py:1541: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=15 warnings.warn("kurtosistest only valid for n>=20 ... continuing "
coef | std err | t | P>|t| | [0.025 | 0.975] | |
---|---|---|---|---|---|---|
const | -0.2149 | 1.939 | -0.111 | 0.913 | -4.404 | 3.974 |
x1 | 4.3127 | 0.213 | 20.224 | 0.000 | 3.852 | 4.773 |
#构建一个简单的趋势函数来进行模型选择。
import warnings #忽视警告信息,当例数较少时会有警告
warnings.filterwarnings("ignore")
def trendmodel(y,x): #定义直线回归模型, x为自变量, y为因变量
fm=sm.OLS(y,sm.add_constant(x)).fit()
sfm=fm.summary2()
print("模型检验:\n",sfm.tables[1])
R2=np.corrcoef(x,y)[0,1]**2 #相关系数平方=sfm.tables[0][1][6])
print("决定系数:%5.4f"%R2)
return fm.fittedvalues
L1=trendmodel(Yt,Xt);
plt.plot(Yt,'o',L1,'r-');
模型检验: Coef. Std.Err. t P>|t| [0.025 0.975] const -0.2149 1.9389 -0.1108 9.1344e-01 -4.4035 3.9738 x1 4.3127 0.2132 20.2240 3.3003e-11 3.8520 4.7734 决定系数:0.9692
L2=trendmodel(np.log(Yt),np.log(Xt)); #对Yt取对数
plt.plot(Yt,'o',np.exp(L2),'r-'); #对Yt的拟合值取反对数
模型检验: Coef. Std.Err. t P>|t| [0.025 0.975] const 1.9528 0.1442 13.5470 4.8185e-09 1.6414 2.2642 x1 0.7585 0.0718 10.5627 9.4984e-08 0.6033 0.9136 决定系数:0.8956
(1)简单平均法
Qt=QTdata.GDP;Qt
YQ 2001Q1 2.330 2001Q2 2.565 2001Q3 2.687 2001Q4 3.384 2002Q1 2.536 ... 2014Q4 21.656 2015Q1 14.067 2015Q2 17.351 2015Q3 17.316 2015Q4 18.937 Name: GDP, Length: 60, dtype: float64
Qt.plot()
<AxesSubplot:xlabel='YQ'>
Qt.mean() #季节数据的平均
8.571633333333335
QtM=pd.DataFrame(Qt);QtM
GDP | |
---|---|
YQ | |
2001Q1 | 2.330 |
2001Q2 | 2.565 |
2001Q3 | 2.687 |
2001Q4 | 3.384 |
2002Q1 | 2.536 |
... | ... |
2014Q4 | 21.656 |
2015Q1 | 14.067 |
2015Q2 | 17.351 |
2015Q3 | 17.316 |
2015Q4 | 18.937 |
60 rows × 1 columns
QtM['M2']=Qt.rolling(3).mean(); #2 阶移动平均
QtM.plot()
<AxesSubplot:xlabel='YQ'>
QtM['M4']=Qt.rolling(5).mean(); #4 阶移动平均
#QtM
QtM.plot();
QtE=pd.DataFrame(Qt);
QtE['E03']=Qt.ewm(alpha=0.3).mean(); #平滑系数=0.3
QtE.plot()
<AxesSubplot:xlabel='YQ'>
QtE['E08']=Qt.ewm(alpha=0.8).mean(); #平滑系数=0.8
QtE.plot()
<AxesSubplot:xlabel='YQ'>
QtE.plot();
import pandas as pd
stock=pd.read_excel('DaPy_data.xlsx','Stock',index_col=0);
stock.info()
<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 3180 entries, 2005-01-03 to 2017-12-29 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Open 3165 non-null float64 1 High 3165 non-null float64 2 Low 3165 non-null float64 3 Close 3165 non-null float64 4 Volume 3165 non-null float64 5 Adjusted 3165 non-null float64 dtypes: float64(6) memory usage: 173.9 KB
stock
Open | High | Low | Close | Volume | Adjusted | |
---|---|---|---|---|---|---|
date | ||||||
2005-01-03 | 0.7025 | 0.7173 | 0.7025 | 0.7130 | 0.0000e+00 | 0.6185 |
2005-01-04 | 0.7099 | 0.7215 | 0.6941 | 0.6960 | 1.0959e+07 | 0.6038 |
2005-01-05 | 0.6951 | 0.7082 | 0.6951 | 0.7053 | 6.1651e+06 | 0.6118 |
2005-01-06 | 0.7023 | 0.7065 | 0.6961 | 0.6968 | 9.8460e+06 | 0.6044 |
2005-01-07 | 0.6960 | 0.7096 | 0.6946 | 0.7020 | 1.3667e+07 | 0.6090 |
... | ... | ... | ... | ... | ... | ... |
2017-12-25 | 12.7300 | 12.7400 | 12.2500 | 12.3800 | 6.5682e+07 | 12.3800 |
2017-12-26 | 12.4600 | 12.5400 | 12.3700 | 12.5200 | 3.0913e+07 | 12.5200 |
2017-12-27 | 12.5400 | 12.5700 | 12.1000 | 12.1800 | 5.3813e+07 | 12.1800 |
2017-12-28 | 12.2000 | 12.2800 | 12.0600 | 12.1800 | 3.3693e+07 | 12.1800 |
2017-12-29 | 12.1800 | 12.3300 | 12.1400 | 12.2900 | 2.5372e+07 | 12.2900 |
3180 rows × 6 columns
stock=stock.dropna() #由于数据中有 15 个缺失值,故须删除缺失数据 NA
stock#.info()
Open | High | Low | Close | Volume | Adjusted | |
---|---|---|---|---|---|---|
date | ||||||
2005-01-03 | 0.7025 | 0.7173 | 0.7025 | 0.7130 | 0.0000e+00 | 0.6185 |
2005-01-04 | 0.7099 | 0.7215 | 0.6941 | 0.6960 | 1.0959e+07 | 0.6038 |
2005-01-05 | 0.6951 | 0.7082 | 0.6951 | 0.7053 | 6.1651e+06 | 0.6118 |
2005-01-06 | 0.7023 | 0.7065 | 0.6961 | 0.6968 | 9.8460e+06 | 0.6044 |
2005-01-07 | 0.6960 | 0.7096 | 0.6946 | 0.7020 | 1.3667e+07 | 0.6090 |
... | ... | ... | ... | ... | ... | ... |
2017-12-25 | 12.7300 | 12.7400 | 12.2500 | 12.3800 | 6.5682e+07 | 12.3800 |
2017-12-26 | 12.4600 | 12.5400 | 12.3700 | 12.5200 | 3.0913e+07 | 12.5200 |
2017-12-27 | 12.5400 | 12.5700 | 12.1000 | 12.1800 | 5.3813e+07 | 12.1800 |
2017-12-28 | 12.2000 | 12.2800 | 12.0600 | 12.1800 | 3.3693e+07 | 12.1800 |
2017-12-29 | 12.1800 | 12.3300 | 12.1400 | 12.2900 | 2.5372e+07 | 12.2900 |
3165 rows × 6 columns
stock.describe().round(2) #round(stock.describe(),3)
Open | High | Low | Close | Volume | Adjusted | |
---|---|---|---|---|---|---|
count | 3165.00 | 3165.00 | 3165.00 | 3165.00 | 3.1650e+03 | 3165.00 |
mean | 9.22 | 9.42 | 9.05 | 9.23 | 7.1767e+07 | 8.91 |
std | 4.22 | 4.31 | 4.12 | 4.22 | 9.3363e+07 | 4.10 |
min | 0.70 | 0.71 | 0.68 | 0.70 | 0.0000e+00 | 0.60 |
25% | 6.73 | 6.89 | 6.63 | 6.75 | 2.2869e+07 | 6.55 |
50% | 10.34 | 10.54 | 10.15 | 10.35 | 4.0174e+07 | 9.96 |
75% | 12.03 | 12.28 | 11.86 | 12.05 | 7.7414e+07 | 11.67 |
max | 22.28 | 23.54 | 21.75 | 22.80 | 8.5600e+08 | 22.52 |
stock[['Close','Volume']] #收盘价与成交量数据
Close | Volume | |
---|---|---|
date | ||
2005-01-03 | 0.7130 | 0.0000e+00 |
2005-01-04 | 0.6960 | 1.0959e+07 |
2005-01-05 | 0.7053 | 6.1651e+06 |
2005-01-06 | 0.6968 | 9.8460e+06 |
2005-01-07 | 0.7020 | 1.3667e+07 |
... | ... | ... |
2017-12-25 | 12.3800 | 6.5682e+07 |
2017-12-26 | 12.5200 | 3.0913e+07 |
2017-12-27 | 12.1800 | 5.3813e+07 |
2017-12-28 | 12.1800 | 3.3693e+07 |
2017-12-29 | 12.2900 | 2.5372e+07 |
3165 rows × 2 columns
stock['2015']['Close'] #年度收盘价数据
date 2015-01-05 9.36 2015-01-06 9.48 2015-01-07 9.34 2015-01-08 9.53 2015-01-09 9.37 ... 2015-12-25 14.00 2015-12-28 13.51 2015-12-29 13.68 2015-12-30 13.75 2015-12-31 13.45 Name: Close, Length: 244, dtype: float64
stock['2017-12']['Close'] #月度价格
#plt.plot(df.index,df.values); #stock['2015']['Close'].plot();
date 2017-12-01 12.01 2017-12-04 12.05 2017-12-05 11.88 2017-12-06 12.33 2017-12-07 12.21 ... 2017-12-25 12.38 2017-12-26 12.52 2017-12-27 12.18 2017-12-28 12.18 2017-12-29 12.29 Name: Close, Length: 21, dtype: float64
stock['Close'].plot(); #收盘价数据框图
YC=stock['2015']['Close']; #年度收盘价数据
YC.index=YC.index.strftime('%Y-%m-%d'); #将横坐标设置为年-月格式
YC.plot();
stock['Volume'].hist(); #成交量分布情况
CV=stock[['Close','Volume']];CV.plot() #收盘价和成交量数据
<AxesSubplot:xlabel='date'>
CV.index=CV.index.strftime('%Y'); #将横坐标设置为年格式
CV.plot(secondary_y='Volume'); #右坐标轴绘制成交量
YCV=stock['2015'][['Close','Volume']] #年度收盘价和成交量数据
YCV.index=YCV.index.strftime('%Y-%m'); #将横坐标设置为年-月-日格式
YCV.plot(secondary_y='Volume');
SC=stock['2015']['Close']; SC #2015年收盘价数据
date 2015-01-05 9.36 2015-01-06 9.48 2015-01-07 9.34 2015-01-08 9.53 2015-01-09 9.37 ... 2015-12-25 14.00 2015-12-28 13.51 2015-12-29 13.68 2015-12-30 13.75 2015-12-31 13.45 Name: Close, Length: 244, dtype: float64
SCM=pd.DataFrame(SC);SCM #构建数据框
SCM['MA5']=SC.rolling(6).mean(); #5日移动平均
SCM['MA20']=SC.rolling(21).mean(); #20日移动平均
SCM['MA60']=SC.rolling(61).mean(); #60日移动平均
SCM.index=SCM.index.strftime('%Y-%m-%d'); #设定时间轴:月-天
SCM.plot(figsize=(10,6),grid=True); # 移动平均线
SCM.plot(subplots=False,figsize=(12,7),grid=True);
SCM.plot(subplots=True,figsize=(12,14),grid=True);
#!pip install mplfinance --upgrade
OHLCV=stock['2015-10':'2015-12'][['Open','High','Low','Close','Volume']];OHLCV
Open | High | Low | Close | Volume | |
---|---|---|---|---|---|
date | |||||
2015-10-08 | 12.12 | 12.94 | 12.12 | 12.70 | 2.0044e+08 |
2015-10-09 | 12.72 | 13.32 | 12.61 | 13.05 | 2.1790e+08 |
2015-10-12 | 13.10 | 14.18 | 13.03 | 13.90 | 3.4242e+08 |
2015-10-13 | 13.71 | 13.93 | 13.62 | 13.85 | 1.9723e+08 |
2015-10-14 | 13.75 | 14.47 | 13.74 | 14.00 | 3.0091e+08 |
... | ... | ... | ... | ... | ... |
2015-12-25 | 14.00 | 14.00 | 14.00 | 14.00 | 0.0000e+00 |
2015-12-28 | 13.92 | 14.00 | 13.50 | 13.51 | 1.1940e+08 |
2015-12-29 | 13.50 | 13.76 | 13.38 | 13.68 | 7.9428e+07 |
2015-12-30 | 13.69 | 13.87 | 13.64 | 13.75 | 6.3783e+07 |
2015-12-31 | 13.79 | 13.79 | 13.35 | 13.45 | 6.9984e+07 |
61 rows × 5 columns
import matplotlib.pyplot as plt
import mplfinance as mpf #加载matplotlib的金融分析模块
mpf.plot(OHLCV,type='ohlc'); #ohlc图
mpf.plot(OHLCV,type='candle'); #candle图
mpf.plot(OHLCV,type='candle',volume=True) #价格candle图及成交量直方图
mpf.plot(OHLCV,type='candle',volume=True,mav=(3,6)) #增加3与6日移动平均线
def Return(Yt): #计算收益率函数
Rt=Yt/Yt.shift(1)-1 #Yt_1=Yt.shift(1)
return(Rt)
SA=stock['2015']['Adjusted']; SA[:10] #2015年调整收盘价
SA_R=Return(SA); SA_R.plot()
<AxesSubplot:xlabel='date'>
SA_R.index=SA_R.index.strftime('%m-%d'); #设定时间轴:月-天
plt.rcParams['axes.unicode_minus']=False; #正常显示图中正负号
plt.stem(SA_R); #SA_R.plot(x=SA_R.index).axhline(y=0);
YR=pd.DataFrame({'Year':stock.index.year,'Adjusted':
Return(stock['Adjusted'])});YR
Year | Adjusted | |
---|---|---|
date | ||
2005-01-03 | 2005 | NaN |
2005-01-04 | 2005 | -0.0238 |
2005-01-05 | 2005 | 0.0133 |
2005-01-06 | 2005 | -0.0120 |
2005-01-07 | 2005 | 0.0075 |
... | ... | ... |
2017-12-25 | 2017 | -0.0290 |
2017-12-26 | 2017 | 0.0113 |
2017-12-27 | 2017 | -0.0272 |
2017-12-28 | 2017 | 0.0000 |
2017-12-29 | 2017 | 0.0090 |
3165 rows × 2 columns
YRm=YR.groupby(['Year']).mean();YRm #年度平均收益率
Adjusted | |
---|---|
Year | |
2005 | 0.0023 |
2006 | 0.0069 |
2007 | 0.0054 |
2008 | -0.0021 |
2009 | 0.0027 |
... | ... |
2013 | 0.0019 |
2014 | 0.0003 |
2015 | 0.0027 |
2016 | -0.0004 |
2017 | 0.0005 |
13 rows × 1 columns
YRm.plot(kind='bar').axhline(y=0);
YMR=pd.DataFrame({'Year':stock.index.year,'Month':stock.index.month,
'Adjusted':Return(stock['Adjusted'])}); YMR
Year | Month | Adjusted | |
---|---|---|---|
date | |||
2005-01-03 | 2005 | 1 | NaN |
2005-01-04 | 2005 | 1 | -0.0238 |
2005-01-05 | 2005 | 1 | 0.0133 |
2005-01-06 | 2005 | 1 | -0.0120 |
2005-01-07 | 2005 | 1 | 0.0075 |
... | ... | ... | ... |
2017-12-25 | 2017 | 12 | -0.0290 |
2017-12-26 | 2017 | 12 | 0.0113 |
2017-12-27 | 2017 | 12 | -0.0272 |
2017-12-28 | 2017 | 12 | 0.0000 |
2017-12-29 | 2017 | 12 | 0.0090 |
3165 rows × 3 columns
YMRm=YMR.groupby(['Year','Month']).mean(); YMRm #按年度和月度计算平均收益率
Adjusted | ||
---|---|---|
Year | Month | |
2005 | 1 | 0.0028 |
2 | 0.0030 | |
3 | 0.0066 | |
4 | 0.0060 | |
5 | -0.0039 | |
... | ... | ... |
2017 | 8 | 0.0002 |
9 | 0.0048 | |
10 | 0.0060 | |
11 | -0.0084 | |
12 | 0.0016 |
156 rows × 1 columns
YMRm.unstack().round(4) #结果重排
Adjusted | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Month | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
Year | ||||||||||||
2005 | 0.0028 | 0.0030 | 0.0066 | 0.0060 | -0.0039 | 0.0061 | 0.0035 | -0.0074 | 0.0067 | -0.0010 | -0.0015 | 0.0070 |
2006 | -0.0018 | 0.0125 | 0.0104 | 0.0075 | 0.0194 | 0.0045 | -0.0038 | 0.0022 | 0.0031 | -0.0022 | 0.0161 | 0.0131 |
2007 | 0.0208 | -0.0101 | 0.0058 | 0.0099 | 0.0147 | -0.0033 | 0.0054 | 0.0137 | -0.0012 | -0.0000 | -0.0077 | 0.0124 |
2008 | -0.0074 | 0.0055 | -0.0072 | 0.0011 | -0.0035 | -0.0102 | 0.0046 | -0.0051 | -0.0061 | -0.0115 | 0.0159 | -0.0003 |
2009 | -0.0083 | 0.0018 | 0.0056 | 0.0111 | -0.0012 | 0.0048 | -0.0014 | -0.0045 | 0.0077 | -0.0007 | 0.0086 | 0.0041 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2013 | 0.0037 | -0.0046 | -0.0016 | -0.0044 | 0.0034 | -0.0128 | 0.0058 | 0.0165 | 0.0265 | -0.0088 | 0.0019 | -0.0082 |
2014 | 0.0069 | -0.0060 | -0.0131 | -0.0044 | 0.0046 | -0.0025 | 0.0034 | 0.0026 | 0.0073 | -0.0019 | 0.0011 | 0.0037 |
2015 | 0.0093 | 0.0065 | 0.0056 | 0.0005 | 0.0192 | -0.0077 | -0.0026 | 0.0042 | -0.0073 | 0.0183 | -0.0076 | -0.0007 |
2016 | -0.0106 | -0.0016 | 0.0050 | -0.0011 | -0.0002 | -0.0005 | -0.0013 | 0.0023 | -0.0011 | 0.0021 | 0.0019 | -0.0007 |
2017 | -0.0018 | 0.0013 | -0.0021 | -0.0036 | 0.0025 | 0.0031 | 0.0030 | 0.0002 | 0.0048 | 0.0060 | -0.0084 | 0.0016 |
13 rows × 12 columns
YMRm.plot().axhline(y=0);
MRm=YMR['2005'].groupby(['Month']).mean();MRm #2005年每月平均收益率
Year | Adjusted | |
---|---|---|
Month | ||
1 | 2005.0 | 0.0028 |
2 | 2005.0 | 0.0030 |
3 | 2005.0 | 0.0066 |
4 | 2005.0 | 0.0060 |
5 | 2005.0 | -0.0039 |
... | ... | ... |
8 | 2005.0 | -0.0074 |
9 | 2005.0 | 0.0067 |
10 | 2005.0 | -0.0010 |
11 | 2005.0 | -0.0015 |
12 | 2005.0 | 0.0070 |
12 rows × 2 columns
MRm['Adjusted'].plot(kind='bar').axhline(y=0);
plt.rcParams.update(plt.rcParamsDefault) #恢复matplotlib原来的显示模式