每月观察的每日几何回报

时间:2014-01-21 07:08:05

标签: r finance

我有每月体重观察和每日回报,我正在尝试计算一个月内每天的几何回报。可能更容易看到模式:

desired output, from Excel

如何重现“所需输出”列?来自R中的基本函数的解决方案或任何包装建议都受到赞赏!

  • 编辑1: 谢谢。

以下是一些示例数据和我一直在努力的解决方案:

set.seed(33)

z <- c(.35,NA,NA,NA,.2,NA,NA)
z1 <- c(.35,.35,.35,.35,.2,.2,.2)
z2 <- rnorm(7)
zCbind <- data.frame(cbind(z,z1,z2))
colnames(zCbind) <- c("months","na.locf(months)","values")

solution1 <- ifelse(zCbind[,1] == zCbind[,2], 
                zCbind[,1],                                   # if TRUE 
                zCbind[,2]*apply(zCbind[,3],2,cumprod))       # if FALSE

我知道我的问题处于错误状态。我尝试过的解决方案是:

  1. 用prod功能替换cumprod
  2. 通过绑定或转换矩阵/ df
  3. 来更改zCbind [,3]的格式
  4. 这看起来很有希望,但我找不到关于cumprod函数的“cumprod.column”包装的更多文献:http://braverock.com/brian/R/PerformanceAnalytics/html/cum.utils.html

1 个答案:

答案 0 :(得分:0)

plyr::ddply()

的情况如何

我重新创建了数据,使其更像原始格式

sheet<-data.frame(date=as.Date(1:100,origin="2012-01-01"),
                  weight=rep(NA,100),
                  increment=rnorm(100,0,0.5)/100
           )

#get the latest date in each month to replace the NAs
last_days<-ddply(sheet,.(month=format(date,"%Y-%b")),summarise,last_day=max(date))
sheet[sheet$date %in% last_days$last_day,]$weight<-runif(nrow(last_days))/2

#now we have a table which matches your data

#set the NA's to 0
sheet$weight[is.na(sheet$weight)]<-0

# OK so here you add your seed value for the first month (0.4 in this example)
# and shift forward into the last month
sheet$shift<-c(0.4,sheet$weight[1:nrow(sheet)-1])

sheet.out<-
ddply(sheet,
      .(month=format(date,"%Y-%b")),
      summarise,
      date=date,
      inc=increment,       
      output=cumprod(ifelse(shift==0,1+increment,max(shift)*(1+increment)))  #cum product of seed val and day rets
      )

# and lastly update the last days to be the original weight
sheet.out$output<-ifelse(sheet$weight!=0,sheet$weight,sheet.out$output)

head(sheet.out)

#     month       date           inc    output
#1 2012-Apr 2012-04-01  0.0018504578 0.3234371
#2 2012-Apr 2012-04-02  0.0017762242 0.3240116
#3 2012-Apr 2012-04-03  0.0091980829 0.3269919
#4 2012-Apr 2012-04-04 -0.0023334368 0.3262289
#5 2012-Apr 2012-04-05  0.0042003969 0.3275992
#6 2012-Apr 2012-04-06  0.0005409113 0.3277764