leaspy.samplers¶
Submodules¶
Attributes¶
Classes¶
Abstract class for samplers of individual random variables. |
|
Abstract class for samplers of population random variables. |
|
Abstract sampler class. |
|
Gibbs sampler for individual variables. |
|
Fast Gibbs sampler for population variables. |
|
Gibbs sampler for population variables. |
|
Metropolis-Hastings sampler for population variables. |
Functions¶
|
Factory for Samplers. |
Package Contents¶
- class AbstractIndividualSampler(name, shape, *, n_patients, acceptation_history_length=25)[source]¶
Bases:
AbstractSamplerAbstract class for samplers of individual random variables.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- n_patients
int Number of patients.
- acceptation_history_length
int> 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.)
- name
- Attributes:
- name
str Name of variable
- shape
tupleofint Shape of variable
- n_patients
int Number of patients.
- acceptation_history_length
int Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (It is the same for population or individual variables.)
- acceptation_history
torch.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.
- name
- Parameters:
- n_patients¶
- class AbstractPopulationSampler(name, shape, *, acceptation_history_length=25, mask=None)[source]¶
Bases:
AbstractSamplerAbstract class for samplers of population random variables.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- acceptation_history_length
int> 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.)
- mask
torch.Tensor, optional A binary (0/1) tensor indicating which elements to sample. Elements with value 1 (True) are included in the sampling; elements with 0 (False) are excluded.
- name
- Attributes:
- name
str Name of variable
- shape
tupleofint Shape of variable
- acceptation_history_length
int Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (It is the same for population or individual variables.)
- acceptation_history
torch.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.
- mask
torch.Tensorofbool, optional A binary (0/1) tensor indicating which elements to sample. Elements with value 1 (True) are included in the sampling; elements with 0 (False) are excluded.
- name
- Parameters:
- mask = None¶
- class AbstractSampler(name, shape, *, acceptation_history_length=25)[source]¶
Bases:
abc.ABCAbstract sampler class.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- acceptation_history_length
int> 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.)
- name
- Attributes:
- name
str Name of variable
- shape
tupleofint Shape of variable
- acceptation_history_length
int Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (Same for population or individual variables by default.)
- acceptation_history
torch.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.
- name
- Raises:
- Parameters:
- name¶
- shape¶
- acceptation_history_length = 25¶
- acceptation_history¶
- property shape_acceptation: Tuple[int, Ellipsis]¶
- Abstractmethod:
- Return type:
Tuple[int, Ellipsis]
Return the shape of acceptation tensor for a single iteration.
- INDIVIDUAL_SAMPLERS¶
- POPULATION_SAMPLERS¶
- sampler_factory(sampler, variable_type, **kwargs)[source]¶
Factory for Samplers.
- Parameters:
- sampler
AbstractSamplerorstr If an instance of a subclass of
AbstractSampler, returns the instance. If a string, returns a new instance of the appropriate class (with optional parameters kwargs).- variable_type
VariableType The type of random variable that the sampler is supposed to sample.
- **kwargs
Optional parameters for initializing the requested Sampler (not used if input is a subclass of
AbstractSampler).
- sampler
- Returns:
AbstractSamplerThe desired sampler.
- Raises:
LeaspyAlgoInputError:If the sampler provided is not supported.
- Parameters:
sampler (SamplerFactoryInput)
- Return type:
- class IndividualGibbsSampler(name, shape, *, n_patients, scale, random_order_dimension=True, mean_acceptation_rate_target_bounds=(0.2, 0.4), adaptive_std_factor=0.1, **base_sampler_kws)[source]¶
Bases:
GibbsSamplerMixin,leaspy.samplers.base.AbstractIndividualSamplerGibbs sampler for individual variables.
Individual variables are handled using a grouped Gibbs sampler. Currently, this is the only sampler implemented for individual variables.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- scale
float> 0 ortorch.FloatTensor> 0 An approximate scale for the variable. It will be used to scale the initial adaptive std-dev used in sampler. An extra factor will be applied on top of this scale :
1% for population parameters (
AbstractPopulationGibbsSampler.STD_SCALE_FACTOR)50% for individual parameters (
IndividualGibbsSampler.STD_SCALE_FACTOR)
Note: If a
torch.Tensoris passed, its shape must be compatible with the shape of the variable.- random_order_dimension
bool(default True) Whether to randomize the order of dimensions during the sampling loop. This is applied only to population variables as group sampling is used for individual variables. Article gives a reason on why we should activate this flag.
- mean_acceptation_rate_target_bounds
tuple[lower_bound:float, upper_bound:float] with 0 < lower_bound < upper_bound < 1 Bounds on the mean acceptation rate. If the rate is outside this range, the adaptive std-dev will be updated to maintain the target acceptance rate. (e.g: ~30%).
- adaptive_std_factor
floatin (0, 1) The factor by which the sampler’s std-dev is adapted:
If acceptance rate is too low → decrease by
1 - factorIf too high → increase by
1 + factor
- **base_sampler_kws
Additional keyword arguments passed to
AbstractSamplerinit method. For example, you may pass acceptation_history_length.
- name
- Parameters:
- STD_SCALE_FACTOR = 0.5¶
- validate_scale(scale)[source]¶
Validate the provided scale.
- Parameters:
- scale
floatortorch.Tensor The scale to be validated.
- scale
- Returns:
torch.TensorValid scale.
- Parameters:
- Return type:
- sample(state, *, temperature_inv)[source]¶
Apply a grouped Metropolis-Hastings (MH) sampling step for individual variables.
- For each individual variable:
Compute the current attachment and regularity.
Propose a new value for the variable.
Recompute the attachment and regularity.
Accept or reject the proposal using the MH criterion, based on the inverse temperature.
- Parameters:
- Parameters:
- Return type:
None
Notes
Population variables remain fixed during this step since we are updating individual variables. However:
In fit, population variables might have just been updated, and their effects not yet propagated to the model. In this case, model computations should use the current MCMC state (default behavior).
In personalization (mode or mean posterior), population variables are not updated. Therefore, the MCMC state should not be used.
- class PopulationFastGibbsSampler(name, shape, *, scale, random_order_dimension=True, mean_acceptation_rate_target_bounds=(0.2, 0.4), adaptive_std_factor=0.1, **base_sampler_kws)[source]¶
Bases:
AbstractPopulationGibbsSamplerFast Gibbs sampler for population variables.
Note
The sampling batches along the dimensions except the first one. This accelerates sampling process for 2D parameters.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- scale
float> 0 ortorch.FloatTensor> 0 An approximate scale for the variable. It will be used to scale the initial adaptive std-dev used in sampler. An extra factor will be applied on top of this scale :
1% for population parameters (
AbstractPopulationGibbsSampler.STD_SCALE_FACTOR)50% for individual parameters (
IndividualGibbsSampler.STD_SCALE_FACTOR)
Note: If a
torch.Tensoris passed, its shape must be compatible with the shape of the variable.- random_order_dimension
bool(default True) Whether to randomize the order of dimensions during the sampling loop. This is applied only to population variables as group sampling is used for individual variables. Article gives a reason on why we should activate this flag.
- mean_acceptation_rate_target_bounds
tuple[lower_bound:float, upper_bound:float] with 0 < lower_bound < upper_bound < 1 Bounds on the mean acceptation rate. If the rate is outside this range, the adaptive std-dev will be updated to maintain the target acceptance rate. (e.g: ~30%).
- adaptive_std_factor
floatin (0, 1) The factor by which the sampler’s std-dev is adapted:
If acceptance rate is too low → decrease by
1 - factorIf too high → increase by
1 + factor
- **base_sampler_kws
Additional keyword arguments passed to
AbstractSamplerinit method. For example, you may pass acceptation_history_length.
- name
- Parameters:
- class PopulationGibbsSampler(name, shape, *, scale, random_order_dimension=True, mean_acceptation_rate_target_bounds=(0.2, 0.4), adaptive_std_factor=0.1, **base_sampler_kws)[source]¶
Bases:
AbstractPopulationGibbsSamplerGibbs sampler for population variables.
The sampling is done iteratively for all coordinate values.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- scale
float> 0 ortorch.FloatTensor> 0 An approximate scale for the variable. It will be used to scale the initial adaptive std-dev used in sampler. An extra factor will be applied on top of this scale :
1% for population parameters (
AbstractPopulationGibbsSampler.STD_SCALE_FACTOR)50% for individual parameters (
IndividualGibbsSampler.STD_SCALE_FACTOR)
Note: If a
torch.Tensoris passed, its shape must be compatible with the shape of the variable.- random_order_dimension
bool(default True) Whether to randomize the order of dimensions during the sampling loop. This is applied only to population variables as group sampling is used for individual variables. Article gives a reason on why we should activate this flag.
- mean_acceptation_rate_target_bounds
tuple[lower_bound:float, upper_bound:float] with 0 < lower_bound < upper_bound < 1 Bounds on the mean acceptation rate. If the rate is outside this range, the adaptive std-dev will be updated to maintain the target acceptance rate. (e.g: ~30%).
- adaptive_std_factor
floatin (0, 1) The factor by which the sampler’s std-dev is adapted:
If acceptance rate is too low → decrease by
1 - factorIf too high → increase by
1 + factor
- **base_sampler_kws
Additional keyword arguments passed to
AbstractSamplerinit method. For example, you may pass acceptation_history_length.
- name
- Parameters:
- class PopulationMetropolisHastingsSampler(name, shape, *, scale, random_order_dimension=True, mean_acceptation_rate_target_bounds=(0.2, 0.4), adaptive_std_factor=0.1, **base_sampler_kws)[source]¶
Bases:
AbstractPopulationGibbsSamplerMetropolis-Hastings sampler for population variables.
Note
The sampling is implemented for all values at once. This accelerates sampling process but usually requires more iterations.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- scale
float> 0 ortorch.FloatTensor> 0 An approximate scale for the variable. It will be used to scale the initial adaptive std-dev used in sampler. An extra factor will be applied on top of this scale :
1% for population parameters (
AbstractPopulationGibbsSampler.STD_SCALE_FACTOR)50% for individual parameters (
IndividualGibbsSampler.STD_SCALE_FACTOR)
Note: If a
torch.Tensoris passed, its shape must be compatible with the shape of the variable.- random_order_dimension
bool(default True) Whether to randomize the order of dimensions during the sampling loop. This is applied only to population variables as group sampling is used for individual variables. Article gives a reason on why we should activate this flag.
- mean_acceptation_rate_target_bounds
tuple[lower_bound:float, upper_bound:float] with 0 < lower_bound < upper_bound < 1 Bounds on the mean acceptation rate. If the rate is outside this range, the adaptive std-dev will be updated to maintain the target acceptance rate. (e.g: ~30%).
- adaptive_std_factor
floatin (0, 1) The factor by which the sampler’s std-dev is adapted:
If acceptance rate is too low → decrease by
1 - factorIf too high → increase by
1 + factor
- **base_sampler_kws
Additional keyword arguments passed to
AbstractSamplerinit method. For example, you may pass acceptation_history_length.
- name
- Parameters: