leaspy.models.obs_models

Attributes

OBSERVATION_MODELS

ObservationModelFactoryInput

Classes

ObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

BernoulliObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

ObservationModelNames

Enumeration defining the possible names for observation models.

FullGaussianObservationModel

Specialized GaussianObservationModel when all data share the same observation model, with default naming.

GaussianObservationModel

Specialized ObservationModel for noisy observations with Gaussian residuals assumption.

AbstractWeibullRightCensoredObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

WeibullRightCensoredObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

WeibullRightCensoredWithSourcesObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

Functions

observation_model_factory(model, **kwargs)

Factory for observation models.

Package Contents

class ObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

In particular, it provides data & linked variables regarding observations and their attachment to the model (the negative log-likelihood - nll - to be minimized).

Parameters:
namestr

The name of observed variable (to name the data variable & attachment term related to this observation).

getterfunction Dataset -> WeightedTensor

The way to retrieve the observed values from the Dataset (as a WeightedTensor): e.g. all values, subset of values - only x, y, z features, one-hot encoded features, …

distSymbolicDistribution

The symbolic distribution, parametrized by model variables, for observed values (so to compute attachment).

extra_varsNone (default) or Mapping[VarName, VariableInterface]

Some new variables that are needed to fully define the symbolic distribution or the sufficient statistics. (e.g. “noise_std”, and “y_L2_per_ft” for instance for a Gaussian model)

name: leaspy.variables.specs.VariableName
getter: Callable[[Dataset], WeightedTensor]
dist: SymbolicDistribution
extra_vars: Mapping[leaspy.variables.specs.VariableName, VariableInterface] | None = None
get_nll_attach_var_name(named_attach_vars=True)

Return the name of the negative log likelihood attachement variable.

Parameters:

named_attach_vars (bool)

Return type:

str

get_variables_specs(named_attach_vars=True)

Automatic specifications of variables for this observation model.

Parameters:

named_attach_vars (bool)

Return type:

dict[leaspy.variables.specs.VariableName, VariableInterface]

serialized()

Nice representation of instance without its name (should be JSON exportable).

Return type:

Any

to_dict()

To be implemented…

Return type:

dict

to_string()

method for parameter saving

Return type:

str

class BernoulliObservationModel(**extra_vars)

Bases: leaspy.models.obs_models._base.ObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

In particular, it provides data & linked variables regarding observations and their attachment to the model (the negative log-likelihood - nll - to be minimized).

Parameters:
namestr

The name of observed variable (to name the data variable & attachment term related to this observation).

getterfunction Dataset -> WeightedTensor

The way to retrieve the observed values from the Dataset (as a WeightedTensor): e.g. all values, subset of values - only x, y, z features, one-hot encoded features, …

distSymbolicDistribution

The symbolic distribution, parametrized by model variables, for observed values (so to compute attachment).

extra_varsNone (default) or Mapping[VarName, VariableInterface]

Some new variables that are needed to fully define the symbolic distribution or the sufficient statistics. (e.g. “noise_std”, and “y_L2_per_ft” for instance for a Gaussian model)

Parameters:

extra_vars (VariableInterface)

string_for_json = 'bernoulli'
static y_getter(dataset)
Parameters:

dataset (Dataset)

Return type:

WeightedTensor

OBSERVATION_MODELS: Dict[ObservationModelNames, Type[leaspy.models.obs_models._base.ObservationModel]]
ObservationModelFactoryInput
class ObservationModelNames(*args, **kwds)

Bases: enum.Enum

Enumeration defining the possible names for observation models.

GAUSSIAN_DIAGONAL = 'gaussian-diagonal'
GAUSSIAN_SCALAR = 'gaussian-scalar'
BERNOULLI = 'bernoulli'
WEIBULL_RIGHT_CENSORED = 'weibull-right-censored'
WEIBULL_RIGHT_CENSORED_WITH_SOURCES = 'weibull-right-censored-with-sources'
classmethod from_string(model_name)
Parameters:

model_name (str)

observation_model_factory(model, **kwargs)

Factory for observation models.

Parameters:
modelstr or ObservationModel or dict [ str, …]
  • If an instance of a subclass of ObservationModel, returns the instance.

  • If a string, then returns a new instance of the appropriate class (with optional parameters kws).

  • If a dictionary, it must contain the ‘name’ key and other initialization parameters.

**kwargs

Optional parameters for initializing the requested observation model when a string.

Returns:
ObservationModel

The desired observation model.

Raises:
LeaspyModelInputError

If model is not supported.

Parameters:

model (ObservationModelFactoryInput)

Return type:

leaspy.models.obs_models._base.ObservationModel

class FullGaussianObservationModel(noise_std, **extra_vars)

Bases: GaussianObservationModel

Specialized GaussianObservationModel when all data share the same observation model, with default naming.

The default naming is:
  • ‘y’ for observations

  • ‘model’ for model predictions

  • ‘noise_std’ for scale of residuals

We also provide a convenient factory default for most common case, which corresponds to noise_std directly being a ModelParameter (it could also be a PopulationLatentVariable with positive support). Whether scale of residuals is scalar or diagonal depends on the dimension argument of this method.

Parameters:
tol_noise_variance = 1e-05
static y_getter(dataset)
Parameters:

dataset (Dataset)

Return type:

WeightedTensor

classmethod noise_std_suff_stats()

Dictionary of sufficient statistics needed for noise_std (when directly a model parameter).

Return type:

dict[leaspy.variables.specs.VariableName, LinkedVariable]

