How to calculate monthly Cvar in R?

2

I have downloaded the daily Ibovespa quotes from 01-01-2012 to 12/31/2017 in R. I need to calculate the monthly CVaR of these quotes. I downloaded the Performaceanalytics package, but when I apply it, it calculates the cvar of quotations for the entire period. Is there any code that I apply to specify that I want the monthly calculation? I thought about dividing the data into monthly columns and then applying the CVaR formula on each column.

Does anyone know the best way?

    
asked by anonymous 30.03.2018 / 21:50

1 answer

2

I think I've managed to do something similar to what the question asks. If it is not the following, you may be able to adapt to your problem.

The trick is to divide the data by month, using the split base function and the as.yearmon function of the zoo package. This package is also used to create a time series, the series z , only with two columns of the tickers database.

library(BatchGetSymbols)
library(PerformanceAnalytics)
library(zoo)


ibvsp <- BatchGetSymbols('^BVSP', first.date = as.Date('2012-01-01'),
                                  last.date = as.Date('2017-12-31'))

tickers <- ibvsp$df.tickers[complete.cases(ibvsp$df.tickers), ]

z <- zoo(tickers[9:10], order.by = tickers[, 7])

z_month <- split(z, as.yearmon(index(z)))
cvar <- as.data.frame(t(sapply(z_month, CVaR)))
names(cvar) <- names(tickers[9:10])
head(cvar)
#         ret.adjusted.prices ret.closing.prices
#jan 2012         -0.01410941        -0.01410941
#fev 2012         -0.02108851        -0.02108851
#mar 2012         -0.02402775        -0.02402775
#abr 2012         -0.02146428        -0.02146428
#mai 2012         -0.03548744        -0.03548744
#jun 2012         -0.03252744        -0.03252744
    
31.03.2018 / 14:17