问题描述
因此,我一直在研究SP500的年度收益以及从quandl订阅中下载的信息。我已经使用resample()和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()
SP 500截至2008年底的期望值应该为-38.5%,但是由于某些原因我的代码显示-17%?如果由于某种原因您无法访问数据,我可以为该数据提供一个.csv文件。感谢一百万的帮助。
sp500_Y_ret_df.loc['2008-12-31']
输出:
-0.17319465450687388
最近20年:
sp500_Y_ret_df.tail(20)
输出:
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
使用规定的“最后”产生了更接近的结果,但不确定那是否纯粹是运气
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
-
aapl_Y_ret_df = aapl_df['price'].resample('Y').last().pct_change()
aapl_Y_ret_df.tail()
date
2013-12-31 0.569359
2014-12-31 -0.179846
2015-12-31 1.370794
2016-12-31 0.172918
2017-12-31 -0.516918
Freq: A-DEC,dtype: float64
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)