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](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/mcmc/windowed_adaptive_nuts)
sampling method with step size and kernel adaptation.

To run the model:

1. 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=None,
            rf_prior_type='coefficient',
            rf_roi_calibration=None,
            organic_media_prior_type='contribution',
            organic_rf_prior_type='contribution',
            non_media_treatments_prior_type='contribution',
            knots=None,
            baseline_geo=None,
            holdout=None,
            population_scaled_controls=None,
            adstock_decay_spec='geometric',
            enable_aks=False,
        )
        ```

2. 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)

   > [!NOTE]
   > **Tip:** Meridian uses the JAX backend by default for MCMC sampling. Learn about precision options, explicit random seeds, and performance considerations in [Use the JAX backend](https://developers.google.com/meridian/docs/advanced-modeling/using-jax).

   | Parameter | Description |
   |---|---|
   | `n_chains` | The 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_chains` argument for a call to `windowed_adaptive_nuts`. |
   | `n_adapt` | The number of MCMC draws per chain, during which step size and kernel are adapted. These draws are always excluded. |
   | `n_burnin` | An 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=0` is sufficient. |
   | `n_keep` | The number of MCMC draws, per chain, to keep for the model analysis and results. |

3. **(Optional) Thin the posterior distribution for faster iterations.**

   > [!CAUTION]
   > **Important:** Posterior thinning is intended strictly for **exploratory analysis, rapid prototyping, and iterations** . Do **not** use thinned posteriors for final client deliverables, official reporting, or production deployment. Always restore or retain the full posterior for production runs.

   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=True` is set, you can restore the full posterior at
   any time before final delivery:

       mmm.restore_full_posterior()

   | Parameter | Description |
   |---|---|
   | `sampling_rate` | The fraction of draws to keep per chain in `(0, 1]`. **Recommended: `0.15` (15%)** for exploratory workflows. Exactly one of `sampling_rate` or `n_draws` must be specified. |
   | `n_draws` | The number of draws to keep per chain in `[1, original_n_draws]`. Exactly one of `sampling_rate` or `n_draws` must be specified. |
   | `method` | The draw selection method. Supports `ThinningMethod.SYSTEMATIC` (default). |
   | `seed` | An optional integer random seed for reproducible draw selection. |
   | `preserve_original` | A boolean flag (default `True`). If `True`, saves a copy of the full posterior so `restore_full_posterior()` can restore it. |

   > [!NOTE]
   > **Downsampling Guidance:**
   > - **Recommended default (15% sampling rate):** A **15% sampling rate** (or retaining \~1,000--1,500 total draws across chains) is the recommended balance for exploratory workflows. It substantially decreases execution time and memory usage for downstream reporting and optimization while closely approximating macro summary metrics.
   > - **When to use:** Use strictly during early model exploration, rapid prototyping, and rapid iterations.
   > - **Outputs that degrade first:** Credible interval widths and tail quantiles for low-spend or noisy channels degrade first under aggressive downsampling. Always restore or use the full posterior for final client deliverables and production baseline runs.

Next, run modeling diagnostics to [assess convergence, check the distributions,
and assess the model fit](https://developers.google.com/meridian/docs/user-guide/model-diagnostics).