Tutorials › Quantitative Systems Pharmacology (QSP) › Worked Birth-Death Simulations with Gillespie’s Algorithm

Delay and Stochastic Extensions · Tutorial 100 of 799

Worked Birth-Death Simulations with Gillespie's Algorithm

Learn to simulate and interpret individual birth–death trajectories with Gillespie’s direct method, including event times, finite-horizon population summaries, and variability across independent replicates.

Advanced 11 min read

What You'll Learn

  • Set birth and death propensities from a finite population’s current cell count.
  • Generate event times and select event types using the direct method.
  • Track the piecewise-constant population trajectory up to a fixed time horizon.
  • Summarize replicate-to-replicate variability without confusing it with parameter uncertainty.
  • Diagnose impossible transitions, incorrect rates, and stopping-rule errors.
  • Compare exact event simulation with smooth deterministic trajectories and time-step approximations.

From a Birth–Death Model to Simulated Cell Paths

A deterministic population model describes a smooth change in cell number. A stochastic birth–death model instead represents individual cell events: one cell divides, or one cell dies, and the population changes by one. Even when the event rates are fixed, repeated simulations can produce different event times and different population sizes. This variation is relevant when cell counts are small or when a particular simulated trajectory matters.

Here, “finite population” means the initial count is finite and the process tracks integer cell counts. The basic birth–death model below has no fixed upper bound on the population; it is observed over a finite time horizon. Its rates are proportional to the current number of cells. The model assumes events occur one at a time, with rates that remain constant between events. It does not include delayed effects or changes in rates between events.

Definition: Let \(N(t)\) be the number of cells at time \(t\), with initial count \(N(0)=n_0\), a nonnegative integer. In the linear birth–death model, each cell gives rise to a birth at rate \(\lambda\) and a death at rate \(\mu\), per cell per unit time. In state \(N(t)=n\), the total birth and death propensities are \(a_B(n)=\lambda n\) and \(a_D(n)=\mu n\). The total event propensity is \(a_0(n)=a_B(n)+a_D(n)\). A birth changes the state to \(n+1\); a death changes it to \(n-1\).

The rates \(\lambda\) and \(\mu\) have units of inverse time, such as day\(^{-1}\), and the propensities have units of events per unit time. Birth and death events are conditionally independent given the current state. At \(n=0\), both propensities are zero: without an immigration process, zero cells is an absorbing state. For \(n>0\), the next event is simulated from the current propensities, then the propensities are recalculated for the new state.

Why the Direct Method Generates These Events

The earlier material on Gillespie’s direct method establishes how exponential waiting times and reaction propensities determine the next event. For this birth–death application, if the current state is \(n>0\), the waiting time to a birth is exponential with rate \(a_B(n)\), and the waiting time to a death is exponential with rate \(a_D(n)\). The first of these competing event times determines what happens next.

The probability that neither event has occurred by elapsed time \(t\) is the product of their survival probabilities. Thus the waiting time to either event has an exponential distribution with rate \(a_0(n)\). The density that the next event occurs at time \(t\) and is a birth is \(a_B(n)\exp[-a_0(n)t]\). Integrating this density over all \(t\geq 0\) gives the probability that the next event is a birth.

$$ P(\tau>t)=e^{-a_B(n)t}e^{-a_D(n)t}=e^{-a_0(n)t}, \qquad P(\text{birth next})=\int_0^\infty a_B(n)e^{-a_0(n)t}\,dt =\frac{a_B(n)}{a_0(n)}. $$

Consequently, with independent draws \(U_1,U_2\sim\operatorname{Uniform}(0,1)\), the waiting time is \(\tau=-\log(U_1)/a_0(n)\). Select a birth if \(U_2a_0(n)<a_B(n)\); otherwise select a death. The event time is advanced by \(\tau\), the cell count is changed by one, and the calculation repeats. These formulas require finite propensities that stay constant while waiting for the next event.

Worked Example: Simulating a Path to a Fixed Horizon

Suppose, hypothetically, that \(\lambda=0.30\) day\(^{-1}\) and \(\mu=0.20\) day\(^{-1}\), with \(N(0)=4\). Simulate to \(T=2\) days using the following independent uniform draws, in order: \((U_1,U_2)=(0.40,0.70)\), then \((0.20,0.30)\), then \((0.60,0.90)\), then \(U_1=0.25\). At each step use the next draw or pair. The last draw is used only to check whether another event occurs before the horizon.

