N-days realized variance in R

48 Views Asked by At

I am trying to replicate the article "The Crypto Cycle and US Monetary Policy" (Che & al., 2023). Page 19, they measure the 90-day variance of the MSCI Index and of Bitcoin price.

Is there a way in R, possibly with the dplyr packages and pipes so that I can integrate the code easily, to create a new variable being the N-days realized variance (calculated summing over daily returns.)

The idea here is that I want to have a measure of volatility which is similar to the one of Miranda-Aggripino & Rey (2019) (US Monetary Policy and Global Financial Cycle), i.e. not comprised between 0 and 1 (that you obtain with any other measure of volatility) so that I can take the log of volatility as in Che & al. (2023). If anyone has a solution, I would appreciate !)

If needed, I can provide the following code to extract bitcoin prices data :

library(quantmod)
startDate=as.Date("2022-12-29")
endDate=as.Date("2024-01-17")

symbol.vec=c("BTC-USD")
getSymbols(symbol.vec , from=startDate, to=endDate)

BTC_USD=`BTC-USD`
names(BTC_USD) = gsub("-", "_", names(BTC_USD))
names(BTC_USD) <- c("Open", "High", "Low", "Close", "Volume", 
                    "Adjusted")
btc <- BTC_USD$Close

btc2 <- subset(btc, !isWeekend(as.timeDate(time(btc)))=="TRUE")
#Here, I import Bitcoin price data (and only keep the Close prices during weekdays). However, no idea how I could compute N-days realised variance

I've posted this question in CrossValidated and still have no clue

1

There are 1 best solutions below

2
r2evans On

Ignoring btc2 for now, we can calculate rolling variance with zoo::rollapply (for example).

BTC_USD$Close_RV <- zoo::rollapply(BTC_USD$Close, 7, FUN = var)
BTC_USD
#                Open     High      Low    Close      Volume Adjusted    Close_RV
# 2022-12-29 16552.32 16651.76 16508.68 16642.34 14472237479 16642.34          NA
# 2022-12-30 16641.33 16643.43 16408.47 16602.59 15929162910 16602.59          NA
# 2022-12-31 16603.67 16628.99 16517.52 16547.50 11239186456 16547.50          NA
# 2023-01-01 16547.91 16630.44 16521.23 16625.08  9244361700 16625.08          NA
# 2023-01-02 16625.51 16759.34 16572.23 16688.47 12097775227 16688.47          NA
# 2023-01-03 16688.85 16760.45 16622.37 16679.86 13903079207 16679.86          NA
# 2023-01-04 16680.21 16964.59 16667.76 16863.24 18421743322 16863.24    9979.115
# 2023-01-05 16863.47 16884.02 16790.28 16836.74 13692758566 16836.74   13964.257
# 2023-01-06 16836.47 16991.99 16716.42 16951.97 14413662913 16951.97   20998.250
# 2023-01-07 16952.12 16975.02 16914.19 16955.08  7714767174 16955.08   18327.057
#        ...                                                                     
# 2024-01-08 43948.71 47218.00 43244.08 46970.50 42746192015 46970.50 1633367.982
# 2024-01-09 46987.64 47893.70 45244.71 46139.73 39821290992 46139.73 2038515.951
# 2024-01-10 46121.54 47647.22 44483.15 46627.78 50114613298 46627.78 1866223.488
# 2024-01-11 46656.07 48969.37 45678.64 46368.59 45833734549 46368.59 1846684.695
# 2024-01-12 46354.79 46498.14 41903.77 42853.17 43332698900 42853.17 2656800.323
# 2024-01-13 42799.45 43234.66 42464.14 42842.38 20601860469 42842.38 3334409.720
# 2024-01-14 42842.26 43065.60 41724.61 41796.27 17521429522 41796.27 4825335.530
# 2024-01-15 41715.07 43319.72 41705.42 42511.97 22320220558 42511.97 4439034.476
# 2024-01-16 42499.34 43566.27 42086.00 43154.95 24062872740 43154.95 3744876.495
# 2024-01-17 43132.10 43189.89 42189.31 42742.65 20851232595 42742.65 2156791.181