我有我的投资组合的时间序列数据。我想计算回报。
T2 T12 T5
2012-08-16 102779.9 102779.9 -102779.9
2012-08-17 106050.8 106050.8 -106050.8
2012-08-20 107883.3 107883.3 -107883.3
2012-08-21 107024.5 107024.5 -107024.5
2012-08-22 108630.7 108630.7 -108630.7
2012-08-23 109067.9 109067.9 -109067.9
我使用公式
rets <- diff(portfolio, arithmetic = FALSE) -1
这一行给出了NULL错误
修改
输出(头(投资组合))
structure(c(102779.89722, 106050.751092, 107883.320688, 107024.464404,
108630.748512, 109067.890932, -102779.89722, -106050.751092,
-107883.320688, -107024.464404, -108630.748512, -109067.890932,
102779.89722, 106050.751092, 107883.320688, 107024.464404, 108630.748512,
109067.890932), .Dim = c(6L, 3L), .Dimnames = list(NULL, c("T2",
"T5", "T12")), index = structure(c(15568, 15569, 15572, 15573,
15574, 15575), class = "Date"), class = "zoo")
T2 T5 T12
2012-08-16 102779.9 -102779.9 102779.9
2012-08-17 106050.8 -106050.8 106050.8
2012-08-20 107883.3 -107883.3 107883.3
2012-08-21 107024.5 -107024.5 107024.5
2012-08-22 108630.7 -108630.7 108630.7
2012-08-23 109067.9 -109067.9 109067.9
答案 0 :(得分:2)
> library(zoo)
> diff(portfolio)
T2 T5 T12
2012-08-17 3270.8539 -3270.8539 3270.8539
2012-08-20 1832.5696 -1832.5696 1832.5696
2012-08-21 -858.8563 858.8563 -858.8563
2012-08-22 1606.2841 -1606.2841 1606.2841
2012-08-23 437.1424 -437.1424 437.1424
似乎按照我的预期工作,给出逐列的连续差异。 (arithmetic
参数是什么?)
但是,对于(百分比)返回(我假设这些是股票报价 - 但为什么是负数?)你真的不想要连续的差异,但比率,像这样:
> rollapply(portfolio,width=2,FUN=function(xx)xx[2]/xx[1])-1
T2 T5 T12
2012-08-16 0.031823868 0.031823868 0.031823868
2012-08-17 0.017280119 0.017280119 0.017280119
2012-08-20 -0.007960974 -0.007960974 -0.007960974
2012-08-21 0.015008569 0.015008569 0.015008569
2012-08-22 0.004024113 0.004024113 0.004024113
查看?rollapply
。