Skip to content

mixdyn.inference

Posterior sampling for the engine's Bayesian models, and their comparison.

LogDensity = Callable[[_Array], tuple[float, _Array]]

A log density on the unconstrained space, up to a constant, and its gradient.

Diagnostics(rhat, ess_bulk, ess_tail) dataclass

Per quantity, rhat, ess_bulk and ess_tail.

rhat instance-attribute

ess_bulk instance-attribute

ess_tail instance-attribute

HealthCard(max_rhat, ppp, ppp_se, total, totals, negative_baseline, negative_baseline_periods, shift, width, consistency) dataclass

The health of a Bayesian fit of one geo (mixdyn.models.bayes_mmm), each cell its number.

  • max_rhat: the largest R-hat over every parameter of the fit, a constant's nan left out.
  • ppp: the two-sided posterior predictive p-value of the KPI's total over the fitted periods, min(1, 2 min(P(T >= t), P(T <= t))), t the observed total and T a replicate's, one replicate at each of the posterior's draws, drawn about the draw's mean by the model's noise; ppp_se its Monte Carlo standard error from the draw count, 2 sqrt(q (1 - q) / S), q the smaller tail's share of S replicates, which reads 0 where no replicate is past the total: ppp is then below 2 / S. totals holds the replicates and total the observed total.
  • negative_baseline: the share of draws whose baseline, the intercept, calendar and controls without the channels (the level less media, ADR 0036), is below 0 at some period of the window; negative_baseline_periods that share at each period.
  • per channel, of its return as ADR 0016 defines it, the window's spend's revenue with its carryover counted, read at draws of the prior and of the posterior: shift, the posterior mean less the prior's over the prior's standard deviation; width, the posterior's standard deviation over the prior's; and consistency, the prior's quantile of the posterior median, the share of prior draws at or below it, near 0 or 1 where the posterior sits in the prior's tail.

No cell is a verdict: flags reads each against a threshold the caller passes.

max_rhat instance-attribute

ppp instance-attribute

ppp_se instance-attribute

total instance-attribute

totals instance-attribute

negative_baseline instance-attribute

negative_baseline_periods instance-attribute

shift instance-attribute

width instance-attribute

consistency instance-attribute

flags(*, max_rhat=None, min_ppp=None, max_negative_baseline=None, max_width=None, tail=None)

Whether each cell passes the threshold given for it, keyed by the cell's name, a channel's cells as "width[tv]" and "consistency[tv]"; a cell whose threshold is not given is not read. max_rhat, max_negative_baseline and max_width pass a cell below them, min_ppp one at or above it, and tail a consistency inside [tail, 1 - tail].

Meridian's model reviewer (Google, Meridian 2.x) reads its fits at a largest R-hat below 1.2, a posterior predictive p-value of 0.05 or more and a probability of a negative baseline below 0.2: flags(max_rhat=1.2, min_ppp=0.05, max_negative_baseline=0.2) reads this card at its thresholds, which are an example, not a default.

Loo(elpd, se, p_loo, pointwise, pareto_k, k_threshold) dataclass

pointwise is each observation's log predictive density left out, elpd their sum and se its standard error; p_loo the log predictive density in sample less elpd, the effective number of parameters; pareto_k each observation's fitted shape, and k_threshold the shape above which its estimate is not to be trusted.

elpd instance-attribute

se instance-attribute

p_loo instance-attribute

pointwise instance-attribute

pareto_k instance-attribute

k_threshold instance-attribute

Psis(log_weights, pareto_k, k_threshold, ess) dataclass

Pareto-smoothed importance weights: log_weights normalised, pareto_k the fitted shape of the ratios' tail, infinite where there is none to fit, k_threshold the shape above which the weights are not to be trusted, and ess Kish's effective sample size of the weights, 1 / sum w^2, times the draws' relative efficiency.

log_weights instance-attribute

pareto_k instance-attribute

k_threshold instance-attribute

ess instance-attribute

Chains(positions, log_density, accept, depth, leapfrogs, divergent, energy, step, inverse_metric) dataclass

Each chain's draws after warm-up, (chains, draws) leading, with what each transition did, and each chain's step size and inverse metric as warm-up left them.

positions instance-attribute

log_density instance-attribute

accept instance-attribute

depth instance-attribute

leapfrogs instance-attribute

divergent instance-attribute

energy instance-attribute

step instance-attribute

inverse_metric instance-attribute

psis(log_ratios, *, efficiency=1.0)

Pareto-smoothed importance sampling of (S,) log importance ratios; efficiency is the draws' relative efficiency r_eff, which sets the tail's length and scales the effective sample size.

Raises:

Type Description
ValueError

on another shape, no ratio, a ratio not finite, or an efficiency not positive and finite.

stacking(pointwise)

The stacking weights of models, pointwise each one's log predictive density of each observation left out, (models, observations).

Raises:

Type Description
ValueError

on another shape or a log density not finite.

sample(log_density, initial, *, seed, warmup=1000, draws=1000, max_depth=10, target=0.8)

NUTS chains from initial, one row a chain, each adapting its step size and diagonal metric over warmup transitions and keeping the draws after; chain c draws its random numbers from numpy.random.default_rng([seed, c + 1]), so that no chain reads default_rng(seed)'s stream, which a caller may draw the starts from.

Raises:

Type Description
ValueError

on settings out of range, a start where the log density or its gradient is not finite, or a density whose step size search fails.

loo(log_likelihood)

log_likelihood is each draw's log density of each observation, (chains, draws, observations).

Raises:

Type Description
ValueError

on another shape, fewer than four draws a chain, or a log density not finite.