Your question is Simulating a Stochastic Process. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Ernst & Young Advisory Services Sdn Bhd uses stochastic simulations to model regime changes in quantitative risk scenarios. Implement a simulator for a continuous-time Markov chain using the Gillespie algorithm.
Given a transition-rate matrix, simulate multiple independent paths from an initial state until a finite time horizon. At each state, the process waits for an exponentially distributed duration, then moves to a new state selected according to the relative transition rates.
Implement simulate_ctmc(rates, initial_state, horizon, paths, seed). rates is an n x n matrix where rates[i][j] is the transition rate from state i to state j. Diagonal values are zero. Return a dictionary containing terminal_counts, an array of length n, and average_jumps, the mean number of transitions per simulated path rounded to six decimal places.
Use random.Random(seed) so results are reproducible. A path remains in its current state if its next transition would occur after the horizon. States with no outgoing transitions are absorbing.
def simulate_ctmc(rates, initial_state, horizon, paths, seed):