这段代码是非感性的,所以得到结果并不是重要的部分,理解为什么这两种方法得出不同的结论是我试图理解的。
# data
days <- c(528, 900, 1050, 1300)
returns <- c(-46.9363726590971, 113.096047917561, -55.1435291957069, 251.540399180745)
# the operation I would like to do; note the NaN for two of them
returns ^ (252 / days)
# pulling out the first values fails in the same way
(returns[1]) ^ (252 / days[1])
# and sapply yields the same result
sapply(X = seq_along(days), FUN = function(i) returns[i] ^ (252 / days[i]))
# just using the numbers gives a different result for the negative numbers
-46.9363726590971 ^ (252 / 528)
113.096047917561 ^ (252/900)
-55.1435291957069 ^ (252/1050)
251.540399180745 ^ (252/1300)
为什么会这样?
谢谢