Prophet › Formula

Michael BrenndoerferAugust 5, 202515 min read
Machine Learning from ScratchAll chapters
Part 8 of 9Time Series
Chapter 59Prophet › Formula
Reading section 2 of 8 · Formula · 15 minSection 2 of 8 · 15 min

Book contents

Section 2 of 8 · 15 min read

Prophet › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

To understand how Prophet works, we need to think about what makes a time series predictable. Most time series contain several distinct patterns that combine to create the values we observe. Prophet's mathematical foundation is built on the idea that we can break down any time series into separate, interpretable components and then add them together to reconstruct the original series. This decomposition approach makes forecasts both accurate and understandable.

Why Decomposition Matters

Imagine you're forecasting daily sales for an e-commerce store. The sales on any given day are influenced by multiple factors: the overall business growth (trend), recurring patterns like higher weekend sales (seasonality), special events like Black Friday (holidays), and random fluctuations (noise). If we can separate these components mathematically, we can understand what drives our forecasts and make better predictions.

Prophet's additive model does exactly this. It assumes that each component contributes independently to the final value, which means we can model and forecast each component separately, then combine them to get the full prediction. This approach is powerful because it matches how we naturally think about time series: as the sum of different effects rather than a single complex pattern.

The Additive Model

The fundamental equation that captures this decomposition is:

y(t)=g(t)+s(t)+h(t)+ϵty(t) = g(t) + s(t) + h(t) + \epsilon_t

where:

-y(t)y(t): the observed time series value at time tt

  • g(t)g(t): the trend component capturing long-term growth or decline
  • s(t)s(t): the seasonal component capturing recurring patterns
  • h(t)h(t): the holiday component capturing irregular events
  • ϵt\epsilon_t: the error term representing random noise

This equation tells us that at any point in time, the value we observe is simply the sum of these four components. The additive structure means each component contributes independently, making the model highly interpretable. If we want to understand why sales were high on a particular day, we can look at each component separately: was it due to overall growth, seasonal patterns, a holiday effect, or just random variation?

Out[3]:
Visualization
Multi-panel plot showing Prophet's additive decomposition of time series into trend, seasonality, holidays, and noise components.
Visualization of Prophet''s additive decomposition showing how the four components (trend, seasonality, holidays, and noise) combine to form the observed time series. Each component is shown separately, and a highlighted time point demonstrates how they add together: y(t) = g(t) + s(t) + h(t) + ε_t. This decomposition makes it easy to understand what drives the forecasts.

Trend Component: g(t)g(t)

The trend component captures the long-term direction of your time series. Think of it as the underlying growth or decline pattern that persists over time, separate from seasonal fluctuations or one-time events. Prophet models this trend in a flexible way that can adapt when the growth rate changes.

Why Changepoints Matter

Real-world time series rarely grow at a constant rate forever. A company might grow rapidly in its early years, then slow down as it matures. A product might see a sudden boost after a marketing campaign. Prophet handles these structural breaks through changepoints, which are points in time where the growth rate is allowed to change. This flexibility lets the model capture realistic business dynamics.

Linear Trend Model

For time series that grow or decline linearly, Prophet uses:

g(t)=(k+a(t)Tδ)⋅t+(m+a(t)Tγ)g(t) = (k + a(t)^T \delta) \cdot t + (m + a(t)^T \gamma)

where:

  • kk: the base growth rate (how fast the series grows per unit time)
  • δ\delta: a vector of rate changes at each changepoint
  • mm: the base offset (the starting value)
  • γ\gamma: a vector of offset adjustments at changepoints (ensures continuity)
  • a(t)a(t): a vector indicating which changepoints are active at time tt (1 if the changepoint has occurred, 0 otherwise)

The term a(t)Tδa(t)^T \delta adjusts the growth rate based on which changepoints have occurred by time tt. Similarly, a(t)Tγa(t)^T \gamma adjusts the offset to ensure the trend function remains continuous even when the growth rate changes. This formulation allows the trend to smoothly adapt to structural breaks in the data.

Out[4]:
Visualization
Three-panel plot showing Prophet's trend with changepoints, binary indicators for active changepoints, and the resulting effective growth rate.
Visualization of Prophet's linear trend model with changepoints. The top panel shows how the trend $g(t) = (k + a(t)^T \delta) \cdot t + (m + a(t)^T \gamma)$ adapts when changepoints occur. The middle panel shows the binary components of the activation vector $a(t)$, and the bottom panel shows the resulting piecewise effective growth rate. The trend remains continuous even as its rate changes, demonstrating how Prophet handles structural breaks in time series data.

Logistic Growth Trend Model

For time series that approach a maximum value (like market saturation), Prophet uses logistic growth:

g(t)=C1+exp⁡(−(k+a(t)Tδ)⋅(t−(m+a(t)Tγ)))g(t) = \frac{C}{1 + \exp(-(k + a(t)^T \delta) \cdot (t - (m + a(t)^T \gamma)))}