Initially \(a_B=0.30(4)=1.20\) day\(^{-1}\), \(a_D=0.20(4)=0.80\) day\(^{-1}\), and \(a_0=2.00\) day\(^{-1}\). The first waiting time is \(-\log(0.40)/2.00=0.4581\) days. Since \(0.70(2.00)=1.40\) is greater than the birth propensity 1.20, the event is a death. At \(t=0.4581\), the population falls from 4 to 3.

At \(n=3\), the propensities are \(a_B=0.90\), \(a_D=0.60\), and \(a_0=1.50\) day\(^{-1}\). The next wait is \(-\log(0.20)/1.50=1.0730\) days, so the event time is \(0.4581+1.0730=1.5311\) days. Because \(0.30(1.50)=0.450\) is less than 0.90, this event is a birth, and \(n\) becomes 4.

At \(n=4\), the total propensity is again 2.00 day\(^{-1}\). The next wait is \(-\log(0.60)/2.00=0.2554\) days, giving event time \(1.5311+0.2554=1.7865\) days. The event is a death because \(0.90(2.00)=1.80>1.20\). The population becomes 3. From \(n=3\), the final waiting-time draw gives \(-\log(0.25)/1.50=0.9242\) days, which would put the next event at \(2.7107\) days. Since this is beyond the horizon, no further event is recorded by day 2. The simulated population is 3 at \(T=2\).

Representing and Summarizing a Trajectory

Between events, \(N(t)\) is constant. A useful record therefore includes the event time, the event type, the count immediately before the event, and the count immediately after it. The trajectory is usually taken to be right-continuous: at an event time, its value is the count after that event. For example, the preceding simulation has a death at day 0.4581, a birth at day 1.5311, and a death at day 1.7865. Recording only the final count would discard the timing and order of those events.

A time-weighted average population over \([0,T]\) is obtained by multiplying each population level by the time spent at that level, summing, and dividing by \(T\). This differs from the unweighted average of the counts observed at event times, because event times need not be evenly spaced.

Worked Example: Calculating a Time-Weighted Population

Consider a second hypothetical simulated path with \(N(0)=3\) and horizon \(T=1.2\) days. The simulated events are a birth at 0.20 days, a death at 0.50 days, and a death at 0.90 days. The population is therefore 3 for 0.20 days, 4 for 0.30 days, 3 for 0.40 days, and 2 for the final 0.30 days.

The total cell-time is \(3(0.20)+4(0.30)+3(0.40)+2(0.30)=0.60+1.20+1.20+0.60=3.60\) cell-days. Dividing by 1.2 days gives a time-weighted average of \(3.60/1.2=3.00\) cells. The units are cells because cell-days are divided by days. This summary describes this path; it is not the average across independent simulations.

A simulation can also be saved at selected observation times, such as \(t=0.5\) and \(t=1.0\), by reading the current state of the event-driven path at each time. If no event occurs between two observation times, the recorded population is unchanged. These saved values are useful for comparing trajectories, but they do not replace the event-time record when the timing of events is important.

Replicate Variability and Its Interpretation

A replicate is a separate simulation using the same model, parameter values, initial condition, and time horizon, but new random draws. Conditional on those shared inputs, differences among replicates are demographic stochasticity: the randomness in the birth and death events. They are not, by themselves, evidence that the rate parameters differ across cells or subjects.

Worked Example: Summarizing Replicate Counts

Suppose five hypothetical, independent exact simulations use the same rates and initial population, and their counts at day 2 are 4, 3, 5, 2, and 4 cells. The sample mean is \(\bar{x}=(4+3+5+2+4)/5=18/5=3.60\) cells.

The squared deviations from 3.60 are \(0.16, 0.36, 1.96, 2.56,\) and \(0.16\), which sum to 5.20 cells\(^{2}\). The sample standard deviation is \(s=\sqrt{5.20/(5-1)}=\sqrt{1.30}=1.140\) cells.

Thus these five simulated outcomes have a mean of 3.60 cells and a sample standard deviation of 1.140 cells at day 2. The standard deviation describes spread among the simulated outcomes in this small set of replicates. It is not a confidence interval for a model parameter, and the five outcomes do not establish the full probability distribution of the count.

