SharedPosterior#
- class pymc_marketing.pytensor_utils.SharedPosterior[source]#
Posterior draws held in PyTensor shared variables.
extract_response_distribution()conditions a graph on posterior draws by substituting them as constants, which welds every graph – and every function compiled from it – to one posterior. Passing aSharedPosteriorinstead binds the draws through shared variables, soset_posterior()can point an already compiled function at a new set of draws without extracting or compiling again. That is what makes a loop over many posteriors (a resampled or updated posterior per iteration) affordable: the compile happens once.One instance can back several graphs. A variable is created the first time a graph needs it and reused by every later extraction that passes the same instance, so a single
set_posterior()call rebinds all of them at once. A later extraction must therefore be conditioned on the draws the instance already holds; handing it a different posterior raises, since the graph would otherwise silently read the bound draws.The contract for a rebind: every bound variable is present with the dtype it was bound with; every non-sample dimension keeps its name and its set of labels, compared by value (so a reordered
channelaxis is realigned, and a datetime axis may change storage unit); a dimension bound without labels must arrive without labels; the number of draws is free. Anything else is refused, and a rebind is atomic: every variable is validated and laid out before any is written.Examples
shared_posterior = SharedPosterior() graph = extract_response_distribution( model, idata, "channel_contribution", shared_posterior=shared_posterior ) fn = function([budgets], graph) # compiled once fn(x) # under ``idata`` shared_posterior.set_posterior(other_idata) fn(x) # under ``other_idata``, no recompile
Methods
Rebind every variable to the draws in
idata.Attributes
The dimension order each variable is stored in,
samplefirst.The shared variables, keyed by posterior variable name.