R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Libraries
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(xts)
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(rugarch)
## Loading required package: parallel
## 
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
## 
##     sigma
# Data upload
getSymbols("AAPL",from = "2008-01-01",to = "2020-09-01")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "AAPL"
#Daily prices
chartSeries(AAPL)

# Daily returns
return <- CalculateReturns(AAPL$AAPL.Close)
return <- return[-1]
hist(return)

chartSeries(return)

#Comments: As seen in plots, price is non-stationary while return is stationary(ie:move around a constant mean over time)
#So we model growth in share price
chart.Histogram(return,
                methods = c('add.density', 'add.normal'),
                colorset = c('blue', 'green', 'red'))

#Comments: Green line is higher than normal distribution (red line). Hence, student t distribution (heavier tail) would be more suitable for model


# 1. sGARCH model with contant mean

#Model specification
s <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                variance.model = list(model = "sGARCH"),
                distribution.model = 'norm')
#Model Estimation
m <- ugarchfit(data = return, spec = s)

f <- ugarchforecast(fitORspec = m, n.ahead = 20)



# 2. GARCH with t-distribution
s <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                variance.model = list(model = "sGARCH"),
                distribution.model = 'sstd')
m <- ugarchfit(data = return, spec = s)


# 3. GJR-GARCH
s <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                variance.model = list(model = "gjrGARCH"),
                distribution.model = 'sstd')
m <- ugarchfit(data = return, spec = s)


#We chose model 3 with lowest AIC and Information Criteria

# Forecasting
sfinal <- s
#Merge parameter
setfixed(sfinal) <- as.list(coef(m))

f2021 <- ugarchforecast(data = return,
                        fitORspec = sfinal,
                        n.ahead = 252)
#Forecasting future variance
plot(sigma(f2021))

sim <- ugarchpath(spec = sfinal,
                  m.sim = 3,
                  n.sim = 1*252,
                  rseed = 123)
plot.zoo(fitted(sim))

plot.zoo(sigma(sim))

#Forecasting from last price
p <- 129.0400*apply(fitted(sim), 2, 'cumsum') + 129.0400
matplot(p, type = "l", lwd = 3)

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.