When you run the model, you add your model specification, and then run the commands to sample the prior distribution and the posterior distribution.
Markov Chain Monte Carlo (MCMC) algorithms are used to sample from the posterior distribution. Meridian uses the No-U-Turn sampling method with step size and kernel adaptation.
To run the model:
Add your model specification.
Example:
model_spec = spec.ModelSpec( prior=prior_distribution.PriorDistribution(), media_effects_dist='log_normal', hill_before_adstock=False, max_lag=8, unique_sigma_for_each_geo=False, media_prior_type='roi', roi_calibration_period=None, rf_prior_type='coefficient', rf_roi_calibration_period=None, organic_media_prior_type='contribution', organic_rf_prior_type='contribution', non_media_treatments_prior_type='contribution', knots=None, baseline_geo=None, holdout_id=None, control_population_scaling_id=None, adstock_decay_spec='geometric', enable_aks=False, ) ```Run the following commands to sample from the prior and posterior distribution. Configure the parameters as needed:
mmm = model.Meridian(input_data=data, model_spec=model_spec) mmm.sample_prior(500) mmm.sample_posterior(n_chains=7, n_adapt=1000, n_burnin=500, n_keep=1000)Parameter Description n_chainsThe number of chains to be sampled in parallel. To reduce memory consumption, you can use a list of integers to allow for sequential MCMC sampling calls. Given a list, each element in the sequence corresponds to the n_chainsargument for a call towindowed_adaptive_nuts.n_adaptThe number of MCMC draws per chain, during which step size and kernel are adapted. These draws are always excluded. n_burninAn additional number of MCMC draws, per chain, to be excluded after the step size and kernel are fixed. These additional draws may be needed to ensure that all chains reach the stationary distribution after adaptation is completed, but in practice we often find that the chains reach the stationary distribution during adaptation and that n_burnin=0is sufficient.n_keepThe number of MCMC draws, per chain, to keep for the model analysis and results. (Optional) Thin the posterior distribution for faster iterations.
To accelerate downstream tasks during iterative workflows (such as generating preliminary model-result reports or evaluating multiple budget optimization scenarios), you can downsample the posterior draws using
posterior_thinning(). This selects a chain-preserving subset of posterior draws across each MCMC chain using systematic sampling to minimize autocorrelation:# Recommended: 15% sampling rate balances fast iteration with # near-approximate inference mmm.posterior_thinning(sampling_rate=0.15, preserve_original=True) # Alternatively, specify the exact number of draws to keep per chain # mmm.posterior_thinning(n_draws=150, preserve_original=True)If
preserve_original=Trueis set, you can restore the full posterior at any time before final delivery:mmm.restore_full_posterior()Parameter Description sampling_rateThe fraction of draws to keep per chain in (0, 1]. Recommended:0.15(15%) for exploratory workflows. Exactly one ofsampling_rateorn_drawsmust be specified.n_drawsThe number of draws to keep per chain in [1, original_n_draws]. Exactly one ofsampling_rateorn_drawsmust be specified.methodThe draw selection method. Supports ThinningMethod.SYSTEMATIC(default).seedAn optional integer random seed for reproducible draw selection. preserve_originalA boolean flag (default True). IfTrue, saves a copy of the full posterior sorestore_full_posterior()can restore it.
Next, run modeling diagnostics to assess convergence, check the distributions, and assess the model fit.