简体   繁体   中英

zoo series and aggregate returns in R

I want to have overall returns for data series over the whole of zoo series time perid, which I have in both a prices or daily returns;

eg

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

or as prices where simple return would be last prices in series minus first / first

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

I've tried some performance analytic packages but I know the returns are wrong.

Returns

Assuming the input data shown reproducibly in the Note at the end and using returns:

apply(rets + 1, 2, prod) - 1
##        GOLD          PA          PL         SLV 
##  0.00000000 -0.02714782 -0.01444600  0.00000000 

or

library(PerformanceAnalytics)
Return.cumulative(rets)
##                   GOLD          PA        PL SLV
## Cumulative Return    0 -0.02714782 -0.014446   0

or approximating using sums:

colSums(rets)
##        GOLD          PA          PL         SLV 
##  0.00000000 -0.02739726 -0.01451613  0.00000000 

Prices

or using prices:

n <- nrow(prices)
diff(prices[c(1, n)], arith = FALSE) - 1
##            GOLD PA          PL  SLV
## 2020-10-11    0  0 0.002252252 0.08

or using n from above:

exp(diff(log(prices[c(1, n)]))) - 1
##            GOLD PA          PL  SLV
## 2020-10-11    0  0 0.002252252 0.08

Note

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM