Q3

We retrieve the stock price data for Microsoft and transform the data time series into \(y_t=\Delta \text{log EURUSD_t}\)

First we load the appropriate packages.

library("Quandl")
library("tseries")
library("urca")
library("fGarch")
library("fTrading")

Then we run the log differences transformation of the EURUSD currency data.

eur<-Quandl("ECB/EURUSD", type="zoo")
logeur<-log(eur)
diffeur<- diff(logeur,1)
par(mfrow=c(1,2))
plot(logeur,xlab="Time", ylab="Log of EURUSD")
plot(diffeur,xlab="Time", ylab="Log Differences of EURUSD")

We can see that \(\Delta \text{log EURUSD_t}\) looks stationary. We can also see that the raw log series does not.

Build Two Volatility Models EWMA and GARCH(1,1) With Normal Innovations

First for the EWMA model, we need to find the mean of the EURUSD. Then we will plot the EWMA model of EURUSD

mdiffeur<-mean(diffeur)
resideur<-diffeur-mdiffeur
diffeur.ewma<-EWMA(resideur^2, lambda=.05)
plot(diffeur.ewma, type="l", main="Exponentially Weighted Moving Average")

Now we construct the GARCH(1,1) model. We will plot the residuals in standardized, conditional, and squared standardized format.

garch11<-garchFit(~garch(1,1), data=diffeur,trace=FALSE)
par(mfrow=c(2,2), cex=0.6, mar=c(2,2,3,1))
plot(garch11@residuals, type="l", xlab="", ylab="", main="residuals")
plot(garch11@sigma.t, type="l", xlab="", ylab="", main="conditional standard deviation")
plot(garch11@residuals/garch11@sigma.t, type="l", xlab="", ylab="", main="standardized residuals")
plot((garch11@residuals/garch11@sigma.t)^2, type="l", xlab="", ylab="", main="squared standardized residuals")

Consider the 5% VAR

Now we look at the 5% VaR in the EWMA and GARCH(1,1) models.

var5.ewma <- mdiffeur - 1.645*sqrt(diffeur.ewma)
var5.garch <-mdiffeur - 1.645*garch11@sigma.t 
t<-index(diffeur)
par(mfrow=c(1,2), cex=0.8)
plot(t, diffeur, type="l", col="steelblue", main="5% VaR - Exponentially Weighted Moving Average")
lines(t, var5.ewma, type="l", col="red")
plot(t, diffeur, type="l", col="steelblue", main="5% VaR - ARCH(8)")
lines(t, var5.garch, type="l", col="red")

The 5% VAR threshold is marked by the red line. Volatility should fall below this value about 5% of the time. However, we should calcuate the actual percentage of times it falls below this threshold.

sum(diffeur < var5.ewma) / length(diffeur)
## [1] 0.04631101
sum(diffeur < var5.garch) / length(diffeur)
## [1] 0.04699205

As we can see, the VAR at 5% seems to be relatively accurate.

Now we do the same for a 1% VAR.

var1.ewma <- mdiffeur - 2.326*sqrt(diffeur.ewma)
var1.garch <-mdiffeur - 2.326*garch11@sigma.t 
t<-index(diffeur)
par(mfrow=c(1,2), cex=0.8)
plot(t, diffeur, type="l", col="steelblue", main="1% VaR - Exponentially Weighted Moving Average")
lines(t, var1.ewma, type="l", col="red")
plot(t, diffeur, type="l", col="steelblue", main="1% VaR - ARCH(8)")
lines(t, var1.garch, type="l", col="red")

The 1% VAR threshold is marked by the red line. Volatility should fall below this value about 1% of the time. However, we should calcuate the actual percentage of times it falls below this threshold.

sum(diffeur < var1.ewma) / length(diffeur)
## [1] 0.007491487
sum(diffeur < var1.garch) / length(diffeur)
## [1] 0.01316686

We can see that the 1% VAR threshold is close to the actual fractional values.

Summary of Day with Largest Drop in EUR/USD

The smallest \(y_t\) is -4.735e-02. This was the smallest return for the time series. We can do this just by looking at the summary.

summary(diffeur)
##      Index               diffeur          
##  Min.   :1999-01-05   Min.   :-4.735e-02  
##  1st Qu.:2003-04-25   1st Qu.:-3.612e-03  
##  Median :2007-08-09   Median : 7.296e-05  
##  Mean   :2007-08-09   Mean   :-1.441e-05  
##  3rd Qu.:2011-11-23   3rd Qu.: 3.622e-03  
##  Max.   :2016-03-16   Max.   : 4.204e-02

However, we also want to know what was the date of this lowest value \(y_t\) is -4.735e-02. For that we have to play around with the data a little bit.We will sort on the values to see what the associated date is.

diff<-as.data.frame(diffeur)
diff<-transform(diff, Dates=as.Date(rownames(diff)))
diff2<-diff[order(diffeur),]
head(diff2)
##                diffeur      Dates
## 2008-12-19 -0.04735441 2008-12-19
## 2015-01-23 -0.03682043 2015-01-23
## 2011-11-01 -0.02707564 2011-11-01
## 2008-10-22 -0.02620506 2008-10-22
## 2008-08-08 -0.02599590 2008-08-08
## 2009-03-27 -0.02319634 2009-03-27

We can now see that the date of the lowest value, -.04735 was on December 19, 2008. In order to find out the value of the 5% and 1% of the EWMA and the ARCH on that day, we need to add a date column to the EWMA and ARCH data.

datewma1<- data.frame(diff$Dates, var1.ewma)
datewma5<- data.frame(diff$Dates, var5.ewma)
datgarch1<- data.frame(diff$Dates, var1.garch)
datgarch5<- data.frame(diff$Dates, var5.garch)

datewma1[which(datewma1$diff.Dates==as.Date("2008-12-19")),]
##      diff.Dates   var1.ewma
## 2553 2008-12-19 -0.04116636
datewma5[which(datewma5$diff.Dates==as.Date("2008-12-19")),]
##      diff.Dates   var5.ewma
## 2553 2008-12-19 -0.02911801
datgarch1[which(datgarch1$diff.Dates==as.Date("2008-12-19")),]
##      diff.Dates  var1.garch
## 2553 2008-12-19 -0.02944243
datgarch5[which(datgarch5$diff.Dates==as.Date("2008-12-19")),]
##      diff.Dates  var5.garch
## 2553 2008-12-19 -0.02082657

We can see that 1% EWMA, 5% EWMA, 1% GARCH, 5% GARCH is -4.12%, -2.91%, -2.94%, and -2.08% respectively. Our lowest value of -4.74% is lower than all of these 4 thresholds.

This marks the end of Problem number 3