[英]zoo series and aggregate returns in R
我想在整個動物園系列時間段內獲得數據系列的總體回報,我在價格或每日回報中都有;
例如
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
我嘗試了一些性能分析包,但我知道返回值是錯誤的。
假設輸入數據在末尾的注釋中可重復顯示並使用返回值:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.