leaspy.models.mixture

Classes

TimeReparametrizedMixtureModel

A time-reparametrized model tailored to handle mixture models with multiple clusters.

RiemanianManifoldMixtureModel

A riemannian manifold model tailored to handle mixture models with multiple clusters.

LogisticMixtureInitializationMixin

LogisticMultivariateMixtureModel

Mixture Manifold model for multiple variables of interest (logistic formulation).

Module Contents

class TimeReparametrizedMixtureModel(name, **kwargs)[source]

Bases: leaspy.models.mcmc_saem_compatible.McmcSaemCompatibleModel

A time-reparametrized model tailored to handle mixture models with multiple clusters.

This class extends TimeReparametrizedModel to incorporate mixture-specific behaviors, including support for multiple clusters (n_clusters) and corresponding vectorized parameters.

Parameters:
namestr

Name of the model.

source_dimensionOptional[int]

Number of sources. Dimension of spatial components (default is None).

**kwargs: :obj:`dict`
Additional hyperparameters for the model. Must include:
  • ‘n_clusters’: int

    Number of mixture components (must be ≥ 2).

  • ‘dimension’ or ‘features’: int or list

    Dimensionality of the input data.

  • ‘obs_models’: str, list, or dict (optional)

    Specification of the observation model(s). Defaults to “gaussian-diagonal”.

Raises:
LeaspyModelInputError

If inconsistent hyperparameters.

Parameters:

name (str)

property xi_mean: Tensor

Return the mean of xi as a tensor.

Return type:

Tensor

property xi_std: Tensor

Return the standard deviation of xi as a tensor.

Return type:

Tensor

property tau_std: Tensor

Return the standard deviation of tau as a tensor.

Return type:

Tensor

property noise_std: Tensor

Return the standard deviation of the model as a tensor.

Return type:

Tensor

property sources_mean: Tensor

Return the mean of the sources as a tensor.

Return type:

Tensor

property sources_std: Tensor

Return the standard deviation of the sources as a tensor.

Return type:

Tensor

source_dimension: int | None = None
get_variables_specs()[source]

Return the specifications of the variables (latent variables, derived variables, model ‘parameters’) that are part of the model.

Returns:
NamedVariables

A dictionary-like object containing specifications for the variables

Return type:

NamedVariables

property has_sources: bool

Indicates whether the model includes sources.

Returns:
bool

True if source_dimension is a positive integer. False otherwise.

Return type:

bool

static time_reparametrization(*, t, alpha, tau)[source]

Tensorized time reparametrization formula.

Warning

Shapes of tensors must be compatible between them.

Parameters:
ttorch.Tensor

Timepoints to reparametrize

alphatorch.Tensor

Acceleration factors of individual(s)

tautorch.Tensor

Time-shift(s) of individual(s)

Returns:
torch.Tensor

Reparametrized time of same shape as timepoints

Parameters:
Return type:

TensorOrWeightedTensor[float]

put_individual_parameters(state, dataset)[source]

Initialize individual latent parameters in the given state if not already set.

Parameters:
stateState

The current state object that holds all the variables

datasetDataset

Dataset used to initialize latent variables accordingly.

Parameters:
to_dict(*, with_mixing_matrix=True)[source]

Export model object as dictionary ready for JSON saving.

Parameters:
with_mixing_matrixbool (default True)

Save the mixing matrix in the exported file in its ‘parameters’ section.

Warning

It is not a real parameter and its value will be overwritten at model loading (orthonormal basis is recomputed from other “true” parameters and mixing matrix is then deduced from this orthonormal basis and the betas)! It was integrated historically because it is used for convenience in browser webtool and only there…

Returns:
KwargsType

The object as a dictionary.

Parameters:

with_mixing_matrix (bool)

Return type:

KwargsType

class RiemanianManifoldMixtureModel(name, variables_to_track=None, **kwargs)[source]

Bases: TimeReparametrizedMixtureModel

A riemannian manifold model tailored to handle mixture models with multiple clusters.

This class extends RiemanianManifoldModel to incorporate mixture-specific behaviors, mainly the handling of sources for multiple clusters.

