You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在面向英国市场的FB Prophet时序模型中纳入COVID-19影响?

Incorporating COVID-19 Impacts into Your FB Prophet Time Series Model

Great question—adding COVID-19 effects to your existing Prophet model is crucial for accurate post-2020 forecasts, especially since your target metric depends on early 2020 data. Below are practical, actionable methods tailored to your UK market setup:

1. Add a COVID-19 Regressor (Direct Intervention)

This is the most straightforward approach: create a binary flag to mark periods where COVID-19 restrictions or impacts were active, then feed this as an external regressor to Prophet.

Steps:

  • Define the COVID start date: For the UK, the first national lockdown started on March 23, 2020—use this as your cutoff (adjust if your metric was affected earlier, e.g., late February 2020).
  • Add the regressor to your historical data: Create a column like covid_regressor where values are 1 for dates on/after March 23, 2020, and 0 before.
  • Include it in your Prophet model: Use add_regressor() to tell Prophet to account for this variable.
  • Set values for the forecast period: If your 30-day forecast falls during ongoing restrictions (or specific COVID conditions), set covid_regressor to 1 (or adjust based on expected policy changes).

Code Example:

import pandas as pd
from prophet import Prophet

# Load your existing data (df has 'ds' and 'y' columns)
df = pd.read_csv('your_data.csv')

# Add COVID regressor
df['covid_regressor'] = (df['ds'] >= '2020-03-23').astype(int)

# Initialize and fit model with the regressor
m = Prophet(seasonality_mode='additive', holidays=your_existing_holidays)
m.add_regressor('covid_regressor')
m.fit(df)

# Create future dataframe and set regressor values for the 30-day forecast
future = m.make_future_dataframe(periods=30)
future['covid_regressor'] = (future['ds'] >= '2020-03-23').astype(int)  # Adjust based on expected conditions

# Generate forecast
forecast = m.predict(future)

2. Tune Changepoints to Highlight COVID's Sudden Impact

Prophet automatically detects trend changepoints, but you can manually specify the COVID onset date to force the model to prioritize this critical shift. Increase the changepoint_prior_scale to make the model more responsive to this abrupt change (default is 0.05; try 0.1–0.2).

Code Example:

# Initialize model with explicit COVID changepoint
m = Prophet(
    changepoints=['2020-03-23'],  # Mark the lockdown start as a key changepoint
    changepoint_prior_scale=0.1,  # Make trend more flexible around this date
    seasonality_mode='additive',
    holidays=your_existing_holidays
)
m.fit(df)

# Generate forecast as usual
future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)

3. Add COVID-Specific "Holidays" for Policy Events

Treat major COVID-related events (lockdowns, tiered restrictions, reopenings) as custom holidays in Prophet. This works well if certain policies had clear, time-bound impacts on your metric.

Steps:

  • Create a DataFrame with COVID events, including holiday, ds (event date), and lower_window/upper_window to capture the duration of the impact (e.g., a lockdown might have a 7-day upper window to account for gradual adoption).
  • Pass this DataFrame to the holidays parameter when initializing Prophet.

Code Example:

# Define COVID-related events for the UK
covid_holidays = pd.DataFrame({
    'holiday': 'uk_national_lockdown_1',
    'ds': pd.to_datetime(['2020-03-23']),
    'lower_window': 0,
    'upper_window': 90,  # Capture 90 days of lockdown impact
})

# Add other events (e.g., tier 3 restrictions, reopening dates) as needed
covid_holidays = pd.concat([covid_holidays, pd.DataFrame({
    'holiday': 'uk_first_reopening',
    'ds': pd.to_datetime(['2020-06-01']),
    'lower_window': -7,  # Impact starts 7 days before reopening
    'upper_window': 14,
})])

# Initialize model with combined holidays (existing + COVID)
m = Prophet(
    seasonality_mode='additive',
    holidays=pd.concat([your_existing_uk_holidays, covid_holidays])
)
m.fit(df)

4. Segment Data and Combine Models (For Extreme Shifts)

If COVID caused a permanent structural change in your metric, consider splitting your data into pre-COVID (2019-01 to 2020-02) and post-COVID (2020-03 onwards) segments:

  • Train a pre-COVID model to capture baseline seasonality/holidays.
  • Train a post-COVID model to learn the new trend and COVID-specific patterns.
  • For forecasts, use the post-COVID model (since your metric relies on 2020+ data) or blend predictions if the pre-COVID baseline is still relevant.

Key Tips for All Methods:

  • Backtest rigorously: Use your 2020-03 onwards historical data to test which method produces the lowest forecast error (e.g., MAE, RMSE).
  • Update dynamically: If your 30-day forecast includes new COVID policy changes, adjust your regressor or holiday data accordingly.
  • Validate with business context: Check Prophet's component plots (m.plot_components(forecast)) to ensure the COVID impact aligns with what you know about your metric and the UK market.

内容的提问来源于stack exchange,提问作者Django0602

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 08:07:47