【问题标题】:Exponentiation with a negative base in R not consistentR中负基数的幂运算不一致
【发布时间】:2015-06-10 22:28:51
【问题描述】:

我正在尝试将负回报年化,但遇到了问题。我有一个 xts 系列,我正在使用以下代码:

x = rebalReturns[,"LBND/SBND (PS DB 25+ Year T-Bond)"]
round((tail(cumprod(na.omit(x) + 1) - 1,1)), 4)^round(1/length(na.omit(x)),4)

这将返回“NaN”。

如果我分别计算两半,我得到:

> round((tail(cumprod(na.omit(x) + 1) - 1,1)), 4)
           LBND/SBND (PS DB 25+ Year T-Bond)
2015-04-02                           -0.2274
> round(252/length(na.omit(x)),4)
[1] .2184
>

如果我手动计算,我会得到一个有意义的结果:

> -.2274^.2184
[1] -0.7236408
> 

我知道有一些关于负数求幂的错误,但为什么它是手动工作的,而不是 xts 对象?有没有办法做到这一点?

谢谢!!

【问题讨论】:

  • 您应该在您的问题中提供rebalReturns[,"LBND/SBND (PS DB 25+ Year T-Bond)"] 以使其更具重现性...
  • 这两种方式是不同的。第一个等效于 (-.2274)^.2184,它确实按预期返回 NaN。

标签: r xts quantmod quantitative-finance exponent


【解决方案1】:

我不聪明。这不是你年化回报的方式。 (而且我误解了操作的顺序。(谢谢,Dason。))

公式应该是:

round(1 + (tail(cumprod(na.omit(x) + 1) - 1,1)), 4)^round(252/length(na.omit(x)),4) - 1

这很好用。对于浪费任何人的努力,我深表歉意,并一如既往地感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-06
    • 2016-12-28
    • 1970-01-01
    • 2016-07-29
    • 2017-04-03
    • 2020-05-14
    • 2011-09-18
    相关资源
    最近更新 更多