HSGPTerm#

class pymc_marketing.terms_gp.HSGPTerm(*, var_name='date', name='hsgp', X_mid=None, dims=None, demeaned_basis=False, time_resolution=None, eta=None, ls=None, m=None, L=None, centered=False, drop_first=True, eta_mass=0.05, eta_upper=1.0, ls_lower=1.0, ls_upper=None, ls_mass=0.9, cov_func=CovFunc.ExpQuad)[source]#

Hilbert Space Gaussian Process term.

A one-dimensional GP over the time reference var_name with optional higher-dimensional coefficients via dims. Composes with the other terms via + / * / -.

All of m, L, X_mid, eta, and ls are deferred: when None, they are resolved from the dataset inside the model context with the same heuristics and prior recommendations as pymc_marketing.mmm.hsgp.HSGP.parameterize_from_data(), then cached on the instance. Explicit values always win.

Parameters:
  • var_name (str) – Name of the time reference in the dataset, read with ds[var_name] (works for both coordinates and data variables). Datetimes are converted to days since the first date, divided by time_resolution, and registered as pmd.Data under {name}_index. Defaults to "date".

  • name (str) – Prefix for the variables and the output deterministic. Defaults to "hsgp".

  • eta (VariableFactory | float | None) – Prior for the GP variance. Defaults to the Exponential prior from pymc_marketing.mmm.hsgp.create_eta_prior().

  • ls (VariableFactory | float | None) – Prior for the lengthscale. Defaults to the complexity-penalizing Weibull prior, or a constrained InverseGamma when ls_upper is set.

  • m (int | None) – Number of basis functions. Defaults to the Ruitort-Mayol et al. recommendation from the data.

  • L (float | None) – Extent of the basis functions. Defaults to the recommendation from the data.

  • dims (str | tuple[str, ...] | None) – Extra dims for the coefficients beyond the time dim, e.g. "channel" for one GP curve per channel. Coordinates are collected from the dataset. The coordinates of these dims are recorded on the first registration, and set_data() refuses a window whose coordinates differ (reordered, dropped, or added), since each curve is fit per coordinate in order and a mismatch would report it under the wrong label.

  • time_resolution (int | None) – Number of days per observation period, dividing the day offsets of a datetime time reference. Default None, which infers it from the observed date spacing so the numeric index counts periods (the same convention as pymc_marketing.mmm.MMM). Pass an explicit value only to override the inferred unit.

  • centered (bool) – Whether the coefficient prior is centered. Default False.

  • drop_first (bool) – Whether to drop the first (constant) basis function. Default True.

  • demeaned_basis (bool) – Whether each basis has its mean subtracted. Default False.

  • eta_mass (float) – Knobs for the deferred prior resolution. Defaults mirror HSGP.parameterize_from_data (0.05, 1.0, 1.0, None, 0.9).

  • eta_upper (float) – Knobs for the deferred prior resolution. Defaults mirror HSGP.parameterize_from_data (0.05, 1.0, 1.0, None, 0.9).

  • ls_lower (float) – Knobs for the deferred prior resolution. Defaults mirror HSGP.parameterize_from_data (0.05, 1.0, 1.0, None, 0.9).

  • ls_upper (float | None) – Knobs for the deferred prior resolution. Defaults mirror HSGP.parameterize_from_data (0.05, 1.0, 1.0, None, 0.9).

  • ls_mass (float) – Knobs for the deferred prior resolution. Defaults mirror HSGP.parameterize_from_data (0.05, 1.0, 1.0, None, 0.9).

  • cov_func (CovFunc) – Covariance function. Default CovFunc.ExpQuad.

Examples

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)

Explicit configuration:

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

trend = HSGPTerm(
    var_name="time",
    name="trend",
    eta=Prior("Exponential", lam=1),
    ls=Prior("InverseGamma", alpha=2, beta=1),
    m=20,
    L=150,
)

Methods

HSGPTerm.__init__(*[, var_name, name, ...])

HSGPTerm.add_coords(ds)

Resolve deferred values.

HSGPTerm.create_variable()

Build the GP curve through the wrapped HSGP class.

HSGPTerm.from_dict(data)

Reconstruct a term from its serialized form.

HSGPTerm.get_coords(ds)

Collect coordinates from the time reference and extra dims.

HSGPTerm.named(name[, dims])

Record this expression as a named deterministic model variable.

HSGPTerm.register_data(ds)

Register the numeric time index as pmd.Data and freeze X_mid.

HSGPTerm.set_data(ds[, model])

Update the shared time index for out-of-sample prediction.

HSGPTerm.sum(dim)

Sum this expression over one named dimension.

HSGPTerm.to_dict([_orig])

Attributes