1

我有问题要求我计算一系列 1 期收益的滚动乘积。滚动窗口的长度是可变的。目的是获得尽可能接近 12 个月窗口的 1 期回报的滚动产品。

for我已经能够通过循环和语句使用蛮力产生一个可行的解决方案if,但是我想知道是否有一个优雅的解决方案。我花了很多时间尝试rollapply其他类似的功能,但我无法获得解决方案。

下面的数据说明了这个问题。

    date        rt_1_period rt_12_mth_window
1   04-04-13    NA          NA
2   10-04-13    0.729096362 NA
3   24-05-13    1.002535647 NA
4   30-05-13    0.993675716 NA
5   21-07-13    1.002662843 NA
6   03-08-13    1.009516582 NA
7   01-09-13    0.963099395 NA
8   20-10-13    1.012470278 NA
9   25-10-13    1.01308502  NA
10  03-11-13    1.005440704 NA
11  01-01-14    1.024208021 NA
12  11-01-14    0.996613924 NA
13  17-02-14    1.009811368 NA
14  24-02-14    1.008139557 NA
15  30-03-14    1.002794709 NA
16  30-04-14    0.998745849 1.042345473
17  02-05-14    1.002324076 1.044767963
18  27-06-14    0.997741026 1.046389027
19  24-08-14    1.015767546 1.050072129
20  05-09-14    1.014405005 1.106010894
21  02-11-14    1.013830296 1.09319212
22  09-11-14    1.013127219 1.101549487
23  16-11-14    1.012614177 1.115444628
24  18-01-15    0.986893629 1.078458006
25  24-01-15    1.028120919 1.108785236
26  10-04-15    0.912452762 0.991025615
27  09-08-15    1.004676152 0.981376513
28  07-01-16    1.004236123 0.934086003
29  01-04-16    1.02341302  0.94215696

在示例中,第 29 行的 12 个月回报计算为从第 26 行到第 29 行的 1 个期间回报的乘积,因为第 25 行和第 26 行之间包含 02-04-15(从 01-04-16 开始的 365 天)。另一方面,第 15 行的 12 个月回报是 NA,因为 30-03-13(从 30-03-14 开始的 365 天)超出了我可观察到的 1 个周期回报的时间窗口。

如果有人能提出一些解决这个问题的方法,我会很高兴。

只是为了清楚起见,如果提供的数据没有多大意义,是因为这是我为说明目的而创建的更大数据库的缩减版本。

4

2 回答 2

1

您可以使用xtsandlubridate来简化日期操作

数据:

require(xts)
require(lubridate)


