Open bijeshcb7 opened 6 years ago
library(fGarch) da<-read.table("w-petroprice.txt",header = T) price<-ts(da$US,frequency = 52,start = c(1997,1)) dp<-ts(diff(price),frequency = 52,start = c(1997,2)) par(mfcol=c(1,1)) plot(price,xlab="year",ylab="price") plot(dp,xlb="year",ylab="changes") cprice<-diff(price) m2<-arima(cprice,order = c(3,0,0),seasonal = list(order=c(2,0,0),period=5)) m2<-arima(cprice,order = c(3,0,0),seasonal = list(order=c(2,0,0),period=5),include.mean = F) m2 length(cprice) m2<-arima(cprice,seasonal = list(order=c(2,0,0),period=5),include.mean = F) m2 adjcp<-cprice[11:716]-0.0983cprice[6:711]-0.1152cprice[1:706] acf(adjcp) pacf(adjcp) m3<-garchFit(~arma(3,0)+garch(1,1),data = adjcp,trace = F,include.mean = F) summary(m3) plot(m3) m4<-garchFit(~arma(3,0)+garch(1,1),data = adjcp,trace = F,include.mean = F,cond.dist = "std") summary(m4) plot(m4) 1 m5<-garchFit(~arma(1,0)+garch(1,1),data = adjcp,trace = F,include.mean = F,cond.dist = "sstd") summary(m5) plot(m5) M3<-arima(adjcp,order = c(3,0,0),include.mean = F) "backtest" <- function(m1,rt,orig,h,xre=NULL,fixed=NULL,inc.mean=TRUE){
# regor=c(m1$arma[1],m1$arma[6],m1$arma[2]) seaor=list(order=c(m1$arma[3],m1$arma[7],m1$arma[4]),period=m1$arma[5]) T=length(rt) if(!is.null(xre) && !is.matrix(xre))xre=as.matrix(xre) ncx=ncol(xre) if(orig > T)orig=T if(h < 1) h=1 rmse=rep(0,h) mabso=rep(0,h) nori=T-orig err=matrix(0,nori,h) jlast=T-1 for (n in orig:jlast){ jcnt=n-orig+1 x=rt[1:n] if (!is.null(xre)){ pretor=xre[1:n,] mm=arima(x,order=regor,seasonal=seaor,xreg=pretor,fixed=fixed,include.mean=inc.mean) nx=xre[(n+1):(n+h),] if(h==1)nx=matrix(nx,1,ncx) fore=predict(mm,h,newxreg=nx) } else { mm=arima(x,order=regor,seasonal=seaor,xreg=NULL,fixed=fixed,include.mean=inc.mean) fore=predict(mm,h,newxreg=NULL) } kk=min(T,(n+h))
nof=kk-n pred=fore$pred[1:nof] obsd=rt[(n+1):kk] err[jcnt,1:nof]=obsd-pred
} # for (i in 1:h){ iend=nori-i+1 tmp=err[1:iend,i] mabso[i]=sum(abs(tmp))/iend rmse[i]=sqrt(sum(tmp^2)/iend) } print("RMSE of out-of-sample forecasts") print(rmse) print("Mean absolute error of out-of-sample forecasts") print(mabso) backtest <- list(origin=orig,error=err,rmse=rmse,mabso=mabso) } M3F<-backtest(M3,adjcp,650,2,inc.mean = F)
"backtestGarch" <- function(rt,orig,h,inc.mean=TRUE,cdist="norm"){
# library(fGarch) T=length(rt) if(orig > T)orig=T if(h < 1) h=1 rmse=rep(0,h) mabso=rep(0,h) nori=T-orig err=matrix(0,nori,h) jlast=T-1 for (n in orig:jlast){ jcnt=n-orig+1 x=rt[1:n] mm=garchFit(~arma(1,0)+garch(1,1),data=x,include.mean=inc.mean,cond.dist=cdist,trace=F) fore=predict(mm,h) kk=min(T,(n+h))
nof=kk-n pred=fore$meanForecast[1:nof] obsd=rt[(n+1):kk] err[jcnt,1:nof]=obsd-pred
} # for (i in 1:h){ iend=nori-i+1 tmp=err[1:iend,i] mabso[i]=sum(abs(tmp))/iend rmse[i]=sqrt(sum(tmp^2)/iend) } print("RMSE of out-of-sample forecasts") print(rmse) print("Mean absolute error of out-of-sample forecasts") print(mabso) backtestGarch <- list(origin=orig,error=err,rmse=rmse,mabso=mabso) } M4F<-backtestGarch(adjcp,650,2,inc.mean = F,cdist = "sstd") M4F
library(fGarch) da<-read.table("w-petroprice.txt",header = T) price<-ts(da$US,frequency = 52,start = c(1997,1)) dp<-ts(diff(price),frequency = 52,start = c(1997,2)) par(mfcol=c(1,1)) plot(price,xlab="year",ylab="price") plot(dp,xlb="year",ylab="changes") cprice<-diff(price) m2<-arima(cprice,order = c(3,0,0),seasonal = list(order=c(2,0,0),period=5)) m2<-arima(cprice,order = c(3,0,0),seasonal = list(order=c(2,0,0),period=5),include.mean = F) m2 length(cprice) m2<-arima(cprice,seasonal = list(order=c(2,0,0),period=5),include.mean = F) m2 adjcp<-cprice[11:716]-0.0983cprice[6:711]-0.1152cprice[1:706] acf(adjcp) pacf(adjcp) m3<-garchFit(~arma(3,0)+garch(1,1),data = adjcp,trace = F,include.mean = F) summary(m3) plot(m3) m4<-garchFit(~arma(3,0)+garch(1,1),data = adjcp,trace = F,include.mean = F,cond.dist = "std") summary(m4) plot(m4) 1 m5<-garchFit(~arma(1,0)+garch(1,1),data = adjcp,trace = F,include.mean = F,cond.dist = "sstd") summary(m5) plot(m5) M3<-arima(adjcp,order = c(3,0,0),include.mean = F) "backtest" <- function(m1,rt,orig,h,xre=NULL,fixed=NULL,inc.mean=TRUE){
m1: is a time-series model object
orig: is the starting forecast origin
rt: the time series
xre: the independent variables
h: forecast horizon
fixed: parameter constriant
inc.mean: flag for constant term of the model.
# regor=c(m1$arma[1],m1$arma[6],m1$arma[2]) seaor=list(order=c(m1$arma[3],m1$arma[7],m1$arma[4]),period=m1$arma[5]) T=length(rt) if(!is.null(xre) && !is.matrix(xre))xre=as.matrix(xre) ncx=ncol(xre) if(orig > T)orig=T if(h < 1) h=1 rmse=rep(0,h) mabso=rep(0,h) nori=T-orig err=matrix(0,nori,h) jlast=T-1 for (n in orig:jlast){ jcnt=n-orig+1 x=rt[1:n] if (!is.null(xre)){ pretor=xre[1:n,] mm=arima(x,order=regor,seasonal=seaor,xreg=pretor,fixed=fixed,include.mean=inc.mean) nx=xre[(n+1):(n+h),] if(h==1)nx=matrix(nx,1,ncx) fore=predict(mm,h,newxreg=nx) } else { mm=arima(x,order=regor,seasonal=seaor,xreg=NULL,fixed=fixed,include.mean=inc.mean) fore=predict(mm,h,newxreg=NULL) } kk=min(T,(n+h))
nof is the effective number of forecats at the forecast origin n.
} # for (i in 1:h){ iend=nori-i+1 tmp=err[1:iend,i] mabso[i]=sum(abs(tmp))/iend rmse[i]=sqrt(sum(tmp^2)/iend) } print("RMSE of out-of-sample forecasts") print(rmse) print("Mean absolute error of out-of-sample forecasts") print(mabso) backtest <- list(origin=orig,error=err,rmse=rmse,mabso=mabso) } M3F<-backtest(M3,adjcp,650,2,inc.mean = F)
"backtestGarch" <- function(rt,orig,h,inc.mean=TRUE,cdist="norm"){
ar: AR-order, ma: MA-order
orig: is the starting forecast origin
rt: the time series
The GARCH model is GARCH(1,1).
h: forecast horizon
inc.mean: flag for constant term of the model (mean-equation).
# library(fGarch) T=length(rt) if(orig > T)orig=T if(h < 1) h=1 rmse=rep(0,h) mabso=rep(0,h) nori=T-orig err=matrix(0,nori,h) jlast=T-1 for (n in orig:jlast){ jcnt=n-orig+1 x=rt[1:n] mm=garchFit(~arma(1,0)+garch(1,1),data=x,include.mean=inc.mean,cond.dist=cdist,trace=F) fore=predict(mm,h) kk=min(T,(n+h))
nof is the effective number of forecats at the forecast origin n.
} # for (i in 1:h){ iend=nori-i+1 tmp=err[1:iend,i] mabso[i]=sum(abs(tmp))/iend rmse[i]=sqrt(sum(tmp^2)/iend) } print("RMSE of out-of-sample forecasts") print(rmse) print("Mean absolute error of out-of-sample forecasts") print(mabso) backtestGarch <- list(origin=orig,error=err,rmse=rmse,mabso=mabso) } M4F<-backtestGarch(adjcp,650,2,inc.mean = F,cdist = "sstd") M4F