leaspy.utils.distributions#

Module defining useful distribution for ordinal noise model.

Classes#

MultinomialDistribution

Class for a multinomial distribution with only one sample based on the Multinomial torch distrib.

MultinomialCdfDistribution

Class for a multinomial distribution with only sample method.

Functions#

discrete_sf_from_pdf(pdf)

Compute the discrete survival function values from a discrete probability density.

compute_ordinal_pdf_from_ordinal_sf(ordinal_sf[, ...])

Compute the probability density of an ordinal model from its survival function.

Module Contents#

discrete_sf_from_pdf(pdf)#

Compute the discrete survival function values from a discrete probability density.

For a discrete variable with levels l=0..L the survival function is \(P(X>l)=P(X\ge l+1)\) for l=0..L-1. This function assumes the last dimension of pdf indexes the discrete levels.

Parameters:
pdftorch.Tensor or np.ndarray

The discrete probability density.

Returns:
np.ndarray

The discrete survival function values.

Parameters:

pdf (Union[Tensor, ndarray])

Return type:

Tensor

compute_ordinal_pdf_from_ordinal_sf(ordinal_sf, dim_ordinal_levels=3)#

Compute the probability density of an ordinal model from its survival function.

Given the survival function probabilities \(P(X>l)=P(X\ge l+1)\) for l=0..L-1, compute \(P(X=l)\) for l=0..L.

Parameters:
ordinal_sftorch.FloatTensor

Survival function values : ordinal_sf[…, l] is the proba to be superior or equal to l+1 Dimensions are:

  • 0=individual

  • 1=visit

  • 2=feature

  • 3=ordinal_level [l=0..L-1]

  • [4=individual_parameter_dim_when_gradient]

dim_ordinal_levelsint, default = 3

The dimension of the tensor where the ordinal levels are.

Returns:
torch.FloatTensor

Probability density where ordinal_pdf[…, l] is the proba to be equal to l (l=0..L). Same shape as input, except for dimension 3 which has one more element.

Parameters:
  • ordinal_sf (Tensor)

  • dim_ordinal_levels (int)

Return type:

Tensor

class MultinomialDistribution(probs, **kwargs)#

Bases: torch.distributions.Multinomial

Class for a multinomial distribution with only one sample based on the Multinomial torch distrib.

Parameters:
probstorch.Tensor

The pdf of the multinomial distribution.

arg_constraints: ClassVar#

Returns a dictionary from argument names to Constraint objects that should be satisfied by each argument of this distribution. Args that are not tensors need not appear in this dict.

classmethod from_sf(sf, **kws)#

Generate a new MultinomialDistribution from its survival function instead of its probability density function.

Parameters:
pdftorch.Tensor

The input probability density function.

**kws

Additional keyword arguments to be passed for instance initialization.

Parameters:

sf (Tensor)

class MultinomialCdfDistribution(sf, *, validate_args=True)#

Bases: torch.distributions.Distribution

Class for a multinomial distribution with only sample method.

Parameters:
sftorch.Tensor

Values of the survival function [P(X > l) for l=0..L-1 where L is max_level] from which the distribution samples. Ordinal levels are assumed to be in the last dimension. Those values must be in [0, 1], and decreasing when ordinal level increases (not checked).

validate_argsbool, optional (default True)

Whether to validate the arguments or not (None for default behavior, which changed after torch >= 1.8 to True).

Attributes:
cdftorch.Tensor

The cumulative distribution function [P(X <= l) for l=0..L] from which the distribution samples. The shape of latest dimension is L+1 where L is max_level. We always have P(X <= L) = 1

arg_constraintsdict

Contains the constraints on the arguments.

Parameters:
arg_constraints: ClassVar#

Returns a dictionary from argument names to Constraint objects that should be satisfied by each argument of this distribution. Args that are not tensors need not appear in this dict.

cdf#

Returns the cumulative density/mass function evaluated at value.

Args:

value (Tensor):

classmethod from_pdf(pdf, **kws)#

Generate a new MultinomialDistribution from its probability density function instead of its survival function.

Parameters:
pdftorch.Tensor

The input probability density function.

**kws

Additional keyword arguments to be passed for instance initialization.

Parameters:

pdf (Tensor)

sample()#

Multinomial sampling.

We sample uniformly on [0, 1( but for the latest dimension corresponding to ordinal levels this latest dimension will be broadcast when comparing with cdf.

Returns:
torch.Tensor

Vector of integer values corresponding to the multinomial sampling. Result is in [[0, L]].

Return type:

Tensor