2014년 2월 24일 월요일

변동성 garch(1)

library(tseries)
library(quadprog)
getwd()
setwd('c:/rdata')

###here kospi data
dat <- read.csv('port.csv', header=TRUE)
#dat$Date <- as.Date(dat$Date, "%Y-%m-%d")

odat<-dat[c(1:nrow(dat)),]
apply(na.omit(odat),2,max)
attach(odat)

x<-odat[order(일자),]

apply(na.omit(x),2,max)


price <- as.numeric(x$안랩)
ret <- 0

for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]*100
}

chg <-0
for (i in 2:length(price)) {
chg[i] <- (price[i]-price[i-1])/price[i-1]*100
}


#garch

  mod.fit<-garch(x = ret - mean(ret), order = c(0,1), trace = TRUE) #Fit ARCH(1)
  summary(mod.fit)                      #Summarizes fit
  names(mod.fit)
  mod.fit$coef
#  plot(mod.fit)

 coef(mod.fit) # see above and compare with coefficient alpha0 and alpha1 caculated by arch model


par(mfrow=c(3,1))
par(mar=c(3,3,1,2))


plot(mod.fit$fitted.values[,1], type='l', xaxt='n',
    panel.first=grid(col = "gray", lty = "dotted"), pch=20)
title(main = "변동성", sub = NULL, xlab = NULL, ylab = NULL,
      line = NA, outer = FALSE)
plot(chg, type='l', xaxt='n'
)
points(chg, pch = 20, col = "red")
title(main = "일등락율%", sub = NULL, xlab = NULL, ylab = NULL,
      line = NA, outer = FALSE)

plot(ret, type='l', xaxt='n'
)
points(ret, pch = 20, col = "red")
title(main = "수익률%", sub = NULL, xlab = NULL, ylab = NULL,
      line = NA, outer = FALSE)



댓글 없음:

댓글 쓰기