When using Prophet for time series forecasting and performing cross-validation, it's important to understand how the model fitting and prediction processes interact. Let me break this down step by step to clarify the correct approach:
1. Understanding Cross-Validation in Prophet
Cross-validation in Prophet involves training the model on a subset of the data and then evaluating its performance on a subsequent holdout set (forecasting window). The process is repeated by progressively expanding the training set and moving the forecast window forward.
In your case:
- You have 130 points for training and cross-validation.
- You are starting the forecast at point 91 with a forecasting window of 26.
- The stride (step size) between consecutive forecasts is 4.
- This results in 4 passes of cross-validation.
For each pass:
- The model is trained on a progressively larger portion of the data up to the start of the forecast window.
- The model then predicts the next 26 points (the forecasting window).
2. How Prophet's fit Function Works
The fit function in Prophet trains the model on the data you provide. Once the model is fitted, you can use the predict function to generate forecasts for a specified future period.
Key points:
- The
fit function does not automatically handle cross-validation or rolling windows. You need to manually manage the training and testing subsets for each pass.
- For each cross-validation pass, you must train the model on the appropriate training subset (up to the start of the forecast window) and then predict for the forecasting window.
3. Should You Pass All 130 Points to fit?
No, you should not pass all 130 points to fit in one go. Instead, you should pass only the data points that correspond to the training subset for each cross-validation pass. Here's why:
- If you pass all 130 points to
fit, the model will be trained on data that includes the forecast window, which violates the principle of cross-validation (training data should not overlap with the test data).
- To ensure proper evaluation, you need to restrict the training data to the subset available up to the start of the forecast window for each pass.
4. Implementation Strategy
To implement cross-validation correctly, you should follow these steps:
Step 1: Define the Cross-Validation Parameters
- Start of the first forecast:
initial = 91
- Forecast window:
horizon = 26
- Stride:
stride = 4
- Number of passes:
4
Step 2: Loop Through Each Cross-Validation Pass
For each pass:
- Determine the training subset:
- Training data for pass
i includes points from the start of the dataset up to start_of_forecast - 1.
- Fit the model on the training subset.
- Generate predictions for the forecasting window.
- Evaluate the predictions against the actual values in the forecasting window.
Here’s an example implementation in Python:
from fbprophet import Prophet
import pandas as pd
# Assume `df` is your DataFrame with columns 'ds' (date) and 'y' (value)
# df = pd.read_csv('your_data.csv')
# Define cross-validation parameters
initial = 91
horizon = 26
stride = 4
num_passes = 4
# Initialize a list to store evaluation metrics
evaluation_results = []
for i in range(num_passes):
# Determine the training subset
train_end = initial + i * stride
train_data = df.iloc[:train_end]
# Determine the forecast window
forecast_start = train_end
forecast_end = forecast_start + horizon
test_data = df.iloc[forecast_start:forecast_end]
# Fit the model on the training subset
model = Prophet()
model.fit(train_data)
# Create a future dataframe for the forecast window
future = model.make_future_dataframe(periods=horizon)
forecast = model.predict(future)
# Extract predictions for the forecast window
predicted_values = forecast['yhat'].iloc[-horizon:]
actual_values = test_data['y']
# Evaluate the predictions (e.g., calculate MAE, RMSE, etc.)
mae = abs(predicted_values - actual_values).mean()
evaluation_results.append({'pass': i + 1, 'MAE': mae})
# Convert evaluation results to a DataFrame
evaluation_df = pd.DataFrame(evaluation_results)
print(evaluation_df)
5. Key Takeaways
- For each cross-validation pass, you should pass only the training subset (up to the start of the forecast window) to the
fit function.
- Avoid passing all 130 points to
fit at once, as this would lead to data leakage and invalidate the cross-validation process.
- Use a loop to iteratively train the model on progressively larger subsets of the data and evaluate its performance on the corresponding forecast windows.
By following this approach, you ensure that your cross-validation process is robust and provides reliable estimates of your model's performance.