leaspy.models.mcmc_saem_compatible

Classes

McmcSaemCompatibleModel

Defines probabilistic models compatible with an MCMC SAEM estimation.

Module Contents

class McmcSaemCompatibleModel(name, *, obs_models, fit_metrics=None, **kwargs)[source]

Bases: leaspy.models.stateful.StatefulModel

Defines probabilistic models compatible with an MCMC SAEM estimation.

Parameters:
namestr

The name of the model.

obs_modelsobs_models or Iterable [obs_models]

The noise model for observations (keyword-only parameter).

fit_metricsdict

Metrics that should be measured during the fit of the model and reported back to the user.

**kwargs

Hyperparameters for the model

Attributes:
is_initializedbool

Indicates if the model is initialized.

namestr

The model’s name.

featureslist [str]

Names of the model features.

parametersdict

Contains the model’s parameters

obs_modelstuple [obs_models, …]

The observation model(s) associated to the model.

fit_metricsdict

Contains the metrics that are measured during the fit of the model and reported to the user.

_stateState

Private instance holding all values for model variables and their derived variables.

Parameters:
obs_models
fit_metrics = None
property observation_model_names: list[str]

Get the names of the observation models.

Returns:
list [str]

The names of the observation models.

Return type:

list[str]

has_observation_model_with_name(name)[source]

Check if the model has an observation model with the given name. Parameters ———- name : str

The name of the observation model to check.

Returns:
bool:

True if the model has an observation model with the given name, False otherwise.

Parameters:

name (str)

Return type:

bool

to_dict(**kwargs)[source]

Export model as a dictionary ready for export.

Returns:
KwargsType

The model instance serialized as a dictionary.

Return type:

KwargsType

compute_individual_trajectory(timepoints, individual_parameters, *, skip_ips_checks=False)[source]

Compute scores values at the given time-point(s) given a subject’s individual parameters.

Note

The model uses its current internal state.

Parameters:
timepointsscalar or array_like [scalar] (list, tuple, numpy.ndarray)

Contains the age(s) of the subject.

individual_parametersDictParams

Contains the individual parameters. Each individual parameter should be a scalar or array_like.

skip_ips_checksbool (default: False)

Flag to skip consistency/compatibility checks and tensorization of individual_parameters when it was done earlier (speed-up).

Returns:
torch.Tensor

Contains the subject’s scores computed at the given age(s) Shape of tensor is (1, n_tpts, n_features).

Parameters:
Return type:

Tensor

compute_prior_trajectory(timepoints, prior_type, *, n_individuals=None)[source]

Compute trajectory of the model for prior mode or mean of individual parameters.

Parameters:
timepointstorch.Tensor [1, n_timepoints]

Contains the timepoints (age(s) of the subject).

prior_typeLatentVariableInitType

The type of prior to use for the individual parameters.

n_individualsint, optional

The number of individuals.

Returns:
torch.Tensor [1, n_timepoints, dimension]

The group-average values at given timepoints.

Raises:
LeaspyModelInputError

If n_individuals is provided but not a positive integer, or if it is provided while prior_type is not LatentVariableInitType.PRIOR_SAMPLES.

Parameters:
Return type:

TensorOrWeightedTensor[float]

compute_mean_traj(timepoints)[source]

Trajectory for average of individual parameters (not really meaningful for non-linear models).

Parameters:
timepointstorch.Tensor [1, n_timepoints]
Returns:
torch.Tensor [1, n_timepoints, dimension]

The group-average values at given timepoints.

Parameters:

timepoints (Tensor)

Return type:

TensorOrWeightedTensor[float]

compute_mode_traj(timepoints)[source]

Most typical individual trajectory.

Parameters:
timepointstorch.Tensor [1, n_timepoints]
Returns:
torch.Tensor [1, n_timepoints, dimension]

The group-average values at given timepoints.

Parameters:

timepoints (Tensor)

Return type:

TensorOrWeightedTensor[float]

abstractmethod compute_jacobian_tensorized(state)[source]

Compute the jacobian of the model w.r.t. each individual parameter, given the input state.

This function aims to be used in ScipyMinimize to speed up optimization.

Parameters:
stateState

Instance holding values for all model variables (including latent individual variables)

Returns:
DictParamsTorch

Tensors are of shape (n_individuals, n_timepoints, n_features, n_dims_param).

Raises:
NotImplementedError
Parameters:

state (State)

Return type:

DictParamsTorch

classmethod compute_sufficient_statistics(state)[source]

Compute sufficient statistics from state.

Parameters:
stateState
Returns:
SuffStatsRW

Contains the sufficient statistics computed from the state.

Parameters:

state (State)

Return type:

SuffStatsRW

classmethod update_parameters(state, sufficient_statistics, *, burn_in)[source]

Update model parameters of the provided state.

Parameters:
clsMcmcSaemCompatibleModel

Model class to which the parameters belong.

stateState

Instance holding values for all model variables (including latent individual variables)

sufficient_statisticsSuffStatsRO

Contains the sufficient statistics computed from the state.

burn_inbool

If True, the parameters are updated in a burn-in phase.

Parameters:
Return type:

None

get_variables_specs()[source]

Get the specifications of the variables used in the model.

Returns:
NamedVariables

Specifications of the variables used in the model, including timepoints and observation models.

Return type:

NamedVariables

abstractmethod put_individual_parameters(state, dataset)[source]

Put the individual parameters inside the provided state (in-place).

Raises:
NotImplementedError
Parameters:
put_data_variables(state, dataset)[source]

Put all the needed data variables inside the provided state (in-place).

Parameters:
stateState

Instance holding values for all model variables (including latent individual variables), as well as: - timepoints : torch.Tensor of shape (n_individuals, n_timepoints)

datasetDataset

The dataset containing the data to be put in the state.

Parameters:
Return type:

None

reset_data_variables(state)[source]

Reset all data variables inside the provided state (in-place).

Parameters:
stateState

Instance holding values for all model variables (including latent individual variables), as well as: - timepoints : torch.Tensor of shape (n_individuals, n_timepoints)

Parameters:

state (State)

Return type:

None