where CC is the carrying capacity, representing the maximum value the series can reach. This model captures the S-shaped growth pattern common in business scenarios where growth slows as a market saturates.

The logistic function starts near zero, grows rapidly in the middle, then slows down as it approaches the carrying capacity. The changepoint mechanism works the same way as in the linear model, allowing the growth rate to change over time while maintaining continuity.

Seasonal Component: s(t)s(t)

Seasonal patterns are recurring fluctuations that repeat at regular intervals. Daily sales might be higher on weekends, monthly revenue might peak in December, or website traffic might spike every Monday morning. Prophet captures these patterns using Fourier series, which can represent any periodic function as a sum of sine and cosine waves.

Why Fourier Series?

Fourier series are powerful because they can approximate any periodic pattern, no matter how complex. Instead of assuming a specific shape for seasonality (like a simple sine wave), Prophet uses multiple Fourier terms to capture detailed patterns. This flexibility means Prophet can model everything from simple weekly patterns to complex multi-year cycles.

The seasonal component is defined as:

s(t)=∑n=1N(ancos⁡(2πntP)+bnsin⁡(2πntP))s(t) = \sum_{n=1}^{N} \left( a_n \cos\left(\frac{2\pi nt}{P}\right) + b_n \sin\left(\frac{2\pi nt}{P}\right) \right)

where:

-NN: the number of Fourier terms (more terms allow more complex patterns)

  • PP: the period of the seasonality (365.25 for yearly, 7 for weekly, etc.)
  • an,bna_n, b_n: Fourier coefficients learned from data (determine the shape of the pattern)

Each term in the sum represents a harmonic of the base frequency. The first term (n=1n=1) captures the fundamental seasonal pattern, while higher-order terms (n=2,3,...n=2, 3, ...) capture more complex variations. By learning the coefficients ana_n and bnb_n from data, Prophet automatically discovers the seasonal pattern without requiring you to specify its exact form.

Out[5]:
Visualization
Multi-line plot showing individual Fourier terms and their combination to form complex seasonal patterns.
Visualization of how Fourier series terms combine to create complex seasonal patterns. The plot shows individual Fourier terms (n=1, n=2, n=3) and how they sum together to form increasingly complex seasonal patterns. This demonstrates the formula $s(t) = \sum_{n=1}^{N} \left( a_n \cos\left(\frac{2\pi nt}{P}\right) + b_n \sin\left(\frac{2\pi nt}{P}\right) \right)$ and how increasing $N$ (the number of Fourier terms) allows Prophet to capture more intricate seasonal patterns.

Multiple Seasonalities

Prophet can model multiple seasonal patterns simultaneously. For example, daily sales data might have both weekly patterns (higher on weekends) and yearly patterns (higher in December). Each seasonality gets its own Fourier series with an appropriate period, and they're all added together in the seasonal component.

Holiday Component: h(t)h(t)

Holidays and special events create irregular spikes or dips that don't follow regular seasonal patterns. Black Friday sales, Christmas shopping, or product launches create one-time effects that need special handling. Prophet's holiday component allows you to explicitly model these events.

The holiday component is:

h(t)=∑iκi⋅Zi(t)h(t) = \sum_{i} \kappa_i \cdot Z_i(t)

where:

-κi\kappa_i: the effect size of holiday ii (learned from data)

  • Zi(t)Z_i(t): an indicator function that equals 1 if time tt falls within the window for holiday ii, and 0 otherwise

This formulation is simple but effective. For each holiday you specify, Prophet learns how much it affects the time series (the κi\kappa_i parameter). The indicator function Zi(t)Z_i(t) ensures the holiday effect only applies during the relevant time window. You can specify windows that extend before and after the holiday to capture effects like pre-Christmas shopping or post-holiday returns.

Mathematical Properties

Prophet's additive decomposition has several mathematical properties that make it powerful and reliable. Under certain conditions, there is typically one way to separate a given time series into its trend and seasonal components, including holiday effects. This uniqueness ensures that the model's interpretation is well-defined and consistent.

The model uses Bayesian inference to estimate all parameters. This provides several advantages. First, it naturally incorporates uncertainty into the parameter estimates, which propagates to forecast uncertainty intervals. Second, the Bayesian framework allows for informative priors that encode domain knowledge, such as expected holiday effect sizes or typical growth rates. Finally, Bayesian inference provides a principled way to handle the tradeoff between model flexibility and overfitting.

The Fourier series representation of seasonality is powerful because of a fundamental mathematical result: any periodic function can be approximated arbitrarily well by a Fourier series with enough terms. By adjusting the number of Fourier terms, Prophet can capture seasonal patterns ranging from simple sine waves to complex multi-periodic structures.

The changepoint detection mechanism uses a sparse prior, which assumes that most time points don't have trend changes, but allows for changes when the data strongly suggests them. This prior prevents the model from overfitting by creating changepoints everywhere, while still being flexible enough to capture structural breaks supported by the data. The balance between flexibility and parsimony is controlled by the changepoint_prior_scale parameter, which determines how easily the model will create new changepoints.