terms_gp#

Gaussian process model terms.

pymc_marketing.terms_gp exposes the Hilbert Space Gaussian Process (HSGP) components from pymc_marketing.mmm.hsgp as composable ModelTerm pieces: HSGPTerm, HSGPPeriodicTerm, and SoftPlusHSGPTerm. They subclass the ModelTerm lifecycle (get_coords / add_coords / register_data / create_variable / set_data) so they compose with the other built-ins via + / * / - — no framework changes required.

The terms are fully deferred. Constructing one touches no data and computes nothing: m, L, X_mid, and the default eta / ls priors are resolved from the dataset inside the model context (cached on the instance once resolved, so repeated builds reuse them). Explicit values always win over the deferred resolution.

The GP graph itself is delegated to the existing pymc_marketing.mmm.hsgp classes: at create_variable time the term builds a transient HSGP (or HSGPPeriodic / SoftPlusHSGP) spec from its resolved fields and delegates variable creation to it, so the two implementations cannot drift. These terms own the modeling lifecycle: time-reference resolution (datetime coordinates welcome), shared data registration, frozen centering, and the terms composition and serialization contract.

Defaults follow the assumptions of the existing HSGP classes: eta_mass=0.05, eta_upper=1.0, ls_lower=1.0, ls_upper=None, ls_mass=0.9, cov_func="expquad", centered=False, drop_first=True, and demeaned_basis=False.

Rules#

  • var_name names the time reference in the dataset and is read with ds[var_name], which works for both coordinates (the common case, e.g. coords={"date": ...}) and data variables. Datetimes are converted to observation periods since the anchored first training date, using time_resolution days per period, and registered as pmd.Data under {name}_index.

  • time_resolution defaults to None, which infers the number of days per period from the observed date spacing – the same convention as pymc_marketing.mmm.tvp.infer_time_index() and pymc_marketing.mmm.MMM, so the numeric index counts periods rather than days and the deferred m / L / lengthscale heuristics see the same axis as the rest of the library. Pass it explicitly only to override the inferred unit. Numeric (non-datetime) references are passed through unchanged with a resolution of 1.

  • register_data must run before create_variable. The module-level register_data() helper handles this.

  • X_mid is frozen at the first registration so out-of-sample predictions via set_data stay centered on the training data. It is excluded from serialization and re-derived from the data on rebuild.

  • Each random variable is prefixed with name ({name}_eta, {name}_ls, {name}_hsgp_coefs) and the basis coordinate is {name}_m. Each term in an expression tree needs a unique name; two no-argument terms collide on every variable.

  • eta / ls priors must be scalar. Higher-dimensional coefficients (e.g. one GP curve per group) are configured with dims, whose coordinates are collected from the dataset like Parameter does, and recorded on first registration. A curve is fit per coordinate in order, so set_data refuses a window whose coordinates for those dims were reordered, dropped, or added rather than reporting each curve under the wrong label. The time index itself stays one-dimensional and is shared across those dims.

Time-varying media#

The canonical time-varying media multiplier is SoftPlusHSGPTerm, matching the time-varying parameters of the stable MMM classes. The GP output is one-dimensional over the time dimension, so it broadcasts across the channel dimension of a media term through *:

tvp_media = SoftPlusHSGPTerm() * media

Examples#

HSGP term with deferred defaults, composed into an outcome equation:

from pymc_extras.prior import Prior
from pymc_marketing.terms import (
    Intercept,
    Dot,
    collect_coords,
    register_data,
    build_param,
)
from pymc_marketing.terms_gp import HSGPTerm

trend = HSGPTerm(var_name="time", name="trend")
mu = (
    Intercept("intercept")
    + trend
    + Dot(var_name="x", prior=Prior("Normal", dims="feature"))
)

coords = collect_coords(mu, ds=ds)
with pm.Model(coords=coords) as model:
    register_data(mu, ds=ds)
    mu_value = build_param(mu)

No-argument construction for a one-dimensional GP over the "date" coordinate, then multiplied by a media term (dims (date, channel)):

from pymc_marketing.terms_gp import SoftPlusHSGPTerm

tvp_media = SoftPlusHSGPTerm() * media

Higher-dimensional coefficients, one GP curve per channel:

from pymc_marketing.terms_gp import HSGPTerm

trend = HSGPTerm(var_name="time", name="trend", dims="channel")

Periodic seasonality term:

from pymc_extras.prior import Prior
from pymc_marketing.terms_gp import HSGPPeriodicTerm

seasonality = HSGPPeriodicTerm(
    var_name="time",
    name="seasonality",
    scale=Prior("HalfNormal", sigma=1),
    ls=Prior("InverseGamma", alpha=2, beta=1),
    period=52,
    m=20,
)

Classes

GPDataTerm(*[, var_name, name, X_mid, dims, ...])

Shared data lifecycle for GP terms.

HSGPPeriodicTerm(*[, var_name, name, X_mid, ...])

Periodic HSGP term.

HSGPTerm(*[, var_name, name, X_mid, dims, ...])

Hilbert Space Gaussian Process term.

ResolvedGPState(*, X_mid[, m, L, eta, ls])

Hyperparameters and centering resolved from a training window.

SoftPlusHSGPTerm(*[, var_name, name, X_mid, ...])

HSGP term with softplus transformation and mean-one centering.