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()
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
)