leaspy.models.lme_model.LMEModel

class LMEModel(name: str, **kwargs)

Bases: GenericModel

LMEModel is a benchmark model that fits and personalize a linear mixed-effects model

The model specification is the following:

y_{ij} = fixed_{intercept} + random_{intercept_i} + (fixed_{slopeAge} + random_{slopeAge_i}) * age_{ij} + \epsilon_{ij}

with:
  • y_{ij}: value of the feature of the i-th subject at his j-th visit,

  • age_{ij}: age of the i-th subject at his j-th visit.

  • \epsilon_{ij}: residual Gaussian noise (independent between visits)

<!> This model must be fitted on one feature only (univariate model).

TODO? add some covariates in this very simple model.

Parameters
namestr

The model’s name

**kwargs
Model hyperparameters:
  • with_random_slope_age : bool (default True)

Attributes
namestr

The model’s name

is_initializedbool

Is the model initialized?

with_random_slope_agebool (default True)

Has the LME a random slope for subject’s age? Otherwise it only has a random intercept per subject

featureslist[str]

List of the model features <!> LME has only one feature.

dimensionint

Will always be 1 (univariate)

parametersdict
Contains the model parameters. In particular:
  • ages_meanfloat

    Mean of ages (for normalization)

  • ages_stdfloat

    Std-dev of ages (for normalization)

  • fe_paramsnp.ndarray[float]

    Fixed effects

  • cov_renp.ndarray[float, float]

    Variance-covariance matrix of random-effects

  • cov_re_unscaled_invnp.ndarray[float, float]

    Inverse of unscaled (= divided by variance of noise) variance-covariance matrix of random-effects. This matrix is used for personalization to new subjects.

  • noise_stdfloat

    Std-dev of Gaussian noise

  • bse_fe, bse_renp.ndarray[float]

    Standard errors on fixed-effects and random-effects respectively (not used in Leaspy).

Methods

compute_individual_trajectory(timepoints, ...)

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

get_hyperparameters(*[, with_features, ...])

Get all model hyperparameters

hyperparameters_ok()

Check all model hyperparameters are ok

initialize(dataset[, method])

Initialize the model given a dataset and an initialization method.

load_hyperparameters(hyperparameters, *[, ...])

Load model hyperparameters from a dict

load_parameters(parameters, *[, list_converter])

Instantiate or update the model's parameters.

save(path, **kwargs)

Save Leaspy object as json model parameter file.

validate_compatibility_of_dataset(dataset)

Raise if the given dataset is not compatible with the current model.

compute_individual_trajectory(timepoints, individual_parameters: dict)

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

Parameters
timepointsarray-like of ages (not normalized)

Timepoints to compute individual trajectory at

individual_parametersdict
Individual parameters:
  • random_intercept

  • random_slope_age (if with_random_slope_age == True)

Returns
torch.Tensor of float of shape (n_individuals == 1, n_tpts == len(timepoints), n_features == 1)
get_hyperparameters(*, with_features=True, with_properties=True, default=None) Dict[str, Any]

Get all model hyperparameters

Parameters
with_features, with_propertiesbool (default True)

Whether to include features and respectively all _properties (i.e. _dynamic_ hyperparameters) in the returned dictionary

defaultAny

Default value is something is an hyperparameter is missing (should not!)

Returns
dict { hyperparam_namestr -> hyperparam_valueAny }
hyperparameters_ok() bool

Check all model hyperparameters are ok

Returns
bool
initialize(dataset: Dataset, method: str = None)

Initialize the model given a dataset and an initialization method.

After calling this method is_initialized should be True and model should be ready for use.

Parameters
datasetDataset

The dataset we want to initialize from.

methodstr, optional (default None)

A custom method to initialize the model

load_hyperparameters(hyperparameters: Dict[str, Any], *, with_defaults: bool = False) None

Load model hyperparameters from a dict

Parameters
hyperparametersdict[str, Any]

Contains the model’s hyperparameters

with_defaultsbool (default False)

If true, it also resets hyperparameters that are part of the model but not included in hyperparameters to their default value.

Raises
LeaspyModelInputError

if inconsistent hyperparameters

load_parameters(parameters, *, list_converter=<built-in function array>) None

Instantiate or update the model’s parameters.

Parameters
parametersdict

Contains the model’s parameters.

list_convertercallable

The function to convert list objects.

save(path: str, **kwargs)

Save Leaspy object as json model parameter file.

Default save method: it can be overwritten in child class but should be generic…

Parameters
pathstr

Path to store the model’s parameters.

**kwargs

Keyword arguments for json.dump method.

validate_compatibility_of_dataset(dataset: Dataset)

Raise if the given dataset is not compatible with the current model.

Parameters
datasetDataset

The dataset we want to model.

Raises
LeaspyDataInputError

if data is not univariate.