leaspy.samplers.AbstractSampler

class AbstractSampler(name: str, shape: Tuple[int, ...], *, acceptation_history_length: int = 25)

Bases: ABC

Abstract sampler class.

Parameters:
namestr

The name of the random variable to sample.

shapetuple of int

The shape of the random variable to sample.

acceptation_history_lengthint > 0 (default 25)

Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (It is the same for population or individual variables.)

Raises:
LeaspyModelInputError
Attributes:
namestr

Name of variable

shapetuple of int

Shape of variable

acceptation_history_lengthint

Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (Same for population or individual variables by default.)

acceptation_historytorch.Tensor

History of binary acceptations to compute mean acceptation rate for the sampler in MCMC-SAEM algorithm. It keeps the history of the last acceptation_history_length steps.

Methods

sample(dataset, model, realizations, ...)

Sample new realization (either population or individual) for a given CollectionRealization state, Dataset, AbstractModel, and temperature.

abstract sample(dataset: Dataset, model: AbstractModel, realizations: CollectionRealization, temperature_inv: float, **attachment_computation_kws) Tuple[Tensor, Tensor]

Sample new realization (either population or individual) for a given CollectionRealization state, Dataset, AbstractModel, and temperature.

<!> Modifies in-place the realizations object, <!> as well as the model through its update_MCMC_toolbox for population variables.

Parameters:
datasetDataset

Dataset class object build with leaspy class object Data, model & algo

modelAbstractModel

Model for loss computations and updates

realizationsCollectionRealization

Contain the current state & information of all the variables of interest

temperature_invfloat > 0

Inverse of the temperature used in tempered MCMC-SAEM

**attachment_computation_kws

Optional keyword arguments for attachment computations. As of now, we only use it for individual variables, and only attribute_type. It is used to know whether to compute attachments from the MCMC toolbox (esp. during fit) or to compute it from regular model parameters (esp. during personalization in mean/mode realization)

Returns:
attachment, regularity_vartorch.Tensor

The attachment and regularity tensors (only for the current variable) at the end of this sampling step (globally or per individual, depending on variable type). The tensors are 0D for population variables, or 1D for individual variables (with length n_individuals).

abstract property shape_acceptation: Tuple[int, ...]

Return the shape of acceptation tensor for a single iteration.

Returns:
tuple of int

The shape of the acceptation history.