Algorithmresearch
Forward flux sampling
Estimates rare transition rates by firing trial runs from a series of interfaces between two states and keeping those that reach the next interface.
- Time
- O((n_0 + n M l) G) expected
- Space
- O(M d) extra
- Where
- G = the cost of one step; M = the number of trial runs per interface; d = the number of coordinates of a state; l = the mean length of a trial run in steps; n = the number of interfaces after the first; n_0 = the length of the first run in steps
- Model
- Real RAM
- Properties
- randomizednot exactnot iterativerandomizedunbiased given first run
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Rosalind J. Allen, Patrick B. Warren, Pieter Rein ten Wolde, 2005
Forward flux sampling (FFS) estimates the rate of a rare transition from a state to a state by splitting it into a chain of more likely steps. An order parameter and interfaces separate the two states. One ordinary run in measures the flux of trajectories that leave and cross , and stores the crossing points. From these points, trial runs estimate the probability to reach before going back to ; the points where they reach start the next stage, and so on. The rate is the product . FFS uses only forward simulation, so it works for dynamics that are not reversible and not in equilibrium, such as biochemical networks simulated with the Gillespie algorithm.
In plain words
Climbing a high wall in one jump almost never works. So mark ledges on the wall. First count how often climbers leave the ground and reach the lowest ledge. Then, from the climbers on that ledge, send many up and count how many reach the next ledge before falling back to the ground. Those who arrive become the starting group for the next ledge.
The chance to get to the top is the product of the chances from ledge to ledge. Each of these chances is not small, so a few hundred tries measure it well. The rate of reaching the top is the rate of reaching the first ledge times this product.
The problem
The input is a dynamics given as a time step, an order parameter , a test for the state (with on ), interfaces with , the length of the first run, and the number of trial runs per interface. The output is an estimate of , the number of transitions from to per unit of time spent in the region of (the time since the last visit to rather than to ), and, as a by-product, the trajectories that reach . The method assumes that the dynamics is Markov in the variables that are stored at the interfaces.
The algorithm
FFS(x, step, λ, A, λ_0..λ_n, n_0, M)- 1
run n_0 steps from x in A; Φ ← (first crossings of λ_0 after a visit to A) / (time in the region of A); S_0 ← the crossing points - 2
for i = 0 to n - 1 - 3
repeat M times: pick y uniformly at random from S_i - 4
run from y until λ(y) ≥ λ_{i+1} or y ∈ A - 5
if λ(y) ≥ λ_{i+1}: add y to S_{i+1} - 6
P_i ← |S_{i+1}| / M; if P_i = 0 return 0 - 7
return Φ P_0 P_1 ... P_{n-1}
Line L1 counts a crossing only on the first arrival at after the last visit to , and counts time only in the region of ; a run that reaches stops counting until it returns to . Line L4 is a plain forward run of the dynamics. A run that reaches fails; a run that jumps over several interfaces succeeds, and its end point is already past the next interface as well. Line L3 resamples the stored points with replacement, which keeps the product of line L7 unbiased. The points of , with the trial runs that led to them traced back, give sample transition paths.
Example
Take a Metropolis random walk on the states with the energies at : propose with probability 1/2 each, reject a move off the ends, accept with . Let , and the interfaces , so . For this walk everything is exact. With the committor (the probability to reach 6 before 0), , each crossing probability is , which gives , the flux is per step, and the rate is per step.
One run with , and random.Random(1) measured the flux 0.05789, the probabilities , and , with 45195 steps of the walk. Over 20 seeds:
| mean estimate | relative standard deviation | largest relative error | steps per run | ||
|---|---|---|---|---|---|
| 2000 | 200 | 0.24 | 0.50 | 4412 to 4727 | |
| 2000 | 300 | 0.21 | 0.44 | 5636 to 5985 | |
| 20000 | 2000 | 0.087 | 0.175 | 45003 to 45686 |
For comparison, a direct run of steps saw 537 transitions and gave . With the 45000 steps of one FFS run, a direct run would see about 23 transitions, a relative error of about 0.2, against 0.087 for FFS. These are measurements for this walk; the gain grows as the transition becomes rarer.
Correctness
The proof is in the correctness proof of this page. Every transition to starts with a first crossing of , and the excursion after a crossing at ends in with probability , the probability to reach before . So the rate is the flux times the average of over the crossing points, which is . To reach the excursion must reach each interface in turn; by the strong Markov property at each interface, the expected product of the stage estimates , given the first run, is exactly the average of over . So the estimate is unbiased given the first run, and consistent as grows. This needs the dynamics to be Markov in the stored state: if hidden variables, such as velocities that are not stored, carry memory across an interface, the restarted runs have the wrong law.
Proof: Why the product of flux and crossing probabilities gives the ratefull
We prove two things for a Markov chain with an order parameter , a state with on , interfaces , and the state . First, the rate is the flux times the probability to reach from the first crossing of . Second, the staged estimator of that probability is unbiased.
Definitions. A trajectory is in the region of A at time if it visited more recently than . A first crossing of is a time at which the trajectory, in the region of , reaches for the first time since its last visit to . For a point , let be the probability that the chain from reaches before it enters , and let be the first time at which it reaches or enters . The rate is the long run number of transitions from the region of to per unit of time spent in the region of .
Lemma 1 (the rate). Let be the points of the successive first crossings of in a long stationary run, and the time spent in the region of up to . If the chain is ergodic, then
where . Writing for the limit of the number of crossings over , and for the average of over the crossing points, .
Proof. Every transition to starts in the region of and must reach on the way, because on and on . So each transition belongs to exactly one first crossing, the last one before it, and the excursion after crossing ends in (a transition) or in (no transition). The indicator that excursion ends in has the conditional mean given the past up to crossing , by the strong Markov property. So is a martingale with bounded increments, and divided by the number of crossings it tends to 0. Dividing by instead changes nothing, because the number of crossings grows like . The number of transitions is , which proves the first formula; the second is a rewriting.
Lemma 2 (the stages). Fix the set of first crossing points. In stage , the estimator draws points uniformly with replacement from , runs each until , and collects the end points that have into ; . Then
Proof. Let be the probability to reach before from , as above; it does not depend on , but we use it at stage . We show by downward induction on that for nonempty . For the product is empty, equal to 1, and on because its points have . For the step, condition on and use the induction hypothesis (if is empty, the product is 0, and so is the sum below):
Each of the trials picks uniformly in and adds if the run reaches first. By the strong Markov property at , the expectation of this term given is the probability to reach before from , which is , because a run that reaches must first reach . So the right side is .
Theorem. The estimate of the implementation, with the number of first crossings over the time in the region of in the first run, satisfies , and this tends to as the first run grows. So is unbiased given the first run, and consistent.
Proof. is fixed by the first run. Lemma 2 gives , and Lemma 1 gives the limit.
For the nearest neighbour walk of the tests. With , and the interfaces , every point on interface is the state , so is a binomial proportion with the mean , where is the committor (, ). The product telescopes to . The flux is the rate of steps from 0 to 1 per unit of time in the region of , , because for a reversible chain the probability that is in the region of is . Together, , the exact value of the tests.
What is not proved. The theorem gives no variance. The variance of depends on the placement of the interfaces and on ; on the page it is measured.
Complexity
The first run costs steps, and each of the stages costs trial runs of mean length , so the work is in the real RAM model, with memory for the stored points. The variance of the probability estimate is about in relative terms if the stages were independent; interfaces placed so that each is not small, and more trials where is small, reduce it. The trial runs of a stage are independent and run in parallel.
Properties
- Forward in time only. No time reverse, no equilibrium and no detailed balance are needed.
- Unbiased given the first run. The product of the stage estimates has the right mean for any number of trials.
- Paths as a by-product. The successful trials, traced back, are sample transition paths.
- Needs an order parameter. The interfaces must separate from ; unlike transition path sampling, the order parameter is part of the method, although it does not need to be a good reaction coordinate.
Variants and improvements
- Branched growth. Each point at an interface fires its own fixed number of trials, which builds a tree of paths and keeps their ancestry.
- The Rosenbluth method. A single path is grown through the interfaces with a weight, as in polymer growth.
- Adaptive interfaces. The interfaces are placed after a pilot run so that the probabilities are about equal.
- Subset simulation and multilevel splitting are the same idea in reliability analysis and applied probability: a rare event as a product of conditional probabilities estimated by sequential Monte Carlo.
- Transition interface sampling uses the same interfaces with path sampling moves for reversible dynamics.
When to use it
Use FFS for rare switching in stochastic systems that are not in equilibrium, such as genetic switches, and for nucleation and other rare events where a simple order parameter is available and the dynamics can be restarted from a stored state. Use transition path sampling when the dynamics is reversible and no order parameter is known, and kinetic Monte Carlo when the rates of the elementary events are known.
Pitfalls
- Lost memory. If the stored state at an interface does not include all the variables of the Markov dynamics, the trial runs start from the wrong distribution.
- Interfaces that are too far apart. If some is very small, few or no trials succeed, and the estimate has a large variance or is 0.
- Few points at an interface. All later stages descend from these points; if they are few, the later trials are strongly correlated and the error is larger than the binomial formula suggests.
- Counting crossings wrongly. Only the first crossing of after a visit to counts, and the time in the region of must not be counted.
- A first interface inside A. The first interface must lie outside the state , so that the trajectories that cross it are leaving.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/forward-flux-sampling/. The authors are listed in prov/0GTKCK.jsonl.
"""Forward flux sampling, direct version (Allen, Warren and ten Wolde 2005).
An order parameter lam(x) and interfaces lam_0 < lam_1 < ... < lam_n
separate the state A (in_a(x) is True, and lam < lam_0 there) from the
state B (lam >= lam_n). The rate constant from A to B is
k_AB = Phi_A0 * P(lam_1 | lam_0) * ... * P(lam_n | lam_(n-1)),
where Phi_A0 is the flux of first crossings of lam_0 by trajectories that
come from A, per unit of time spent in the region of A (the time since the
last visit to A rather than to B), and P(lam_(i+1) | lam_i) is the
probability that a trajectory at its first crossing of lam_i reaches
lam_(i+1) before it returns to A.
The flux and the first point set come from one long run in the region of
A. Each later stage fires trial runs from points picked at random in the
set of the previous stage, and keeps the end points of the runs that reach
the next interface. Only forward simulation is needed, so the dynamics
need not be reversible or in equilibrium.
"""
from typing import Callable
def forward_flux_sampling(x0, step: Callable, lam: Callable, in_a: Callable, interfaces, rng,
n_flux_steps: int, n_trials: int, max_trial_steps: int = 10**6):
"""Estimate k_AB per unit of time step.
Returns (rate, flux, probabilities, work): the rate estimate, the flux
estimate, the list of the n estimated crossing probabilities, and the
number of dynamics steps used. Time is counted in steps.
"""
lam_0, lam_b = interfaces[0], interfaces[-1]
x = x0
in_region_a, armed = True, True
time_a, crossings = 0, 0
points = []
work = 0
for _ in range(n_flux_steps): # @line L1
x = step(x, rng)
work += 1
if lam(x) >= lam_b:
in_region_a = False
elif in_a(x):
in_region_a, armed = True, True
if in_region_a:
time_a += 1
if armed and lam(x) >= lam_0:
crossings += 1
points.append(x)
armed = False
flux = crossings / time_a if time_a else 0.0
probabilities = []
if not points:
return 0.0, flux, probabilities, work
for i in range(len(interfaces) - 1): # @line L2
target = interfaces[i + 1]
reached = []
for _ in range(n_trials):
y = rng.choice(points) # @line L3
steps = 0
while lam(y) < target and not in_a(y): # @line L4
if steps == max_trial_steps:
raise RuntimeError("forward_flux_sampling: a trial run did not end")
y = step(y, rng)
steps += 1
work += steps
if lam(y) >= target:
reached.append(y) # @line L5
p = len(reached) / n_trials # @line L6
probabilities.append(p)
if p == 0.0:
return 0.0, flux, probabilities, work
points = reached
rate = flux
for p in probabilities:
rate *= p
return rate, flux, probabilities, work # @line L7
test_forward_flux_sampling.py 97 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/forward-flux-sampling/. The authors are listed in prov/0GTKCK.jsonl.
import math
import random
import pytest
from forward_flux_sampling import forward_flux_sampling
# Metropolis random walk on 0..6 with a barrier in the middle, beta = 1.
ENERGY = [0.0, 2.0, 4.0, 5.0, 4.0, 2.0, 0.0]
S = len(ENERGY)
L = S - 1
INTERFACES = [1, 2, 3, 4, 5, 6]
def step(i, rng):
j = i + (1 if rng.random() < 0.5 else -1)
if j < 0 or j >= S:
return i
if rng.random() < math.exp(-(ENERGY[j] - ENERGY[i])):
return j
return i
def lam(x):
return x
def in_a(x):
return x == 0
def exact():
"""k_AB, the flux and the crossing probabilities of the walk, per step.
With the committor q (q(0) = 0, q(L) = 1), the equilibrium pi and
p(x, x+1), the rate of A to B transitions per step of time in the region
of A is k = pi(0) p(0, 1) q(1) / sum_x pi(x) (1 - q(x)). The flux is
the same without q(1), and P(i+1 | i) = q(i) / q(i+1).
"""
w = [math.exp(-e) for e in ENERGY]
pi = [v / sum(w) for v in w]
up = [0.5 * min(1.0, math.exp(-(ENERGY[x + 1] - ENERGY[x]))) for x in range(L)]
r = [1.0 / (pi[x] * up[x]) for x in range(L)]
q = [sum(r[:x]) / sum(r) for x in range(S)]
den = sum(pi[x] * (1.0 - q[x]) for x in range(S))
flux = pi[0] * up[0] / den
probabilities = [q[i] / q[i + 1] for i in range(1, L)]
return flux * q[1], flux, probabilities
def test_exact_values_of_the_page():
k, flux, probabilities = exact()
assert k == pytest.approx(1.02696e-3, rel=1e-4)
assert flux == pytest.approx(0.058484, rel=1e-4)
assert probabilities[0] == pytest.approx(1.0 / (1.0 + math.e ** 2))
assert flux * math.prod(probabilities) == pytest.approx(k)
def test_estimates_agree_with_the_exact_values():
k, flux, exact_p = exact()
rate, f, probabilities, work = forward_flux_sampling(0, step, lam, in_a, INTERFACES, random.Random(1), 2000, 300)
assert work <= 10000
# For this walk every point on an interface is the same state, so each
# estimate is a binomial proportion of 300 trials.
for p_hat, p in zip(probabilities, exact_p):
assert abs(p_hat - p) < 3.0 * math.sqrt(p * (1.0 - p) / 300)
assert f == pytest.approx(flux, rel=0.2)
assert rate == pytest.approx(k, rel=0.5)
assert rate == pytest.approx(f * math.prod(probabilities))
def test_jumps_over_interfaces_by_hand():
def jump(x, rng):
return min(x + 2, L)
rate, f, probabilities, work = forward_flux_sampling(0, jump, lam, in_a, INTERFACES, FirstChoice(), 5, 3)
# Flux run: 0 -> 2 (a crossing of lam_0 = 1), 4, then 6 in B. Time in the
# region of A is 2 steps, so the flux is 1/2. Points at 2 and 4 already lie
# on the next interface and need no step.
assert f == 0.5
assert probabilities == [1.0] * 5
assert rate == 0.5
assert work == 5 + 3 * 2
class FirstChoice:
def choice(self, seq):
return seq[0]
def test_no_success_gives_rate_zero():
def back_to_a(x, rng):
return 1 if x == 0 else 0
rate, f, probabilities, _ = forward_flux_sampling(0, back_to_a, lam, in_a, INTERFACES, FirstChoice(), 10, 4)
assert f == pytest.approx(0.5)
assert probabilities == [0.0] and rate == 0.0
History
Allen, Warren and ten Wolde introduced forward flux sampling in 2005 to compute the switching rates of genetic toggle switches, which are stochastic and far from equilibrium, so that methods based on detailed balance did not apply. Allen, Frenkel and ten Wolde compared the direct method with the branched and Rosenbluth versions in 2006, and Allen, Valeriani and ten Wolde reviewed the method in 2009. The idea of splitting a rare event at intermediate levels goes back to the splitting methods of Monte Carlo for particle transport.
References
- Rosalind J. Allen, Patrick B. Warren and Pieter Rein ten Wolde, Sampling rare switching events in biochemical networks, Physical Review Letters 94(1), 2005, 018104, the interfaces, the flux through the first interface and the product of the crossing probabilities
- Rosalind J. Allen, Daan Frenkel and Pieter Rein ten Wolde, Simulating rare events in equilibrium or nonequilibrium stochastic systems, The Journal of Chemical Physics 124(2), 2006, 024102, the direct method and its variants, with a comparison of their efficiency
- Rosalind J. Allen, Chantal Valeriani and Pieter Rein ten Wolde, Forward flux sampling for rare event simulations, Journal of Physics Condensed Matter 21(46), 2009, 463102, the review of the method, the choice of the interfaces and the error analysis
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 0GTKCK Edit this page History Report an error Markdown