setwd("\\\\vrisi01/users$/oadesanya/Documents")
install.packages("tseries", repos ="http://cran.rstudio.com/") #installing tseries package
## package 'tseries' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\oadesanya\AppData\Local\Temp\Rtmp8aEhMp\downloaded_packages
library(tseries)
## Warning: package 'tseries' was built under R version 3.3.1
COdata <- get.hist.quote('co',quote="Close")
## time series starts 2007-05-09
head(COdata)
## Close
## 2007-05-09 5.50
## 2007-05-10 5.50
## 2007-05-11 5.50
## 2007-05-14 5.46
## 2007-05-15 5.46
## 2007-05-16 5.50
COret <- log(lag(COdata)) - log(COdata)
COvol <- sd(COret) * sqrt(250) * 100
COvol
## [1] 49.78543
Vol <- function(d, logrets)
{
var = 0
lam = 0
varlist <- c()
for (r in logrets) {
lam = lam*(1 - 1/d) + 1
var = (1 - 1/lam)*var + (1/lam)*r^2
varlist <- c(varlist, var)
}
sqrt(varlist)
}
volest <- Vol(10,COret)
volest2 <- Vol(40,COret)
volest3 <- Vol(80,COret)
There is a high volatility estimate change around the 600 index point .The estimate for the .2 decay factor (blue line) is smoother compared to the other two, the spike is not as sharp as the other decay factors.The plot shows that the volatility is fairly stable majority of the time except for some small spikes.
plot(volest,type="l")
lines(volest2,type="l",col="red")
lines(volest3, type = "l", col="blue")
legend("topright",legend = c(".9 decay factor", ".6 decay factor", ".2 decay factor"),
col = c( "black","red", "blue"), lty =1.2,cex =0.8)