classmethod scalar_noise_std_update(*, state, y_x_model, model_x_model)

Update rule for scalar noise_std (when directly a model parameter), from state & sufficient statistics.

Parameters:
Return type:

Tensor

classmethod diagonal_noise_std_update(*, state, y_x_model, model_x_model)

Update rule for feature-wise noise_std (when directly a model parameter), from state & sufficient statistics.

Parameters:
Return type:

Tensor

classmethod noise_std_specs(dimension)

Default specifications of noise_std variable when directly modelled as a parameter (no latent population variable).

Parameters:

dimension (int)

Return type:

ModelParameter

classmethod with_noise_std_as_model_parameter(dimension)

Default instance of FullGaussianObservationModel with noise_std (scalar or diagonal depending on dimension) being a ModelParameter.

Parameters:

dimension (int)

classmethod compute_rmse(*, y, model)

Compute root mean square error.

Parameters:
Return type:

Tensor

classmethod compute_rmse_per_ft(*, y, model)

Compute root mean square error, per feature.

Parameters:
Return type:

Tensor

to_string()

method for parameter saving

Return type:

str

class GaussianObservationModel(name, getter, loc, scale, **extra_vars)

Bases: leaspy.models.obs_models._base.ObservationModel

Specialized ObservationModel for noisy observations with Gaussian residuals assumption.

Parameters:
  • name (leaspy.variables.specs.VariableName)

  • getter (Callable[[Dataset], WeightedTensor])

  • loc (leaspy.variables.specs.VariableName)

  • scale (leaspy.variables.specs.VariableName)

  • extra_vars (VariableInterface)

class AbstractWeibullRightCensoredObservationModel

Bases: leaspy.models.obs_models._base.ObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

In particular, it provides data & linked variables regarding observations and their attachment to the model (the negative log-likelihood - nll - to be minimized).

Parameters:
namestr

The name of observed variable (to name the data variable & attachment term related to this observation).

getterfunction Dataset -> WeightedTensor

The way to retrieve the observed values from the Dataset (as a WeightedTensor): e.g. all values, subset of values - only x, y, z features, one-hot encoded features, …

distSymbolicDistribution

The symbolic distribution, parametrized by model variables, for observed values (so to compute attachment).

extra_varsNone (default) or Mapping[VarName, VariableInterface]

Some new variables that are needed to fully define the symbolic distribution or the sufficient statistics. (e.g. “noise_std”, and “y_L2_per_ft” for instance for a Gaussian model)

static getter(dataset)
Parameters:

dataset (Dataset)

Return type:

WeightedTensor

get_variables_specs(named_attach_vars=True)

Automatic specifications of variables for this observation model.

Parameters:

named_attach_vars (bool)

Return type:

dict[leaspy.variables.specs.VariableName, VariableInterface]

class WeibullRightCensoredObservationModel(nu, rho, xi, tau, **extra_vars)

Bases: AbstractWeibullRightCensoredObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

In particular, it provides data & linked variables regarding observations and their attachment to the model (the negative log-likelihood - nll - to be minimized).

Parameters:
namestr

The name of observed variable (to name the data variable & attachment term related to this observation).

getterfunction Dataset -> WeightedTensor

The way to retrieve the observed values from the Dataset (as a WeightedTensor): e.g. all values, subset of values - only x, y, z features, one-hot encoded features, …

distSymbolicDistribution

The symbolic distribution, parametrized by model variables, for observed values (so to compute attachment).

extra_varsNone (default) or Mapping[VarName, VariableInterface]

Some new variables that are needed to fully define the symbolic distribution or the sufficient statistics. (e.g. “noise_std”, and “y_L2_per_ft” for instance for a Gaussian model)

Parameters:
  • nu (leaspy.variables.specs.VariableName)

  • rho (leaspy.variables.specs.VariableName)

  • xi (leaspy.variables.specs.VariableName)

  • tau (leaspy.variables.specs.VariableName)

  • extra_vars (VariableInterface)

string_for_json = 'weibull-right-censored'
classmethod default_init(**kwargs)
class WeibullRightCensoredWithSourcesObservationModel(nu, rho, xi, tau, survival_shifts, **extra_vars)

Bases: AbstractWeibullRightCensoredObservationModel

Base class for valid observation models that may be used in probabilistic models (stateless).

In particular, it provides data & linked variables regarding observations and their attachment to the model (the negative log-likelihood - nll - to be minimized).

Parameters:
namestr

The name of observed variable (to name the data variable & attachment term related to this observation).

getterfunction Dataset -> WeightedTensor

The way to retrieve the observed values from the Dataset (as a WeightedTensor): e.g. all values, subset of values - only x, y, z features, one-hot encoded features, …

distSymbolicDistribution

The symbolic distribution, parametrized by model variables, for observed values (so to compute attachment).

extra_varsNone (default) or Mapping[VarName, VariableInterface]

Some new variables that are needed to fully define the symbolic distribution or the sufficient statistics. (e.g. “noise_std”, and “y_L2_per_ft” for instance for a Gaussian model)

Parameters:
  • nu (leaspy.variables.specs.VariableName)

  • rho (leaspy.variables.specs.VariableName)

  • xi (leaspy.variables.specs.VariableName)

  • tau (leaspy.variables.specs.VariableName)

  • survival_shifts (leaspy.variables.specs.VariableName)

  • extra_vars (VariableInterface)

string_for_json = 'weibull-right-censored-with-sources'
classmethod default_init(**kwargs)