leaspy.variables.distributions¶
This module defines the distributions used for sampling variables.
Attributes¶
Classes¶
Abstract base class defining a stateless interface for distribution families. |
|
Wrapper to build a StatelessDistributionFamily class from an existing torch distribution class. |
|
Bernoulli family (stateless). |
|
Normal / Gaussian family (stateless). |
|
Multivariate Normal family with diagonal covariance (stateless). |
|
A stateless mixture of univariate normal distributions. |
|
Abstract base class defining a stateless interface for distribution families. |
|
Abstract base class defining a stateless interface for distribution families. |
|
Abstract base class defining a stateless interface for distribution families. |
|
Class providing symbolic methods for distribution families. |
Module Contents¶
- class StatelessDistributionFamily[source]¶
Bases:
abc.ABCAbstract base class defining a stateless interface for distribution families.
This class represents a family of probability distributions in a stateless manner: no parameters are stored in the instance. All methods operate purely via classmethods, using explicitly passed distribution parameters.
Notes
Subclasses must define the parameters class variable, listing parameter names in order.
Each method operates solely on the passed tensors; no state or caching is assumed.
- classmethod validate_parameters(*params)[source]¶
-
Validate consistency of distribution parameters, returning them with out-of-place modifications if needed.
- classmethod shape(*params_shapes)[source]¶
Shape of distribution samples (without any additional expansion), given shapes of distribution parameters.
- Parameters:
- Returns:
- Raises:
LeaspyInputErrorIf the number of provided shapes does not match the expected number of parameters.
NotImplementedErrorIf the distribution has no parameters, and sample shape cannot be inferred.
- Parameters:
- Return type:
- classmethod sample(*params, sample_shape=())[source]¶
-
Sample values, given distribution parameters (sample_shape is prepended to shape of distribution parameters).
- classmethod mode(*params)[source]¶
-
Mode of distribution (returning first value if discrete ties), given distribution parameters.
- classmethod mean(*params)[source]¶
-
Mean of distribution (if defined), given distribution parameters.
- classmethod stddev(*params)[source]¶
-
Standard-deviation of distribution (if defined), given distribution parameters.
- classmethod nll(x, *params)[source]¶
Negative log-likelihood of value, given distribution parameters.
- Parameters:
x (WeightedTensor[float])
params (Tensor)
- Return type:
- classmethod regularization(x, *params)[source]¶
Negative log-likelihood of value, given distribution parameters.
- Parameters:
- Return type:
- classmethod nll_jacobian(x, *params)[source]¶
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)[source]¶
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[source]¶
Bases:
StatelessDistributionFamilyWrapper to build a StatelessDistributionFamily class from an existing torch distribution class.
- Attributes:
- dist_factory
Callable[…,torch.distributions.Distribution] A class variable that points to a factory function or class used to instantiate the corresponding PyTorch distribution.
- dist_factory
- dist_factory: ClassVar[Callable[Ellipsis, torch.distributions.Distribution]]¶
- classmethod validate_parameters(*params)[source]¶
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=())[source]¶
Sample values, given distribution parameters (sample_shape is prepended to shape of distribution parameters).
- Parameters:
- params
torch.Tensor The distribution parameters.
- sample_shape
tupleofint, optional Desired shape of the sample batch (prepended to the shape of a single sample). Defaults to an empty tuple, i.e., a single sample.
- params
- Returns:
torch.TensorSampled tensor
- Parameters:
- Return type:
- classmethod mode(*params)[source]¶
-
Mode of distribution (returning first value if discrete ties), given distribution parameters.
This method should be overridden in subclasses that wrap torch distributions which explicitly define a mode.
- Parameters:
- params
torch.Tensor The distribution parameters.
- params
- Returns:
torch.TensorThe value of the distribution’s mode.
- Raises:
NotImplementedErrorIf the mode is not explicitly implemented for the specific distribution subclass.
- Parameters:
params (Tensor)
- Return type:
- classmethod mean(*params)[source]¶
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)[source]¶
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[source]¶
Bases:
StatelessDistributionFamilyFromTorchDistributionBernoulli family (stateless).
Inherits from StatelessDistributionFamilyFromTorchDistribution.
- parameters: ClassVar = ('loc',)¶
- dist_factory: ClassVar¶
- class NormalFamily[source]¶
Bases:
StatelessDistributionFamilyFromTorchDistributionNormal / Gaussian family (stateless).
Inherits from StatelessDistributionFamilyFromTorchDistribution.
- parameters: ClassVar = ('loc', 'scale')¶
- dist_factory: ClassVar¶
- nll_constant_standard: ClassVar¶
- classmethod mode(loc, scale)[source]¶
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)[source]¶
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)[source]¶
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 MultivariateNormalFamily[source]¶
Bases:
StatelessDistributionFamilyMultivariate Normal family with diagonal covariance (stateless).
- parameters: ClassVar = ('loc', 'scale')¶
- dist_factory: ClassVar¶
- nll_constant_standard: ClassVar¶
- classmethod mode(loc, scale)[source]¶
Mode of distribution (returning first value if discrete ties), given distribution parameters.
- class MixtureNormalFamily[source]¶
Bases:
StatelessDistributionFamilyA stateless mixture of univariate normal distributions.
This class defines a mixture distribution where each component is a univariate normal distribution, and the mixture weights are defined by a categorical distribution.
- Parameters:
loc (Tensor): Mean of each normal component, one for each cluster.
scale (Tensor): Standard deviation of each normal component.
probs (Tensor): Probabilities associated with each cluster; must sum to 1.
- The mixture is modeled using torch.distributions.MixtureSameFamily, where:
The mixing distribution is a Categorical defined by probs.
The component distribution is Normal(loc, scale).
- parameters: ClassVar = ('loc', 'scale', 'probs')¶
- nll_constant_standard: ClassVar¶
- classmethod dist_factory(loc, scale, probs)[source]¶
Construct a MixtureSameFamily distribution of univariate normal components.
- Parameters:
- loc
torch.Tensor Mean(s) of the normal components. Shape should be broadcastable with scale and probs.
- scale
torch.Tensor Standard deviation(s) of the normal components.
- probs
torch.Tensor Probabilities of each mixture component. Must sum to 1 along the component axis.
- loc
- Returns:
- class:
.MixtureSameFamily A mixture distribution with categorical mixing and normal components.
- Parameters:
- Return type:
- classmethod sample(*params, sample_shape=())[source]¶
Draw samples from the mixture of normal distributions.
- Parameters:
- *params
torch.Tensor Distribution parameters in the order (loc, scale, probs). These should be broadcastable to define a valid MixtureSameFamily distribution.
- sample_shape
tupleofint, optional The desired sample shape. Defaults to an empty tuple for a single sample.
- *params
- Returns:
torch.TensorA tensor of samples drawn from the specified mixture distribution. The shape is sample_shape + batch_shape.
- Parameters:
- Return type:
- classmethod set_component_distribution(component_distribution, loc, scale)[source]¶
Ensure that the component distribution is an instance of the
torch.distributions.Normal.- Parameters:
- component_distribution
torch.distributions.Distribution The distribution object to validate. Must be an instance of torch.distributions.Normal.
- loc
torch.Tensor Mean(s) for the normal distribution.
- scale
torch.Tensor Standard deviation(s) for the normal distribution.
- component_distribution
- Returns:
torch.distributions.DistributionThe newly set Normal distribution instance.
- Raises:
ValueErrorIf component_distribution is not an instance of torch.distributions.Normal.
- Parameters:
- Return type:
torch.distributions
- classmethod extract_probs(*params)[source]¶
Extract the mixture probabilities from the distribution parameters.
- Parameters:
- *params
Any Distribution parameters (expected: loc, scale, probs), passed to dist_factory.
- *params
- Returns:
torch.TensorA 1D tensor of probabilities for each component in the mixture.
- Parameters:
params (Any)
- Return type:
- classmethod extract_n_clusters(*params)[source]¶
Return the number of mixture components (i.e., clusters) in the distribution.
- classmethod extract_cluster_parameters(which_cluster)[source]¶
Extract the parameters (probability, mean, standard deviation) for a specific cluster.
- Parameters:
- which_cluster
int The index of the cluster to extract parameters for.
- which_cluster
- Returns:
tupleoftorch.TensorA tuple containing: - prob: Probability of the selected cluster. - loc: Mean of the selected normal component. - scale: Standard deviation of the selected normal component.
- Parameters:
which_cluster (int)
- Return type:
- classmethod mode(*params)[source]¶
Return the mode of the mixture distribution.
For a mixture of normal distributions, this returns the mean of the overall mixture distribution, which serves as a proxy for the mode.
- Parameters:
- *params
Any Distribution parameters (expected: loc, scale, probs), passed to dist_factory.
- *params
- Returns:
torch.TensorThe mode of the mixture distribution.
- Parameters:
params (Any)
- Return type:
- classmethod mean(*params)[source]¶
Return the mean of the mixture distribution.
For a mixture of normal distributions, this returns the mean of the overall mixture distribution.
- Parameters:
- *params
Any Distribution parameters (expected: loc, scale, probs), passed to dist_factory.
- *params
- Returns:
torch.TensorThe mean of the mixture distribution.
- Parameters:
params (Any)
- Return type:
- classmethod stddev(*params)[source]¶
Return the standard deviation of the mixture distribution.
For a mixture of normal distributions, this returns the standard deviation of the overall mixture distribution.
- Parameters:
- *params
Any Distribution parameters (expected: loc, scale, probs), passed to dist_factory.
- *params
- Returns:
torch.TensorThe standard deviation of the mixture distribution.
- Parameters:
params (Any)
- Return type:
- class AbstractWeibullRightCensoredFamily[source]¶
Bases:
StatelessDistributionFamilyAbstract base class defining a stateless interface for distribution families.
This class represents a family of probability distributions in a stateless manner: no parameters are stored in the instance. All methods operate purely via classmethods, using explicitly passed distribution parameters.
Notes
Subclasses must define the parameters class variable, listing parameter names in order.
Each method operates solely on the passed tensors; no state or caching is assumed.
- dist_weibull: ClassVar¶
- classmethod validate_parameters(*params)[source]¶
-
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=())[source]¶
Sample values from a Weibull distribution.
- Parameters:
- nu
torch.Tensor - rho
torch.Tensor - xi
torch.Tensor - tau
torch.Tensor - sample_shape
tupleofint, optional Shape of the samples to draw
- nu
- Returns:
torch.TensorSamples drawn from the transformed Weibull distribution, shaped according to sample_shape combined with the distribution parameter shapes.
- Parameters:
- Return type:
- classmethod mode(*params)[source]¶
-
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)[source]¶
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)[source]¶
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)[source]¶
Compute the log hazard component of the likelihood for given data and parameters.
- Parameters:
- x
WeightedTensor The observed data values with associated weights.
- nu
torch.Tensor - rho
torch.Tensor - xi
torch.Tensor - tau
torch.Tensor - params :class:`torch.Tensor`
Additional parameters for reparametrization if needed.
- x
- Returns:
torch.TensorThe log hazard values for each observation, zeroed out for censored data.
- Parameters:
- Return type:
- classmethod compute_hazard(x, nu, rho, xi, tau, *params)[source]¶
Compute the hazard function values for given observations and parameters.
- Parameters:
- x
WeightedTensor The observed data values with associated weights.
- nu
torch.Tensor - rho
torch.Tensor - xi
torch.Tensor - tau
torch.Tensor - params :class:`torch.Tensor`
Additional parameters for reparametrization if needed.
- x
- Returns:
torch.TensorHazard values computed for each observation.
- Parameters:
- Return type:
- classmethod compute_log_survival(x, nu, rho, xi, tau, *params)[source]¶
Compute the log survival function for the Weibull distribution given observations and parameters.
- Parameters:
- x
torch.Tensor - nu
torch.Tensor - rho
torch.Tensor - xi
torch.Tensor - tau ::class:`torch.Tensor`
- params
torch.Tensor Additional optional parameters used in reparametrization.
- x
- Returns:
torch.TensorLog survival values for each observation.
- Parameters:
- Return type:
- classmethod compute_predictions(x, nu, rho, xi, tau, *params)[source]¶
Compute predicted survival or cumulative incidence probabilities for time-to-event data using a reparametrized Weibull model.
- Parameters:
- x
torch.Tensor - nu
torch.Tensor - rho
torch.Tensor - xi
torch.Tensor - tau ::class:`torch.Tensor`
- params
torch.Tensor Additional optional parameters used in reparametrization.
- x
- Returns:
torch.TensorPredicted survival probabilities or cumulative incidence values (depending on event type count), normalized by baseline survival at the last visit.
- Parameters:
- Return type:
- class WeibullRightCensoredFamily[source]¶
Bases:
AbstractWeibullRightCensoredFamilyAbstract base class defining a stateless interface for distribution families.
This class represents a family of probability distributions in a stateless manner: no parameters are stored in the instance. All methods operate purely via classmethods, using explicitly passed distribution parameters.
Notes
Subclasses must define the parameters class variable, listing parameter names in order.
Each method operates solely on the passed tensors; no state or caching is assumed.
- parameters: ClassVar = ('nu', 'rho', 'xi', 'tau')¶
- class WeibullRightCensoredWithSourcesFamily[source]¶
Bases:
AbstractWeibullRightCensoredFamilyAbstract base class defining a stateless interface for distribution families.
This class represents a family of probability distributions in a stateless manner: no parameters are stored in the instance. All methods operate purely via classmethods, using explicitly passed distribution parameters.
Notes
Subclasses must define the parameters class variable, listing parameter names in order.
Each method operates solely on the passed tensors; no state or caching is assumed.
- parameters: ClassVar = ('nu', 'rho', 'xi', 'tau', 'survival_shifts')¶
- class SymbolicDistribution[source]¶
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)[source]¶
Retrieve a function (e.g., ‘sample’, ‘mode’, ‘mean’) from the associated stateless distribution family, wrapped as a
NamedInputFunction.- Parameters:
- Returns:
NamedInputFunctionA callable wrapper over the distribution method with named inputs.
- Parameters:
- get_func_sample(sample_shape=())[source]¶
Factory of symbolic sampling function.
- Parameters:
- Returns:
NamedInputFunctionThe sample function.
- Parameters:
- Return type:
- get_func_sample_multivariate(sample_shape=())[source]¶
Factory of symbolic sampling function.
- Parameters:
- sample_shapetuple of int, optional
The shape of the sample. Default=().
- Returns:
- NamedInputFunction
The sample function.
- Parameters:
- Return type:
- get_func_regularization(value_name)[source]¶
Factory method to return a symbolic function computing the negative log-likelihood (used for regularization) from a given value.
- get_func_nll(value_name)[source]¶
Factory method to return a symbolic function computing the negative log-likelihood (NLL) from a given value.
- get_func_nll_jacobian(value_name)[source]¶
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)[source]¶
Factory of symbolic function: state -> (negative log-likelihood, its jacobian w.r.t. value).
- Parameters:
- value_name
str
- value_name
- Returns:
tuple`[:class:`~leaspy.utils.functional._named_input_function.NamedInputFunction`[:class:`~leaspy.utils.weighted_tensor._weighted_tensor.WeightedTensor]]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)[source]¶
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:
- factory
Callable`[..., :class:`~leaspy.variables.distributions.SymbolicDistribution] The factory.
- factory
- Parameters:
dist_family (Type[StatelessDistributionFamily])
- Return type:
Callable[Ellipsis, SymbolicDistribution]
- Normal¶
- Bernoulli¶
- WeibullRightCensored¶
- WeibullRightCensoredWithSources¶