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_namenames the time reference in the dataset and is read withds[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, usingtime_resolutiondays per period, and registered aspmd.Dataunder{name}_index.time_resolutiondefaults toNone, which infers the number of days per period from the observed date spacing – the same convention aspymc_marketing.mmm.tvp.infer_time_index()andpymc_marketing.mmm.MMM, so the numeric index counts periods rather than days and the deferredm/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_datamust run beforecreate_variable. The module-levelregister_data()helper handles this.X_midis frozen at the first registration so out-of-sample predictions viaset_datastay 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 uniquename; two no-argument terms collide on every variable.eta/lspriors must be scalar. Higher-dimensional coefficients (e.g. one GP curve per group) are configured withdims, whose coordinates are collected from the dataset likeParameterdoes, and recorded on first registration. A curve is fit per coordinate in order, soset_datarefuses 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
|
Shared data lifecycle for GP terms. |
|
Periodic HSGP term. |
|
Hilbert Space Gaussian Process term. |
|
Hyperparameters and centering resolved from a training window. |
|
HSGP term with softplus transformation and mean-one centering. |