迭代计算不同长度 [英] Iterate calculations of different length

查看:90
本文介绍了迭代计算不同长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个n * 2 df,第一列的起始月份为月,第二列的月份为回报率(以下示例).使用Zoo软件包,日期采用年份星期一的形式.

I have an n*2 df, with starting month in the first column and monthly returns in the second (sample below). The dates are in year-mon form using the zoo package.

我想计算从每个开始月份的回报开始的1到12个月的回报,并使用它们创建n * 13数据框,复合回报构成最后12列(Col 2:1m回报,Col 3:2m返回,...颜色13:12m返回).

I'd like to calculate the 1 to 12 month returns beginning with each starting month return and use those to create an n*13 dataframe, with the compounded returns making up the last 12 columns (Col 2: 1m return, Col 3: 2m return, ...Col 13: 12m return).

要计算收益,我使用一个简单的cumprod:

To calculate returns, I am using a simple cumprod:

allreturns= function (x) {
  cumprod(1+x/100)-1 }

我的问题是:是否有一种聪明的方法来使用apply和lapply来遍历return列的长度(每次为1:12的累积量)和起始月份开始迭代此函数?似乎这将涉及所有返回值的for循环(类似于{for(长度为(i in length(sample $ monthlyreturns))),但我希望采用一种更好的方法).

My question is: is there a clever way to use apply and lapply to iterate this function both by length going down the returns column (cumprod of 1:12 every time) and by the starting month? It seems like this would involve a for loop for all the return values (something like {for (i in length(sample$monthlyreturns)), but I'd prefer to do it in a better way.

谢谢您的帮助!

structure(list(startmonth = structure(c(2005, 2005.08333333333, 
2005.16666666667, 2005.25, 2005.33333333333, 2005.41666666667, 
2005.5, 2005.58333333333, 2005.66666666667, 2005.75, 2005.83333333333, 
2005.91666666667, 2006, 2006.08333333333, 2006.16666666667, 2006.25, 
2006.33333333333, 2006.41666666667, 2006.5, 2006.58333333333, 
2006.66666666667, 2006.75, 2006.83333333333, 2006.91666666667
), class = "yearmon"), monthlyreturns = c(7.60884596500546, 4.31712970370427, 
1.7181678651832, 4.86275367671624, 8.06177110411227, 8.07952171890065, 
7.45263583026826, 9.86292108893394, 4.06634262995794, 2.36454397207126, 
9.12716506049037, 3.72667369898409, 1.2204843852669, 7.80610600719228, 
0.640116988215595, 6.94793848553672, 1.73743493855, 2.57189674302936, 
4.7653386532329, 1.79362375289202, 7.56623527035117, 2.70907687023282, 
4.45359382545575, 5.50409059040248)), .Names = c("startmonth", 
"monthlyreturns"), row.names = c(NA, 24L), class = "data.frame")

推荐答案

我不确定我确切地了解您的要求,但是像下面这样的事情会做到吗?您的数据在收益矩阵中.

I'm not sure I understand exactly what you're asking for but would something like the following do it? Your data are in the returns matrix.

cum_ret <- rollapply(returns[,2], width=12, FUN = function(x) cumprod(1+x/100)-1 )
cum_ret <- data.frame(Date=returns[1:nrow(cum_ret),1], cum_mon_ret=cum_ret)
dates <- seq(as.Date(cum_ret$Date[1]), by = "month", length.out=nrow(returns))
col_lines <- rainbow(nrow(cum_ret))
plot(dates[1:12], cum_ret[1,-1], xlim=c(dates[1], tail(dates,1)), col=col_lines[1], type="b", pch=19, , ylab="Cummulative Returns", xaxt="n")
for( i_plot in 2:nrow(cum_ret)) lines(dates[i_plot:(11 +i_plot)], cum_ret[i_plot,-1], col=col_lines[i_plot], type="b", pch=19 )
axis.Date(1, at=dates, format="%b-%y")
grid( length(dates) + 1 )

每个开始月份的累积12个月的回报是

Cummulative 12 month returns from each starting month would be

这篇关于迭代计算不同长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