CompiledNumPyroBlock#
- class impulso.numpyro_block.CompiledNumPyroBlock(numpyro_site_names, site_shapes, recorded_names, log_density, compute_recorded_quantities, dimension_names, coordinate_labels, initial_point, latent_names=(), n_endog_rows=None)[source]#
Bases:
objectA VAR compiled once from its PyMC model, called inside a NumPyro model.
Built by VAR.compile_for_numpyro, through from_pymc_model; see VAR.compile_for_numpyro for what a call adds to the NumPyro model and for the limitations.
- Parameters:
n_endog_rows (int | None)
- numpyro_site_names#
NumPyro sample-site name of each PyMC value variable, keyed by the value variable’s name. A PyMC value variable is the unconstrained value of one free parameter of the PyMC model, and its site holds it on that scale.
- Type:
- site_shapes#
Shape of each site’s value, keyed by NumPyro site name.
- Type:
collections.abc.Mapping[str, tuple[int, …]]
- recorded_names#
Names under which a call records the recorded quantities with numpyro.deterministic, in the order of compute_recorded_quantities’s outputs.
- log_density#
JAX function from the value of every site (in numpyro_site_names order) and endog to a one-element sequence holding the VAR’s joint log-density, PyMC’s transform corrections included. JAX passes every term that the PyMC model gives a zero-gradient override, such as the latent_stationarity Potential, no gradient (see add_with_zero_gradient).
- Type:
collections.abc.Callable[[…], Any]
- compute_recorded_quantities#
JAX function from the same inputs to the recorded quantities, in recorded_names order.
- Type:
collections.abc.Callable[[…], Any]
- dimension_names#
Dimension names of each recorded quantity, keyed by its recorded name, without the sample dimensions chain and draw. They are the PyMC model’s own (B on (“var”, “coeff”); an intercept for a strict subset of equations on (“var_intercept”,)); a quantity the PyMC model gives no dimension names gets PyMC’s default ones, {name}_dim_0, {name}_dim_1, and so on, and the scalar nu gets none.
- Type:
collections.abc.Mapping[str, tuple[str, …]]
- coordinate_labels#
Labels along each dimension in dimension_names that the PyMC model labels, keyed by dimension name: the variable names on “var”, the lag-major L{lag}.{name} labels on “coeff”, the exogenous names on “exog”, and the equations that have an intercept on “var_intercept”. Together with dimension_names they are the PyMC model’s coords and dims for the recorded quantities. posterior_from_samples uses both.
- Type:
collections.abc.Mapping[str, tuple[str, …]]
- initial_point#
PyMC’s initial point of the VAR’s parameters, keyed by NumPyro site name, on PyMC’s unconstrained scale. Pass it to NumPyro’s init_to_value(values=…) to start the sampler where PyMC would.
- Type:
- latent_names#
Names of the latent series, in the column order of the recorded latent paths; empty without latent series.
- n_endog_rows#
Number of rows of the endog every call must receive, or None when a call accepts an endog of any length.
- Type:
int | None
- classmethod from_pymc_model(model, endog, handles, *, degrees_of_freedom=None, n_endog_rows=None)[source]#
Compile a VAR’s PyMC model, built by VAR.build_in_model with a symbolic endog, into a block.
VAR.compile_for_numpyro builds the model and calls this constructor.
- Parameters:
model (Any) – The pymc.Model that holds the VAR build_in_model registered, and nothing else.
endog (Any) – The symbolic endog that build_in_model received. Both compiled functions take it after the value of every site.
handles (VARModelHandles) – The handles build_in_model returned.
degrees_of_freedom (Any) – The PyMC variable nu, the degrees of freedom of the Student-t errors, under heavy-tailed errors; None under Gaussian errors.
n_endog_rows (int | None) – Number of rows of the endog every call of the block must receive, or None when a call accepts an endog of any length.
- Returns:
The compiled block.
- Return type:
- posterior_from_samples(samples)[source]#
Turn NumPyro samples of a model that calls this block into a posterior for FittedVAR.from_posterior.
NumPyro samples carry no dimension names, and the posterior schema checks B, intercept and B_exog by dimension name. This method names the sample axes chain and draw and gives each recorded quantity its dimension_names and coordinate_labels. The example in VAR.compile_for_numpyro shows the whole sequence, from compiling to impulse responses.
The posterior holds:
each recorded quantity, under its posterior-schema name. The intercept has an entry for every equation, on var: an equation that intercept_equations excluded gets zero, as in the PyMC model’s mean;
every other variable in samples, such as the NumPyro model’s own sample and deterministic sites, under its own name with each / replaced by ., and with PyMC’s default dimension names built from that name: {name}_dim_0, {name}_dim_1, and so on. ArviZ 1 refuses / in a variable name, and numpyro.handlers.scope(prefix=”user”) names a site user/scale. The posterior holds that site as user.scale under ArviZ 0 as well, so the names are the same under both. FittedVAR.from_posterior reads none of these variables.
It leaves out the block’s own sites: the sample sites in numpyro_site_names, which hold the VAR’s parameters on PyMC’s unconstrained scale and whose names contain /, which ArviZ 1 refuses in a variable name; and the log-density site.
Two arguments of FittedVAR.from_posterior are the caller’s to get right:
data, a VARData (ADR-0016): the series given to the block as endog, with the endog_names, exog and exog_names the block was compiled with. With latent series it also needs a plug-in column for each latent series, ahead of the observed ones, such as the posterior mean of latent.
error_dist=spec.resolved_error_dist, as in the example in VAR.compile_for_numpyro. Under Gaussian errors it makes no difference. Under Student-t errors, leaving it out makes FittedVAR.from_posterior accept the posterior as Gaussian, so forecasts draw Gaussian innovations.
- Parameters:
samples (Mapping[str, Any]) – NumPyro samples keyed by site name, each with a chain axis and a draw axis first: mcmc.get_samples(group_by_chain=True).
- Returns:
an arviz.InferenceData under ArviZ 0 and an xarray.DataTree under ArviZ 1, the container FittedVAR.from_posterior takes on each.
- Return type:
A container with one group, posterior
- Raises:
ValueError – If samples lacks a recorded quantity (as when the block was called inside a numpyro.handlers.scope), or a recorded quantity lacks its chain or draw axis (as from mcmc.get_samples() without group_by_chain=True).
ValueError – If two variables in samples other than the block’s own sites would get the same name in the posterior once each / is replaced by ., such as user/scale and user.scale.