Skip to content

Gillespie gives the wrong mean when a reaction rate depends explicitly on time.#1328

Merged
luciansmith merged 1 commit into
sys-bio:issue-1318-time-dep-gillespiefrom
wshlavacek:gillespie-1318-timedep
Jun 9, 2026
Merged

Gillespie gives the wrong mean when a reaction rate depends explicitly on time.#1328
luciansmith merged 1 commit into
sys-bio:issue-1318-time-dep-gillespiefrom
wshlavacek:gillespie-1318-timedep

Conversation

@luciansmith

Copy link
Copy Markdown

Fixes #1318. The direct method samples the waiting time from a propensity it treats as constant until the next reaction, which is only correct when the rate laws are time-homogeneous. When a rate depends explicitly on time (directly, or through a time-dependent assignment or rate rule), the SSA mean drifts off the ODE: a rate that is zero at t=0 froze the first reporting interval, and coarse output intervals trailed the true mean by about one interval.

Now we check once, on the first step, whether any rate varies with time (the detection adds no random draws, so time-homogeneous models keep the original code path and produce bit-identical results). When a rate does depend on time, the waiting time tau satisfies integral over [t, t+tau] of s(u) du = -log(r1), where s is the total propensity re-evaluated as time advances; we accumulate that integral with an adaptive trapezoidal sub-step and select the reaction using the propensities at the firing time. This is the direct-method form of the integrated-propensity representation in D. F. Anderson, J. Chem. Phys. 127, 214107 (2007). The representation is exact; this implementation realizes it up to the quadrature error of the sub-step, so it is exact in the limit of small steps and converges as the tolerance is tightened, not bit-exact at a finite tolerance.

Adds regression test TimeDependentPropensityMatchesODE, an immigration-death process with a time-dependent immigration rate and a closed-form ODE mean. The time-homogeneous path is byte-for-byte identical to before, and the stochastic test suite is unaffected (its rate laws are all time-homogeneous; the CSymbolTime models use time only in event triggers).

The time-dependence check is a value-based heuristic; inspecting the rate-law expression trees for the time symbol would be more robust, which is noted in the code.

…y on time.

Fixes sys-bio#1318. The direct method samples the waiting time from a propensity it
treats as constant until the next reaction, which is only correct when the rate
laws are time-homogeneous. When a rate depends explicitly on time (directly, or
through a time-dependent assignment or rate rule), the SSA mean drifts off the
ODE: a rate that is zero at t=0 froze the first reporting interval, and coarse
output intervals trailed the true mean by about one interval.

Now we check once, on the first step, whether any rate varies with time (the
detection adds no random draws, so time-homogeneous models keep the original code
path and produce bit-identical results). When a rate does depend on time, the
waiting time tau satisfies integral over [t, t+tau] of s(u) du = -log(r1), where
s is the total propensity re-evaluated as time advances; we accumulate that
integral with an adaptive trapezoidal sub-step and select the reaction using the
propensities at the firing time. This is the direct-method form of the
integrated-propensity representation in D. F. Anderson, J. Chem. Phys. 127,
214107 (2007). The representation is exact; this implementation realizes it up to
the quadrature error of the sub-step, so it is exact in the limit of small steps
and converges as the tolerance is tightened, not bit-exact at a finite tolerance.

Adds regression test TimeDependentPropensityMatchesODE, an immigration-death
process with a time-dependent immigration rate and a closed-form ODE mean. The
time-homogeneous path is byte-for-byte identical to before, and the stochastic
test suite is unaffected (its rate laws are all time-homogeneous; the CSymbolTime
models use time only in event triggers).

The time-dependence check is a value-based heuristic; inspecting the rate-law
expression trees for the time symbol would be more robust, which is noted in the
code.
@luciansmith
luciansmith merged commit f3b560e into sys-bio:issue-1318-time-dep-gillespie Jun 9, 2026
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants