Sample

class alan.Sample[source]

Constructed by calling Problem.sample, and represents K samples for each latent variable from the approximate posterior.

elbo_vi(computation_strategy=checkpoint)[source]

The ELBO, returned in a form that makes sense for doing VI (i.e. with reparameterised sampling).

Keyword Arguments:

computation_strategy – (see Computation Strategy)

elbo_rws(computation_strategy=checkpoint)[source]

The ELBO, returned in a form that makes sense for doing RWS (i.e. with non-reparameterised sampling).

Keyword Arguments:

computation_strategy – (see Computation Strategy)

elbo_nograd(computation_strategy=checkpoint)[source]

The ELBO, with no gradients at all (e.g. useful for reducing memory consumption when trying to estimate the model evidence). Note that checkpoint vs no checkpoint won’t make a difference here, as there are no gradients anyway. But split will make a difference as it will reduce the size of the largest tensors being computed.

Keyword Arguments:

computation_strategy – (see Computation Strategy)

importance_sample(self, N: int, computation_strategy=checkpoint)[source]

Uses importance sampling on all combinations of latent variables to draw N posterior samples, and returns them as an ImportanceSample object.

If you’re just trying to get posterior moments, this is a bad idea, as the importance sampling adds extra randomness. Instead, you should just use sample.moments or sample.marginals(). However, this is necessary e.g. for predictive log-likelihoods.

Parameters:

N (int) – The number of samples to draw.

Keyword Arguments:

computation_strategy – (see Computation Strategy)

marginals(joints=(), computation_strategy=<alan.Split.Checkpoint object>)[source]

Returns a marginals object.

Using the sample.moments can be annoying for interactive use, as it must e.g. compute and propagate log-probabilities each time you ask for a new moment, which can be slow with larger models / larger K. Using marginals speeds things up in this case. In particular, the marginals object represents the marginal distribution over the K particles for each latent variable. That allows you to compute new moments without needign to e.g. compute and propagate log-probabilities.

Keyword Arguments:
  • joints (list[tuple]) – By default, marginals will compute all univariate marginals, which allows you to compute all moments that depend on only a single variable. However, that doesn’t allow you to compute marginals that depends on multiple variables. To do that, you include the relevant combination of variables in joints. For instance, if we wanted to compute the covariance of a and b, we’d use joints = [('a', 'b')]. Note that if a random variable is in a group, you should give the name of the group, not the name of the variable.

  • computation_strategy – (see Computation Strategy)

update_qem_params(lr: float, computation_strategy=<alan.Split.NoCheckpoint object>)[source]
moments()

See Moments