With more replicates, a histogram or empirical quantiles can show the range and shape of simulated counts at a selected time. Individual trajectories can also reveal whether variability is concentrated early or late, or whether paths frequently reach zero. Any such summary is conditional on the specified model, parameter values, initial state, and simulation horizon.

Assumptions, Diagnostics, and Alternatives

The direct simulation is exact for the specified continuous-time Markov birth–death model when its assumptions hold: the state is an integer count, events change the count by one, the stated propensities are correct, and rates do not change during a waiting interval. Exactness means that the event-driven algorithm samples the model’s distribution; it does not mean that the model is a complete description of real cell biology. On a finite horizon, the linear birth–death process starting from a finite count has a finite count and finitely many events with probability one.

Check implementation and output before interpreting a simulation. The following checks are particularly useful:

  • Every event time must be later than the previous one, and no event after the chosen horizon should be applied.
  • Each event must change the count by exactly \(+1\) or \(-1\). A death must not be selected when \(n=0\), and the count must never become negative.
  • Recompute \(a_B=\lambda n\), \(a_D=\mu n\), and \(a_0=a_B+a_D\) after every event. Using rates from the prior state gives incorrect waiting times and event probabilities.
  • Check units: a propensity in day\(^{-1}\) produces a waiting time in days. A rate intended per cell must be multiplied by the current cell count.
  • Compare replicated runs with different random draws. Identical paths can indicate that the random stream is being reused; unusually large changes can indicate a unit or rate error.

A common mistake is to simulate every event but keep the population fixed when calculating the next waiting time. For example, after a death from \(n=4\) to \(n=3\), the total propensity in the worked model changes from \(2.00\) to \(1.50\) day\(^{-1}\). Reusing \(2.00\) would make the next waiting time too short on average and distort the trajectory. The correction is to update the state first, then calculate all propensities from that state.

The exact direct method is event-driven and preserves integer counts, making it a natural choice when event timing or small populations matter. A deterministic ordinary differential equation instead gives a smooth trajectory and does not represent individual random event histories. Time-step approximations can reduce computational cost in some settings, but require care about step size and possible invalid count changes. The appropriate alternative depends on whether the goal is individual event timing, a smooth population trend, or computational efficiency.

Key takeaway: For each direct-method event, calculate propensities from the current count, draw the waiting time and event type, update the count, and stop at the specified horizon. Across independent replicates, summarize path variability as stochastic variability conditional on the model—not as parameter uncertainty.

Check Your Understanding

Answer each question using the birth–death model and simulation rules described above. Brief solutions are included for checking.

  1. If \(n=6\), \(\lambda=0.10\) hour\(^{-1}\), and \(\mu=0.05\) hour\(^{-1}\), what are the birth, death, and total propensities?
  2. At a state with \(a_B=0.8\) hour\(^{-1}\) and \(a_D=0.2\) hour\(^{-1}\), what is the probability that the next event is a birth?
  3. At \(n=2\) with \(\lambda=0.3\) day\(^{-1}\) and \(\mu=0.2\) day\(^{-1}\), calculate the waiting time for \(U_1=0.5\). If \(U_2=0.8\), which event occurs?
  4. Why is the average of populations recorded only at event times generally not the time-weighted average population?
  5. Five replicate counts at a fixed time are 1, 2, 2, 3, and 2 cells. Calculate \(\bar{x}\) and \(s\).

Solutions. (1) \(a_B=0.10(6)=0.60\), \(a_D=0.05(6)=0.30\), and \(a_0=0.90\) events per hour. (2) The probability is \(0.8/(0.8+0.2)=0.80\). (3) \(a_0=(0.3+0.2)(2)=1.0\) day\(^{-1}\), so \(\tau=-\log(0.5)/1.0=0.6931\) days. Since \(0.8(1.0)=0.80\) is greater than \(a_B=0.60\), the event is a death. (4) Event times are generally unequally spaced, so an unweighted average gives the same weight to short and long intervals. Weight each population by its interval duration to obtain a time average. (5) The mean is \(10/5=2.00\) cells. The squared deviations sum to \(2\), so \(s=\sqrt{2/(5-1)}=0.7071\) cells.

Clinical Biostats