Modeling Volatility

GARCH Models

  • Normal day distribution/ Turbuolence day distribution

  • Relative variation of the series
    • Mean
    • Standard deviation (σ_t)

      • Annual volatility [sqrt(252)*dayli_volatility

 

R packages

  • Xts
  • PerformanceAnalytics
  • Rugarch
  •  

 


 https://www.datacamp.com/cheat-sheet/xts-cheat-sheet-time-series-in-r

library(rugarch)

 library(xts)
library(tydiverse)
# GARCH familiarization

alpha <- 0.1
beta <- 0.8
sp500ret <- rnorm(1000, mean = 0, sd = 0.8)
omega <- var(sp500ret) * (1 - alpha - beta)
omega
plot(sp500ret, type = "l")

e <-  sp500ret-mean(sp500ret)
e2 <- e^2
plot(e2, type = "l")


# predicte variance
nobs <- length(sp500ret)
predvar <- rep(NA, nobs)
predvar[1] <- var(sp500ret)

for (t in 2:nobs){
  predvar[t] <- omega + alpha*e2[t-1] + beta*predvar[t-1]
}

plot(predvar, type="l")


# GARCH volatility
  # sqrt of predicted vairance
predvol <- sqrt(predvar)
predvol <- xts(predvol, order.by = time(sp500ret))
# plot(predvol, type = "l")

# unconditional volatility
uncvol <-sqrt(omega/(1-alpha-beta))
uncvol <-rep(uncvol, nobs)
uncovol <-xts((uncvol), order.by = time(sp500ret))

plot(predvol, type = "l")
lines(uncvol, col = "red", lwd =2)


# UGARCH package

garchspec <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                        variance.model = list(model = "sGARCH"),
                        distribution.model = "norm")

garchfit <- ugarchfit(data = sp500ret, spec = garchspec)
garchforecast <- ugarchforecast(fitORspec = garchfit, n.ahead = 5)

garchcoef <- coef(garchfit)

garchvol <- sigma(garchfit)
plot(garchvol)


tail(garchvol, 1)
sigma(garchforecast)

------------------------

Return.annualized() and StdDev.annualized() 

skewness()

 kurtosis().

 qqnorm()


  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

library(rugarch)
library(xts)
library(tydiverse)
# GARCH familiarization

alpha <- 0.1
beta <- 0.8
sp500ret <- rnorm(1000, mean = 0, sd = 0.8)
omega <- var(sp500ret) * (1 - alpha - beta)
omega
plot(sp500ret, type = "l")

e <-  sp500ret-mean(sp500ret)
e2 <- e^2
plot(e2, type = "l")


# predicte variance
nobs <- length(sp500ret)
predvar <- rep(NA, nobs)
predvar[1] <- var(sp500ret)

for (t in 2:nobs){
  predvar[t] <- omega + alpha*e2[t-1] + beta*predvar[t-1]
}

plot(predvar, type="l")


# GARCH volatility
  # sqrt of predicted vairance
predvol <- sqrt(predvar)
predvol <- xts(predvol, order.by = time(sp500ret))
# plot(predvol, type = "l")

# unconditional volatility
uncvol <-sqrt(omega/(1-alpha-beta))
uncvol <-rep(uncvol, nobs)
uncovol <-xts((uncvol), order.by = time(sp500ret))

plot(predvol, type = "l")
lines(uncvol, col = "red", lwd =2)


# UGARCH package

garchspec <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                        variance.model = list(model = "sGARCH"),
                        distribution.model = "norm")

garchfit <- ugarchfit(data = sp500ret, spec = garchspec)
garchforecast <- ugarchforecast(fitORspec = garchfit, n.ahead = 5)

garchcoef <- coef(garchfit)

garchvol <- sigma(garchfit)
plot(garchvol)


tail(garchvol, 1)
sigma(garchforecast)
 
#//////////////////////////////////////////////////////////////////////////////-
#------------------------------------------------------------------------------/
#------------------------------------------------------------------------------/
# Normal simultation and testing for normality

par(mfrow=c(2,2))
serie1 <- rnorm(1000, mean=3, sd=2)
qqnorm(serie1)
qqline(serie1)

plot(serie1)
plot(serie1, type="l")
lines(rep(mean(serie1), length(serie1)), col="red")

hist(serie1, nclass=100, probability=TRUE)
lines(seq(min(serie1), max(serie1), lenght(serei1)), dnorm(serie1, mean=mean(serie1), sd=sd(serie1)), col="red")

hist(serie1, nclass=100, probability=TRUE, col="blue")
lines(density(serie1), col="red", lwd=2)

moments::skewness(serie1)
moments::kurtosis(serie1)
moments::jarque.test(serie1)


# Daily returns are usually very non-normal
# By CLT longer-intervasl (summarization) suggest they may by more normal

#------------------------------------------------------------------------------/
#------------------------------------------------------------------------------/
# t-student
  # the parameter "v", as "v" gest largter the distribution tends to normal

# rstudent()

n <- 1000
df <- 30
t_values <- rt(n, df)

hist(t_values, breaks = 40, probability = TRUE,
     main = paste("Student's t-distribution (df =", df, ")"),
     xlab = "Value", col = "lightblue", border = "black")
lines(density(t_values), col = "red", lwd = 2)

tfit <-  QRM::fit.st(t_values)
tpars <- tfit$par.ests
nu <- tpars[1]
mu <- tpars[2]
sigma <- tpars[3]

hist(t_values, nclass=100, probability=TRUE)
lines(t_values, dnorm(t_values, mean=mean(t_values), sd=sd(t_values)), col="red")

yvals <- dt((t_values-mu)/sigma, df=nu)/sigma
lines(t_values, yvals, col="blue")


https://www.datacamp.com/cheat-sheet/xts-cheat-sheet-time-series-in-r

Entradas populares

SQL