leaspy.variables.distributions
This module defines the distributions used for sampling variables.
Attributes
Classes
Interface to represent stateless distribution families (i.e. no distribution parameters are stored in instance). |
|
Wrapper to build a StatelessDistributionFamily class from an existing torch distribution class. |
|
Bernoulli family (stateless). |
|
Normal / Gaussian family (stateless). |
|
Interface to represent stateless distribution families (i.e. no distribution parameters are stored in instance). |
|
Interface to represent stateless distribution families (i.e. no distribution parameters are stored in instance). |
|
Interface to represent stateless distribution families (i.e. no distribution parameters are stored in instance). |
|
Class providing symbolic methods for distribution families. |
Module Contents
- class StatelessDistributionFamily
Bases:
abc.ABCInterface to represent stateless distribution families (i.e. no distribution parameters are stored in instance).
TODO / WIP? allow WeightedTensor for parameters as well? (e.g. batched_deltas = Normal(batched_deltas_mean, …) which should be masked at some indices) –> mask at latent pop. variable level (batched_deltas) or
directly at model parameter level batched_deltas_mean?
- classmethod validate_parameters(*params)
-
Validate consistency of distribution parameters, returning them with out-of-place modifications if needed.
- classmethod shape(*params_shapes)
Shape of distribution samples (without any additional expansion), given shapes of distribution parameters.
- classmethod sample(*params, sample_shape=())
-
Sample values, given distribution parameters (sample_shape is prepended to shape of distribution parameters).
- classmethod mode(*params)
-
Mode of distribution (returning first value if discrete ties), given distribution parameters.
- classmethod mean(*params)
-
Mean of distribution (if defined), given distribution parameters.
- classmethod stddev(*params)
-
Standard-deviation of distribution (if defined), given distribution parameters.
- classmethod nll(x, *params)
Negative log-likelihood of value, given distribution parameters.
- Parameters:
x (WeightedTensor[float])
params (Tensor)
- Return type:
- classmethod regularization(x, *params)
Negative log-likelihood of value, given distribution parameters.
- Parameters:
- Return type:
- classmethod nll_jacobian(x, *params)
Jacobian w.r.t. value of negative log-likelihood, given distribution parameters.
- Parameters:
x (WeightedTensor[float])
params (Tensor)
- Return type:
- classmethod nll_and_jacobian(x, *params)
Negative log-likelihood of value and its jacobian w.r.t. value, given distribution parameters.
- Parameters:
x (WeightedTensor[float])
params (Tensor)
- Return type:
- class StatelessDistributionFamilyFromTorchDistribution
Bases:
StatelessDistributionFamilyWrapper to build a StatelessDistributionFamily class from an existing torch distribution class.
- dist_factory: ClassVar[Callable[Ellipsis, torch.distributions.Distribution]]
- classmethod validate_parameters(*params)
Validate consistency of distribution parameters, returning them with out-of-place modifications if needed.
- Parameters:
- paramsAny
The parameters to pass to the distribution factory.
- Returns:
tuple[torch.Tensor, …]The validated parameters.
- Parameters:
params (Any)
- Return type:
- classmethod sample(*params, sample_shape=())
Sample values, given distribution parameters (sample_shape is prepended to shape of distribution parameters).
- classmethod mode(*params)
-
Mode of distribution (returning first value if discrete ties), given distribution parameters.
- Parameters:
- params
torch.Tensor The distribution parameters.
- params
- Returns:
torch.TensorThe value of the distribution’s mode.
- Parameters:
params (Tensor)
- Return type:
- classmethod mean(*params)
Return the mean of the distribution, if defined.
- Parameters:
- params
torch.Tensor The distribution parameters.
- params
- Returns:
torch.TensorThe value of the distribution’s mean.
- Parameters:
params (Tensor)
- Return type:
- classmethod stddev(*params)
Return the standard-deviation of the distribution.
- Parameters:
- params
torch.Tensor The distribution parameters.
- params
- Returns:
torch.TensorThe value of the distribution’s standard deviation.
- Parameters:
params (Tensor)
- Return type:
- class BernoulliFamily
Bases:
StatelessDistributionFamilyFromTorchDistributionBernoulli family (stateless).
- parameters: ClassVar = ('loc',)
- dist_factory: ClassVar
- class NormalFamily
Bases:
StatelessDistributionFamilyFromTorchDistributionNormal / Gaussian family (stateless).
- parameters: ClassVar = ('loc', 'scale')
- dist_factory: ClassVar
- nll_constant_standard: ClassVar
- classmethod mode(loc, scale)
Return the mode of the distribution given the distribution’s loc and scale parameters.
- Parameters:
- loc
torch.Tensor The distribution loc.
- scale
torch.Tensor The distribution scale.
- loc
- Returns:
torch.TensorThe value of the distribution’s mode.
- Parameters:
- Return type:
- classmethod mean(loc, scale)
Return the mean of the distribution, given the distribution loc and scale parameters.
- Parameters:
- loc
torch.Tensor The distribution loc parameters.
- scale
torch.Tensor The distribution scale parameters.
- loc
- Returns:
torch.TensorThe value of the distribution’s mean.
- Parameters:
- Return type:
- classmethod stddev(loc, scale)
Return the standard-deviation of the distribution, given loc and scale of the distribution.
- Parameters:
- loc
torch.Tensor The distribution loc parameter.
- scale
torch.Tensor The distribution scale parameter.
- loc
- Returns:
torch.TensorThe value of the distribution’s standard deviation.
- Parameters:
- Return type:
- class AbstractWeibullRightCensoredFamily
Bases:
StatelessDistributionFamilyInterface to represent stateless distribution families (i.e. no distribution parameters are stored in instance).
TODO / WIP? allow WeightedTensor for parameters as well? (e.g. batched_deltas = Normal(batched_deltas_mean, …) which should be masked at some indices) –> mask at latent pop. variable level (batched_deltas) or
directly at model parameter level batched_deltas_mean?
- dist_weibull: ClassVar
- classmethod validate_parameters(*params)
-
Validate consistency of distribution parameters, returning them with out-of-place modifications if needed.
- Parameters:
- paramsAny
The parameters to pass to the distribution factory.
- Returns:
tuple[torch.Tensor, …]The validated parameters.
- Parameters:
params (Any)
- Return type:
- classmethod sample(nu, rho, xi, tau, sample_shape=())
Sample values, given distribution parameters (sample_shape is prepended to shape of distribution parameters).
- classmethod mode(*params)
-
Return the mode of the distribution (returning first value if discrete ties).
- Parameters:
- params
torch.Tensor The distribution parameters.
- params
- Returns:
torch.TensorThe value of the distribution’s mode.
- Parameters:
params (Tensor)
- Return type:
- classmethod mean(nu, rho, xi, tau)
Return the mean of the distribution, if defined.
- Parameters:
- nu
torch.Tensor - rho
torch.Tensor - xi
torch.Tensor - tau
torch.Tensor
- nu
- Returns:
torch.TensorThe value of the distribution’s mean.
- Parameters:
- Return type:
- classmethod stddev(nu, rho, xi, tau)
Return the standard-deviation of the distribution, given distribution parameters.
- Parameters:
- nu
torch.Tensor - rho
torch.Tensor - xi
torch.Tensor - tau
torch.Tensor
- nu
- Returns:
torch.TensorThe value of the distribution’s standard deviation.
- Parameters:
- Return type:
- classmethod compute_log_likelihood_hazard(x, nu, rho, xi, tau, *params)
- classmethod compute_hazard(x, nu, rho, xi, tau, *params)
- classmethod compute_log_survival(x, nu, rho, xi, tau, *params)
- class WeibullRightCensoredFamily
Bases:
AbstractWeibullRightCensoredFamilyInterface to represent stateless distribution families (i.e. no distribution parameters are stored in instance).
TODO / WIP? allow WeightedTensor for parameters as well? (e.g. batched_deltas = Normal(batched_deltas_mean, …) which should be masked at some indices) –> mask at latent pop. variable level (batched_deltas) or
directly at model parameter level batched_deltas_mean?
- parameters: ClassVar = ('nu', 'rho', 'xi', 'tau')
- class WeibullRightCensoredWithSourcesFamily
Bases:
AbstractWeibullRightCensoredFamilyInterface to represent stateless distribution families (i.e. no distribution parameters are stored in instance).
TODO / WIP? allow WeightedTensor for parameters as well? (e.g. batched_deltas = Normal(batched_deltas_mean, …) which should be masked at some indices) –> mask at latent pop. variable level (batched_deltas) or
directly at model parameter level batched_deltas_mean?
- parameters: ClassVar = ('nu', 'rho', 'xi', 'tau', 'survival_shifts')
- class SymbolicDistribution
Class providing symbolic methods for distribution families.
- dist_family: Type[StatelessDistributionFamily]
- validate_parameters: Callable[Ellipsis, tuple[Tensor, Ellipsis]]
Function of named distribution parameters, to validate these parameters.
- shape: Callable[Ellipsis, tuple[int, Ellipsis]]
Function of named shapes of distribution parameters, to get shape of distribution samples.
- mode: Callable[Ellipsis, Tensor]
Function of named distribution parameters, to get mode of distribution.
- mean: Callable[Ellipsis, Tensor]
Function of named distribution parameters, to get mean of distribution.
- stddev: Callable[Ellipsis, Tensor]
Function of named distribution parameters, to get std-deviation of distribution.
- get_func(func, *extra_args_names, **kws)
Get keyword-only function from the stateless distribution family.
- get_func_sample(sample_shape=())
Factory of symbolic sampling function.
- Parameters:
- Returns:
NamedInputFunctionThe sample function.
- Parameters:
- Return type:
- get_func_regularization(value_name)
Factory of symbolic function: state -> negative log-likelihood of value.
- Parameters:
- value_name
str
- value_name
- Returns:
NamedInputFunctionThe named input function to use to compute negative log likelihood.
- Parameters:
value_name (str)
- Return type:
- get_func_nll(value_name)
Factory of symbolic function: state -> negative log-likelihood of value.
- Parameters:
- value_name
str
- value_name
- Returns:
NamedInputFunctionThe named input function to use to compute negative log likelihood.
- Parameters:
value_name (str)
- Return type:
- get_func_nll_jacobian(value_name)
Factory of symbolic function: state -> jacobian w.r.t. value of negative log-likelihood.
- Parameters:
- value_name
str
- value_name
- Returns:
NamedInputFunctionThe named input function to use to compute negative log likelihood jacobian.
- Parameters:
value_name (str)
- Return type:
- get_func_nll_and_jacobian(value_name)
Factory of symbolic function: state -> (negative log-likelihood, its jacobian w.r.t. value).
- Parameters:
- value_name
str
- value_name
- Returns:
tuple[NamedInputFunction,NamedInputFunction]The named input functions to use to compute negative log likelihood and its jacobian.
- Parameters:
value_name (str)
- Return type:
NamedInputFunction[tuple[WeightedTensor[float], WeightedTensor[float]]]
- classmethod bound_to(dist_family)
Return a factory to create SymbolicDistribution bound to the provided distribution family.
- Parameters:
- dist_family
StatelessDistributionFamily The distribution family to use to create a SymbolicDistribution.
- dist_family
- Returns:
- factoryCallable[…,
SymbolicDistribution] The factory.
- factoryCallable[…,
- Parameters:
dist_family (Type[StatelessDistributionFamily])
- Return type:
Callable[Ellipsis, SymbolicDistribution]
- Normal
- Bernoulli
- WeibullRightCensored
- WeibullRightCensoredWithSources