Question: # R code to be used for Learning Exercise 1 ############################################################################## ############################################################################## rm(list = ls()) # clear memory par(mfrow=c(1,1)) # one window for graphics ##############################################################################
# R code to be used for Learning Exercise 1
##############################################################################
##############################################################################
rm(list = ls()) # clear memory
par(mfrow=c(1,1)) # one window for graphics
##############################################################################
##Below make changes to the code and answer questions in LE1
library(quantmod) #
getSymbols("^GSPC", from="2005-01-01",to="2022-12-31") #
Price=GSPC$GSPC.Adjusted #
ret=diff(log(Price))
plot(Price)
plot(ret)
##
ret=na.omit(ret)
length(ret) #
library(xts)
dates=index(ret)#
library(fBasics) #
basicStats(ret) #
#
mean(ret) #mean
var(ret) #variance
stdev(ret) # standard deviation
sd(ret) #standard deviation
skewness(ret) #skewness
kurtosis(ret) #
normalTest(ret,method='jb') # #
#Correlogram
q = acf(ret,20) #
plot(q[1:20]) #
b = Box.test(ret,lag=20,type="Ljung-Box") #
b
q2 = acf(ret^2,20) #
plot(q2[1:20]) #
b2 = Box.test(ret^2,lag=20,type="Ljung-Box") #
b2
# Monthly Historical Standard Deviation (Rolling function)
vol=rollapply(ret,22,sd) # moving average of 22 observations standard deviations
vol_22=100*sqrt(252)*vol #Annualized volatility
plot(vol_22,main ="Historical Monthly Volatility", ylab="Standard Deviation, annualized", col="red")
# Annualized Historical Standard Deviation (Rolling function)
vol=rollapply(ret,252,sd) # moving average of 252 observations standard deviations
vol_252=100*sqrt(252)*vol #Annualized volatility
plot(vol_252,main ="Historical Annual Volatility", ylab="Standard Deviation, annualized", col="red")
##Plot two graphs on one plot
vol=cbind(vol_22,vol_252) # combine two time series in a matrix
plot(vol, col=c(1,2))
title("Volatility plots: Historical Rolling Standard Deviations")
legend("right", inset=0.01, legend=c("monthly", "annual"),pch=1, col=c(1,2), horiz=F)
# EWMA Riskmetrics model
library("MTS")
m1=EWMAvol(ret, lambda = 0.99) # this is RISKMETRICS model with smoothing .99
sig2_ewma=m1$Sigma.t #estimated daily variance from object m1
# Annualized EWMA volatility
s_smooth=100*sqrt(252*sig2_ewma)
library(xts)
vol_s99=as.xts(s_smooth, dates)
plot(vol_s99)
title("Riskmetrics Volatility with lambda=.99")
m2=EWMAvol(ret, lambda = 0.94) #
# Annualized EWMA volatility
s_smooth=100*sqrt(252*m2$Sigma.t)
vol_s94=as.xts(s_smooth, dates)
plot(vol_s94)
title("Riskmetrics Volatility with lambda=.94")
vol_smooth=cbind(vol_s94,vol_s99) # combine two time series in a matrix
plot(vol_smooth, col=c(1,2))
title("Volatility plots: EWMA")
legend("right", inset=0.01, legend=c("vol_s94","vol_s99"),pch=1, col=c(1,2), horiz=F)
# R code to be used for Learning Exercise 1
##############################################################################
##############################################################################
rm(list = ls()) # clear memory
par(mfrow=c(1,1)) # one window for graphics
##############################################################################
##Below make changes to the code and answer questions in LE1
library(quantmod) #
getSymbols("^GSPC", from="2005-01-01",to="2022-12-31") #
Price=GSPC$GSPC.Adjusted #
ret=diff(log(Price))
plot(Price)
plot(ret)
##
ret=na.omit(ret)
length(ret) #
library(xts)
dates=index(ret)#
library(fBasics) #
basicStats(ret) #
#
mean(ret) #mean
var(ret) #variance
stdev(ret) # standard deviation
sd(ret) #standard deviation
skewness(ret) #skewness
kurtosis(ret) #
normalTest(ret,method='jb') # #
#Correlogram
q = acf(ret,20) #
plot(q[1:20]) #
b = Box.test(ret,lag=20,type="Ljung-Box") #
b
q2 = acf(ret^2,20) #
plot(q2[1:20]) #
b2 = Box.test(ret^2,lag=20,type="Ljung-Box") #
b2
# Monthly Historical Standard Deviation (Rolling function)
vol=rollapply(ret,22,sd) # moving average of 22 observations standard deviations
vol_22=100*sqrt(252)*vol #Annualized volatility
plot(vol_22,main ="Historical Monthly Volatility", ylab="Standard Deviation, annualized", col="red")
# Annualized Historical Standard Deviation (Rolling function)
vol=rollapply(ret,252,sd) # moving average of 252 observations standard deviations
vol_252=100*sqrt(252)*vol #Annualized volatility
plot(vol_252,main ="Historical Annual Volatility", ylab="Standard Deviation, annualized", col="red")
##Plot two graphs on one plot
vol=cbind(vol_22,vol_252) # combine two time series in a matrix
plot(vol, col=c(1,2))
title("Volatility plots: Historical Rolling Standard Deviations")
legend("right", inset=0.01, legend=c("monthly", "annual"),pch=1, col=c(1,2), horiz=F)
# EWMA Riskmetrics model
library("MTS")
m1=EWMAvol(ret, lambda = 0.99) # this is RISKMETRICS model with smoothing .99
sig2_ewma=m1$Sigma.t #estimated daily variance from object m1
# Annualized EWMA volatility
s_smooth=100*sqrt(252*sig2_ewma)
library(xts)
vol_s99=as.xts(s_smooth, dates)
plot(vol_s99)
title("Riskmetrics Volatility with lambda=.99")
m2=EWMAvol(ret, lambda = 0.94) #
# Annualized EWMA volatility
s_smooth=100*sqrt(252*m2$Sigma.t)
vol_s94=as.xts(s_smooth, dates)
plot(vol_s94)
title("Riskmetrics Volatility with lambda=.94")
vol_smooth=cbind(vol_s94,vol_s99) # combine two time series in a matrix
plot(vol_smooth, col=c(1,2))
title("Volatility plots: EWMA")
legend("right", inset=0.01, legend=c("vol_s94","vol_s99"),pch=1, col=c(1,2), horiz=F)
Using the attached R code (lel.R) get the daily equity prices data for the Japanese Nikkei 225 stock index for the period 2005/01/012022/12/31 from finance.yahoo.com (use ticker N225 ) and answer questions 1-3. Note that you need to make several changes in the code, such as replacing ^GSPC index with N225 and several other parameters in the code. 1. Compute the log returns for Nikkei and find the statistics below. a) Find the mean annualized return. b) Find the annualized volatility (standard deviation for the whole sample). c) Find the skewness and kurtosis and explain if there is any asymmetry and fat tails? d) Formulate and test the efficient market hypothesis (EMH) using the Q test for returns for k=15 autocorrelations. e) Formulate and test the null hypothesis of no volatility clustering using the Q test for squared returns for k=15 autocorrelations. 2. Using the same data find annualized Monthly and Annual historical volatilities for the whole period. Here use moving average standard deviations with monthly ( 22 days) window and annual ( 252 days) window. In both cases annualize the volatilities using sqrt(252) multiplier since we use daily data. Present a plot of both volatilities on one graph. Do you observe that volatilities are changing over time? What are the main events that caused volatility peaks for Nikkei index? 3. Find exponentially weighted volatilities (EWMA) using .94 and .98 as the smoothing parameter. Plot both of these series on the same graph and attach it. Which parameter gives smoother volatility graph? 1. Compute the log returns for Nikkei and find the statistics below. a) Find the mean annualized return. b) Find the annualized volatility (standard deviation for the whole sample). c) Find the skewness and kurtosis and explain if there is any asymmetry and fat tails? d) Formulate and test the efficient market hypothesis (EMH) using the Q test for returns for k=15 autocorrelations. e) Formulate and test the null hypothesis of no volatility clustering using the Q test for squared returns for k=15 autocorrelations. 2. Using the same data find annualized Monthly and Annual historical volatilities for the whole period. Here use moving average standard deviations with monthly (22 days) window and annual ( 252 days) window. In both cases annualize the volatilities using sqrt(252) multiplier since we use daily data. Present a plot of both volatilities on one graph. Do you observe that volatilities are changing over time? What are the main events that caused volatility peaks for Nikkei index? 3. Find exponentially weighted volatilities (EWMA) using .94 and .98 as the smoothing parameter. Plot both of these serize on the same graph and attach it. Which parameter gives smoother volatility graph
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
