SoftPlusHSGP#
- class pymc_marketing.mmm.hsgp.SoftPlusHSGP(**data)[source]#
HSGP with softplus transformation.
The use of the softplus transformation centers the data around 1 and keeps the values positive.
Examples
Literature recommended SoftPlusHSGP configuration:
import numpy as np import pandas as pd import matplotlib.pyplot as plt from pymc_marketing.mmm import SoftPlusHSGP seed = sum(map(ord, "Out of the box GP")) rng = np.random.default_rng(seed) n = 52 X = np.arange(n) hsgp = SoftPlusHSGP.parameterize_from_data( X=X, dims="time", ) dates = pd.date_range("2022-01-01", periods=n, freq="W-MON") coords = { "time": dates, } prior = hsgp.sample_prior(coords=coords, random_seed=rng) curve = prior["f"] hsgp.plot_curve(curve, sample_kwargs={"rng": rng}) plt.show()
(
Source code
,png
,hires.png
,pdf
)New data predictions with SoftPlusHSGP
Note
In making the predictions, the model needs to be transformed in order to keep the data centered around 1. There is a helper function
pymc_marketing.model_graph.deterministics_to_flat()
that can be used to transform the model upon out of sample predictions.This transformation is automatically handled in the provided MMMs.
import numpy as np import pandas as pd import pymc as pm import matplotlib.pyplot as plt from pymc_marketing.mmm import SoftPlusHSGP from pymc_marketing.model_graph import deterministics_to_flat from pymc_marketing.prior import Prior seed = sum(map(ord, "New data predictions with SoftPlusHSGP")) rng = np.random.default_rng(seed) eta = Prior("Exponential", lam=1) ls = Prior("InverseGamma", alpha=2, beta=1) hsgp = SoftPlusHSGP( eta=eta, ls=ls, m=20, L=150, dims=("time", "channel"), ) n = 52 X = np.arange(n) channels = ["A", "B", "C"] dates = pd.date_range("2022-01-01", periods=n, freq="W-MON") coords = {"time": dates, "channel": channels} with pm.Model(coords=coords) as model: data = pm.Data("data", X, dims="time") hsgp.register_data(data).create_variable("f") idata = pm.sample_prior_predictive(random_seed=rng) prior = idata.prior n_new = 10 X_new = np.arange(n - 1 , n + n_new) last_date = dates[-1] new_dates = pd.date_range(last_date, periods=n_new + 1, freq="W-MON") with deterministics_to_flat(model, hsgp.deterministics_to_replace("f")): pm.set_data( new_data={ "data": X_new, }, coords={"time": new_dates}, ) post = pm.sample_posterior_predictive( prior, var_names=["f"], random_seed=rng, ) chain, draw = 0, rng.choice(prior.sizes["draw"]) colors = [f"C{i}" for i in range(len(channels))] def get_sample(curve): return curve.loc[chain, draw].to_series().unstack() ax = prior["f"].pipe(get_sample).plot(color=colors) post.posterior_predictive["f"].pipe(get_sample).plot( ax=ax, color=colors, linestyle="--", legend=False ) ax.set(xlabel="time", ylabel="f", title="New data predictions") plt.show()
(
Source code
,png
,hires.png
,pdf
)Methods
SoftPlusHSGP.__init__
(**data)Create a new model by parsing and validating input data from keyword arguments.
SoftPlusHSGP.construct
([_fields_set])SoftPlusHSGP.copy
(*[, include, exclude, ...])Returns a copy of the model.
Create the variable.
Name of the Deterministic variables that are replaced with pm.Flat for out-of-sample.
SoftPlusHSGP.dict
(*[, include, exclude, ...])SoftPlusHSGP.from_dict
(data)Create an object from a dictionary.
SoftPlusHSGP.json
(*[, include, exclude, ...])SoftPlusHSGP.model_construct
([_fields_set])Creates a new instance of the
Model
class with validated data.SoftPlusHSGP.model_copy
(*[, update, deep])!!! abstract "Usage Documentation"
SoftPlusHSGP.model_dump
(*[, mode, include, ...])!!! abstract "Usage Documentation"
SoftPlusHSGP.model_dump_json
(*[, indent, ...])!!! abstract "Usage Documentation"
SoftPlusHSGP.model_json_schema
([by_alias, ...])Generates a JSON schema for a model class.
Compute the class name for parametrizations of generic classes.
SoftPlusHSGP.model_post_init
(context, /)Override this method to perform additional initialization after
__init__
andmodel_construct
.SoftPlusHSGP.model_rebuild
(*[, force, ...])Try to rebuild the pydantic-core schema for the model.
SoftPlusHSGP.model_validate
(obj, *[, ...])Validate a pydantic model instance.
SoftPlusHSGP.model_validate_json
(json_data, *)!!! abstract "Usage Documentation"
Validate the given object with string data against the Pydantic model.
SoftPlusHSGP.parameterize_from_data
(X, dims)Create a HSGP informed by the data with literature-based recommendations.
SoftPlusHSGP.parse_file
(path, *[, ...])SoftPlusHSGP.parse_raw
(b, *[, content_type, ...])SoftPlusHSGP.plot_curve
(curve[, ...])Plot the curve.
Register the data to be used in the model.
SoftPlusHSGP.sample_prior
([coords])Sample from the prior distribution.
SoftPlusHSGP.schema
([by_alias, ref_template])SoftPlusHSGP.schema_json
(*[, by_alias, ...])Convert the object to a dictionary.
SoftPlusHSGP.update_forward_refs
(**localns)SoftPlusHSGP.validate
(value)Attributes
model_computed_fields
model_config
Configuration for the model, should be a dictionary conforming to [
ConfigDict
][pydantic.config.ConfigDict].model_extra
Get extra fields set during validation.
model_fields
model_fields_set
Returns the set of fields that have been explicitly set on this model instance.
ls
eta
L
centered
drop_first
cov_func
m
X
X_mid
dims
transform
demeaned_basis