R 中的动物园系列和聚合 returns

zoo series and aggregate returns in R

我想在整个动物园系列时间周期内拥有总体 returns 的数据系列,我在价格或每日 returns;

中都有

例如

                   GOLD           PA           PL  SLV
2001-05-22  0.000000000 -0.009132420 -0.004838710  0.0

或作为价格,其中简单的 return 将是系列中的最后价格减去第一个/第一个

        GOLD   PA  PL SLV
2020-10-09 1920 2454 888  25

我尝试了一些性能分析包,但我知道 return 是错误的。

Returns

假设输入数据在末尾的注释中可重复显示并使用 returns:

apply(rets + 1, 2, prod) - 1
##        GOLD          PA          PL         SLV 
##  0.00000000 -0.02714782 -0.01444600  0.00000000 

library(PerformanceAnalytics)
Return.cumulative(rets)
##                   GOLD          PA        PL SLV
## Cumulative Return    0 -0.02714782 -0.014446   0

或使用总和进行近似:

colSums(rets)
##        GOLD          PA          PL         SLV 
##  0.00000000 -0.02739726 -0.01451613  0.00000000 

价格

或使用价格:

n <- nrow(prices)
diff(prices[c(1, n)], arith = FALSE) - 1
##            GOLD PA          PL  SLV
## 2020-10-11    0  0 0.002252252 0.08

或使用上面的n

exp(diff(log(prices[c(1, n)]))) - 1
##            GOLD PA          PL  SLV
## 2020-10-11    0  0 0.002252252 0.08

备注

Lines <- "
Date               GOLD           PA           PL  SLV
2001-05-22  0.000000000 -0.009132420 -0.004838710  0.0
2001-05-23  0.000000000 -0.009132420 -0.004838710  0.0
2001-05-24  0.000000000 -0.009132420 -0.004838710  0.0"

Lines2 <- "
Date       GOLD   PA  PL SLV
2020-10-09 1920 2454 888  25
2020-10-10 1900 2454 899  26
2020-10-11 1920 2454 890  27"

library(zoo)
rets <- read.zoo(text = Lines, header = TRUE)
prices <- read.zoo(text = Lines2, header = TRUE)