meridian_tools.log_likelihood
Log-likelihood computation and attachment for Meridian models.
Module: meridian_tools.log_likelihood
Functions
compute_log_likelihood_dataset
Compute the pointwise log-likelihood dataset for a fitted Meridian model.
This function reconstructs the joint distribution from the posterior samples and computes observation-level log-likelihood values. It handles both geo-panel and national models.
The reconstruction recovers unsaved posterior parameters (e.g. geo
deviations, tau_g_excl_baseline) that Meridian does not persist to
InferenceData by default.
Parameters:
meridian_model— A fitted Meridian model with posterior samples and a compatibleposterior_sampler_callable.
Returns: An xarray Dataset with a log_likelihood variable.
Raises: ModelSelectionError if the model does not expose the required
internal reconstruction seams or lacks posterior samples.
attach_log_likelihood
Attach a log_likelihood group to a Meridian model’s InferenceData.
If the model’s InferenceData already has a non-empty log_likelihood
group, it is returned as-is (or the existing InferenceData is returned
for in_place=True).
Parameters:
meridian_model— A fitted Meridian model.in_place— IfTrue, mutatesmeridian_model.inference_datadirectly. IfFalse(default), returns a deep copy with thelog_likelihoodgroup attached. The original model is never modified.
Returns: An ArviZ InferenceData with a log_likelihood group.
Raises:
ModelSelectionErrorwithreason_code="meridian_internal_seam_incompatible"if the Meridian version lacks the required private reconstruction methods.ModelSelectionErrorwithreason_code="requires_fitted_meridian_model"if the model has no posterior samples.ModelSelectionErrorwithreason_code="holdout_fit_unsupported"if the model was fitted with a holdout mask.
Example:
Implementation notes
The reconstruction accesses three private methods on Meridian’s
posterior_sampler_callable:
_get_joint_dist_unpinned_prepare_latents_for_reconstruction_reconstruct_posteriors
These are Meridian-internal and may change without notice. If any method is
missing, a ModelSelectionError with
reason_code="meridian_internal_seam_incompatible" is raised instead of
crashing. See the
Meridian integration notes for
details on this coupling boundary.