Instructions:
Please complete Learning Exercise 1 in the attached Word Document. You need to download a zipped file that includes R code, unzip it into a separate folder, run R code line by line and make necessary changes, and finally copy all results into the word document. Make sure to answer each question in the word document and discuss the results. Then submit the completed assignment in the word file.
a document from Rstudio is not loading onto this cite, this is the following code:
# 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) # <=== load the package "quantmod" getSymbols("^GSPC", from="2005-01-01",to="2022-12-31") # <=== get daily SP500 stock data from Yahoo Finance Price=GSPC$GSPC.Adjusted # <==Column 6 of the object "GSPC" in R ret=diff(log(Price)) plot(Price) plot(ret) ##<==notice that there is NA 1st observation in returns. Let's remove it ret=na.omit(ret) length(ret) #<===now we have 4530 observations for returns after dropping NA obs library(xts) dates=index(ret)#<===extract dates from xts object library(fBasics) #<== Load the package "fBasics" basicStats(ret) #<== Compute descriptive statistics of log returns. NOTE that Excess Kurtosis is reported. #<==(Kurtosis-3) mean(ret) #mean var(ret) #variance stdev(ret) # standard deviation sd(ret) #standard deviation skewness(ret) #skewness kurtosis(ret) #<==(Kurtosis-3) normalTest(ret,method='jb') # #<== Perform normality test.JB-test #Correlogram q = acf(ret,20) #<== Compute and plot autocorrelation for 20 lags starting from lag 0. plot(q[1:20]) #<== plot autocorrelation for 20 lags starting from lag 1. b = Box.test(ret,lag=20,type="Ljung-Box") #<==Q test b q2 = acf(ret^2,20) #<== Compute and plot autocorrelation of squared returns for 20 lags starting from lag 0. plot(q2[1:20]) #<== plot autocorrelation for 20 lags starting from lag 1. b2 = Box.test(ret^2,lag=20,type="Ljung-Box") #<==Q test 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) #<--smoothing with lambda .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)