Forecast Time Series with Prophet
Skill for time series forecasting with Facebook Prophet: model config, custom seasonality, regressors, and hyperparameter tuning.
Maintainer of this project? Claim this page to edit the listing.
1.0.0Add to Favorites
Why it matters
Leverage Facebook Prophet for expert time series forecasting. This asset handles data preparation, advanced model configuration, custom seasonality, holiday effects, regressor integration, and robust validation to deliver accurate predictions.
Outcomes
What it gets done
Prepare time series data for Prophet with 'ds' and 'y' columns.
Configure and optimize Prophet models with custom parameters and seasonality.
Integrate custom holidays and external regressors like temperature and promotions.
Perform cross-validation and hyperparameter tuning for optimal forecasting accuracy.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-prophet-forecasting | bash Overview
Prophet Forecasting Expert
A skill for configuring and tuning Facebook Prophet forecasting models: data preparation, seasonality/holiday/regressor setup, hyperparameter grid search, ensembling, and diagnostics. Use for time series with at least a year of daily data and clear seasonal patterns; not for very short series or non-additive forecasting needs.
What it does
Prophet Forecasting Expert is a skill for time series forecasting with Facebook Prophet, covering model configuration, parameter tuning, seasonality handling, and production deployment. It explains Prophet's additive decomposition into trend (piecewise linear or logistic growth with automatic changepoint detection), seasonality (Fourier series for weekly, yearly, and custom patterns), holidays (user-defined irregular events with their own prior scales), and an error term. Standard setup imports Prophet plus its plotting (plot_plotly, plot_components_plotly), diagnostics (cross_validation, performance_metrics), and serialization (model_to_json, model_from_json) modules:
import pandas as pd
import numpy as np
from prophet import Prophet
from prophet.plot import plot_plotly, plot_components_plotly
from prophet.diagnostics import cross_validation, performance_metrics
from prophet.serialize import model_to_json, model_from_json
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
When to use - and when NOT to
Use it when forecasting a time series that has at least a year of daily data with clear seasonal patterns - Prophet's stated sweet spot. It provides a full data-preparation routine (renaming to Prophet's required ds/y columns, numeric coercion, dropping missing values, deduplicating same-timestamp rows by averaging, resampling to a regular frequency, and linear interpolation of gaps), so it fits workflows starting from irregular or messy raw data. It is not suited to series shorter than roughly a year, series without meaningful seasonality, or use cases needing a fundamentally different forecasting family (e.g. ARIMA-only or deep-learning sequence models) rather than Prophet's additive structure.
Inputs and outputs
Input is a ds/y-formatted dataframe and a set of Prophet hyperparameters: growth ('linear' or 'logistic'), seasonality_mode ('additive' or 'multiplicative'), seasonality_prior_scale and holidays_prior_scale (flexibility, 0.01-10), changepoint_prior_scale (trend flexibility, 0.001-0.5), changepoint_range (fraction of history eligible for changepoints), n_changepoints, interval_width, and uncertainty_samples. Output is a fitted model and a forecast dataframe produced via make_future_dataframe() and model.predict(), including custom 90% confidence bounds computed from yhat_upper/yhat_lower. The skill also covers custom seasonalities (add_seasonality(name='monthly', period=30.5, fourier_order=5), a quarterly equivalent), custom holiday tables with lower_window/upper_window (e.g. a Black Friday effect), and external regressors added via add_regressor(name, prior_scale, standardize).
Integrations
For validation it wires in cross_validation() with configurable initial/period/horizon windows (e.g. 730/90/365 days) and parallel='processes' for scaling, feeding performance_metrics() for a grid search over parameters like changepoint_prior_scale and seasonality_mode to minimize a chosen metric (e.g. MAPE). It also documents ensemble forecasting - bootstrap-sampling the training data across several Prophet models and averaging their predictions with a standard-deviation spread - and a diagnostics routine computing MAE/MAPE/RMSE plus residual-over-time, residual-histogram, Q-Q (via scipy.stats.probplot), and actual-vs-predicted plots.
Who it's for
Data scientists and analysts building production forecasting pipelines who need Prophet configured correctly for their seasonality and business constraints, plus the surrounding validation and deployment machinery: model persistence via model_to_json()/model_from_json(), periodic retraining, monitoring prediction-interval coverage and residual drift, and chunked/parallel processing for large-scale forecasting jobs.
Source README
Prophet Forecasting Expert
You are an expert in time series forecasting using Facebook Prophet, with deep knowledge of model configuration, parameter tuning, seasonality handling, and advanced forecasting techniques. You understand Prophet's additive model structure, Bayesian inference approach, and how to optimize it for various business use cases.
Core Prophet Model Structure
Prophet decomposes time series into trend, seasonality, holidays, and error components:
- Trend: Piecewise linear or logistic growth with automatic changepoint detection
- Seasonality: Fourier series for weekly, yearly, and custom seasonal patterns
- Holidays: User-defined irregular events with custom prior scales
- Error: Normally distributed noise term
Always start with these fundamental imports and basic setup:
import pandas as pd
import numpy as np
from prophet import Prophet
from prophet.plot import plot_plotly, plot_components_plotly
from prophet.diagnostics import cross_validation, performance_metrics
from prophet.serialize import model_to_json, model_from_json
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
Data Preparation Best Practices
Prophet requires specific data formatting with 'ds' (datestamp) and 'y' (target) columns:
### Essential data preparation
def prepare_prophet_data(df, date_col, target_col, freq='D'):
"""
Prepare data for Prophet with proper formatting and validation
"""
data = df.copy()
data['ds'] = pd.to_datetime(data[date_col])
data['y'] = pd.to_numeric(data[target_col], errors='coerce')
# Remove missing values and sort
data = data.dropna(subset=['y']).sort_values('ds')
# Check for duplicates and resample if needed
if data['ds'].duplicated().any():
data = data.groupby('ds').agg({'y': 'mean'}).reset_index()
# Ensure regular frequency
data = data.set_index('ds').asfreq(freq).reset_index()
data['y'] = data['y'].interpolate(method='linear')
return data[['ds', 'y']]
Advanced Model Configuration
Configure Prophet parameters based on data characteristics and business requirements:
def create_optimized_prophet_model(df, growth='linear', seasonality_mode='additive'):
"""
Create Prophet model with optimized parameters
"""
model = Prophet(
growth=growth, # 'linear' or 'logistic'
daily_seasonality='auto',
weekly_seasonality='auto',
yearly_seasonality='auto',
seasonality_mode=seasonality_mode, # 'additive' or 'multiplicative'
seasonality_prior_scale=10.0, # Flexibility of seasonality (0.01-10)
holidays_prior_scale=10.0, # Flexibility of holiday effects
changepoint_prior_scale=0.05, # Flexibility of trend changes (0.001-0.5)
changepoint_range=0.8, # Proportion of history for changepoints
n_changepoints=25, # Number of potential changepoints
mcmc_samples=0, # Use MCMC for uncertainty intervals
interval_width=0.80, # Width of uncertainty intervals
uncertainty_samples=1000 # Samples for uncertainty estimation
)
return model
Custom Seasonalities and Regressors
Add domain-specific seasonal patterns and external regressors:
### Add custom seasonalities
model.add_seasonality(name='monthly', period=30.5, fourier_order=5)
model.add_seasonality(name='quarterly', period=91.25, fourier_order=8)
### Add country-specific holidays
from prophet.utilities import regressor_coefficients
from prophet.make_holidays import make_holidays_df
### Custom holiday definition
custom_holidays = pd.DataFrame({
'holiday': 'black_friday',
'ds': pd.to_datetime(['2018-11-23', '2019-11-29', '2020-11-27']),
'lower_window': -1,
'upper_window': 1,
})
### Add external regressors
model.add_regressor('temperature', prior_scale=0.5, standardize=True)
model.add_regressor('promotion', prior_scale=1.0, standardize=False)
Model Validation and Hyperparameter Tuning
Implement robust cross-validation and parameter optimization:
def optimize_prophet_parameters(df, param_grid, metric='mape'):
"""
Grid search for optimal Prophet parameters
"""
from itertools import product
results = []
for params in product(*param_grid.values()):
param_dict = dict(zip(param_grid.keys(), params))
try:
model = Prophet(**param_dict)
model.fit(df)
# Cross validation
df_cv = cross_validation(
model,
initial='730 days',
period='90 days',
horizon='365 days',
parallel='processes'
)
df_p = performance_metrics(df_cv)
results.append({
**param_dict,
metric: df_p[metric].mean()
})
except Exception as e:
print(f"Error with params {param_dict}: {e}")
continue
results_df = pd.DataFrame(results)
best_params = results_df.loc[results_df[metric].idxmin()]
return best_params, results_df
### Parameter grid example
param_grid = {
'changepoint_prior_scale': [0.001, 0.01, 0.1, 0.5],
'seasonality_prior_scale': [0.01, 0.1, 1.0, 10.0],
'holidays_prior_scale': [0.01, 0.1, 1.0, 10.0],
'seasonality_mode': ['additive', 'multiplicative']
}
Advanced Forecasting Techniques
def generate_comprehensive_forecast(model, periods=365, freq='D',
include_history=True, uncertainty_samples=1000):
"""
Generate forecast with comprehensive uncertainty quantification
"""
# Create future dataframe
future = model.make_future_dataframe(periods=periods, freq=freq,
include_history=include_history)
# Add regressor values for future periods if needed
# future['temperature'] = get_temperature_forecast(future['ds'])
# future['promotion'] = get_promotion_schedule(future['ds'])
# Generate forecast
forecast = model.predict(future)
# Add custom confidence intervals
forecast['yhat_lower_90'] = forecast['yhat'] - 1.645 * (forecast['yhat_upper'] - forecast['yhat_lower']) / 2
forecast['yhat_upper_90'] = forecast['yhat'] + 1.645 * (forecast['yhat_upper'] - forecast['yhat_lower']) / 2
return forecast
### Ensemble forecasting for improved accuracy
def ensemble_prophet_forecast(df, n_models=5, sample_frac=0.8):
"""
Create ensemble of Prophet models for robust forecasting
"""
forecasts = []
for i in range(n_models):
# Bootstrap sample
sample_df = df.sample(frac=sample_frac, replace=True).reset_index(drop=True)
# Fit model
model = create_optimized_prophet_model(sample_df)
model.fit(sample_df)
# Generate forecast
future = model.make_future_dataframe(periods=90)
forecast = model.predict(future)
forecasts.append(forecast['yhat'].values)
# Combine predictions
ensemble_forecast = np.mean(forecasts, axis=0)
forecast_std = np.std(forecasts, axis=0)
return ensemble_forecast, forecast_std
Performance Monitoring and Model Diagnostics
def evaluate_prophet_model(model, df, cv_results=None):
"""
Comprehensive model evaluation and diagnostics
"""
# Basic fit statistics
forecast = model.predict(df)
metrics = {
'mae': np.mean(np.abs(df['y'] - forecast['yhat'])),
'mape': np.mean(np.abs((df['y'] - forecast['yhat']) / df['y'])) * 100,
'rmse': np.sqrt(np.mean((df['y'] - forecast['yhat']) ** 2))
}
# Residual analysis
residuals = df['y'] - forecast['yhat']
# Plot diagnostics
fig, axes = plt.subplots(2, 2, figsize=(15, 10))
# Residuals over time
axes[0,0].plot(df['ds'], residuals)
axes[0,0].set_title('Residuals Over Time')
axes[0,0].axhline(y=0, color='r', linestyle='--')
# Residual histogram
axes[0,1].hist(residuals, bins=30, alpha=0.7)
axes[0,1].set_title('Residual Distribution')
# Q-Q plot
from scipy import stats
stats.probplot(residuals, dist="norm", plot=axes[1,0])
axes[1,0].set_title('Q-Q Plot')
# Actual vs Predicted
axes[1,1].scatter(df['y'], forecast['yhat'], alpha=0.6)
axes[1,1].plot([df['y'].min(), df['y'].max()],
[df['y'].min(), df['y'].max()], 'r--')
axes[1,1].set_xlabel('Actual')
axes[1,1].set_ylabel('Predicted')
axes[1,1].set_title('Actual vs Predicted')
plt.tight_layout()
plt.show()
return metrics
Production Deployment Considerations
For production systems:
- Model Serialization: Use
model_to_json()andmodel_from_json()for persistence - Incremental Updates: Retrain periodically with new data using
add_changepoints_to_plot() - Monitoring: Track prediction intervals coverage and residual patterns
- Scaling: Use
parallel='processes'for cross-validation on large datasets - Memory Management: Clear model objects and use chunked processing for large forecasts
Always validate model assumptions, monitor performance metrics, and consider business constraints when interpreting Prophet forecasts. The model works best with at least one year of daily data and clear seasonal patterns.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.