【发布时间】:2020-07-22 01:14:58
【问题描述】:
我有一个 xts 的每日回报,我想将其转换为每月回报。
我可以找到大量线程将每日价格转换为期间收益,但我需要转换每日收益。
遵循this 线程中的建议,效果很好,我注意到返回不是几何的,而是算术的。
因此,我需要像 cumprod(x+1)^(365/12)-1 这样的东西。
但是,将 sum(cx) 替换为 that 不起作用。
这是我的代码:
# Generate data like the type I'm working with
testdata <- cbind(rnorm(100,0.0001,0.01),rnorm(100,0.0001,0.01))
testdata <- as.xts(testdata, order.by = seq(Sys.Date()-99,Sys.Date(),1))
myFun <- function(x) {
# need coredata, so c.xts will not be dispatched
cx <- coredata(x)
Return = sum(cx)
}
MonthlyReturns <- NULL
for (i in 1:ncol(testdata)){
MonthlyReturns <- cbind(MonthlyReturns,period.apply(testdata[,i], endpoints(testdata[,i], "months"),
myFun))
}
任何帮助表示赞赏!
编辑 - 输出应该与输入的格式相同 - 每月回报表而不是每日回报表。 xts 或数据框/矩阵。
编辑 - 对于那些对返回矩阵的来源感兴趣的人,我正在使用 Performance Analytics 包中的 Return.annualized 函数,如 here 所示。 (实际上,我已经使用Return.cumulative 对其进行了修改,速度要快得多)。所以是的,虽然我确实有一个价格矩阵并且可以从中轻松计算每月回报,但我的每日回报矩阵中有其他计算的额外列,因此我需要转换每日回报,而不是每日价格。
【问题讨论】:
-
您能分享一下您希望输出的样子吗?
-
更新了原帖。基本上输出应该与输入相同,除了按月而不是按天。
-
为了澄清
returns,这已经根据basis计算?我想 dput() 在这里会很有用。
标签: r zoo quantmod performanceanalytics