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)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started