How Prophet Implements Time Series Decomposition and Trend Modeling
This article explains Prophet’s algorithmic approach to time‑series forecasting, covering decomposition into trend, seasonality, holidays and error components, logistic and piecewise linear trend models, automatic change‑point detection, Fourier‑based seasonality, holiday handling, model fitting with PyStan, and practical Python code examples.
Prophet Algorithm Implementation
In time series analysis, decomposition splits a series into seasonal, trend, and residual components, in additive or multiplicative form. Prophet extends this by also modeling holidays, resulting in four components: trend, seasonality, holidays, and error.
Trend Component Model
Prophet uses two trend functions: a logistic growth function and a piecewise linear function. The logistic function follows the sigmoid form, with parameters that become time‑varying functions. Change points can be set manually or detected automatically; by default Prophet selects n_changepoints points uniformly within the first changepoint_range (80 %) of the series.
Change‑point handling uses np.linspace to distribute points uniformly, and the growth‑rate change at each point is represented by an indicator function.
changepoint_range
n_changepoints
changepoint_prior_scale
Seasonality
Seasonality is modeled with Fourier series. Yearly and weekly periods are expressed as sums of sine and cosine terms, with a seasonality_prior_scale controlling the strength. The mode can be additive or multiplicative.
Holiday Effects
Prophet incorporates holidays from a built‑in database (hdays.py) and allows custom holidays. Each holiday can have its own prior scale and a window to model pre‑ and post‑effects.
Model Fitting
After constructing the components, Prophet fits the model using PyStan’s L‑BFGS optimizer (see forecast.py ). Main configurable parameters are capacity (for logistic growth), change points, seasonality/holidays, and smoothing scales ( changepoint_prior_scale , seasonality_prior_scale , holidays_prior_scale ).
Practical Usage of Prophet
Simple Usage
Prophet expects a DataFrame with columns ‘ds’ (timestamp) and ‘y’ (value). Example code to rename columns, convert Unix timestamps, normalize the series, initialize, fit, and forecast:
<code>df = df.rename(columns={'timestamp':'ds','value':'y'})
df['ds'] = pd.to_datetime(df['ds'], unit='s')
df['y'] = (df['y'] - df['y'].mean()) / df['y'].std()
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=30, freq='min')
forecast = m.predict(future)</code>Plotting the forecast and its components:
<code>m.plot(forecast)
m.plot_components(forecast)</code>Code Example 1
<code>from prophet import Prophet
model1 = Prophet(yearly_seasonality=True, weekly_seasonality=True, daily_seasonality=True)
model1.add_country_holidays(country_name="CN")
model1.fit(data1)
future = model1.make_future_dataframe(periods=10)
forecast = model1.predict(future)</code>Code Example 2
<code>from prophet import Prophet
model2 = Prophet(growth='logistic', yearly_seasonality=False, weekly_seasonality=True, n_changepoints=2)
model2.add_country_holidays(country_name="CN")
data1['cap'] = 500
model2.fit(data1)
future = model2.make_future_dataframe(periods=10)
future['cap'] = 500
forecast = model2.predict(future)</code>Conclusion
Prophet works well for many business time‑series but may be unsuitable for series with weak seasonality or trend. It provides an accessible forecasting method for users with limited time‑series expertise, though suitability depends on the specific data.
Source
https://zhuanlan.zhihu.com/p/52330017
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.