Python 如何计算熊猫的年收益率

Python 如何计算熊猫的年收益率,python,pandas,finance,quandl,Python,Pandas,Finance,Quandl,因此,我一直在研究SP500的年度回报,并从我的quandl订阅中下载信息。我使用了重采样()和pct_change()来研究数据,但由于某些原因,我的结果与预期不符 sp500_df = quandl.get("MULTPL/SP500_REAL_PRICE_MONTH", authtoken="YOUR OWN AUTH KEY") sp500_Y_ret_df = sp500_df['Value'].resample('Y').mean().pct_

因此,我一直在研究SP500的年度回报,并从我的quandl订阅中下载信息。我使用了重采样()和pct_change()来研究数据,但由于某些原因,我的结果与预期不符

sp500_df = quandl.get("MULTPL/SP500_REAL_PRICE_MONTH", authtoken="YOUR OWN AUTH KEY")
sp500_Y_ret_df = sp500_df['Value'].resample('Y').mean().pct_change().dropna()
截至2008年底,标普500指数的预期回报率应为-38.5%,但我的代码显示,出于某种原因,回报率为-17%?如果由于某种原因您无法访问数据,我可以为数据提供一个.csv文件。非常感谢你的帮助

sp500_Y_ret_df.loc['2008-12-31']
输出:

-0.17319465450687388
2001-12-31   -0.164631
2002-12-31   -0.164795
2003-12-31   -0.032081
2004-12-31    0.173145
2005-12-31    0.067678
2006-12-31    0.085836
2007-12-31    0.126625
2008-12-31   -0.173195
2009-12-31   -0.224552
2010-12-31    0.203406
2011-12-31    0.113738
2012-12-31    0.087221
2013-12-31    0.190603
2014-12-31    0.175436
2015-12-31    0.067610
2016-12-31    0.014868
2017-12-31    0.170363
2018-12-31    0.121093
2019-12-31    0.065247
2020-12-31    0.061747
Freq: A-DEC, Name: Value, dtype: float64
过去20年:

sp500_Y_ret_df.tail(20)
输出:

-0.17319465450687388
2001-12-31   -0.164631
2002-12-31   -0.164795
2003-12-31   -0.032081
2004-12-31    0.173145
2005-12-31    0.067678
2006-12-31    0.085836
2007-12-31    0.126625
2008-12-31   -0.173195
2009-12-31   -0.224552
2010-12-31    0.203406
2011-12-31    0.113738
2012-12-31    0.087221
2013-12-31    0.190603
2014-12-31    0.175436
2015-12-31    0.067610
2016-12-31    0.014868
2017-12-31    0.170363
2018-12-31    0.121093
2019-12-31    0.065247
2020-12-31    0.061747
Freq: A-DEC, Name: Value, dtype: float64
使用随机生成的数据:

aapl_df = pd.DataFrame({ 
'ticker':np.repeat( ['aapl'], 2500 ),
'date':pd.date_range('1/1/2011', periods=2500, freq='D'),
'price':(np.random.randn(2500).cumsum() + 10) }).set_index('date')
aapl_df.head()

date        
2011-01-01  aapl    9.011290
2011-01-02  aapl    9.092603
2011-01-03  aapl    9.139830
2011-01-04  aapl    7.782112
2011-01-05  aapl    8.316270
如前所述,使用“last”可以产生更接近的结果,但不确定这是否纯粹是运气

aapl_Y_ret_df = aapl_df['price'].resample('Y').last()
aapl_Y_ret_df.tail()
输出

    date
2013-12-31    18.535328
2014-12-31    15.201832
2015-12-31    36.040411
2016-12-31    42.272464
2017-12-31    20.421079
Freq: A-DEC, Name: price, dtype: float64
--

  • 通过查找
    关闭
    调整关闭
    中的每日百分比变化来计算年度回报,然后
    求和
    并乘以100
  • 使用
    groupby
    按年份获取值
  • df['Adj Close'].resample('Y').mean()
    返回每年
    'Adj Close'
    值的平均值,这不是确定年度回报的方法。
    • 从2007年到2008年,平均收盘价的百分比变化为
      -17.4%
      。这不是回报
将数据读取器导入web
作为pd进口熊猫
#加载标准普尔500指数数据
df=web.DataReader(“^gspc”,data_source='yahoo',start='2000-01-01',end='2020-01-01')。重置索引()
#显示(df)
日期高低开闭量调整关闭
0 2000-01-03  1478.000000  1438.359985  1469.250000  1455.219971   931800000  1455.219971
1 2000-01-04  1455.219971  1397.430054  1455.219971  1399.420044  1009000000  1399.420044
2 2000-01-05  1413.270020  1377.680054  1399.420044  1402.109985  1085500000  1402.109985
3 2000-01-06  1411.900024  1392.099976  1402.109985  1403.449951  1092300000  1403.449951
4 2000-01-07  1441.469971  1400.729980  1403.449951  1441.469971  1225200000  1441.469971
#按年份分组,并确定每年的每日百分比变化,并将其作为列添加到df中
df['pct_ch']=df.groupby(df.Date.dt.year)['Adj Close'].应用(pd.Series.pct_change)
#按年度分组和pct的总和,以获得年度回报
按年更新=df.groupby(df.Date.dt.year)['pct_ch'].sum().mul(100).reset_index().rename(列={'pct_ch':'cum_pct_ch'year})
#显示(每年一次)
日期兼pct ch________________年
0   2000        -7.274088
1   2001        -8.890805
2   2002       -23.811947
3   2003        21.552072
4   2004         9.535574
5   2005         4.295586
6   2006        11.626670
7   2007         4.860178
8   2008       -38.702107
9   2009        21.622674
10  2010        12.052038
11  2011         1.575069
12  2012        11.840560
13  2013        24.012739
14  2014        12.320664
15  2015         0.501799
16  2016        11.494988
17  2017        17.127082
18  2018        -5.822426
19  2019        26.031938