The data used in this forecasting is a 4 years data, from 1 October 2018 to 30 September 2022. The two-year data from 1 October 2018 to 30 September 2021 constitute the first estimation window. The testing window is between 1 October 2021 and 30 September 2022. Later, VaR forecast will be compared with the actual outcome.
Rule of thumb of Violation Rate (VR): If VR = [0.8, 1.2] the model is good If VR = [0.5, 0.8] or VR = [1.2, 1.5] the model is acceptable If VR = [0.3, 0.5] or VR = [1.5, 2] the model is bad If VR<0.5 or VR>2 the model is useless
VR is Violation Rate. VaR is Value at Risk. In forecast model, it estimates the Value at Risk taht relates to “p” or probability. We determine the probability before running the forecast model. If the VaR result is more than the determined probability, we can concludes that the model is under forecast. On the other hand, if the VaR result is less than the determined probability, it is over forecast as our conclusion.
install.packages("zoo", repos = "http://cran.us.r-project.org")
install.packages("tseries", repos = "http://cran.us.r-project.org")
library(zoo)
library(tseries)
Secondly, call all the stocks that we are going to analyze.
p = get.hist.quote(instrument = "SMMA.JK", start = "2018-10-01",
end = "2022-09-30",quote = "AdjClose",quiet = T)
# download the prices
y = diff(log(p)) # get returns
y = coredata(y) # strip date information from returns
Note that in order to get returns data, it is better to use logarithm natural.
To understanding the data; find out number of observations, set up the window length, probability, and matrix to hold the 4 models of forecast.
T = length(y) # number of observations for return y
WE = 700 # estimation window length
p = 0.01 # probability
l1 = WE * p # HS observation
value = 1; # portfolio
VaR = matrix(nrow=T,ncol=4) # matrix to hold VaR forecasts for 4 models
Historical vs. Implied Volatility; First, let’s put this metric into
a bit of perspective. There are two broad approaches: historical and
implied (or implicit) volatility. The historical approach assumes that
the past is prologue; we measure history in the hope that it is
predictive. Implied volatility, on the other hand, ignores history; it
solves for the volatility implied by market prices. It hopes that the
market knows best and that the market price contains, even if
implicitly, a consensus estimate of volatility.
lambda = 0.94;
s11 = var(y[1:30]);
for(t in 2:WE) s11 = lambda * s11 + (1 - lambda) * y[t - 1]^2
install.packages("fGarch", repos = "http://cran.us.r-project.org")
library(fGarch)
Run the data analysis by determining first the data window. The next step is making a forecast using EWMA, MA, HS, and GARCH.
for (t in (WE + 1):T){
t1 = t - WE; # start of the data window
t2 = t - 1; # end of the data window
window = y[t1:t2] # data for estimation
# EWMA
s11 = lambda * s11 + (1 - lambda) * y[t - 1]^2
VaR[t,1] = -qnorm(p) * sqrt(s11) * value
# MA
VaR[t,2] = -sd(window) * qnorm(p)* value
# HS
ys = sort(window) # sort returns
VaR[t,3] = -ys[l1]* value # VaR number
# GARCH(1,1)
g=garchFit(formula = ~ garch(1,1), window ,trace=FALSE,
include.mean=FALSE)
par=g@fit$matcoef # put parameters into vector par
s4=par[1]+par[2]* window[WE]^2+par[3]* g@h.t[WE]
VaR[t,4] = -sqrt(s4) * qnorm(p) * value
}
The next step of coding is printing the analysis result.
W1 = WE+1
for (i in 1:4){
VR = sum(y[W1:T] < -VaR[W1:T,i])/(p*(T - WE))
s = sd(VaR[W1:T,i]) # VaR volatility
cat(i,"VR",VR,"VaR vol",s,"\n") # print results
}
The last step of coding is displaying the data visualization.
matplot(cbind(y[W1:T],VaR[W1:T,]),type='l')
Related to previous rule of thumbs of VR:
If VaR = [0.8, 1.2] the
model is good
If VaR = [0.5, 0.8] or VR = [1.2, 1.5] the model is
acceptable
If VaR = [0.3, 0.5] or VR = [1.5, 2] the model is bad
If VaR < 0.5 or VR > 2 the model is useless
So, VaR forecast analysis for SMMA with each model:
1.
EWMA (Exponentially Weighted Moving Average)
VR
2.054795 VR vol (VaR) 0.02255548 –> Based on VR result, but the model
is useless. It has under-forecast VaR, since the probability (p) was
determined 0.01
2. MA (Moving Average)
VR 0 VR
vol (VaR) 0.008527537 –> Based on VR result, but the model is
useless. It has over-forecast VaR, since the probability (p) was
determined 0.01
3. HS (Historical Simulation)
VR 0 VR vol (VaR) 0.01373547 –> Based on VR result, but the model
is useless. It has under-forecast VaR, since the probability (p) was
determined 0.01
4. GARCH (Generalized Auto
Regressive Conditional Heteroskedasticity)
VR 1.369863 VR vol (VaR)
0.0177873 –> Based on VR result, but the model is acceptable. It has
under-forecast VaR, since the probability (p) was determined 0.01