Forecasting Exchange Rates Using R Time Series

Time Series is the historical representation of data points collected at periodic intervals of time. Statistical tools like R use forecasting models to analyse historical time series data to predict future values with reasonable accuracy. In this post I will be using R time series to forecast the exchange rate of Australian dollar using daily closing rate of the dollar collected over a period of two years.

The data for this post is sourced from AdventureWorksDW which is a SQL Server database that’s available for download from CodePlex. Within AdventureWorksDW database, the table FactCurrencyRate contains daily closing rate (EndOfDayRate) of different currencies (CurrencyKey) pegged against the US dollar.

Screen Capture 1 - FactCurrencyRate Dataset
Screen Capture 1 – FactCurrencyRate Dataset

R Time Series

Data from SQL Server is first loaded into an R dataset using functions from RODBC package.

library(RODBC)
aw <- odbcDriverConnect(connection="Driver={SQL Server Native Client 11.0};server=localhost;database=AdventureWorksDW2014;trusted_connection=yes;")
aud <- sqlQuery(aw,'select EndOfDayRate from [dbo].[FactCurrencyRate] where CurrencyKey=6')
View(aud)
odbcClose(aw)

The above code above does these

  • Loads the RODBC package using library() function
  • Establishes connection to SQL Server database using odbcDriverConnect() function.
  • Fetches data from FactCurrenyRate for AUD (CurrencyKey=6) into a dataset using sqlQuery() function
  • Displays data using View() function (Screen Capture 2)
  • Screen Capture 2 - Output of View(aud)
    Screen Capture 2 – Output of View(aud)
  • And finally closes the database connection using odbcClose() function
  • Next, the ts() function is called to plot time series.
    tsAud <- ts(aud,frequency=7)
    plot.ts(tsAud)
    

    The time series output is shown below (Screen Capture 3)

    Screen Capture 3 - R Time Series of AUD
    Screen Capture 3 – R Time Series of AUD

Removing Seasonal Variations

It is important to know the extent of exchange rate fluctuation that is attributed to specific times (or seasons) of the year. Seasonality in time series can impact the forecasted value and it is usually a good practise to eliminate such variations from time series. To eliminate seasonal variations, the time series has to be deconstructed to its components using decompose () function. The decompose() function splits the time series into $seasonal and $trend components.

tsAudComp <- decompose(tsAud)
dev.new()
plot.ts(tsAudComp$seasonal)
dev.new()
plot.ts(tsAudComp$trend)  

Now the seasonal variation can be removed from the time series

tsAudSeasonalAdjusted <- tsAud -tsAudComp$seasonal
dev.new()
plot.ts(tsAudSeasonalAdjusted )

The resulting time series will look like Screen Capture 4.

Screen Capture 4 - R Time Series for AUD adjusted for seasonal variations
Screen Capture 4 – R Time Series for AUD adjusted for seasonal variations

It is worth noting that the adjusted time series does not show much deviation from before. This indicates that the seasonal variation in this instance is minimal.

Forecasting

So far the historical data has been plotted as time series and adjusted for seasonal variations. Now we move on to forecasting. The fluctuations in AUD time series plot is fairly consistent over time and therefore forecasts can be made using simple exponential smoothing. HoltWinters() function in R is used for simple exponential smoothing

audHW <-HoltWinters(tsAudSeasonalAdjusted ,beta=FALSE,gamma=FALSE)
plot(audHW)

The parameter beta=FALSE sets the filter to exponential smoothing and the parameter gamma=FALSE indicates a non-seasonal model. The resulting plot is shown in Screen Capture 5

Screen Capture 5 - Holt Winters Exponential Smoothing
Screen Capture 5 – Holt Winters Exponential Smoothing

With the exponential smoothing done, the dataset is prepared and ready for forecast. The forecast.HoltWinters() function in forecast package does the forecasting.

audHWforecast <- forecast.HoltWinters(audHW,h=30)
plot.forecast(audHWforecast)

The parameter h=30 indicates forecasting for the next 30 intervals which is next 30 days in this instance. The output of forecast.HoltWinters is shown below (Screen Capture 6)

Screen Capture 6 - Holt Winters Forecast AUD Exchange Rate
Screen Capture 6 – Holt Winters Forecast AUD Exchange Rate

Interpretation of Forecast Model

The dark grey shaded area in Screen Capture 6 represents the range of AUD exchange rate for next 30 days with 80% prediction. In this case it’s in the range of 0.525 to 0.57 USD. The lighter shade of grey represents the AUD exchange rate with 90% prediction which is in the range of 0.52 to 0.585

Validating the Forecast by Analysing Prediction Errors

Any forecast model is subject to certain level of prediction errors. So it is important to validate the forecast model by studying the distribution of forecast errors. The Holt Winters forecast model creates a dataset called $residuals which is forecast error dataset. This dataset has the sum of squared values between expected and observed value for each data point. A plot of errors should produce a normal distribution or bell curve with mean of zero. If the plot of errors does not produce a bell curve, it indicates the forecast errors are too high and the forecast model is not recommended.

The following lines of code plots a histogram and a normal distribution curve for the forecast errors

hist(audHWforecast$residuals, col="yellow")
dnormResiduals <- dnorm(audHWforecast$residuals,mean=mean(audHWforecast$residuals), sd=sd(audHWforecast$residuals) )
dev.new()
plot(audHWforecast$residuals,dnormResiduals,col="blue")
Screen Capture 7- Plot of Forecast Errors
Screen Capture 7- Plot of Forecast Errors

The plots in Screen Capture 7 shows the forecast errors are normally distributed with a mean close to 0. This validates that the forecast model is a correct fit and the predicted values are reasonably accurate.

Download Source Code

The code used in this post is available for download from here http://1drv.ms/1D909Yt

4 thoughts on “Forecasting Exchange Rates Using R Time Series

    1. Albert, sorry for the late reply. The onedrive folder is open to public, I haven’t put any restriction. However I have had feedback that some networks block storage drives and that might be the case in your instance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s