Outline

Introduction to Volatility Modelling

Definition

Types of Volatility

Volatility Modelling Techniques

Important Papers to Ref

Steps in Financial Time Series Forecasting

Data Retrieval and Data Exploration

#Clear working environment 
#rm(list = ls()) 

#Install packages
#install.packages("quantmod")
library(quantmod)

#getSymbols(c("","",...)) -> obtain historical quotes 
getSymbols(c("AAPL", "TSLA"))

s1p <- AAPL[,6]
s1r <- dailyReturn(s1p)

s2p <- TSLA[,6]
s2r <- dailyReturn(s2p)

Data Retrieval and Data Exploration

##    vars    n  mean    sd median trimmed   mad  min    max  range skew kurtosis
## X1    1 4129 43.66 49.22  23.12   34.63 25.36 2.37 180.43 178.06 1.42     0.63
##      se
## X1 0.77
##    vars    n  mean    sd median trimmed   mad  min    max  range skew kurtosis
## X1    1 3251 62.41 96.26  16.54   41.35 13.19 1.05 409.97 408.92 1.69     1.48
##      se
## X1 1.69
##    vars    n mean   sd median trimmed  mad   min  max range  skew kurtosis se
## X1    1 4129    0 0.02      0       0 0.01 -0.18 0.14  0.32 -0.13      5.8  0
##    vars    n mean   sd median trimmed  mad   min  max range skew kurtosis se
## X1    1 3251    0 0.04      0       0 0.03 -0.21 0.24  0.45 0.32     4.97  0

Data Retrieval and Data Exploration

#install.packages("psych")
library(psych)
describe(s1p)
describe(s2p)
describe(s1r)
describe(s2r)

Data Preprocessing

First Model: Autoregressive Integrated Moving Average (ARIMA)

Explanation of ARIMA models and their components (AR, I, MA)

Notes on Stationarity

ARIMA Equations

Autocorrelation Function and Partial Autocorrelation Function

ACF Plots

par(mfrow = c(2,2))
acf(s1p)
acf(s1r)
acf(s2p)
acf(s2r)

PACF Plots

par(mfrow = c(2,2))
pacf(s1p)
pacf(s1r)
pacf(s2p)
pacf(s2r)

Parameter estimation and model selection using AIC and BIC

Model Estimation

# ARIMA
res1 <- arima(s1p, order = c(1,0,0))
res1

res2 <- arima(s1p, order = c(1,1,1))
res2

res3 <- arima(s1p, order = c(2,1,1))
res3

# AUTO.ARIMA
# install.packages("forecast")
library(forecast)
res4 <- auto.arima(s1p)
res4

Evaluation of ARIMA models

Residuals Diagnostics

# We need tseries library for some test statistics
# install.packages("tseries")
library(tseries)

# Obtain residuals and run some diagnostics 
err4 <- res4$residuals
fit4 <- res4$fitted

# Calculate model accuracy measures
describe(err4)
mse4 <- mean(err4^2)
mad4 <- mean(abs(err4))
mape4 <- mean(abs(err4/fit4))

Residuals Diagnostics: What the error should look like?

Residuals Diagnostics: What the error should look like?

Residuals Diagnostics

# Run a few more tests 
# Ljung-Box Test -> H1: Error is not stationary 
Box.test(err4, lag = 16, type = "Ljung")

# ADF Test -> H1: Error has no unit root 
adf.test(err4)

# Jarque-Bera Test -> H1: Error is not normal 
jarque.bera.test(err4)

Residuals Diagnostics: What the test results should be

# Run a few more tests 
# Ljung-Box Test -> H1: Error is not stationary 
Box.test(err, lag = 16, type = "Ljung")

# ADF Test -> H1: Error has no unit root 
adf.test(err)

# Jarque-Bera Test -> H1: Error is not normal 
jarque.bera.test(err)

Applcation: Stock Price and Stock Returns Forecasting in R

Second Model: GARCH

Data Exploration: Returns

Data Exploration: Squared Returns

Explanation of GARCH models and their components (ARCH, GARCH)

$$

\[\begin{aligned} r_t &= `ARIMA' + \sigma_t\epsilon_t \\ \sigma_t &= `GARCH' \\ \epsilon_t &\rightarrow `Distribution' \end{aligned}\]

$$

Explanation of GARCH models and their components (ARCH, GARCH)

\[ \mathrm{ARCH}(p) : \sigma_t^2 = \omega + \sum_{i=1}^p \alpha_i \epsilon_{t-i}^2 \]

where \(\sigma_t^2\) is the conditional variance at time \(t\), \(\omega\) is a constant, \(\alpha_i\) are the ARCH parameters, and \(\epsilon_{t-i}^2\) are the squared residuals at time \(t-i\).

Explanation of GARCH models and their components (ARCH, GARCH)

The GARCH component is defined as:

\[ \mathrm{GARCH}(q) : \sigma_t^2 = \omega + \sum_{i=1}^p \alpha_i \epsilon_{t-i}^2 + \sum_{j=1}^q \beta_j \sigma_{t-j}^2 \]

where \(\beta_j\) are the GARCH parameters, and \(\sigma_{t-j}^2\) are the past conditional variances.

Together, the ARCH and GARCH components form the GARCH(p, q) model, which can be expressed as:

\[ \mathrm{GARCH}(p, q) : \sigma_t^2 = \omega + \sum_{i=1}^p \alpha_i \epsilon_{t-i}^2 + \sum_{j=1}^q \beta_j \sigma_{t-j}^2 \]

Parameter estimation and model selection using AIC and BIC

# install.packages("rugarch")
library(rugarch)

gspec1 <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)),
                    mean.model = list(armaOrder = c(0, 0)),
                    distribution.model = "std")

# You can use different variance, mean and distribution models

gfit1 <- ugarchfit(gspec1, s2r)
gfit1

Residuals Diagnostics

Residuals Diagnostics

# Run a few more tests 
# Ljung-Box Test -> H1: Error is not stationary 
Box.test(gerr1, lag = 16, type = "Ljung")

# ADF Test -> H1: Error has no unit root 
adf.test(gerr1)

# Jarque-Bera Test -> H1: Error is not normal 
jarque.bera.test(gerr1)

Application: Returns Forecasting

Application: Returns Simulation

Extension of GARCH

DIY

getSymbols('TSLA')
getSymbols('BTC-USD')
getSymbols('USDTHB=X')