library("zoo")
library("forecast")
library("tseries")
library("urca")
library ("Quandl")
library("fGarch")
library("rugarch")
library("stargazer")
library("fTrading")

Problem 3

Get data for the EUR/USD exchange rate ECB/EURUSD, construct the log change \(y_t = \Delta log EURUSD_t\)

Introduction and Data:

In this problem, data is analysed about EUR vs USD Foreign Exchange Reference Rate from 1999 to 2016. The daily data for this problem is collectd from Quandl.

EUvUS<- Quandl("ECB/EURUSD", type="zoo")
#EUvUS<- ts(EUvUS_d[,2], start=c(1999,1), frequency = 4)
str(EUvUS)
## '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" ...
head(EUvUS)
## 1999-01-04 1999-01-05 1999-01-06 1999-01-07 1999-01-08 1999-01-11 
##     1.1789     1.1790     1.1743     1.1632     1.1659     1.1569
tail(EUvUS)
## 2016-03-15 2016-03-16 2016-03-17 2016-03-18 2016-03-21 2016-03-22 
##     1.1109     1.1064     1.1311     1.1279     1.1271     1.1212
tsdisplay(EUvUS, lag.max = 100, xlab=" Years 1999-2016", ylab="Exchange Rate", main="EUR vs USD Foreign Exchange Reference Rate from 1999 to 2016,Daily")

l.EUvUS<- log (EUvUS)
dl.EUvUS<-diff(l.EUvUS)
tsdisplay(dl.EUvUS, lag.max = 100, xlab=" Years 1999-2016", ylab="diff log Exchange Rate", main="log and firt difference EUR vs USD Foreign Exchange Reference Rate from 1999 to 2016,Daily")

The plot above indicate increasing and decreasing trend over the time frame. The ACF plot has slow continous decresaing, while PACF has no peaks. After taking log and first difference of the exchange rate, the data looks stationary. The ACF and PACF has peak at 10 and 12 lag.

(a) Build two volatility models: (i) EWMA, (ii) GARCH(1,1) with normal innovations.

The EWMA includes all prior observations, but with exponentially declining weights throughout time. The EWMA approach has one attractive feature: it requires relatively little stored data. To update our estimate at any point, we only need a prior estimate of the variance rate and the most recent observation value.

A secondary objective of EWMA is to track changes in the volatility. For small values, recent observations affect the estimate promptly. For closer to one, the estimate changes slowly based on recent changes in the returns of the underlying variable. The EWMA formula does not assume a long run average variance level. Thus, the concept of volatility mean reversion is not captured by the EWMA. The ARCH/GARCH models are better suited for this purpose.

# EWMA model 
E.dl.EUvUS <- mean(dl.EUvUS)
t <- index(dl.EUvUS)
dl.EUvUS.ewma <- EWMA((dl.EUvUS-E.dl.EUvUS)^2,lambda=.05) # note: in EWMA lambda is actually 1-lambda
plot(dl.EUvUS.ewma, type="l", main="exponentially weighted moving average")

#GARCH (1,1)
EvUS.garch11 <- garchFit( ~ garch(1,1), data=dl.EUvUS, trace=FALSE)
summary(EvUS.garch11)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 1), data = dl.EUvUS, trace = FALSE) 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 1)
## <environment: 0x00000000111590e8>
##  [data = dl.EUvUS]
## 
## 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 23:08:40 2016 by user: kluitel 
## 
## 
## 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.

## 5% Var 
VaR.5.EWMA <- E.dl.EUvUS - 1.645*sqrt(dl.EUvUS.ewma)    # 5% VaR for EWMA
sum(dl.EUvUS < VaR.5.EWMA) / length(dl.EUvUS)     # fraction of sample where loss exceeds 5% VaR for EWMA
## [1] 0.046269
VaR.5.garch <- E.dl.EUvUS - 1.645*EvUS.garch11@sigma.t        # 5% VaR for GARCH(1,1)
sum(dl.EUvUS < VaR.5.garch) / length(dl.EUvUS)       # fraction of sample where loss exceeds 5% VaR for GARCH(1,1)
## [1] 0.04672261
par(mfrow=c(1,2), cex=0.8)
plot(t, dl.EUvUS, type="l", col="steelblue", main="5% VaR - Exponentially Weighted Moving Average")
lines(t, VaR.5.EWMA, type="l", col="red")

plot(t, dl.EUvUS, type="l", col="steelblue", main="5% VaR - GARCH(1,1)")
lines(t, VaR.5.garch, type="l", col="red")

## 1% Var 
VaR.1.EWMA <- E.dl.EUvUS - 2.3*sqrt(dl.EUvUS.ewma)    # 1% VaR for EWMA
sum(dl.EUvUS < VaR.1.EWMA) / length(dl.EUvUS)     # fraction of sample where loss exceeds 1% VaR for EWMA
## [1] 0.009072352
VaR.1.garch <- E.dl.EUvUS - 2.3*EvUS.garch11@sigma.t        # 1% VaR for GARCH(1,1)
sum(dl.EUvUS < VaR.1.garch) / length(dl.EUvUS)       # fraction of sample where loss exceeds 1% VaR for GARCH(1,1)
## [1] 0.01406215
par(mfrow=c(1,2), cex=0.8)
plot(t, dl.EUvUS, type="l", col="steelblue", main="1% VaR - Exponentially Weighted Moving Average")
lines(t, VaR.1.EWMA, type="l", col="red")

plot(t, dl.EUvUS, type="l", col="steelblue", main="1% VaR - GARCH(1,1)")
lines(t, VaR.1.garch, type="l", col="red")

(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(dl.EUvUS)          # the largest drop or smalest value 
## [1] -0.04735441
index(dl.EUvUS[dl.EUvUS==min(dl.EUvUS)]) # the day of largest drop or smallest value
## [1] "2008-12-19"

The lagest drop is smaller than the fraction of timee in the sample where the log chnage falls below teh 5% and 1% of both EWMA and GARCH model.