Parameters:
namestr

The name of the model.

**kwargs

Hyperparameters of the model (including noise_model)

Raises:
LeaspyModelInputError
  • If hyperparameters are inconsistent

Parameters:
  • name (str)

  • variables_to_track (Optional[Iterable[str]])

tracked_variables
tracked_variables_ordered = ['g', 'v0', 'noise_std', 'tau_mean', 'tau_std', 'xi_mean', 'xi_std', 'nll_attach',...
classmethod compute_sufficient_statistics(state)[source]

Compute the model’s sufficient statistics.

Parameters:
stateState

The state to pick values from.

Returns:
SuffStatsRW

The computed sufficient statistics.

Parameters:

state (State)

Return type:

SuffStatsRW

get_variables_specs()[source]

Return the specifications of the variables (latent variables, derived variables, model ‘parameters’) that are part of the model.

Returns:
NamedVariables

A dictionary-like object mapping variable names to their specifications. These include ModelParameter, Hyperparameter, PopulationLatentVariable, and LinkedVariable instances.

Return type:

NamedVariables

static metric(*, g)[source]
Abstractmethod:

Parameters:

g (Tensor)

Return type:

Tensor

classmethod model_no_sources(*, rt, metric, v0, g)[source]

Return the model output when sources(spatial components) are not present.

Parameters:
rttorch.Tensor

The reparametrized time.

metricAny

The metric tensor used for computing the spatial/temporal influence.

v0Any

The values of the population parameter v0 for each feature.

gAny

The values of the population parameter g for each feature.

Returns:
torch.Tensor

The model output without contribution from source shifts.

Parameters:

rt (Tensor)

Return type:

Tensor

Notes

This implementation delegates to model_with_sources with space_shifts set to a zero tensor of shape (1, 1), effectively removing source effects.

classmethod model_with_sources(*, rt, space_shifts, metric, v0, g)[source]
Abstractmethod:

Parameters:
Return type:

Tensor

class LogisticMixtureInitializationMixin[source]
class LogisticMultivariateMixtureModel(name, **kwargs)[source]

Bases: LogisticMixtureInitializationMixin, RiemanianManifoldMixtureModel

Mixture Manifold model for multiple variables of interest (logistic formulation).

Parameters:

name (str)

get_variables_specs()[source]

Return the specifications of the variables (latent variables, derived variables, model ‘parameters’) that are part of the model.

Returns:
NamedVariables

A dictionary-like object mapping variable names to their specifications. These include ModelParameter, Hyperparameter, PopulationLatentVariable, and LinkedVariable instances.

Return type:

NamedVariables

static metric(*, g)[source]

Compute the metric tensor from input tensor g. This function calculates the metric as ((g + 1)^2 / g) element-wise.

Parameters:
gt torch.Tensor

Input tensor with values of the population parameter g for each feature.

Returns:
torch.Tensor

The computed metric tensor, same shape as g(number of features)

Parameters:

g (Tensor)

Return type:

Tensor

classmethod model_with_sources(*, rt, space_shifts, metric, v0, g)[source]

Return the model output when sources(spatial components) are present.

Parameters:
rtTensorOrWeightedTensor`[:obj:`float]

Tensor containing the reparametrized time.

space_shifts~leaspy.uitls.weighted_tensor._weighted_tensor.TensorOrWeightedTensor`[:obj:`float]

Tensor containing the values of the space-shifts

metric :`~leaspy.uitls.weighted_tensor._weighted_tensor.TensorOrWeightedTensor`[:obj:`float`]

Tensor containing the metric tensor used for computing the spatial/temporal influence.

v0~leaspy.uitls.weighted_tensor._weighted_tensor.TensorOrWeightedTensor`[:obj:`float]

Tensor containing the values of the population parameter v0 for each feature.

g~leaspy.uitls.weighted_tensor._weighted_tensor.TensorOrWeightedTensor`[:obj:`float]

Tensor containing the values of the population parameter g for each feature.

Returns:
torch.Tensor

Weighted value tensor after applying sigmoid transformation, representing the model output with sources.

Parameters:
Return type:

Tensor