Forecasting Commodity Prices with Alternative Time Series Methods Using Python

Forecasting Commodity Prices with Alternative Time Series Methods Using Python

Forecasting commodity prices is a critical task for procurement team reliant on raw. Commodity prices can be highly volatile due to supply-demand imbalances, seasonal trends, and geopolitical influences. By employing time series forecasting techniques, stakeholders can anticipate price changes, manage risks, and improve budgeting and planning. In Python, Exponential Smoothing and SARIMA (Seasonal ARIMA) are two popular time series models used for forecasting, each with unique strengths.

Key Concepts in Forecasting Commodity Prices

  1. Commodity Price Forecasting: Commodities like oil, metals, and agricultural products follow distinct trends and seasonal cycles that influence their prices. Forecasting these prices enables industries to make informed strategic decisions about inventory, procurement, and investments (Hyndman & Athanasopoulos, 2018).
  2. Time Series Forecasting Models: Time series models analyze historical data to identify patterns and relationships, allowing for the prediction of future values.
  3. Python for Forecasting: Python is an excellent tool for time series forecasting, thanks to its powerful data manipulation and modeling libraries. Libraries like pandas and statsmodels streamline data cleaning, model development, and visualization. Coding in Python allows analysts to efficiently handle large datasets and automate the forecasting process.

Applications and Benefits

Commodity price forecasting aids businesses in decision-making areas like procurement. By applying alternative time series methods in Python, analysts can compare model accuracy, identify the best-fitting approach, and produce reliable predictions to guide business strategies. This method also enables industries to adapt to market fluctuations and prepare for potential price changes.

This Python code performs time series forecasting for multiple commodity prices using two forecasting methods: Exponential Smoothing and SARIMA (Seasonal ARIMA). Here’s a breakdown of what each section of the code does:

1. Import Required Libraries

  • pandas is used for data manipulation.
  • statsmodels provides forecasting methods (ExponentialSmoothing and SARIMAX).
  • matplotlib.pyplot is used to plot the historical and forecasted data.

2. Load the Data File

  • The code loads a CSV file containing monthly commodity prices. The file path must be updated to the correct location. Example Data File: World Bank

3. Data Cleaning

  • Clean Column Names: Removes extra spaces from column names.
  • Convert Data Types: Ensures numeric columns are in the proper float format by:Removing non-numeric characters.Replacing commas with dots for decimal points.
  • Convert Date Column: Converts the “Period” column into a datetime format for time series compatibility.

4. Define Commodities for Forecasting

  • A list of specific commodities is selected for analysis (e.g., "Crude oil Brent," "Natural gas Europe").

5. Forecast Parameters

  • Forecast Steps: Sets a forecast period of 12 months.
  • Result Storage: Creates dictionaries to store forecast results from each method.

6. Apply Forecasting Models (Exponential Smoothing and SARIMA)

  • Loop through Each Commodity:Prepare Data: Extracts time series data by setting “Period” as the index and removing missing values. Exponential Smoothing: Uses Holt-Winters Exponential Smoothing with additive seasonality to forecast. Stores the forecasted values for the next 12 months. SARIMA: Sets SARIMA parameters to capture seasonality, trend, and noise in the data.Stores forecasted values for the next 12 months.If either method encounters an issue, it prints an error message.

7. Plot the Forecasts

  • For each commodity, the code: Plots historical data in black. Plots Exponential Smoothing forecasts as a blue dashed line with circular markers. Plots SARIMA forecasts as a red solid line with x markers. Adds a title, labels, and a legend to each plot for clarity.

Summary

This code loads, cleans, and processes commodity price data, then forecasts the next 12 months of prices for selected commodities using two methods. The code concludes by visually comparing these forecasts to the historical data, allowing users to assess trends and seasonal patterns in each commodity's price.

PYTHON CODE:

# Required imports
import pandas as pd
import statsmodels.api as sm
from statsmodels.tsa.holtwinters import ExponentialSmoothing
import matplotlib.pyplot as plt

# Load the data file
file_path = '/Users/bovacik/Downloads/Monthly_Comoddity_Prices.csv'  # Update with the correct path
monthly_prices_df = pd.read_csv(file_path, delimiter=";", engine='python')

# Step 1: Clean the column names by stripping extra spaces and formatting issues
monthly_prices_df.columns = monthly_prices_df.columns.str.strip()

