leaspy.algo.utils.samplers.abstract_sampler module

class AbstractSampler(info: Dict[str, Any], n_patients: int, *, acceptation_history_length: int = 25)

Bases: ABC

Abstract sampler class.

Parameters
infodict[str, Any]

The dictionary describing the random variable to sample. It should contains the following entries:

  • name : str

  • shape : tuple[int, …]

  • type : ‘population’ or ‘individual’

n_patientsint > 0

Number of patients (useful for individual variables)

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

Shape of variable

acceptation_history_lengthint

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

ind_param_dims_but_individualtuple[int, …], optional (only for individual variable)

The dimension(s) to aggregate when computing regularity of individual parameters For now there’s only one extra dimension whether it’s tau, xi or sources but in the future it could be extended. We do not sum first dimension (=0) which will always be the dimension reserved for individuals.

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.

maskUnion[None, torch.FloatTensor]

If not None, mask should be 0/1 tensor indicating the sampling variable to adapt variance from 1 indices are kept for sampling while 0 are excluded. <!> Only supported for population variables.

Methods

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

Sample new realization (either population or individual) for a given realization state, dataset, model and temperature

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

Sample new realization (either population or individual) for a given realization state, dataset, model 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.FloatTensor 0D (population variable) or 1D (individual variable, with length n_individuals)

The attachment and regularity (only for the current variable) at the end of this sampling step (globally or per individual, depending on variable type).