SARIMA Model for Forecasting Currency Exchange Rates

Ata Amrullah Last Updated : 20 Jun, 2023
7 min read

Introduction

Forecasting currency exchange rates is the practice of anticipating future changes in the value of one currency about another. Currency forecasting may assist people, corporations, and financial organizations make educated financial decisions. One of the forecasting techniques that can be used is SARIMA. SARIMA is an excellent time series forecasting technique for estimating time series data with seasonal patterns.

It works by modeling the link between past and current values of a time series and recognizing patterns in the data. SARIMA utilizes a variety of auto-regression (AR) and moving average (MA) models, as well as differencing, to capture trends and seasonality in data. “seasonality” refers to data variations that occur regularly and predictably throughout a specified period, such as daily, weekly, or annual cycles. We can be better informed about changes in currency values by anticipating exchange rates. Now, let’s make the forecasting through the steps in the article.

Learning Objectives

  1. To help individuals, businesses, and financial institutions anticipate market trends by identifying patterns and trends in historical data.
  2. To reduce risk by identifying potential risks associated with currency fluctuations.
  3. To optimize currency conversions by identifying the best time to convert currencies.
  4. Improve decision-making by providing businesses and individuals with information about the future direction of currency exchange rates.

Based on these objectives, we will use SARIMA to develop a model to estimate currency exchange rates by aggregating seasonal data patterns to make more accurate predictions of future values.

This article was published as a part of the Data Science Blogathon.

Step 1: Import Library

!pip install pmdarima
from pmdarima.arima import auto_arima
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.seasonal import seasonal_decompose
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
import plotly.graph_objs as go
import plotly.io as pio

We must install the ‘pmdarima’ library to use the auto_arima function. This function fits an ARIMA model with time series data and automatically determines the appropriate model parameters based on the data provided.

Step 2: Read the Data

We need historical data about exchange rates between two currencies to forecast exchange rates. Thus, we can download historical data containing weekly exchange rates between INR and USD at yahoo finance web. And we can utilize a period of December 1, 2003, to June 15, 2023. Fortunately, I’ve made it public on GitHub.

alamat = 'https://raw.githubusercontent.com/ataislucky/Data-Science/main/dataset/USD-INR_Weekly.csv'
data = pd.read_csv(alamat)
print(data.sample(11))
 Data Overview
Data Overview

Let’s check whether the dataset includes any missing values before proceeding further. It is essential to verify.

print(data.isna().sum())
 Missing Value
Missing Value

Several missing values in the dataset have been identified. As a result, we must eliminate them.

data = data.dropna()

Examine descriptive statistics to acquire a better understanding of the data set and the factors that underpin it. We may get essential insights into data set characteristics, spot potential outliers, grasp data distribution elements, and establish the framework for future exploratory data analysis and modeling efforts. Let’s look at the descriptive statistics for this data collection.

print(data.describe())
 Data Descriptive Statistics
Data Descriptive Statistics

The dataset contains the value of INR for 1 USD for a given time. Below are all the features in the data:

  1. The date represents the specific day of the exchange rate data.
  2. Open refers to the exchange rate at the start of a specific trading period, such as the opening price for the day or the week.
  3. High represents the highest exchange rate observed during a specific trading period.
  4. Low represents the lowest exchange rate observed during a specific trading period.
  5. Close shows the currency rate at the end of a certain trading period.
  6. Adjusted Closing considers any business activities that may impact the Closing Price, such as stock splits or dividends.
  7. Volume refers to the number of USD-INR currency pairs traded during a specific period.

Step 3: Conversion Rate Analysis

Let’s analyze the conversion rates between the two currencies over the years. By examining historical trends, we can gain valuable insights into exchange rate dynamics and potentially uncover important patterns or events that affect these exchange rates. To visualize this analysis, we will use a line chart to illustrate the trend of the USD-INR conversion rate over time.

figure = px.line(data, x="Date",
                 y="Close",
                 title='Conversion Rate over the years (USD/INR)')