# Step 2: Convert numeric values to proper float format
for col in monthly_prices_df.columns[1:]:
    if monthly_prices_df[col].dtype == 'object':
        # Remove any non-numeric characters except comma and dot
        monthly_prices_df[col] = monthly_prices_df[col].replace({r'[^\d,]': ''}, regex=True)
        # Replace commas with dots and convert to float
        monthly_prices_df[col] = monthly_prices_df[col].str.replace(",", ".").astype(float)

# Step 3: Convert 'Period' to datetime format
monthly_prices_df['Period'] = pd.to_datetime(monthly_prices_df['Period'], format='%b.%y', errors='coerce')

# Step 4: Define the selected commodities for forecasting
selected_commodities = [
    "Crude oil Brent", "Natural gas Europe", "Cocoa", 
    "Aluminum", "Copper", "Lead", "Nickel", "Zinc", "Platinum"
]

# Step 5: Set up forecast parameters and dictionaries to store results
forecast_steps = 12
forecasts_exp_smoothing = {}
forecasts_sarima = {}

# Step 6: Forecasting with Exponential Smoothing and SARIMA for each commodity
for col in selected_commodities:
    try:
        # Prepare the time series data
        commodity_df = monthly_prices_df[['Period', col]].dropna()
        commodity_series = commodity_df.set_index('Period')[col]
        
        # Exponential Smoothing
        model_exp_smoothing = ExponentialSmoothing(commodity_series, seasonal='add', seasonal_periods=12)
        fit_exp_smoothing = model_exp_smoothing.fit()
        forecast_exp_smoothing = fit_exp_smoothing.forecast(steps=forecast_steps)
        forecasts_exp_smoothing[col] = forecast_exp_smoothing
        
        # SARIMA (Seasonal ARIMA)
        model_sarima = sm.tsa.SARIMAX(commodity_series, order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
        fit_sarima = model_sarima.fit(disp=False)
        forecast_sarima = fit_sarima.forecast(steps=forecast_steps)
        forecasts_sarima[col] = forecast_sarima
    
    except Exception as e:
        print(f"Forecasting failed for {col}: {e}")

# Step 7: Plot the forecasts
for col in selected_commodities:
    if col in forecasts_exp_smoothing and col in forecasts_sarima:
        plt.figure(figsize=(12, 6))
        
        # Plot historical data
        commodity_df = monthly_prices_df[['Period', col]].dropna()
        plt.plot(commodity_df['Period'], commodity_df[col], label='Historical Data', color='black', linewidth=2)
        
        # Plot Exponential Smoothing forecast
        forecast_dates = pd.date_range(commodity_df['Period'].iloc[-1], periods=forecast_steps+1, freq='M')[1:]
        plt.plot(forecast_dates, forecasts_exp_smoothing[col], color='blue', linestyle='--', marker='o', label='Exp. Smoothing Forecast')
        
        # Plot SARIMA forecast
        plt.plot(forecast_dates, forecasts_sarima[col], color='red', linestyle='-', marker='x', label='SARIMA Forecast')
        
        # Add plot details
        plt.title(f'{col} - 12-Month Price Forecast')
        plt.xlabel('Date')
        plt.ylabel('Price')
        plt.legend(loc='upper left')
        plt.grid(True)
        plt.tight_layout()
        plt.show()        



Platinum - 12 Month Price Forecast


Zinc - 12 Month Price Forecast



Nickel - 12 Month Price Forecast


Lead - 12 Month Price Forecast


Copper - 12 Month Price Forecast


Aluminium - 12 Month Price Forecast


Natural Gas Europe - 12 Month Price Forecast


Crude Oil Brent - 12 Month Price Forecast

References

  • Box, G. E., Jenkins, G. M., Reinsel, G. C., & Ljung, G. M. (2015). Time Series Analysis: Forecasting and Control. John Wiley & Sons.
  • Gardner, E. S. (1985). Exponential smoothing: The state of the art. Journal of Forecasting, 4(1), 1-28.
  • Gardner, E. S. (2006). Exponential smoothing: The state of the art–Part II. International Journal of Forecasting, 22(4), 637-666.
  • Holt, C. C. (2004). Forecasting seasonals and trends by exponentially weighted moving averages. International Journal of Forecasting, 20(1), 5-10.
  • Hyndman, R. J., & Athanasopoulos, G. (2018). Forecasting: Principles and Practice. OTexts.
  • Makridakis, S., Wheelwright, S. C., & Hyndman, R. J. (1998). Forecasting Methods and Applications. John Wiley & Sons.
  • Winters, P. R. (1960). Forecasting sales by exponentially weighted moving averages. Management Science, 6(3), 324-342.

要查看或添加评论,请登录

BURAK OVACIK的更多文章