leaspy.algo.simulate.simulate.SimulationAlgorithm

class SimulationAlgorithm(settings)

Bases: AbstractAlgo

To simulate new data given existing one by learning the individual parameters joined distribution.

You can choose to only learn the distribution of a group of patient. To do so, choose the cofactor(s) and the cofactor(s) state of the wanted patient in the settings. For instance, for an Alzheimer’s disease patient, you can load a genetic cofactor informative of the APOE4 carriers. Choose cofactor [‘genetic’] and cofactor_state [‘APOE4’] to simulate only APOE4 carriers.

Parameters:
settingsAlgorithmSettings

The algorithm settings. They may include the following parameters, described in __Attributes__ section:

  • noise

  • bandwidth_method

  • cofactor

  • cofactor_state

  • number_of_subjects

  • mean_number_of_visits, std_number_of_visits, min_number_of_visits, max_number_of_visits

  • delay_btw_visits

  • reparametrized_age_bounds

  • sources_method

  • prefix

  • features_bounds

  • features_bounds_nb_subjects_factor

Raises:
LeaspyAlgoInputError

If algorithm parameters are of bad type or do not comply to detailed requirements.

Notes

The baseline ages are no more jointly learnt with individual parameters. Instead, we jointly learn the _reparametrized_ baseline ages, together with individual parameters. The baseline ages are then reconstructed from the simulated reparametrized baseline ages and individual parameters.

By definition, the relation between age and reparametrized age is:

\psi_i (t) = e^{\xi_i} (t - \tau_i) + \bar{\tau}

with t the real age, \psi_i (t) the reparametrized age, \xi_i the individual log-acceleration parameter, \tau_i the individual time-shift parameter and \bar{\tau} the mean conversion age derived by the model object.

One can restrict the interval of the baseline reparametrized age to be _learnt_ in kernel, by setting bounds in reparametrized_age_bounds. Note that the simulated reparametrized baseline ages are unconstrained and thus could, theoretically (but very unlikely), be out of these prescribed bounds.

Attributes:
name'simulation'

Algorithm’s name.

seedint

Used by numpy.random & torch.random for reproducibility.

algo_parametersdict

Contains the algorithm’s parameters.

bandwidth_methodfloat or str or callable, optional

Bandwidth argument used in scipy.stats.gaussian_kde in order to learn the patients’ distribution.

cofactorlist[str], optional (default = None)

The list of cofactors included used to select the wanted group of patients (ex - [‘genetic’]). All of them must correspond to an existing cofactor in the attribute Data of the input result of the run() method. TODO? should we allow to learn joint distribution of individual parameters and numeric/categorical cofactors (not fixed)?

cofactor_statelist[str], optional (default None)

The cofactors states used to select the wanted group of patients (ex - [‘APOE4’]). There is exactly one state per cofactor in cofactor (same order). It must correspond to an existing cofactor state in the attribute Data of the input result of the run() method. TODO? it could be replaced by methods to easily sub-select individual having certain cofactors PRIOR to running this algorithm + the functionality described just above (included varying cofactors as part of the distribution to estimate).

features_boundsbool or dict[str, (float, float)] (default False)

Specify if the scores of the generated subjects must be bounded. This parameter can express in two way:

  • bool : the bounds are the maximum and minimum scores observed in the baseline data (TODO: “baseline” instead?).

  • dict : the user has to set the min and max bounds for every features. For example: {'feature1': (score_min, score_max), 'feature2': (score_min, score_max), ...}

features_bounds_nb_subjects_factorfloat > 1 (default 10)

Only used if features_bounds is not False. The ratio of simulated subjects (> 1) so that there is at least number_of_subjects that comply to features bounds constraint.

mean_number_of_visitsint or float (default 6)

Average number of visits of the simulated patients. Examples - choose 5 => in average, a simulated patient will have 5 visits.

std_number_of_visitsint or float > 0, or None (default 3)

Standard deviation used into the generation of the number of visits per simulated patient. If <= 0 or None: number of visits will be deterministic

min_number_of_visits, max_number_of_visitsint (optional for max)

Minimum (resp. maximum) number of visits. Only used when std_number_of_visits > 0. min_number_of_visits should be >= 1 (default), max_number_of_visits can be None (no limit, default).

delay_btw_visits
Control by how many years consecutive visits of a patient are delayed. Multiple options are possible:
  • float > 0 : regular spacing between all visits

  • dictionary : {‘min’: float > 0, ‘mean’: float >= min, ‘std’: float > 0 [, ‘max’: float >= mean]}

Specify a Gaussian random spacing (truncated between min, and max if given) * function : n (int >= 1) => 1D numpy.ndarray[float > 0] of length n giving delay between visits (e.g.: 3 => [0.5, 1.5, 1.])

noiseNone or str in {‘model’, ‘inherit_struct’} or DistributionFamily or dict or float or array-like[float]
Wanted noise-model for the generated observations:
  • Set noise to None will lead to patients follow the model exactly (no noise added).

  • Set to 'inherit_struct', the noise added will follow the model noise structure and for Gaussian noise it will be computed from reconstruction errors on data & individual parameters provided.

  • Set noise to 'model', the noise added will follow the model noise structure as well as its parameters.

  • Set to a valid input for noise_model_factory to get the corresponding noise-model, e.g. set to 'bernoulli', to simulate Bernoulli realizations.

  • Set a float will add for each feature’s scores a noise of standard deviation the given float (‘gaussian_scalar’ noise).

  • Set an array-like[float] (1D of length n_features) will add for the feature j a noise of standard deviation noise[j] (‘gaussian_diagonal’ noise).

