Machine Learning
&
Neural Networks Blog

Predicting Future Interest Rates

In this project, we demonstrate a practical approach to forecasting interest rates using a Linear Regression model in Python. The goal is to analyze historical interest rate data, build a predictive model, and generate forecasts for future periods.

Financial analysts can use these techniques to forecast interest rates, aiding in portfolio management and strategic decision-making.
Economists and policymakers can utilize these forecasts to assess economic conditions and formulate effective monetary policies.

Load and Prepare the Data
pd.read_csv(): This function reads the CSV file into a pandas DataFrame (data). The parse_dates=['Date'] argument ensures that the 'Date' column is parsed as dates. dayfirst=True handles dates in DD/MM/YYYY format appropriately, and index_col='Date' sets the 'Date' column as the index of the DataFrame.

        
 file_path = 'interest_rates.csv'  
 data = pd.read_csv(file_path, parse_dates=['Date'], dayfirst=True, index_col='Date')
                        

Plotting Historical Data
plt.figure(figsize=(10, 6)): Creates a new figure for plotting with a size of 10 by 6.
plt.plot(): Plots the historical interest rates (data['Interest_Rate']) against dates (data.index).
label='Historical Data': Assigns a label to this plot for the legend.

        
 plt.figure(figsize=(10, 6))
 plt.plot(data.index, data['Interest_Rate'], label='Historical Data')                    
                        

Forecasting using Linear Regression Model
data['Days'] = (data.index - data.index[0]).days: Calculates the number of days from the first date in the dataset (data.index[0]). This helps in creating a simple time-based predictor for the Linear Regression model.
X = data[['Days']]: Selects the 'Days' column as the predictor variable (X).
y = data['Interest_Rate']: Selects the 'Interest_Rate' column as the target variable (y).
model = LinearRegression(): Initializes a Linear Regression model from scikit-learn.
model.fit(X, y): Fits the model using the predictors (X) and the target (y), training the model to predict interest rates based on the number of days.

        
 data['Days'] = (data.index - data.index[0]).days
 X = data[['Days']]
 y = data['Interest_Rate']
                            
 model = LinearRegression()
 model.fit(X, y)
                        

Generating and Plotting Forecasts
future_steps = 180: Specifies the number of days to forecast.
future_index = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=future_steps, freq='D'): Generates a new date range (future_index) starting from the day after the last date in data, spanning future_steps days with a frequency of daily ('D').
future_days = (future_index - data.index[0]).days: Calculates the number of days from the first date in the dataset for the future dates.
future_days.values.reshape(-1, 1): Reshapes future_days into a 2D array for prediction by the Linear Regression model.
forecast = model.predict(future_days): Predicts future interest rates (forecast) using the trained Linear Regression model.
forecast_data = pd.DataFrame(forecast, index=future_index, columns=['Forecasted Interest Rate']): Creates a DataFrame (forecast_data) to store the forecasted interest rates with dates as index and a column name for clarity.
plt.plot(forecast_data.index, forecast_data['Forecasted Interest Rate'], label='Forecast', linestyle='--', color='orange'): Plots the forecasted interest rates against dates (forecast_data.index).

        
 future_steps = 180  
 future_index = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=future_steps, freq='D')
 future_days = (future_index - data.index[0]).days
 future_days = future_days.values.reshape(-1, 1)  
 forecast = model.predict(future_days)
                            
 forecast_data = pd.DataFrame(forecast, index=future_index, columns=['Forecasted Interest Rate'])
                            
 plt.plot(forecast_data.index, forecast_data['Forecasted Interest Rate'], label='Forecast', linestyle='--', color='orange')                   
                        

Finalizing the Plot
plt.title('Interest Rate Forecast for the Next 2 Months'): Sets the title of the plot.
plt.xlabel('Date'): Labels the x-axis as 'Date'.
plt.ylabel('Interest Rate'): Labels the y-axis as 'Interest Rate'.
plt.legend(): Displays the legend to distinguish between 'Historical Data' and 'Forecast'.
plt.tight_layout(): Adjusts subplot parameters to give a cleaner appearance.
plt.show(): Displays the plot.

                           
 plt.title('Interest Rate Forecast for the Next Months')
 plt.xlabel('Date')
 plt.ylabel('Interest Rate')
 plt.legend()
 plt.tight_layout()
 plt.show()
                        


interest


Below is the full code with additional comments embedded.

        
 import pandas as pd
 import numpy as np
 import matplotlib.pyplot as plt
 from sklearn.linear_model import LinearRegression
        
 # Load the dataset
 file_path = 'interest_rates.csv'  
        
 # Read the data
 data = pd.read_csv(file_path, parse_dates=['Date'], dayfirst=True, index_col='Date')
        
 # Ensure all values are numeric and handle non-numeric values
 data['Interest_Rate'] = pd.to_numeric(data['Interest_Rate'], errors='coerce')
        
 # Drop rows with any NaN values
 data.dropna(inplace=True)
        
 # Ensure the data is sorted by date
 data.sort_index(inplace=True)
        
 # Plot the historical data
 plt.figure(figsize=(10, 6))
 plt.plot(data.index, data['Interest_Rate'], label='Historical Data')
        
 # Prepare the data for forecasting
 data['Days'] = (data.index - data.index[0]).days
 X = data[['Days']]
 y = data['Interest_Rate']
        
 # Fit a linear regression model
 model = LinearRegression()
 model.fit(X, y)
        
 # Forecast future interest rates
 future_steps = 180  # Number of days to forecast 
 future_index = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=future_steps, freq='D')
 future_days = (future_index - data.index[0]).days
 future_days = future_days.values.reshape(-1, 1)  
 forecast = model.predict(future_days)
        
 # Create a DataFrame for the forecasted data
 forecast_data = pd.DataFrame(forecast, index=future_index, columns=['Forecasted Interest Rate'])
        
 # Plot the forecasted data
 plt.plot(forecast_data.index, forecast_data['Forecasted Interest Rate'], label='Forecast', linestyle='--', color='orange')
        
 # Plot labels and legend
 plt.title('Interest Rate Forecast for the Next Months')
 plt.xlabel('Date')
 plt.ylabel('Interest Rate')
 plt.legend()
 plt.tight_layout()
 plt.show()
                        


Get the Jupyter Notebook and the dataset used in this project.

If you found this project interesting, you can share a coffee with me, by accessing the below link.

Boost Your Brand's Visibility

Partner with us to boost your brand's visibility and connect with our community of tech enthusiasts and professionals. Our platform offers great opportunities for engagement and brand recognition.

Interested in advertising on our website? Reach out to us at office@ml-nn.eu.