#1 Background of the Problem
#The Question Asks me to build two volatility models ; EWMA and GARCH (1,1) and estimating VAR at 5% and 1%. Similarly, the question asks to find the log return on the day with largest drop in EUR/USD exchange.
# Methods
#Firstly attaching the packages required for this calculation.
library(Quandl)
## Warning: package 'Quandl' was built under R version 3.2.4
## Loading required package: xts
## Warning: package 'xts' was built under R version 3.2.4
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.2.4
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(fGarch)
## Warning: package 'fGarch' was built under R version 3.2.4
## Loading required package: timeDate
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 3.2.4
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
## Loading required package: fBasics
## Warning: package 'fBasics' was built under R version 3.2.4
##
## Rmetrics Package fBasics
## Analysing Markets and calculating Basic Statistics
## Copyright (C) 2005-2014 Rmetrics Association Zurich
## Educational Software for Financial Engineering and Computational Science
## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
## https://www.rmetrics.org --- Mail to: info@rmetrics.org
library(fTrading)
## Warning: package 'fTrading' was built under R version 3.2.4
#(a) Build two volatility models: (i) EWMA, (ii) and GARCH(1,1) with normal innovations
exc <- Quandl("ECB/EURUSD", type = "zoo")
str(exc)
## 'zoo' series from 1999-01-04 to 2016-03-22
## Data: num [1:4410] 1.18 1.18 1.17 1.16 1.17 ...
## Index: Date[1:4410], format: "1999-01-04" "1999-01-05" "1999-01-06" "1999-01-07" ...
dlexc <- diff(log(exc))
mdlexc <- mean(dlexc)
ind<- index(dlexc)
dlexc1 <- SMA((dlexc-mdlexc)^2,n=60)
dlexc2 <- SMA((dlexc-mdlexc)^2,n=150)
dlexcea<- EWMA((dlexc - mdlexc)^2,lambda=0.05)
par(mfrow=c(2,2), cex=0.5, mar =c(2,2,3,1))
plot((dlexc-mdlexc)^2)
plot(dlexc1)
plot(dlexc2)
plot(dlexcea)

ga1 <- garchFit( ~ garch(1,1), data=dlexc, trace=FALSE)
ga1
##
## Title:
## GARCH Modelling
##
## Call:
## garchFit(formula = ~garch(1, 1), data = dlexc, trace = FALSE)
##
## Mean and Variance Equation:
## data ~ garch(1, 1)
## <environment: 0x0000000017b15308>
## [data = dlexc]
##
## Conditional Distribution:
## norm
##
## Coefficient(s):
## mu omega alpha1 beta1
## 4.9387e-05 1.2311e-07 2.8629e-02 9.6888e-01
##
## Std. Errors:
## based on Hessian
##
## Error Analysis:
## Estimate Std. Error t value Pr(>|t|)
## mu 4.939e-05 8.533e-05 0.579 0.56275
## omega 1.231e-07 4.207e-08 2.926 0.00343 **
## alpha1 2.863e-02 3.076e-03 9.307 < 2e-16 ***
## beta1 9.689e-01 3.155e-03 307.084 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log Likelihood:
## 16242.7 normalized: 3.683987
##
## Description:
## Tue Mar 22 20:04:46 2016 by user: t420
summary(ga1)
##
## Title:
## GARCH Modelling
##
## Call:
## garchFit(formula = ~garch(1, 1), data = dlexc, trace = FALSE)
##
## Mean and Variance Equation:
## data ~ garch(1, 1)
## <environment: 0x0000000017b15308>
## [data = dlexc]
##
## Conditional Distribution:
## norm
##
## Coefficient(s):
## mu omega alpha1 beta1
## 4.9387e-05 1.2311e-07 2.8629e-02 9.6888e-01
##
## Std. Errors:
## based on Hessian
##
## Error Analysis:
## Estimate Std. Error t value Pr(>|t|)
## mu 4.939e-05 8.533e-05 0.579 0.56275
## omega 1.231e-07 4.207e-08 2.926 0.00343 **
## alpha1 2.863e-02 3.076e-03 9.307 < 2e-16 ***
## beta1 9.689e-01 3.155e-03 307.084 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log Likelihood:
## 16242.7 normalized: 3.683987
##
## Description:
## Tue Mar 22 20:04:46 2016 by user: t420
##
##
## Standardised Residuals Tests:
## Statistic p-Value
## Jarque-Bera Test R Chi^2 466.1978 0
## Shapiro-Wilk Test R W 0.9898003 0
## Ljung-Box Test R Q(10) 6.2115 0.7971919
## Ljung-Box Test R Q(15) 15.93686 0.3862604
## Ljung-Box Test R Q(20) 18.90362 0.5280988
## Ljung-Box Test R^2 Q(10) 7.002851 0.7251758
## Ljung-Box Test R^2 Q(15) 7.453383 0.9438272
## Ljung-Box Test R^2 Q(20) 12.13943 0.9111933
## LM Arch Test R TR^2 7.083462 0.852049
##
## Information Criterion Statistics:
## AIC BIC SIC HQIC
## -7.366160 -7.360361 -7.366161 -7.364115
# b) Consider first the 5% VaR. For each of the models plot the log change and the in the sample 5% VaR.Calculate the fraction of times in the sample where the log change falls below the 5% VaR. Repeat the same with 1% VaR.
var5e <- mdlexc - 1.6 * sqrt(dlexc)
## Warning in sqrt(dlexc): NaNs produced
var5g <- mdlexc - 1.6 * ga1@sigma.t
par(mfrow = c(1,2))
plot(ind, dlexc, main = "5% VaR")
lines(ind, var5e, col = "blue")
plot(ind, dlexc, main = "5% VaR garch")
lines(ind, var5g, col ="green")

var1e <- mdlexc - 2.3 * sqrt(dlexcea)
var1g <- mdlexc - 2.3 * ga1@sigma.t
plot(ind, dlexc, main = "1% VaR")
lines(ind, var1e, col = "blue")

plot(ind, dlexc, main = "1% VaR garch")
lines(ind, var1g, col ="yellow")

# c)Find the day in the sample with largest drop in EUR/USD (and thus smallest yt). What was the log return yt for this day, and how does it compare to the 5% and 1% VaR based on the two models?
min(dlexc)
## [1] -0.04735441
min(var5e)
## [1] NaN
min(var1e)
## [1] -0.04070219
index(dlexc[dlexc==min(dlexc)])
## [1] "2008-12-19"
# The day with smallest ecxchange rate was on 2008/12/19. The daily return of highest drop day was -0.04 and the daily return on the day was -0.04.