N-BEATS for time series forecasting in a simple way
Imagine you have a list of numbers showing how many ice creams were sold each month at your local shop for the past two years. Now, if you want to guess how many ice creams will be sold in the next year, you could use N-BEATS to help you predict these numbers.
Here’s how N-BEATS works, in easy steps:
- Starting Data: You begin with the data you know — the ice cream sales from the past 24 months.
- Breaking It Down: N-BEATS uses what we call “blocks” to analyze your data. Think of each block as a mini detective, where each one is really good at figuring out specific patterns:
- One detective might be great at noticing overall trends, like if sales are generally going up or down over the years.
- Another might specialize in spotting seasonal trends, such as higher sales in summer.
3. Making Predictions: Each of these detectives (blocks) makes a guess about future sales based on what they’re best at. For example, the trend detective might predict increasing sales, while the seasonal detective expects a spike every summer.
4. Making Predictions: Each of these detectives (blocks) makes a guess about future sales based on what they’re best at. For example, the trend detective might predict increasing sales, while the seasonal detective expects a spike every summer.
5. Combining Clues: In the end, N-BEATS combines all the clues from these detectives to make a final prediction about the ice cream sales for the next 12 months.
6. Result: What you get is a forecast, a kind of informed guess, about how many ice creams will be sold in the future months.
Let’s go through a simple example of using N-BEATS for forecasting using Python. We’ll forecast fictional monthly ice cream sales data. For this, we’ll use a Python library specifically designed for forecasting, such as darts
which includes an implementation of N-BEATS.
Step-by-Step Guide with Code
- Installation: First, we need to install the
darts
library if it's not already installed. You can do this using pip:
pip install darts
2. Prepare the Data: We’ll create some fictional monthly ice cream sales data.
3. Load and Train the Model: We’ll load the N-BEATS model from darts
and train it on our data.
4. Forecasting: Finally, we’ll make a forecast for the next 12 months.
Here’s how the code looks:
from darts import TimeSeries
from darts.models import NBEATSModel
import pandas as pd
import numpy as np
# Create some fictional monthly sales data
data = {
"Month": pd.date_range(start="2010-01-01", periods=120, freq='M'),
"Sales": np.random.poisson(lam=200, size=120) + np.linspace(50, 300, 120) # Increasing trend
}
df = pd.DataFrame(data)
# Convert the DataFrame into a TimeSeries object for darts
series = TimeSeries.from_dataframe(df, 'Month', 'Sales')
# Initialize and train the N-BEATS model
model = NBEATSModel(input_chunk_length=24, output_chunk_length=12)
model.fit(series)
# Perform forecasting
forecast = model.predict(n=12)
# Plotting the results
import matplotlib.pyplot as plt
series.plot(label='Actual Sales')
forecast.plot(label='Forecasted Sales', color='red')
plt.title('Ice Cream Sales Forecast')
plt.legend()
plt.show()
Explanation
- Data Preparation: We’re creating a simple dataset with a linearly increasing trend in sales over 120 months.
- Model Setup: The
NBEATSModel
is configured to look at the last 24 months of sales to predict the next 12 months. - Fitting the Model: The model learns from the actual sales data.
- Forecasting: The model uses what it has learned to forecast the next year’s sales.
- Plotting: We plot the actual and forecasted sales to see how well our model did.
This script will show a plot comparing the actual sales with the forecasted sales, giving a visual insight into the model’s performance. If you have Python set up and the necessary libraries installed, you can run this code to see how N-BEATS performs with this fictional dataset!
N-BEATS is important because it is highly accurate and versatile, making it a great choice for forecasting future trends in various situations, from business sales to weather patterns. Its simple structure makes it easier to use and understand compared to other more complex models. One of the standout features of N-BEATS is its interpretability. This means it’s easier to see and understand why the model makes certain predictions. For example, if N-BEATS is used to forecast sales, it can show whether the predictions are based on trends, like increasing demand over the years, or seasonal changes, like higher sales during holidays. This clarity helps users trust and make better decisions based on the model’s forecasts.
Reference: https://www.sciencedirect.com/science/article/abs/pii/S0306261921003986