Exchange Rate Forecasting Example

The time series models allow for the inclusion of information from the past observations of a series, but not for the inclusion of other information that may be relevant.
For example, the effects of holidays, competitor activity, changes in the law, the wider economy, or some other external variables may explain some of the historical variation and allow more accurate forecasts.

As an example I will show the exchange rate forecasting model which uses the best ARIMA model according to AICc value with additional  two external regressors (predictor variables ): EUR/USD ratio and quarterly rate of GDP (http://www.dzs.hr/default_e.htm). The exchange rate data is available from http://www.hnb.hr/tecajn/etecajn.htm.

Forecasts are, of course, only possible if we have future values of changes in those external regressors.
xreg should contain the exogenous variables for the training set (mat), and matnew should contain those variables for the prediction set. If the matnew contains variables you do not know the future values of, you will either need to forecast these variables or exclude them from the model.

The AIC can be calculated for the final model, and this value can be used to determine the best predictors. That is, the procedure should be repeated for all subsets of predictors to be considered, and the model with the lowest AICc value selected. For example we can add Consumer Price Indexand remove EUR/USD ratio.

The R code consists of two parts – loading data and forecasting:

library(“xts”)
library(“forecast”)

#These cause R to use the Internet Explorer internals
setInternet2(TRUE)

from=as.Date(“1.1.2009″,”%d.%m.%Y”)
to=as.Date(“19.4.2013″,”%d.%m.%Y”)
sequence=seq(from,to,by=”day”)
tbl <- data.frame(opis=character(),kupovni=numeric(),srednji=numeric(),prodajni=numeric())

collect<-function(tbl,sequence) {
dates=format(sequence[((as.numeric(sequence)-2) %% 7) > 1],format=”%d%m%y”)
for(name in dates)
{ url=paste(“
http://www.hnb.hr/tecajn/f,name,”.dat”,sep=””)
      tryCatch( { pom <- read.table(url,dec=”,”,col.names=c(“opis”,”kupovni”,”srednji”,”prodajni”),
colClasses=c(“character”,”numeric”,”numeric”,”numeric”),skip=1,strip.white=TRUE)
pom$date=as.Date(name,”%d%m%y”)
tbl <- rbind(tbl,pom[pom$opis %in% c(“756CHF001″,”840USD001″,”978EUR001”),])
},error = function(e) print(e))
}
return(tbl)
}

tbl<-collect(tbl,sequence)
save(tbl, file=”c:/Temp/tbl2009-2013.Rdata”)

———

load(“c:/Temp/tbl2009-2013.Rdata”)
sequence=seq(from=as.Date(“19.4.2013″,”%d.%m.%Y”),to=as.Date(“19.4.2013″,”%d.%m.%Y”), by=”day”)
tbl<-collect(tbl,sequence)

chf=tbl[tbl$opis==”756CHF001″,]
xts=xts(chf$srednji,order.by=chf$date)
eur=tbl[tbl$opis==”978EUR001″,c(“srednji”)]
usd=tbl[tbl$opis==”840USD001″,c(“srednji”)]

bdp=c(-8.3,-7.6,-7.2,-4.7,
-3.1,-3.9,-1.0,-1.2,
-1.4,0.6,0.8,-0.3,
-1.1,-2.5,-1.9,-2.3,
-0.4,-0.4)

sequence=seq(from,to,by=”day”)
quarters=seq(from,to,by=”3 months”)

dframe<-data.frame(list(time=sequence))
qframe<-data.frame(list(time=quarters))
qframe[“bdp”]<-bdp
frame=merge(dframe,qframe,all=T)
frame$bdp<-na.locf(frame$bdp)

xts1=xts(frame$bdp,order.by=frame$time)
xts2=merge(xts,xts1, join=’left’)

mat<-matrix(c(as.vector(xts2[,2]),eur,usd),ncol=3)
matnew=matrix(rep(mat[nrow(mat),], each=10),ncol=3)

fit <- auto.arima(ts(xts),xreg=mat)
forecast=forecast(fit,xreg=matnew)
summary(forecast)

Ovaj unos je objavljen u Nekategorizirano. Bookmarkirajte stalnu vezu.

2 odgovora na Exchange Rate Forecasting Example

  1. Povratni ping: R Predictive Analytics Recap | Tmilinovic's Blog

  2. Povratni ping: Selection of blogs | Tmilinovic's Blog

Komentiraj

Ova web-stranica koristi Akismet za zaštitu protiv spama. Saznajte kako se obrađuju podaci komentara.