Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Simulating a Stochastic Process

HardPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Formal Specification

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.

Constraints

  • 1 <= n <= 50
  • 0 <= rates[i][j] <= 10^6
  • rates[i][i] = 0
  • 0 <= initial_state < n
  • 0 <= horizon <= 10^6
  • 1 <= paths <= 10^5
  • The rate matrix is square

Function Signature

def simulate_ctmc(rates, initial_state, horizon, paths, seed):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output