【问题标题】:zoo series and aggregate returns in RR中的动物园系列和总回报
【发布时间】:2020-11-15 09:50:02
【问题描述】:

我希望在整个动物园系列时间周期内获得数据系列的总体回报,我在价格或每日回报中都有;

例如

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

或者作为简单回报的价格是系列中的最后一个价格减去第一个/第一个

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

我尝试了一些性能分析包,但我知道返回错误。

【问题讨论】:

  • 您好,请补充一些细节。我认为您已经使用zoo 创建了一系列时间序列。要计算每个系列的总体回报,您可以使用colSums。如果您对特定时间段感兴趣,请使用subset 选择它们。使用分组变量,您可以使用aggregate
  • 嗨约瑟夫,谢谢,是的,我尝试了 Colsums,它有效,但返回肯定不正确我知道这一点,因为我有另一个例程,它给了我 vol 和 rtn,但它基于矩阵且复杂。我试图简化所以 Colsums(z.rtn) GOLD PA PL SLV 0.5702461 1.0564206 -0.0231898 0.5643624 在那个时期是不正确的
  • 嗨@jez1511 - 你能提供一个小数据集来证明colSums() 没有提供正确的值。请参阅reproducible example 指导。

标签: r zoo


【解决方案1】:

返回

假设输入数据在最后的注释中重复显示并使用返回:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2014-04-21
    • 2012-03-30
    • 2012-01-12
    • 2012-06-26
    相关资源
    最近更新 更多