figure.show()
 Annual Conversion Rate | SARIMA forecasting technique
Annual Conversion Rate

Let’s add year and month fields to the data so we can enable deeper temporal analysis.

data["Date"] = pd.to_datetime(data["Date"], format = '%Y-%m-%d')
data['Year'] = data['Date'].dt.year
data["Month"] = data["Date"].dt.month
print(data.head())
 Adding New Column
Adding New Column

Let’s examine the compounded yearly increase of the INR-USD exchange rate to discover times of economic strength or weakness, important events impacting currency rates, or long-term patterns in INR-USD conversion rates.

growth = data.groupby('Year').agg({'Close': lambda x: (x.iloc[-1]-x.iloc[0])/x.iloc[0]*100})

fig = go.Figure()
fig.add_trace(go.Bar(x=growth.index,
                     y=growth['Close'],
                     name='Yearly Growth'))

fig.update_layout(title="Yearly Growth of Conversion Rate (USD/INR)",
                  xaxis_title="Year",
                  yaxis_title="Growth (%)",
                  width=900,
                  height=600)

pio.show(fig)
 Conversion Rate Annual Growth | SARIMA model
Conversion Rate Annual Growth

Now let’s break it down again by looking at the combined monthly conversion rate growth between INR and USD.

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
# Calculate monthly growth
data['Growth'] = data.groupby(['Year', 'Month'])['Close'].
transform(lambda x: (x.iloc[-1] - x.iloc[0]) / x.iloc[0] * 100)

# Group data by Month and calculate average growth
grouped_data = data.groupby('Month').mean().reset_index()

fig = go.Figure()

fig.add_trace(go.Bar(
    x=grouped_data['Month'],
    y=grouped_data['Growth'],
    marker_color=grouped_data['Growth'],
    hovertemplate='Month: %{x}<br>Average Growth: %{y:.2f}%<extra></extra>'
))

fig.update_layout(
    title="Aggregated Monthly Growth of Conversion Rate (USD/INR)",
    xaxis_title="Month",
    yaxis_title="Average Growth (%)",
    width=900,
    height=600
)

pio.show(fig)
 Monthly Growth of the Conversion Rate | SARIMA model
Monthly Growth of the Conversion Rate

The graph illustrates that the USD value has consistently decreased in January and March. This observation shows that the INR tends to strengthen against the USD during these months, reducing the conversion rate. Meanwhile, in the second quarter, the USD boosted against the INR every year. The USD value against INR peaked in August but fell in September, rose annually in the fourth quarter, and fell again in December.

Step 4: Build a SARIMA Model and Make a Forecasting

We must perform a seasonal decomposition of the USD – INR exchange rate data. This method separates the different data components: trends, seasonality, and residual or random fluctuations.

result = seasonal_decompose(data["Close"], model='multiplicative', period=24)
fig = plt.figure()
fig = result.plot()
fig.set_size_inches(8, 6)
fig.show()
 Seasonal Decomposition | SARIMA forecasting technique
Seasonal Decomposition

We can see that there is a seasonal pattern to this data. So, we use SARIMA as the most appropriate algorithm for this data. Before using SARIMA, we need to find the p, d, and q values first. We can use the ‘pmdarima’ library to find those values automatically.

model = auto_arima(data['Close'], seasonal=True, m=52, suppress_warnings=True)
print(model.order)
 p,d,q value
p,d,q value

The parameter seasonal=True determines that the time series shows a seasonal pattern. Meanwhile, the parameter m=52 shows the seasonal periodicity of weekly data. And 2, 1, 0 is the p, d, q value.

We are ready to train our model using SARIMA to estimate currency exchange rates.

from statsmodels.tools.sm_exceptions import  ValueWarning
warnings.simplefilter('ignore', ValueWarning)

p, d, q = 2, 1, 0
model = SARIMAX(data["Close"], order=(p, d, q),
                seasonal_order=(p, d, q, 52))
fitted = model.fit()
print(fitted.summary())
 SARIMAX model summary
SARIMAX model summary

