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.
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.
!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.
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))
Let’s check whether the dataset includes any missing values before proceeding further. It is essential to verify.
print(data.isna().sum())
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())
The dataset contains the value of INR for 1 USD for a given time. Below are all the features in the data:
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()
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())
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)
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)
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.
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()
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)
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())
We now predict future currency exchange rates from the fitted ARIMA model.
predictions = fitted.predict(len(data), len(data)+90)
print(predictions)
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)
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:
This article provides a comprehensive guide to currency exchange rate forecasting with SARIMA using Python.
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.
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.
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.
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.
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.