Algorithmadvanced
Transition path sampling
Samples reactive trajectories between two stable states by Monte Carlo in path space with shooting and shifting moves.
- Time
- O(M N G) expected
- Space
- O(N d) extra
- Where
- G = the cost of one time step; M = the number of path moves; N = the number of time steps of a path; d = the number of coordinates of a state
- Model
- Real RAM
- Properties
- randomizednot exactiterativerandomized
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Christoph Dellago, Peter G. Bolhuis, Felix S. Csajka, David Chandler, 1998
Transition path sampling (TPS) samples the rare trajectories that take a system from one stable state to another state , without knowing the mechanism or a reaction coordinate in advance. A path is a trajectory of time steps. The transition path ensemble weights each path by its probability under the dynamics, restricted to paths that start in and end in . TPS samples this ensemble with a Metropolis Monte Carlo chain whose states are whole paths. The main move, shooting, takes a point of the current path and grows a new piece of trajectory from it with the true dynamics; the shifting move translates the path in time. For stochastic dynamics with these moves, a trial path is accepted exactly when it connects to .
In plain words
Picture a mountain range between two valleys. A walker in a fog takes random steps and almost never crosses. Searching for crossings by trying many walks from the first valley wastes almost all the effort.
TPS starts from one crossing, however crude. It picks a point on it and lets a new walk grow from that point, forward or backward in time. If the new walk still links the two valleys, keep it; if not, keep the old one. Repeat. Each new crossing differs a bit from the last, so the collection wanders through all the ways of crossing, and each way shows up as often as the true walker would use it.
The problem
The input is a dynamics given as a time step (and its time reverse), the indicator functions of two stable states and , a path length , and one initial path from to , which need not be typical. The output is a sequence of paths whose distribution tends to the transition path ensemble
where is the equilibrium distribution and the transition probability. Averages over these paths describe the mechanism, and the rate constant follows from a related ensemble (see Variants).
The algorithm
TPS(x[0..N], step, back, A, B, M)- 1
repeat M times - 2
choose shooting or shifting, each with probability 1/2 - 3
shooting: pick k uniform in 0..N; with probability 1/2 regrow x[k+1..N] forward from x[k] with step, else regrow x[0..k-1] backward from x[k] with back - 4
shifting: pick s uniform in 1..s_max; with probability 1/2 drop x[0..s-1] and grow s steps at the end with step, else drop the last s points and grow s steps before x[0] with back - 5
if the trial path starts in A and ends in B, accept it; else keep x - 6
record x (also after a rejection) - 7
return the recorded paths
The function back is one step of the time reversed chain, ; for a reversible dynamics, such as Metropolis Monte Carlo or overdamped Langevin dynamics, it is the same as step. Because each new piece is generated by the dynamics itself, its probability in the path weight cancels the probability of generating it, and the Metropolis ratio reduces to the indicator of line L5. This is the one-way shooting move. For deterministic dynamics, such as Newtonian molecular dynamics, shooting changes the momenta at the slice slightly and integrates both ways; the acceptance then has an extra factor from the change of the equilibrium weight of the shooting point. Line L6 counts a rejected move as a new copy of the old path, which the Metropolis rule needs.
Example
Take a Metropolis random walk on the states with the energies at : from state propose with probability 1/2 each, reject a move off the ends, and accept a move with the probability . Let , and . A path from 0 reaches 4 at step 8 with probability , and the exact ensemble follows from matrix powers: . At the middle slice this is , and the mean first time at which the path enters is .
Start from the crude path , which crosses at once. With , 20000 moves from random.Random(0) gave an acceptance of 0.488 and used 54548 steps of the walk. The measured slice was , and the mean entry time 6.432. The crude start, with entry time 4, is forgotten: typical paths wait in and cross late. Runs of 2000 moves with seeds 0 to 5 gave entry times between 6.21 and 6.59, which shows the size of the Monte Carlo error at that length.
In this small example the crossing is not rare, and simple sampling (run the walk from 0 and keep the paths that end in 4) needs only about 38 runs per reactive path. TPS pays off when the probability of a crossing within steps is so small that simple sampling never finds one.
Correctness
The proof is in the correctness proof of this page. The path weight factors at any slice into a part before , which by stationarity equals times the probability of generating it backward from , and a part after , which is the probability of generating it forward. A shooting move replaces one of the two parts by a freshly generated one, so the path weight times the generation probability is symmetric between the old and the new path. A shift by is reversed by a shift by in the other direction, with the same cancellation. So every move satisfies detailed balance with respect to , with the acceptance . Invariance gives the correct ensemble if the path chain is also irreducible, which holds for the example and is an assumption in general.
Proof: Why shooting and shifting sample the transition path ensemblefull
We prove that each move of the implementation satisfies detailed balance with respect to the transition path ensemble, so the path chain has this ensemble as a stationary distribution.
Setting. is a Markov chain with transition probabilities and a stationary distribution . The backward kernel is ; it is a transition probability because is stationary, and if the chain is reversible. A path is . Its weight is
with the normalizing sum. is the transition path ensemble.
Lemma 1 (the backward identity). For every , .
Proof. Each factor ; the product telescopes to times .
So the path weight has the two factorizations
Lemma 2 (shooting). Fix the slice and the direction. A forward shot from keeps and generates with the probability . The reverse move, from back to , is a forward shot from the same slice with . Then . The same holds for a backward shot, with and the part before .
Proof. By the factorization, and with the same factor for the shared part before . Both sides equal . For a backward shot, the part after is shared, and the generated part has the probability , which by Lemma 1 is the part before of divided by . The same computation applies.
Lemma 3 (shifting). A forward shift by maps to , with for . Its reverse is the backward shift by , which regenerates from with . Then .
Proof. By Lemma 1, . And . Multiply the first by and the second by ; the two products are equal.
Theorem. The path chain of the implementation leaves invariant.
Proof. The implementation chooses the move type, the slice (uniform), the shift (uniform) and the direction with probabilities that are the same for a move and its reverse: a forward shot from is reversed by a forward shot from , a forward shift by by a backward shift by , and each is chosen with the same probability. It accepts the trial if and only if . For two reactive paths , the probability of the move from to is with the same as for the reverse, and so
by Lemmas 2 and 3, because on both sides. If is not reactive, and the move is rejected, so it contributes nothing on either side. Detailed balance with respect to follows, and detailed balance implies invariance.
The acceptance is the Metropolis rule ; the lemmas show that the ratio inside is 1 for every reactive trial.
Ergodicity. Invariance gives the right ensemble only if the path chain is irreducible on the reactive paths. In the test chain, a forward shot from can produce any reactive path in one move, because every path from state 0 has positive probability; so the chain is irreducible and, with rejections, aperiodic. For dynamics with a deterministic part, such as molecular dynamics, shooting needs a perturbation of the momenta, and irreducibility is an assumption.
What is not proved. The rate of convergence, and so the number of moves that a given accuracy needs, is not part of the theorem. On the page, it is measured.
Complexity
Each move generates on average about steps for a shot and steps for a shift, so moves cost in the real RAM model, with memory for the path. The number of moves needed for a given accuracy depends on how fast the path chain decorrelates; it is measured, not proved. Shots from slices near the middle of the transition are accepted most often and change the path most; shots from slices deep in or are almost always accepted but change little.
Properties
- No reaction coordinate. The states and must be defined, but the path between them is not.
- True dynamics. Every accepted path is a trajectory of the dynamics, so the ensemble contains the real mechanisms, with their real weights.
- Markov chain in path space. Successive paths are correlated, and the results are averages with Monte Carlo errors.
- Fixed length. The basic ensemble has a fixed path length ; it must be longer than the time of a crossing but short compared with the waiting time in .
Variants and improvements
- Rate constants. The time correlation grows linearly, , for times between the crossing time and the waiting time. TPS computes with umbrella sampling in path space, and follows from its slope.
- Two-way shooting. For deterministic dynamics, perturb the momenta at the slice and integrate both ways; this is the standard move for molecular dynamics.
- Flexible length and transition interface sampling. Paths stop when they reach or , which avoids waiting in the states; interfaces between and give the rate as a product of crossing probabilities, as in forward flux sampling.
- Committor analysis. From points on the sampled paths, fire many short runs and measure the probability of reaching first; the points with probability 1/2 form the transition state ensemble, which tests a guessed collective variable.
When to use it
Use TPS for a rare transition between two well defined states when the mechanism is unknown, the dynamics matters, and a single crossing can be found, for example for nucleation, folding of small proteins, and chemical reactions in solution. Use the string method or the nudged elastic band when a minimum energy path is enough, and forward flux sampling when the dynamics is not reversible or the time reverse is not available.
Pitfalls
- A bad definition of the states. If or includes points from which the system often returns, the ensemble contains paths that are not transitions. Both states must be small enough to exclude the transition region and large enough to be visited often.
- A path that is too short. Paths of length below the crossing time are rare in the ensemble, and the acceptance drops.
- Slow decorrelation. If there are several channels separated in path space, the path chain may stay in one channel. Check with several initial paths.
- Shooting with the wrong backward dynamics. For a dynamics that is not reversible, using the forward step backward breaks detailed balance.
- The initial path. A path forced over the barrier by an artificial bias is not typical. Discard the early moves.
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/transition-path-sampling/. The authors are listed in prov/MA0G4J.jsonl.
"""Transition path sampling (Dellago, Bolhuis, Csajka and Chandler 1998).
A path is a list x[0..N] of states of a Markov chain at successive time
steps. The transition path ensemble gives the path x the weight
pi(x[0]) p(x[0], x[1]) ... p(x[N-1], x[N]) h_A(x[0]) h_B(x[N]),
where pi is the equilibrium distribution, p the transition probability,
and h_A, h_B the indicators of the two stable states. This module samples
the ensemble by Metropolis Monte Carlo in path space with two moves:
- one-way shooting: pick a time slice k, then either regenerate the part
after k with the forward dynamics, or the part before k with the
backward dynamics;
- shifting: drop s states at one end and grow s new states at the other.
step(x, rng) is one step of the dynamics. back(x, rng) is one step of the
time reversed dynamics, the chain with p_back(y, x) = pi(x) p(x, y) / pi(y);
for a reversible chain it is the same as step. With these moves the
generation probability cancels the path weight, and a trial path is
accepted if and only if it starts in A and ends in B.
"""
from typing import Callable, Optional
def transition_path_sampling(path, step: Callable, in_a: Callable, in_b: Callable, rng, n_moves: int,
back: Optional[Callable] = None, max_shift: int = 2,
observe: Optional[Callable] = None):
"""Run n_moves path moves from a reactive path.
Returns (path, samples, accepted, work): the last path, the list of
observe(path) after every move (the path itself if observe is None),
the number of accepted moves, and the number of dynamics steps made.
"""
if back is None:
back = step
x = list(path)
if not (in_a(x[0]) and in_b(x[-1])):
raise ValueError("transition_path_sampling: the initial path must go from A to B")
n = len(x) - 1
samples, accepted, work = [], 0, 0
for _ in range(n_moves): # @line L1
if rng.random() < 0.5: # @line L2
k = rng.randrange(n + 1) # @line L3
if rng.random() < 0.5:
trial = x[:k + 1]
for _ in range(n - k):
trial.append(step(trial[-1], rng))
work += n - k
else:
head = [x[k]]
for _ in range(k):
head.append(back(head[-1], rng))
trial = head[::-1] + x[k + 1:]
work += k
else:
s = rng.randrange(1, max_shift + 1) # @line L4
if rng.random() < 0.5:
trial = x[s:]
for _ in range(s):
trial.append(step(trial[-1], rng))
else:
head = [x[0]]
for _ in range(s):
head.append(back(head[-1], rng))
trial = head[::-1] + x[1:n - s + 1]
work += s
if in_a(trial[0]) and in_b(trial[-1]): # @line L5
x = trial
accepted += 1
samples.append(observe(x) if observe else list(x)) # @line L6
return x, samples, accepted, work # @line L7
test_transition_path_sampling.py 91 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/transition-path-sampling/. The authors are listed in prov/MA0G4J.jsonl.
import math
import random
import pytest
from transition_path_sampling import transition_path_sampling
# Metropolis random walk on 0..4 with a barrier in the middle, beta = 1.
ENERGY = [0.0, 2.0, 3.0, 2.0, 0.0]
S = len(ENERGY)
N = 8
INITIAL = [0, 1, 2, 3, 4, 4, 4, 4, 4]
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 in_a(i):
return i == 0
def in_b(i):
return i == S - 1
def transition_matrix():
p = [[0.0] * S for _ in range(S)]
for i in range(S):
for j in (i - 1, i + 1):
if 0 <= j < S:
p[i][j] = 0.5 * min(1.0, math.exp(-(ENERGY[j] - ENERGY[i])))
p[i][i] = 1.0 - sum(p[i])
return p
def powers(p, n):
out = [[[float(i == j) for j in range(S)] for i in range(S)]]
for _ in range(n):
a = out[-1]
out.append([[sum(a[i][k] * p[k][j] for k in range(S)) for j in range(S)] for i in range(S)])
return out
def exact_slice(k):
"""P(x_k = j) in the path ensemble: p^k(0, j) p^(N-k)(j, 4) / p^N(0, 4)."""
pw = powers(transition_matrix(), N)
z = pw[N][0][S - 1]
return [pw[k][0][j] * pw[N - k][j][S - 1] / z for j in range(S)]
def first_entry(x):
return next(t for t, v in enumerate(x) if in_b(v))
def test_exact_ensemble_of_the_page():
assert [round(v, 4) for v in exact_slice(4)] == [0.1033, 0.2667, 0.2601, 0.2667, 0.1033]
assert [round(v, 4) for v in exact_slice(2)] == [0.4642, 0.3406, 0.1952, 0.0, 0.0]
def test_paths_stay_reactive_and_keep_their_length():
x, samples, accepted, work = transition_path_sampling(INITIAL, step, in_a, in_b, random.Random(7), 300)
assert 0 < accepted < 300
for path in samples:
assert len(path) == N + 1 and in_a(path[0]) and in_b(path[-1])
assert all(abs(a - b) <= 1 for a, b in zip(path, path[1:]))
assert samples[-1] == x
def test_non_reactive_start_is_refused():
with pytest.raises(ValueError):
transition_path_sampling([0] * (N + 1), step, in_a, in_b, random.Random(1), 1)
def test_slice_and_first_entry_match_the_exact_ensemble():
rng = random.Random(0)
_, samples, _, work = transition_path_sampling(INITIAL, step, in_a, in_b, rng, 3500)
assert work <= 10000
n = len(samples)
exact = exact_slice(4)
for j in range(S):
assert abs(sum(x[4] == j for x in samples) / n - exact[j]) < 0.08
# Exact mean first entry time into B in the ensemble: 6.4283.
assert abs(sum(first_entry(x) for x in samples) / n - 6.4283) < 0.3
History
Dellago, Bolhuis, Csajka and Chandler gave transition path sampling in 1998, with the shooting and shifting moves for stochastic dynamics and the computation of rate constants from the correlation function. The method was extended to deterministic dynamics and to many applications in the following years, and Bolhuis, Chandler, Dellago and Geissler reviewed it in 2002. Transition interface sampling and forward flux sampling later used interfaces between the states to compute rates more efficiently.
References
- Christoph Dellago, Peter G. Bolhuis, Felix S. Csajka and David Chandler, Transition path sampling and the calculation of rate constants, The Journal of Chemical Physics 108(5), 1998, 1964-1977, the path ensemble, the shooting and shifting moves with their acceptance rules, and the rate constant from the time correlation function
- Peter G. Bolhuis, David Chandler, Christoph Dellago and Phillip L. Geissler, Transition path sampling: throwing ropes over rough mountain passes, in the dark, Annual Review of Physical Chemistry 53, 2002, 291-318, the review of the method, the moves, the committor analysis and the rate computation
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 MA0G4J Edit this page History Report an error Markdown