# R 3.4.1 # Daily Volatilities Ex3_DailyVola_R.txt library("tseries") library("zoo") # Read data require(quantmod) # Finanzdaten download spc <- new.env() dax <- new.env() setDefaults(getSymbols,src="yahoo") getSymbols("^GSPC", env = spc, from = "1998-10-01", to = "2004-10-01") getSymbols("^GDAXI", env = dax, from = "1998-10-01", to = "2004-10-01") # read in series are of xts type, plot is different for xts and ts x.o <-as.zoo( spc$GSPC[,1]); y.o <- as.zoo(dax$GDAXI[,1]); x.h <-as.zoo( spc$GSPC[,2]); y.h <- as.zoo(dax$GDAXI[,2]); x.l <-as.zoo( spc$GSPC[,3]); y.l <- as.zoo(dax$GDAXI[,3]); x.c <-as.zoo( spc$GSPC[,4]); y.c <- as.zoo(dax$GDAXI[,4]); ### Plot the data plot(merge(x.c,y.c), main = "^GSPC , ^GDAXI") # plot price series r.x <- diff(log(x.c)); # return US stock index r.y <- diff(log(y.c)); # return other index r.x <- r.x[is.na(r.x)==FALSE] r.y <- r.y[is.na(r.y)==FALSE] x.o <- x.o[is.na(x.o)==FALSE] x.h <- x.h[is.na(x.h)==FALSE] x.l <- x.l[is.na(x.l)==FALSE] x.c <- x.c[is.na(x.c)==FALSE] y.o <- y.o[is.na(y.o)==FALSE] y.h <- y.h[is.na(y.h)==FALSE] y.l <- y.l[is.na(y.l)==FALSE] y.c <- y.c[is.na(y.c)==FALSE] ########################### ### volatility measures ### #as in the slides ########################### s2_0_x <- diff(x.c)^2 s2_0_y <- diff(y.c)^2 s2_2_x <- 0.3697*(x.h - x.l)^2 s2_2_y <- 0.3697*(y.h - y.l)^2 s2_5_x <- 0.5*(x.h - x.l)^2 - 0.386*(x.c - x.o)^2 s2_5_y <- 0.5*(y.h - y.l)^2 - 0.386*(y.c - y.o)^2 # s_0_x <- sqrt(s2_0_x) s_0_y <- sqrt(s2_0_y) s_2_x <- sqrt(s2_2_x) s_2_y <- sqrt(s2_2_y) s_5_x <- sqrt(s2_5_x) s_5_y <- sqrt(s2_5_y) ### x nx <- length(s_5_x) plot(s_0_x, main="x: daily volatility measures: 0 black, 2 blue, 5 green") lines(s_2_x[2:nx], col="blue") lines(s_5_x[2:nx], col="green") plot(density(s_0_x), ylim=c(0,0.09), main="x: daily volatility measures: 0 black, 2 blue, 5 green") lines(density(s_2_x[2:nx]), col="blue") lines(density(s_5_x[2:nx]), col="green") # correlation between 3 volatility measures cor(cbind ( as.vector(s_0_x), as.vector(s_2_x[2:nx]) , as.vector(s_5_x[2:nx]) )) ### y ny <- length(s_5_y) plot(s_0_y, main="y: daily volatility measures: 0 black, 2 blue, 5 green") lines(s_2_y[2:ny], col="blue") lines(s_5_y[2:ny], col="green") plot(density(s_5_y[2:ny]), col="green", main="y: daily volatility measures: 0 black, 2 blue, 5 green") lines(density(s_2_y[2:ny]), col="blue") lines(density(s_0_y)) # correlation between 3 volatility measures cor(cbind ( as.vector(s_0_y), as.vector(s_2_y[2:ny]) , as.vector(s_5_y[2:ny]) )) ## EXERCISE: # (1) Which vola measure would you prefer and why.