PerturbationDSGEPosterior#

class cultivars.multivariate.structural.perturbation.PerturbationDSGEPosterior(*, data, parameter_names, theta_draws, loglik_draws, log_posterior_draws, acceptance_rate, order, n_particles, nobs, n_draws, n_burn, thin, _engine, _unconstrained)[source]#

A particle-chain posterior over a perturbation model.

Variables:
  • data (NDArray[double]) – The observables.

  • parameter_names (tuple[str, ...]) – Structural parameter labels.

  • theta_draws (NDArray[double]) – (S, d) kept draws in the model’s own space.

  • loglik_draws (NDArray[double]) – (S,) particle likelihood estimates carried with each kept draw.

  • log_posterior_draws (NDArray[double]) – (S,) log prior plus log-likelihood estimate at each kept draw, in the chain’s unconstrained space.

  • acceptance_rate (float) – Acceptance rate over the whole run.

  • order (int) – Perturbation order.

  • n_particles (int) – Particles per likelihood estimate.

  • nobs (int) – Observations.

  • n_draws (int) – Total iterations.

  • n_burn (int) – Burn-in discarded.

  • thin (int) – Post-burn thinning.

The draws are stored twice. theta_draws holds them in the model’s own, bounded space and is what every summary reads. _unconstrained holds the same draws in the space the chain moved in, which is the space log_posterior_draws is a density on and therefore the space the marginal likelihood is computed in.

The record keeps a reference to the engine that produced it, because everything beyond the draws (solution, state_space, impulse_responses, simulate) solves the model again at a parameter value. None of it is cached.

See also

Example

A latent AR(1) written as a one-state model, with a chain far too short for inference:

>>> import numpy as np
>>> from cultivars.multivariate.structural.perturbation import PerturbationDSGE
>>> class Latent:
...     parameter_names, bounds = ("rho", "sigma"), ((0.0, 0.99), (0.01, 1.0))
...     n_states, n_controls = 1, 1
...
...     def equations(self, th, y_next, y, x_next, x):
...         return np.array([y[0] - x[0], x_next[0] - th[0] * x[0]])
...
...     def steady_state(self, th):
...         return np.zeros(1), np.zeros(1)
...
...     def shock_loading(self, th):
...         return np.array([[th[1]]])
...
...     def observation(self, th):
...         return np.array([[0.0, 1.0]]), np.zeros(1), np.array([[1e-4]])
...
...     def log_prior(self, th):
...         return 0.0
>>> rng = np.random.default_rng(0)
>>> x = np.zeros(80)
>>> for t in range(1, 80):
...     x[t] = 0.8 * x[t - 1] + 0.5 * rng.standard_normal()
>>> model = PerturbationDSGE(Latent(), x, order=2)
>>> start = model.fit([0.5, 0.3]).theta
>>> post = model.sample(start, n_particles=50, n_draws=30, n_burn=10, seed=0)
>>> isinstance(post, _PerturbationDSGEPosterior), post.n_kept, list(post.params)
(True, 20, ['rho', 'sigma'])
>>> states, controls = post.impulse_responses(horizon=5, n_draws=4, seed=0)
>>> states.shape, controls.shape
((4, 1, 5, 1), (4, 1, 5, 1))

Attributes#

data

The observables, (n, p); kept out of the repr.

parameter_names

Structural parameter labels, in the column order of theta_draws.

theta_draws

Kept draws in the model's own, constrained space, (S, d).

loglik_draws

Particle log-likelihood estimate carried with each kept draw, (S,).

log_posterior_draws

Log prior plus log-likelihood estimate at each kept draw, (S,).

acceptance_rate

Acceptance rate of the chain over the whole run, burn-in included.

order

Perturbation order of the solution the likelihood was evaluated on.

n_particles

Particles per likelihood estimate.

nobs

Number of observations \(n\).

n_draws

Total chain iterations, burn-in included.

n_burn

Iterations discarded as burn-in.

thin

Thinning interval applied after burn-in.

n_kept

Posterior draws retained.

params

The posterior mean by name.

solution

The perturbation solution at the posterior mean.

state_space

The solution at the posterior mean on its substrate.

theta_mean

The posterior mean.

Methods#

convergence(*chains[, include, exclude, ...])

Assess the sampler's convergence over the retained draws.

impulse_responses(*[, horizon, size, ...])

Posterior draws of the responses.

marginal_likelihood(*[, tau])

Geweke's modified harmonic mean of the log marginal likelihood.

simulate([n, seed, burn, draw])

A fresh sample of the observables at the posterior mean or at one kept draw.

summary()

The estimation summary, renderable as text, HTML, or a dataframe.