DF = read.csv(text="
date,rt_1_period,rt_12_mth_window
04-04-13,           ,
10-04-13,0.729096362,
24-05-13,1.002535647,
30-05-13,0.993675716,
21-07-13,1.002662843,
03-08-13,1.009516582,
01-09-13,0.963099395,
20-10-13,1.012470278,
25-10-13,1.01308502 ,
03-11-13,1.005440704,
01-01-14,1.024208021,
11-01-14,0.996613924,
17-02-14,1.009811368,
24-02-14,1.008139557,
30-03-14,1.002794709,
30-04-14,0.998745849,1.042345473
02-05-14,1.002324076,1.044767963
27-06-14,0.997741026,1.046389027
24-08-14,1.015767546,1.050072129
05-09-14,1.014405005,1.106010894
02-11-14,1.013830296,1.09319212
09-11-14,1.013127219,1.101549487
16-11-14,1.012614177,1.115444628
18-01-15,0.986893629,1.078458006
24-01-15,1.028120919,1.108785236
10-04-15,0.912452762,0.991025615
09-08-15,1.004676152,0.981376513
07-01-16,1.004236123,0.934086003
01-04-16,1.02341302 ,0.94215696",header=TRUE,stringsAsFactors=FALSE,na.strings="")


#Convert to xts time series for ease in date manipulation

DF_xts = xts(DF[,-1],order.by = as.Date(DF[,1],format="%d-%m-%y"))
head(DF_xts)
#   
#              rt_1_period rt_12_mth_window
#2013-04-04          NA               NA
#2013-04-10 0.729096362               NA
#2013-05-24 1.002535647               NA
#2013-05-30 0.993675716               NA
#2013-07-21 1.002662843               NA
#2013-08-03 1.009516582               NA




#set lag period as 1 year
lagPeriod = 1

累计12m产品:

为每个日期构建一个窗口[prevYearDate,date],子集1m返回位于该窗口中,计算累积产品并选择最后一个产品

rt_12_mth_window_Calc = do.call(rbind,lapply(as.Date(index(DF_xts)),function(x) {

prevYearDate = x-years(lagPeriod)

rt_12_mth_window_Calc = last(cumprod(DF_xts[paste0(prevYearDate,"/",x),"rt_1_period"]))
colnames(rt_12_mth_window_Calc) = "rt_12_mth_window_Calc"

return(rt_12_mth_window_Calc)

}))

最终数据集:

#Merge with original time series for final dataset

new_DF = merge.xts(DF_xts,rt_12_mth_window_Calc)

#Calculate difference in original and calculated 12 month returns
new_DF$delta = new_DF$rt_12_mth_window_Calc - new_DF$rt_12_mth_window
new_DF

#           rt_1_period rt_12_mth_window rt_12_mth_window_Calc           delta
#2013-04-04          NA               NA                    NA              NA
#2013-04-10 0.729096362               NA                    NA              NA
#2013-05-24 1.002535647               NA                    NA              NA
#2013-05-30 0.993675716               NA                    NA              NA
#2013-07-21 1.002662843               NA                    NA              NA
#2013-08-03 1.009516582               NA                    NA              NA
#2013-09-01 0.963099395               NA                    NA              NA
#2013-10-20 1.012470278               NA                    NA              NA
#2013-10-25 1.013085020               NA                    NA              NA
#2013-11-03 1.005440704               NA                    NA              NA
#2014-01-01 1.024208021               NA                    NA              NA
#2014-01-11 0.996613924               NA                    NA              NA
#2014-02-17 1.009811368               NA                    NA              NA
#2014-02-24 1.008139557               NA                    NA              NA
#2014-03-30 1.002794709               NA                    NA              NA
#2014-04-30 0.998745849      1.042345473           1.042345470 -2.64001643e-09
#2014-05-02 1.002324076      1.044767963           1.044767960 -2.54864396e-09
#2014-06-27 0.997741026      1.046389027           1.046389025 -1.97754613e-09
#2014-08-24 1.015767546      1.050072129           1.050072127 -1.66086833e-09
#2014-09-05 1.014405005      1.106010894           1.106010893 -1.34046041e-09
#2014-11-02 1.013830296      1.093192120           1.093192120 -6.47777387e-11
#2014-11-09 1.013127219      1.101549487           1.101549488  5.99306826e-10
#2014-11-16 1.012614177      1.115444628           1.115444628 -1.89856353e-10
#2015-01-18 0.986893629      1.078458006           1.078458005 -1.15637744e-09
#2015-01-24 1.028120919      1.108785236           1.108785235 -9.57268265e-10
#2015-04-10 0.912452762      0.991025615           0.991025613 -1.54581248e-09
#2015-08-09 1.004676152      0.981376513           0.996850412  1.54738992e-02
#2016-01-07 1.004236123      0.934086003           0.934086002 -9.15302278e-10
#2016-04-01 1.023413020      0.942156960           0.942156960 -1.82048598e-10

除 2015-08-09 外,所有观测值的计算值和原始值都非常接近,值的偏差为 1.55%,您能否确认您在此期间的计算

于 2016-04-24T12:48:21.137 回答
0

这是一个仅依赖于 xts 的解决方案,对某些人来说可能更直接。

library(xts)
x <- as.xts(read.zoo(text="date,rt_1_period,rt_12_mth_window
04-04-13,           ,
10-04-13,0.729096362,
24-05-13,1.002535647,
30-05-13,0.993675716,
21-07-13,1.002662843,
03-08-13,1.009516582,
01-09-13,0.963099395,
20-10-13,1.012470278,
25-10-13,1.013085020,
03-11-13,1.005440704,
01-01-14,1.024208021,
11-01-14,0.996613924,
17-02-14,1.009811368,
24-02-14,1.008139557,
30-03-14,1.002794709,
30-04-14,0.998745849,1.042345473
02-05-14,1.002324076,1.044767963
27-06-14,0.997741026,1.046389027
24-08-14,1.015767546,1.050072129
05-09-14,1.014405005,1.106010894
02-11-14,1.013830296,1.09319212
09-11-14,1.013127219,1.101549487
16-11-14,1.012614177,1.115444628
18-01-15,0.986893629,1.078458006
24-01-15,1.028120919,1.108785236
10-04-15,0.912452762,0.991025615
09-08-15,1.004676152,0.981376513
07-01-16,1.004236123,0.934086003
01-04-16,1.023413020,0.94215696", header=TRUE, sep=",", format="%d-%m-%y"))
ix <- index(x)       # index values
ixlag <- ix-365      # 1-year lag index values
x$rt_12 <- NA_real_  # initialize result column
for(i in which(ixlag > ix[1])) {
   # 1-year subset
   xyear <- window(x, start=ixlag[i], end=ix[i])
   # calculate product and update result column
   x[i,"rt_12"] <- prod(xyear[,"rt_1_period"])
}
于 2016-04-24T21:50:44.433 回答