Path sampling

Once we have the fitted model (see previous section),
we can use it and run Monte-Carlo simualtions to obtain path samples for a given covariate set and state of origin.

# Quickly fit a multistate model for Rotterdam data
from pymsm.datasets import prep_rotterdam
dataset, states_labels = prep_rotterdam()

#Init MultistateModel
from pymsm.multi_state_competing_risks_model import MultiStateModel, default_update_covariates_function
multi_state_model = MultiStateModel(
    dataset,
    terminal_states=[3],
    update_covariates_fn=default_update_covariates_function)

# Fit to data
multi_state_model.fit()
Fitting Model at State: 1
>>> Fitting Transition to State: 3, n events: 195
>>> Fitting Transition to State: 2, n events: 1518
Fitting Model at State: 2
>>> Fitting Transition to State: 3, n events: 1077

Once the model is ready, we can run the run_monte_carlo_simulation() method which samples random paths using Monte Carlo simulations.
Initial sample covariates, along with the sample’s current state need to be supplied.
The next states are sequentially sampled via the model parameters.
The process concludes when the sample arrives at a terminal state or the number of transitions exceeds the specified maximum,
and returns a list of length n_random_samples, contining the randomly create PathObjects

# Run Monte-carlo simulation
simulated_paths = multi_state_model.run_monte_carlo_simulation(
              # Initial sample covariates, when entering the origin state
              sample_covariates = dataset[0].covariates.values,
              # Initial state where the path begins from
              origin_state = 1,
              # Time when starting the sample path. Defaults to 0.
              current_time = 0,
              # Number of random paths to create. Defaults to 100.
              n_random_samples = 10,
              # Max number of transitions to allow in the paths. Defaults to 10.
              max_transitions = 2,
              # Number of parallel jobs to run. Defaults to -1 (all available).
              n_jobs = 3,
              # Whether to print the paths or not. Defaults to False.
              print_paths=True
              )
100%|██████████| 10/10 [00:07<00:00,  1.42it/s]

States: [1, 2, 3]
Transition times: [332.00005223077005, 4342.99995976923]


States: [1, 2, 3]
Transition times: [631.0000141, 3465.0001279]


States: [1, 2, 3]
Transition times: [4794.000067, 1373.99982]


States: [1, 2, 3]
Transition times: [905.9999772, 1476.0000568]


States: [1, 2, 3]
Transition times: [2382.000034, 2637.000018]


States: [1, 2, 3]
Transition times: [832.0000313617267, 1102.9999616382734]


States: [1, 2, 3]
Transition times: [1139.0000637501248, 2711.0000012498754]


States: [1, 2, 3]
Transition times: [2226.999889, 3038.000241]


States: [1, 2, 3]
Transition times: [3371.000033, 2339.9998520000004]


States: [1, 2, 3]
Transition times: [526.0000380373596, 5459.000045962641]