问题描述
我正在努力计算等权重的投资组合回报。我目前正在使用 Return.portfolio
包中的 PerformanceAnalytics
,但在处理 NA 时遇到问题。
举一个可复制的例子:
structure(c(5.49295774647889,4.80640854472629,-0.127388535031836,4.71938775510203,5.75517661388552,NA,1.46627565982405,4.09441233140655,1.31031876023686,10.3718442979729,-5.16056338028169,0.237614351906856,1.35119118169966,2.26775883557192,5.05941761423306,-1.76265063843784,2.14109258894431,3.73359337566391,3.40163825335787,7.58912642134959,-2.64397595765676,3.733593376,2.47850105350444,3.73359337566391
),.Dim = 5:6,.Dimnames = list(NULL,c("ALVGY","BAYNGY","BMWGY","PF.return.wrong","PF.return.expected","PF.return.weighted"
)),index = structure(c(1480464000,1483142400,1485820800,1488240000,1490918400),tzone = "UTC",tclass = "Date"),class = c("xts","zoo"),ret_type = "discrete",coredata_content = "discreteReturn")
ALVGY BAYNGY BMWGY PF.return.wrong PF.return.expected PF.return.weighted
2016-11-30 5.4929577 NA 1.3103188 2.267759 3.401638 NA
2016-12-31 4.8064085 NA 10.3718443 5.059418 7.589126 7.589126
2017-01-31 -0.1273885 NA -5.1605634 -1.762651 -2.643976 -2.643976
2017-02-28 4.7193878 1.466276 0.2376144 2.141093 2.141093 2.478501
2017-03-31 5.7551766 4.094412 1.3511912 3.733593 3.733593 3.733593
当我运行 Example$PF.return <- Return.portfolio(Example[,1:3],geometric = F)
时,我收到一个警告读数 In Return.portfolio(Example,geometric = F) : NA's detected: filling NA's with zeros
,并且实际上结果 PF.return.wrong
在计算中将 NA 作为零包含在内。
我确实希望在 PF.return.expected
中获得结果。
我想要做的是从投资组合回报计算中排除 NA。但是我无法做到这一点。
作为一种潜在的解决方法,我考虑过将 return.portfolio
与下面的加权矩阵 Example.wt 结合使用:
structure(c(0.5,0.5,0.333333333333333,0.333333333333333),.Dim = c(5L,3L),.Dimnames = list(
NULL,"BMWGY")),"zoo"))
ALVGY BAYNGY BMWGY
2016-11-30 0.5000000 0.0000000 0.5000000
2016-12-31 0.5000000 0.0000000 0.5000000
2017-01-31 0.5000000 0.0000000 0.5000000
2017-02-28 0.3333333 0.3333333 0.3333333
2017-03-31 0.3333333 0.3333333 0.3333333
运行 Example$PF.return.weighted <- Return.portfolio(Example[,geometric = F,weights = Example.wt)
我遇到三个问题:
- 出于某种原因,PF.return.weighted 的第一行返回 NA
- 第四行的值不等于 PF.return.expected,我不明白为什么
-
Example.wt
是手动构建的,因为我没有设法计算它。
您知道为什么 PF.return.weighted 有 NA 以及为什么第四行值与预期值不同吗?
至于权重矩阵的计算,你知道我如何从Example
中自动计算吗?我想过使用rowSums
和{{的某种组合1}} 但没有设法构建它。
很抱歉这篇长文章,但我真的被困在这里,希望得到任何帮助/提示!非常感谢!
解决方法
我认为您遇到了这个问题,因为在重新平衡之前需要知道权重,即如果您从权重指数中减去一天,您可以获得有意义的值:
library(xts)
library(PerformanceAnalytics)
Example.wt2 <- Example.wt
index(Example.wt2) <- index(Example.wt) - 1 # subtract one day from the index
test <- PerformanceAnalytics::Return.portfolio(Example,weights = Example.wt,rebalance_on = "months")
test2 <- PerformanceAnalytics::Return.portfolio(Example,weights = Example.wt2,rebalance_on = "months")
res <- merge.xts(test,test2)
names(res) <- paste0("portfolio.returns",c(".old",".new"))
> print(res)
portfolio.returns.old portfolio.returns.new
2016-11-30 NA 3.401638
2016-12-31 7.589126 7.589126
2017-01-31 -2.643976 -2.643976
2017-02-28 2.478501 2.141093
2017-03-31 3.733593 3.733593
编辑:您可以使用如下功能方法获得权重:
####
fCalcWeights <- function(x){
y <- 1/sum(!is.na(x))
x[!is.na(x)] <- y
x[is.na(x)] <- 0
x
}
Example.wt3 <- t(apply(Example[,c(1:3)],1,fCalcWeights))
Example.wt3 <- xts(Example.wt3,order.by = index(Example)-1)
> Example.wt3
ALVGY BAYNGY BMWGY
2016-11-29 0.5000000 0.0000000 0.5000000
2016-12-30 0.5000000 0.0000000 0.5000000
2017-01-30 0.5000000 0.0000000 0.5000000
2017-02-27 0.3333333 0.3333333 0.3333333
2017-03-30 0.3333333 0.3333333 0.3333333
>
> all.equal(Example.wt2,Example.wt3)
[1] TRUE