demoAIStatistical mechanics
Kinetic Monte Carlo against the master equation
A molecule hops between three states, A to B to C and back, at fixed rates. The master equation gives the exact probability of each state at every time; kinetic Monte Carlo instead follows 10000 molecules one random jump at a time. At 25 times the fraction of molecules in each state matches the exact probability within four standard errors, the intermediate B rises to 0.460 and falls back, and the mean time to first reach C, exactly 1.75, is measured as .
Open in the workbenchDownload .vivOutputs below are from the site build’s run; Run all runs it again in your browser.
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
The exact solution in the reference,
posts/refs/kinetic-monte-carlo-against-the-master-equation.py, is the matrix
exponential from scipy.linalg.expm. The program solves
the same equation a different way, by Runge-Kutta steps, and the first test
compares the two. The same file runs a separate Gillespie code 200 times with
numpy's random numbers, and its run-to-run spread matches the error bars used
here.
Rates and the master equation
Many processes are jumps between a few states at known rates: a molecule changing shape, an atom hopping between sites on a surface, a gene switching on and off. A rate means that in a short time a system in state jumps to with probability , independently of how long it has already been in .
Here the states are A, B and C in a chain,
and every molecule starts in A. The probability of being in state gains from jumps into and loses from jumps out of it. That balance is the master equation written above, a linear system with a 3 by 3 matrix . Its long-time answer needs no solving: each forward and backward pair balances when and , so .
The program integrates with the classical fourth-order Runge-Kutta method at step . The fastest decay rate of is 2.82, so a step of makes the method's error at between and .
Printed
Tests 3 of 3 pass
- Runge-Kutta gives scipy's matrix exponential at t = 1 and t = 5 to 1e-12 (passes)
- the Runge-Kutta error at t = 1 is between 3e-14 and 3e-13 (passes)
- B peaks at 0.46014 near t = 0.703, as the root of d p_B / dt in the reference (passes)
Kinetic Monte Carlo
The master equation tracks probabilities. Kinetic Monte Carlo (KMC) follows one molecule instead, and gets the probabilities by counting many molecules. The version here is Gillespie's algorithm (1977). In state the total rate of leaving is . Since each jump happens at a constant rate, the waiting time until the next one is exponential with mean , drawn as from a uniform random number . Which jump it is follows from a second draw: jump to with probability . No time step is chosen, and none of the jumps is approximated.
The cell runs 10000 molecules to , records which state each is in at the 25 sample times, and notes when it first reaches C. Three trajectories are kept for the picture.
Printed
How close should they be?
At a fixed time each molecule is in state or not, independently of the others, so the count in is binomial and the fraction has standard error , at most 0.005 for . The reference's 200 repeated runs spread by that amount. There are 75 comparisons (3 states at 25 times), so the test allows four standard errors: one comparison exceeds that with probability , and any of the 75 with at most 0.5%.
The mean first-passage time from A to C is the average time until a molecule starting in A first reaches C. Treating C as a trap, it solves and (wait in the state, then take the next jump), so . The measured mean has a standard error from the sample spread of the 10000 first-passage times.
Printed
Tests 4 of 4 pass
- at all 25 times the KMC fractions are the exact probabilities within 4 standard errors (passes)
- the mean time to first reach C is 1.75 within three standard errors, and that error is 0.0144 (passes)
- the measured mean, 1.721, is 2.05 standard errors below 1.75 (passes)
- at t = 5 the chain is close to (1, 4, 16) / 21 but not there: p_C is 0.7511 against 0.7619 (passes)
The bars on the points are four standard errors, the test's tolerance; the
largest deviation of the 75 is printed as max_z, in standard errors: 2.6.
The measured first-passage mean, 1.721, is 2.05 standard errors below 1.75;
a normal variable lands that far out about one time in 25. B is
an intermediate: it fills as A empties and drains into C, and its peak of
0.460 at is above its final share of . By the
slowest part of the approach, which decays as (the smaller
nonzero eigenvalue of ), has not died out, so is still 0.011 short
of .