<!> When you simulate data from an ordinal model, you HAVE to keep the default noise=’inherit_struct’ (default)

(or use ‘model’, which is the same in this case since there are no scaling parameter for ordinal noise)

number_of_subjectsint > 0

Number of subject to simulate.

reparametrized_age_boundstuple[float, float], optional (default None)

Define the minimum and maximum reparametrized ages of subjects included in the kernel estimation. See Notes section. Example: reparametrized_age_bounds = (65, 70)

sources_methodstr in {‘full_kde’, ‘normal_sources’}
  • 'full_kde' : the sources are also learned with the gaussian kernel density estimation.

  • 'normal_sources' : the sources are generated as multivariate normal distribution linked with the other individual parameters.

prefixstr

Prefix appended to simulated patients’ identifiers

Methods

load_parameters(parameters)

Update the algorithm's parameters by the ones in the given dictionary.

run(model, *args[, return_loss])

Main method, run the algorithm.

run_impl(model, individual_parameters, data)

Run simulation - learn joined distribution of patients' individual parameters and return a results object containing the simulated individual parameters and the simulated scores.

set_output_manager(output_settings)

Set a FitOutputManager object for the run of the algorithm

load_parameters(parameters: dict)

Update the algorithm’s parameters by the ones in the given dictionary. The keys in the io which does not belong to the algorithm’s parameters keys are ignored.

Parameters:
parametersdict

Contains the pairs (key, value) of the wanted parameters

Examples

>>> settings = leaspy.io.settings.algorithm_settings.AlgorithmSettings("mcmc_saem")
>>> my_algo = leaspy.algo.fit.tensor_mcmcsaem.TensorMCMCSAEM(settings)
>>> my_algo.algo_parameters
{'n_iter': 10000,
 'n_burn_in_iter': 9000,
 'eps': 0.001,
 'L': 10,
 'sampler_ind': 'Gibbs',
 'sampler_pop': 'Gibbs',
 'annealing': {'do_annealing': False,
  'initial_temperature': 10,
  'n_plateau': 10,
  'n_iter': 200}}
>>> parameters = {'n_iter': 5000, 'n_burn_in_iter': 4000}
>>> my_algo.load_parameters(parameters)
>>> my_algo.algo_parameters
{'n_iter': 5000,
 'n_burn_in_iter': 4000,
 'eps': 0.001,
 'L': 10,
 'sampler_ind': 'Gibbs',
 'sampler_pop': 'Gibbs',
 'annealing': {'do_annealing': False,
  'initial_temperature': 10,
  'n_plateau': 10,
  'n_iter': 200}}
run(model: AbstractModel, *args, return_loss: bool = False, **extra_kwargs) Any

Main method, run the algorithm.

TODO fix proper abstract class method: input depends on algorithm… (esp. simulate != from others…)

Parameters:
modelAbstractModel

The used model.

datasetDataset

Contains all the subjects’ observations with corresponding timepoints, in torch format to speed up computations.

return_lossbool (default False), keyword only

Should the algorithm return main output and optional loss output as a 2-tuple?

Returns:
Depends on algorithm class: TODO change?
run_impl(model: AbstractModel, individual_parameters: IndividualParameters, data: Data) Tuple[Result, torch.FloatTensor | None]

Run simulation - learn joined distribution of patients’ individual parameters and return a results object containing the simulated individual parameters and the simulated scores.

<!> The AbstractAlgo.run signature is not respected for simulation algorithm… TODO: respect it… at least use (model, dataset, individual_parameters) signature…

Parameters:
modelAbstractModel

Subclass object of AbstractModel. Model used to compute the population & individual parameters. It contains the population parameters.

individual_parametersIndividualParameters

Object containing the computed individual parameters.

dataData

The data object.

Returns:
Result

Contains the simulated individual parameters & individual scores.

Notes

In simulation_settings, one can specify in the parameters the cofactor & cofactor_state. By doing so, one can simulate based only on the subject for the given cofactor & cofactor’s state.

By default, all the subjects provided are used to estimate the joined distribution.

set_output_manager(output_settings: OutputsSettings) None

Set a FitOutputManager object for the run of the algorithm

Parameters:
output_settingsOutputsSettings

Contains the logs settings for the computation run (console print periodicity, plot periodicity …)

Examples

>>> from leaspy import AlgorithmSettings
>>> from leaspy.io.settings.outputs_settings import OutputsSettings
>>> from leaspy.algo.fit.tensor_mcmcsaem import TensorMCMCSAEM
>>> algo_settings = AlgorithmSettings("mcmc_saem")
>>> my_algo = TensorMCMCSAEM(algo_settings)
>>> settings = {'path': 'brouillons',
                'console_print_periodicity': 50,
                'plot_periodicity': 100,
                'save_periodicity': 50
                }
>>> my_algo.set_output_manager(OutputsSettings(settings))