简体   繁体   English

将Zoo聚合函数应用于时间序列的麻烦

[英]Troubles in applying the zoo aggregate function to a time series

We have the following function to compute monthly returns from a daily series of prices: 我们具有以下功能,可以根据一系列每日价格计算每月回报:

PricesRet = diff(Prices)/lag(Prices,k=-1)
tail(PricesRet)
# Monthly simple returns
MonRet = aggregate(PricesRet+1, as.yearmon, prod)-1
tail(MonRet)

The problem is that it returns wrong values, take for example the simple return for the month of Feb 2013, the function returns a return -0.003517301 while it should have been -0.01304773. 问题在于它返回了错误的值,例如,2013年2月的简单收益,该函数返回的收益为-0.003517301,而应该为-0.01304773。

Why that happens? 为什么会这样?

Here are the last prices observations: 这是最近的价格观察:

> tail(Prices,30)
                 Prices
2013-01-22      165.5086
2013-01-23      165.2842
2013-01-24      168.4845
2013-01-25      170.6041
2013-01-28      169.7373
2013-01-29      169.8724
2013-01-30      170.6554
2013-01-31      170.7210
2013-02-01      173.8043
2013-02-04      172.2145
2013-02-05      172.8400
2013-02-06      172.8333
2013-02-07      171.3586
2013-02-08      170.5602
2013-02-11      171.2172
2013-02-12      171.4126
2013-02-13      171.8687
2013-02-14      170.7955
2013-02-15      171.2848
2013-02-19      170.9482
2013-02-20      171.6355
2013-02-21      170.0300
2013-02-22      169.9319
2013-02-25      170.9035
2013-02-26      168.6822
2013-02-27      168.5180
2013-02-28      168.4935
2013-03-01      169.6546
2013-03-04      169.3076
2013-03-05      169.0579

Here are price returns: 以下是价格退货:

> tail(PricesRet,50)
              PricesRet
2012-12-18  0.0055865274
2012-12-19 -0.0015461900
2012-12-20 -0.0076140194
2012-12-23  0.0032656346
2012-12-26  0.0147750923
2012-12-27  0.0013482760
2012-12-30 -0.0004768131
2013-01-01  0.0128908541
2013-01-02 -0.0047646818
2013-01-03  0.0103372029
2013-01-06 -0.0024547278
2013-01-07 -0.0076920352
2013-01-08  0.0064368720
2013-01-09  0.0119663301
2013-01-10  0.0153828814
2013-01-13  0.0050590540
2013-01-14 -0.0053324785
2013-01-15 -0.0027043105
2013-01-16  0.0118840383
2013-01-17 -0.0005876459
2013-01-21 -0.0145541598
2013-01-22 -0.0013555548
2013-01-23  0.0193624621
2013-01-24  0.0125802978
2013-01-27 -0.0050807744
2013-01-28  0.0007959058
2013-01-29  0.0046096266
2013-01-30  0.0003844082
2013-01-31  0.0180603867
2013-02-03 -0.0091473127
2013-02-04  0.0036322298
2013-02-05 -0.0000390941
2013-02-06 -0.0085320734
2013-02-07 -0.0046591956
2013-02-10  0.0038517581
2013-02-11  0.0011412046
2013-02-12  0.0026607502
2013-02-13 -0.0062440496
2013-02-14  0.0028645616
2013-02-18 -0.0019651341
2013-02-19  0.0040206637
2013-02-20 -0.0093543648
2013-02-21 -0.0005764665
2013-02-24  0.0057176118
2013-02-25 -0.0129979321
2013-02-26 -0.0009730782
2013-02-27 -0.0001453191
2013-02-28  0.0068911863
2013-03-03 -0.0020455332
2013-03-04 -0.0014747845

The results of the function is instead: 该函数的结果改为:

> tail(data.frame(MonRet))
               MonRet
ott 2012 -0.000848156
nov 2012  0.009833881
dic 2012  0.033406884
gen 2013  0.087822700
feb 2013 -0.023875638
mar 2013 -0.003517301

Your returns are wrong. 您的退货是错误的。 The return for 2013-01-23 should be: 2013-01-23的回报应为:

> 165.2842/165.5086-1
[1] -0.001355821

but you have 0.0193624621 . 但是你有0.0193624621 I suspect this is because Prices is an xts object, not a zoo object. 我怀疑这是因为Prices是xts对象,而不是zoo对象。 lag.xts breaks the convention in lag.ts and lag.zoo of k=1 implying a "lag" of (t+1) for the more common convention of using k=1 to imply a "lag" of (t-1). lag.xts打破了lag.tslag.zook=1的约定,这意味着使用( k=1 -1)暗示(t-1)的“ lag”的更常见约定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM