【问题标题】:Daily Geometric Returns from Monthly Observations每月观察的每日几何回报
【发布时间】:2014-02-10 15:40:33
【问题描述】:

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

如何重现“期望的输出”列?来自 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. 将 cumprod 替换为 prod 函数
  2. 通过绑定或转换矩阵/df 来更改 zCbind[,3] 的格式
  3. 这看起来很有希望,但我找不到更多关于 cumprod 函数的“cumprod.column”包装器的文献:http://braverock.com/brian/R/PerformanceAnalytics/html/cum.utils.html

【问题讨论】:

  • 您应该提供一些数据以及您尝试过的内容。请阅读this
  • 所以你想根据上个月的最后一天的固定权重加上每日增量的汇总计算每日权重?

标签: r finance


【解决方案1】:

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

【讨论】:

  • 特洛伊这似乎是一个解决方案!我应该在我的原始帖子中进行澄清,但我希望对同时具有自己的权重和增量值的多个代码进行此分析。我可以按比例放大您的方法以输出矩阵吗?
猜你喜欢
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多