【问题标题】:Cumulative return of positive and negative numbers正负数累计回报
【发布时间】:2020-12-25 18:16:53
【问题描述】:

我正在尝试使用函数 cummulative_return(如下所示)查找以下 4 个向量的累积返回,但结果与预期不符。

library(TTR) # ROC function

a = c(-1000,-2000,-3000,-4000,-5000,-6000,-7000,-8000)
b = c(1,2,3,4,5,6,7,8)
c = c(-7, -1, -20, -50, -93, 112, 212)
d = c(7, 1, 20, 50, 93, -112, -212)

cummulative_return <- function(x){
  y <- ROC(x, type = c("discrete"), na.pad = T) * sign(lag(x))
  cumprod(na.omit(y) + 1) - 1
}

cummulative_return(a)
[1] -1 -1 -1 -1 -1 -1 -1
cummulative_return(b)
[1] 1 2 3 4 5 6 7
cummulative_return(c)
[1]   0.8571429 -34.4285714  15.7142857   1.3400000   6.4980645  13.1927650
cummulative_return(d)
[1]  -0.8571429   1.8571429   6.1428571  12.2857143 -17.0000000  -2.7142857

只有向量'a'的答案不正确,预期的结果应该是

cummulative_return(a)
[1] -1 -2 -3 -4 -5 -6 -7

你能指出函数cummulative_return的错误吗?

另外,任何库中是否有类似的函数可以找到负数和正数的累积返回而不会出现此类错误?

【问题讨论】:

    标签: r ttr


    【解决方案1】:

    如果我得到了正确的累积回报定义,它是位置 x 的值与初始值之间的差值,除以初始值以进行标准化。这可以通过以下函数来实现:

    cummulative_return <- function(x) {
      (x[2:length(x)]-x[1])/abs(x[1])
    }
    
    > cummulative_return(a)
    [1] -1 -2 -3 -4 -5 -6 -7
    > cummulative_return(b)
    [1] 1 2 3 4 5 6 7
    > cummulative_return(c)
    [1]   0.8571429  -1.8571429  -6.1428571 -12.2857143  17.0000000  31.2857143
    > cummulative_return(d)
    [1]  -0.8571429   1.8571429   6.1428571  12.2857143 -17.0000000 -31.2857143
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-04
      • 1970-01-01
      • 2017-06-26
      • 2016-05-23
      • 2013-12-02
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多