We now predict future currency exchange rates from the fitted ARIMA model.

predictions = fitted.predict(len(data), len(data)+90)
print(predictions)
 Prediction Values | SARIMA forecasting technique
Prediction Values

We display the prediction value on the graph to make it more engaging.

fig = go.Figure()

# Add training data line plot
fig.add_trace(go.Scatter(
    x=data.index,
    y=data['Close'],
    mode='lines',
    name='Training Data',
    line=dict(color='blue')
))

# Add predictions line plot
fig.add_trace(go.Scatter(
    x=predictions.index,
    y=predictions,
    mode='lines',
    name='Predictions',
    line=dict(color='red')
))

fig.update_layout(
    title="Training Data VS Predictions",
    xaxis_title="Date",
    yaxis_title="Close",
    legend_title="Data",
    width=1000,
    height=600
)

pio.show(fig)
 Graphic Prediction Result | SARIMA model | SARIMA forecasting technique
Graphic Prediction Result

Conclusion

This article starts by checking if there are missing values in the dataset and analyzing the data with descriptive statistics. Then explore the conversion rate between the two currencies aggregated annually and monthly before forecasting the currency exchange rate using the SARIMA. We have discussed the following:

  • The SARIMA model is a statistical model that captures seasonal trends in the past values of the data to predict future discounts.
  • The SARIMA model can forecast currency exchange rates for various currencies.
  • The SARIMA model helps make informed decisions related to currency trading, financial planning, or international business operations.
  • The accuracy of the model depends on several factors, including the data quality and the currency market’s stability.

This article provides a comprehensive guide to currency exchange rate forecasting with SARIMA using Python.

Frequently Asked Questions

Q1. What is the forecasting currency exchange rate?

A. Forecasting currency exchange rates is predicting changes in the value of one currency compared to another at a particular time. Generally influenced by various factors, including economic figures, political events, market mood, and technical analysis.

Q2. What is SARIMA Technique?

A. SARIMA (Seasonal Autoregressive Integrated Moving Average) is a statistical technique used for forecasting time series data, a series of observations recorded at regular intervals over time. SARIMA models are a combination of autoregressive (AR) models, moving average (MA) models, and differencing.

Q3. What’s the distinction between ARIMA and SARIMA?

A. ARIMA and SARIMA are statistical models that forecast time series data. They are both based on the autoregressive integrated moving average (ARIMA) model, but SARIMA includes a seasonality element.

Q4. What are the SARIMA model’s benefits?

A. 1. Models: SARIMA models can accurately forecast time series data.
2. Adaptability: SARIMA models can anticipate a wide range of time series data, including trends, seasonality, and noise.
3. Robustness: SARIMA models are relatively resistant to data changes.

Q5. What are the risks associated with using the SARIMA model?

A. 1. Overfitting happens when a model fits the data too closely and does not generalize well to new data. SARIMA models are susceptible to overfitting.
2. Complexity: SARIMA models can be challenging to comprehend and apply.
3. Computational Cost: Training and forecasting SARIMA models can be computationally expensive.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Let me introduce you to someone who is passionate about learning and teaching Python programming, data science, and the Internet of Things (IoT) - that's me! My name is Ata Amrullah.

I am a self-taught programmer who enjoys sharing knowledge with others. I believe that everyone can learn to code and become a successful developer, and that's why I started my journey as a Python tutorial maker. I have created a variety of tutorials on different topics such as data analysis, web scraping, machine learning, and IoT, which can be found on my YouTube channel: https://www.youtube.com/@PythonProjecTutorial.

Apart from creating tutorials, I also enjoy working on personal projects that help me enhance my skills and explore new technologies. One of my recent projects involved building an IoT-based healthcare system that uses a Raspberry Pi as communication protocol gateway to control vary protocols.

I am a firm believer in the power of technology to change people's lives for the better, and that's what motivates me to keep learning and sharing my knowledge with others. Whether you are a beginner or an experienced programmer, I am always happy to help and answer any questions you may have about Python programming, data science, or IoT.

Responses From Readers

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details