Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Reproducible stochastic experiments

A protocol gives the time and conditions of an experiment. A count model describes the reactions. Timed interventions and reaction completions use the native count scheduler. Prepared projections and stationary laws run on the native engine; the binomial-detection and fluorescence readouts of crn_craft.likelihood need the optional crn-craft[likelihood] dependency (SciPy).

Scheduled stochastic simulation

import crn_craft as crn

model = crn.Model("timed_birth")
model.add_species("X", 0)
model.add_parameter("birth", 2.0)
model.add_reaction(crn.mass_action("birth"), [], ["X"], name="arrival")

protocol = crn.Protocol(time_unit="hour")
protocol.at(3.0, parameters={"birth": 4.0}, partition={"X": 0.5})
run = crn.ensemble(
    model, end=5, report_times=[0, 3, 5], protocol=protocol,
    seed=123, replicates=20_000, threads=4,
)
result = run.simulate()

At hour three the daughter count is Poisson with mean 3. At hour five it is Poisson with mean 11. Reports are right-continuous. Completions due at a boundary precede external action groups. Equal-time action groups follow their order in the protocol. Each group reads its pre-group counts. Groups at time zero apply before the initial report. The stop predicate is tested after each group, and a stopped trajectory retains its counts. With no reaction possible, the trajectory waits for the next group.

The methods are linear static_direct and next_reaction for closed attribute-free count models. Other methods, and a Static Direct selector other than linear, are refused with CRN-R020 at /method. At each boundary the engine updates the counts and switches to rate constants that were compiled during preparation for every group that changes parameters. Static Direct discards a wait that passes the boundary and draws a new one under the new rates. Next Reaction draws fresh clocks for all channels at every boundary. Both are exact because the exponential waits are memoryless. A reaction drawn at exactly the time of a boundary fires after it. Reports between boundaries draw no random numbers. Each replicate keeps one random stream across all phases, so results do not depend on threads or on the report grid.

Count actions include signed increments (change), absolute assignments (set) and partitions. A partition independently retains each molecule with the given probability. A group may hold at most one count operation per species, and every operation reads the counts before the group. Parameter values replace the current values until a later group changes them. A parameter that sets an initial count cannot be changed. The time_unit field is a label. The engine does not convert units: action times, durations, the horizon and the model’s rates must use the same unit.

A request is refused with CRN-R020 at /protocol/... for an unknown species or parameter, conflicting operations on one species, a probability outside [0, 1], or action times that decrease, are negative, or lie after end. A protocol holds at most 4,096 action groups and 4,096 delays. A count change that would leave a species negative, or above the count limit, fails the replicate with CRN-R030 at /protocol/actions/i. The message names the group, its time, the species and its count.

crn-craft.run/1.1 is a versioned request with required protocol. A crn-craft.run/1.0 request with protocol is refused with CRN-R020, and 1.0 requests without it run as before. The native CLI supports its usual check, run, --archive and verify commands. crn-craft.result/1.1 logs calendar events with kind, pending and cancelled. An unfiltered event log stores changed counts before and after a deterministic event in its participant fields. Report events and rule_events count stochastic reaction events. Interventions and completions consume work from the same replicate event budget. To migrate a 1.0 count request, change its version and add protocol, then choose linear Static Direct or Next Reaction.

Protocol runs have their own sampler identities: chacha8-u53-ziggurat/rules-protocol-direct/1 (Static Direct with the default Ziggurat waits), chacha8-u53/rules-protocol-direct/1 (inverse waits) and chacha8-u53/rules-protocol-next-reaction/1. The Rust tests in crates/crn-craft/tests/protocol.rs check exact laws for all three: a rate change and a channel that is switched on, the moments across a binomial division, and the fixed-delay laws below. validation/check_paper_capabilities.py checks the example above against its two Poisson laws.

Fixed-delay completion

Protocol.delay(reaction, duration, products=..., owner=...) adds a fixed-time completion to a reaction. The named reaction still applies its immediate stoichiometry at initiation. The duration is a positive number in the run’s time unit. An optional owner is a busy count species that starts at zero, and only that initiation reaction may create it, one copy per firing. Busy copies are exchangeable. When a reaction, a partition or a count action removes k busy copies, the engine cancels k pending completions of that owner, chosen uniformly at random from the replicate’s stream. An action that would increase a busy count fails the replicate with CRN-R030. A completion that comes due consumes one busy copy and creates the specified products. Pending completions are ordered by due time, then by creation. At an exact tie, due completions run first, then action groups in their declared order, then the report. The queue holds at most max_pending entries (1 to 4,096, default 4,096). A replicate that needs more fails with CRN-R030. Pending completions survive parameter changes. prepare_fsp raises ValueError for a protocol with delays, because a count state cannot represent the pending history.

The delayed-reaction implementation follows the event-queue semantics of the modified next-reaction method. crates/crn-craft/tests/protocol.rs checks three laws for each sampler. Products completed by time t from Poisson starts at rate r with delay τ are Poisson with mean r max(0, t − τ). With owner loss at rate δ that mean is multiplied by exp(−δτ). When a partition keeps one of two busy owners, each owner is kept with probability 1/2.

The delayed feedback example compares the first power-spectrum peak and the autocorrelation at twice the delay with an independent delayed direct method written in Python. With 12 paths per sampler, the peaks were 0.15390 (native) and 0.12877 (independent) rad per time unit, within the 0.03 tolerance. Over six batches of 12 paths, the peak varied with a standard deviation of 0.0051 (native) and 0.0097 (independent). The tolerance is about ten frequency bins, so the check detects only large changes of the period. The batch means, 0.1487 and 0.1392, differ by 2.1 standard errors. This is inconclusive, and more paths would be needed to resolve it. The example is not run in CI.

For a paper specifying hourly degradation trials, model that discrete sequence separately from the continuous-time degradation hazard. Fixed replication-time example

Prepared probability laws

projection = crn.prepare_fsp(model, bounds={"X": 48}, protocol=protocol)
law = projection.solve(5, [0, 3, 5], tolerance=1e-12)
daughter = law.marginal("X", at=1)
continued = projection.solve(5, [5], initial=law.snapshots[1],
                             tolerance=1e-12)

The native engine lowers the model, evaluates the rates (including count-dependent rates and stop conditions), enumerates the states, assembles the sparse generator, applies interventions and propagates the law by uniformization, by an implicit variable-order integrator (solver="bdf") and by the exponential midpoint rule; it also solves stationary laws. Sparse LU factorizations use the Rust library faer, compiled into the extension. SciPy is used only by the binomial-detection and Gaussian fluorescence readouts of crn_craft.likelihood (count_probability with detection below 1, binned_probability with a gain and noise). Install it with the likelihood extra (pip install "crn-craft[likelihood]"); those two readouts raise ModuleNotFoundError naming the extra without it. The existing Model.fsp (the fsp method of crn-craft.run/1.0) is a separate native solver. Use it for one fixed model from one initial state. Use prepare_fsp for protocols, prescribed signals, initial mixtures, checkpoints or many parameter vectors on one graph.

Prepare with initial={(0,): 0.25, (1,): 0.75} to seed a distribution. The labels are count tuples in the order of projection.species. The probabilities must be finite, nonnegative and sum to one within 1e-12. Preparation enumerates every transition whose reactants are present, whatever its rate, and every state an action can reach. So a rule with zero rate at preparation can be switched on later. solve(parameters=...) reuses this graph and only re-evaluates the rates. A binding that changes the topology or the initial counts raises ValueError; prepare a new projection for new bounds or initial support. A prepared projection keeps no per-solve state, so threads can share it. It cannot be pickled; prepare one per process.

An action maps the retained law through its count kernel, one species at a time; a partition is binomial. Escaped probability stays in the absorbing sink; an action can add to it but never returns it. Previously stopped probability carries forward. A snapshot from law.snapshots continues a solve under the same parameters and signals. To change parameters at a time point, use a protocol action.

Pass signals={"arrival": crn.Signal.table(times, rates)} to replace the count-independent rate constant of a named rule by a prescribed time series in the rule’s rate units and the model’s time unit. Tables use linear interpolation or an explicit right-continuous step curve and hold their end values outside the table. Step times split the integration; the knots of a linear table do not. Signal(function, change_times=...) accepts a Python rate function; list its discontinuities in change_times. Python callables are supplied by the caller when rerunning the experiment. Both forced-segment solvers evaluate tables natively and call a Python function from the native step loop; an interval is integrated with the value just before its end, so a discontinuity at a boundary is approached from the left and the next interval starts with the right-continuous value.

Constant segments use uniformization with a truncated Poisson series: an interval of length t splits into ceil(λt/16) pieces (λ the largest exit rate), and each piece stops the series once the omitted Poisson weight is at most its share of the tolerance. Smoothly forced segments use the implicit integrator (solver="bdf", the default) or an adaptive exponential midpoint rule (solver="midpoint").

The implicit integrator uses the numerical differentiation formulas (NDF) of orders 1 to 5 of Shampine and Reichelt (“The MATLAB ODE Suite”, 1997) in backward-difference form, with a quasi-constant step size. Each step’s local error estimate is held to rtol = tolerance and atol = 1e-4 · tolerance in the root-mean-square norm. Step size and order change only after k + 1 equal steps of order k; steps are at most max_step, land exactly on segment ends, and reports inside a segment come from the interpolating polynomial of the last step. The problem is linear, so the iteration matrix I − c Q(t) is exact when it is factorized at the step’s own time: it is factorized when the step size or order changes, and during a run of equal steps the factorization of an earlier time is reused with up to four simplified Newton iterations before refactorizing. One symbolic analysis of the matrix pattern serves every factorization of a generator. Before the first factorization the analysis bounds the memory of the factorization and its workspace; above max_factor_bytes (default 2 GiB) the solve stops with CRN-E070. The joint nuclear and cytoplasmic RNA projection of tutorial O3 (35,496 states) needs a bound of about 0.26 GB; 99,856 states need about 1.1 GB.

The midpoint rule compares one step exp(h Q(t + h/2)) with two half steps and accepts when their L1 gap is at most tolerance · h / (end − start); each matrix exponential action is a uniformization of the frozen generator whose omitted Poisson weight is at most 1/1024 of that step goal. result.solver names the method used. Each snapshot reports the absorbing sink, the Poisson series-tail bound, the mass defect and the minimum probability. The series-tail bound covers the uniformization of constant segments only. It excludes rounding and adds nothing for BDF or midpoint segments, which report no integration error estimate (the midpoint rule’s truncation shows in the mass defect). BDF can return slightly negative probabilities; check minimum_probability. The midpoint rule stops with an error when its tolerance is below the rounding level of its step estimate. max_work counts series terms, intervention outcomes, matrix exponential actions and, for BDF, each product of the generator with a vector, each solve and each numeric factorization. Check tolerance refinement on the selected observable for a smooth signal. See the RNA fit replay.

Use stationary_fsp(model, bounds) for one finite closed communicating class or specify class_state. With a return_state, escaped rates redirect to a retained state. The native engine enumerates the states, builds the generator, finds the closed communicating classes (an iterative strongly-connected-components pass; a class is closed when no transition leaves it), anchors the largest state of the class (or the return state) at probability 1 and solves the balance equations of the others with one sparse LU factorization, bounded by max_factor_bytes like the BDF. The law is clipped at zero and normalized; the result reports its L1 residual, which must be at most 1e-9 times the largest exit rate (at least 1). Successive enlargements compare marginal CDFs and outward flux. These diagnostics are empirical on unbounded networks. Certified error bounds require additional recurrence evidence. result.method separates the exact finite law (finite-closed-class) from the redirected approximation (stationary-fsp-return-state). Stationary FSP method

Retained state sets

prepare_fsp(model, bounds, retained=states) restricts a projection to a finite set of count tuples inside the bounds, for example the hyperbolic cut (x - 4)(y - 4) < 2500 of a toggle switch. Enumeration starts from the initial law and keeps only retained states; every transition to another state flows to the absorbing sink, so the sink probability measures the truncation. The initial law must lie in the set. Duplicates count once; an empty set, negative counts, tuples of another length, tuples outside the bounds and more than max_states tuples raise ValueError. Scheduled count interventions are refused with a retained set. Without retained the states, their order and the prepared id are those of the box.

Probability sensitivities and Fisher information

from crn_craft.sensitivities import fisher_information, forward_sensitivities

graph = crn.prepare_fsp(model, {"on": 1, "off": 1, "rna": 215})
segments = [(60.0, {"kon": 0.05, "koff": 0.15}),
            (120.0, {"kon": 0.10, "koff": 0.15})]
result = forward_sensitivities(graph, segments, [30.0, 60.0, 120.0],
                               ["koff", "kon"], logarithmic=True,
                               tolerance=1e-9)
bins, p, g = result.observed(["rna"])
matrix, excluded = fisher_information(p, g, cells=1000)

forward_sensitivities integrates the law p' = Q p and its parameter derivatives s_j' = Q s_j + (dQ/dθ_j) p on the prepared graph through piecewise-constant segments (end_time, bindings). The law and the derivatives pass continuously across segment ends. dQ/dθ_j is the centered difference of the generator at θ_j (1 ± 10⁻⁴) (or θ_j e^{±10⁻⁴} with logarithmic=True, which differentiates in natural-log coordinates): exact up to roundoff for rates linear in the parameter and second-order accurate for any other rate expression, such as a Hill function. The augmented system is integrated by the implicit integrator above, with the derivatives in the error norm (rtol = tolerance, atol = 10⁻⁴ tolerance); the iteration matrix is block lower triangular with identical diagonal blocks, so one sparse factorization of I − c Q serves the law and every direction. The initial law is the one given to prepare_fsp unless initial replaces it; it does not depend on the parameters unless initial_derivatives says so. The last column of probabilities and derivatives is the sink: each row of the law sums to one and each derivative row to zero, sink included; a loss above 1e-7 raises ValueError. At most 16 directions; signals, stop states and count interventions are outside this interface. max_work counts augmented solves.

result.observed(axes) sums states and derivatives into the bins of the measured species before any score: a Fisher matrix of hidden states overstates what a measurement holds. fisher_information(p, g, cells) returns Σ_t n_t Σ_b g gᵀ / p over bins with p > 0, exactly symmetric and positive semidefinite up to roundoff, and the probability of the excluded bins per time. A bin with p = 0 is unreachable or has underflowed and carries no information; probability_floor reproduces a published convention that drops rare bins. The run of chapter C8 of the optimization tutorials checks the derivatives against a closed form and centered differences and reproduces published Fisher matrices.

Verification. validation/check_projection_native.py compares the native engine with an independent Python and SciPy implementation (validation/reference_projection.py, not shipped, with SciPy’s BDF and sparse LU) on a declared case set: identical states and prepared ids, laws within the solve tolerance (twice it for forced segments, including BDF against SciPy’s BDF), identical errors on budget and input failures, and stationary laws within 1e-10 on chains with two closed classes and transient states, an absorbing state, a nearly decomposable stiff block structure and redirected expansions. Forced laws are also compared with closed-form Poisson laws. It also checks the binomial and Poisson-tail kernels against SciPy and, where SciPy loses accuracy for large counts, against a 60-digit evaluation. It also compares forward sensitivities with a closed form and with centered differences of uniformization solves through a parameter switch, and checks retained sets against the ordinary box.

Spatial models

SpatialModel(name, volumes) expands named, well-mixed compartments to an ordinary count model. volumes maps region IDs (letters, digits and _) to positive, fixed volumes. Each species gets one count per region, labeled <species>_<region>; label(species, region) returns that name and profile(run.paths, species) selects the region columns in region order. Ensemble means of a profile are the means of those columns. There is no separate helper for conserved totals; sum the region columns.

  • add_species(name, initial, reservoirs=()) takes one count for every region or a mapping by region.
  • local(name, rate, reactants, products, combinatorial=False, regions=None) adds one reaction per region. A string rate may name the local species as {X} and the region volume as {volume}. Volumes are not applied automatically: write a second-order rate constant as, for example, "k / {volume}". A local reaction may not change a reservoir species, so pass regions to leave reservoir regions out.
  • transport(species, origin, target, rate) adds a directed hop at rate per copy and day. Volumes do not enter the rate. For detailed balance between unequal volumes choose rates with V_i * k_ij = V_j * k_ji.
  • diffusion(species, first, second, diffusivity, spacing) adds both directions at diffusivity / spacing**2 per copy. It requires equal volumes.

A region without an edge is a reflecting end. A reservoir is a species whose region count never changes. A hop out of it fires at rate * initial count and adds one copy to the target. A hop into it removes the copy from the origin; the copy is absorbed.

spatial_turnover(initial, end=, target=, feedback=, mutant_weight=, death=, hop=, seed=0, mutation=0.0, fixed_left=False, labels=False, max_events=3_000_000_000) samples the endpoint of one exact path of the two-genotype chain of the spatial spread study. initial lists (wild, mutant) counts by region, at most 4,096 regions. Every copy is born at max(0, death + feedback * (target - W - mutant_weight * M)), dies at death and hops to each nearest neighbor at hop per day. With mutation > 0 a birth makes a new mutant clone with that probability. With labels=True the result lists the clone ID of every mutant copy by region: 0 for initial mutants, then 1, 2, … in order of creation. Labels do not change the hazards. With fixed_left=True region 0 is a reservoir with the same semantics as above. The result holds wild, mutant, labels, events, mutations and the sampler identity chacha8-u53-ziggurat/spatial-turnover/2. Seed s uses the same ChaCha8 stream as replicate 0 of a generic run with root seed s; do not use equal seeds for a comparison between the two. The sampler raises CrnError with CRN-R030 for invalid input, CRN-R031 for a positive weight below 16 × 2⁻⁵³ of its total (the resolution of the selection draw), CRN-R040 when the event budget is reached, and CRN-R044 when the run is cancelled.

The sampler keeps the region weights in a binary sum tree that recomputes each parent from its children, so no running total drifts. It updates the event’s region and the destination of a hop. Against the generic engine on a four-region chain with the clamp and the reservoir, five count CDFs over 20,000 paths each differed by at most 0.0099, against a simultaneous 99% bound of 0.0276. On the 500-region chain it took about 150 ns per event, 4 to 7 times less than Next Reaction. Use it through study.py --source-chain --wave-sampler local; fiber i uses seed seed + 1000 + i and its own checkpoint.

Observation models

crn_craft.likelihood maps states to counts, sums, joint measurements and intervals. It implements binomial detection and binned Gaussian fluorescence. Fluorescence bins integrate the Gaussian over the bin edges. histogram_score returns the negative log likelihood in nats per cell and the contribution of each group. Empty bins contribute zero. An occupied bin with zero retained probability makes the score infinite and is counted in impossible_bins. The optimistic score adds the missing probability of a group to each occupied bin. mean_score takes a symmetric positive definite covariance. The RNA example binds candidates to the prepared projection and supplies the score to fcmaes. The example’s legacy_tail argument reproduces the guide’s coarse overflow convention.

Published experiment files

Use crn.read_experiment(path, convention="stochastic_count_propensity") and then preflight() or run(). path is a COMBINE/OMEX archive (.omex or .zip) or a SED-ML file whose models lie in its folder. The wheel also installs crn-craft-experiment EXPERIMENT --convention stochastic_count_propensity, with --check (preflight only), --seed, --replicates, --threads and --archive DIRECTORY. DIRECTORY must not exist; it receives one sealed native run per task, named by the task ID, and experiment.json with the source member hashes, parameter changes, effective rates, events and output mappings; preflight() returns the same without simulating. Task i in document order uses seed seed + i. A refused input raises ImportFailure (CRN-E090); the command prints CRN-E090: message and exits with status 2.

FormatSupported subset
SBML L3V1 and L3V2 CoreFixed compartments of any size and dimension; species with item amount units, as amounts or as concentrations in a sized compartment; an explicit time unit; acyclic scalar parameter assignment rules, recalculated after each edit; time triggers time >= t with a literal t, instantaneous assignments to non-constant independent parameters or species counts, literal priorities
SED-ML L1V3 and L1V4Local SBML models and #model references; changeAttribute of a parameter value given as the absolute path /sbml/model/listOfParameters/parameter[@id='…']/@value (namespace prefixes allowed and not resolved); uniform time courses with Gillespie direct (KISAO:0000029) and no algorithm parameters; repeated tasks with one subtask, one uniform range and resetModel="true", which add replicates; data generators that select one time or species variable; reports and 2D plots
COMBINE/OMEXOne master SED-ML document from the manifest; members read from the archive, never extracted

Rate convention. A kinetic law is read as the jump hazard in items per model time unit, with species amounts in items and concentrations as items per compartment size. A law must equal c × Π C(n, k) for the reactants of its reaction, the convention of every native rate, so k*A*(A - 1) for 2 A → … is accepted with c = 2k, and k*A*A is refused. The explicit split_signed_mass_action convention also accepts a law u × Π C(n_reactant, k) − v × Π C(n_product, k) with u, v ≥ 0, found by exact rational arithmetic; it becomes a forward and a reverse jump, both nonnegative in every state. This handles the two signed promoter-binding laws in the published oscillator archive. Every law of that model is multiplied by the size of compartment cell (4.18879), so its rates are those of the paper times 4.18879 per archive time unit. Use the strict count convention for ordinary irreversible laws.

Grid and events. numberOfPoints (L1V3) or numberOfSteps (L1V4) counts intervals: 400 gives 401 report times from outputStartTime to outputEndTime. The model starts at initialTime. Reports at an event time show the state after the event. An event whose trigger is true at time 0 with initialValue="true" never fires; with initialValue="false" it fires at time 0. Events at the same time need distinct priorities and run from the highest; their assignments read the state before that time unless useValuesFromTriggerTime="false". State triggers, delays, assignments that read species, and events before initialTime are refused.

Input safety and limits. Every XML document the package reads (SED-ML, SBML, the archive manifest, and the SBML of crn.importers.import_sbml) goes through one guard. It refuses a document that is not bytes, is larger than 16 MiB, contains NUL bytes (UTF-16 and UTF-32, with or without a byte order mark), declares an encoding other than UTF-8, is not valid UTF-8, nests elements deeper than 256 levels or is not well-formed XML. It also refuses <!DOCTYPE and <!ENTITY in any letter case anywhere in the bytes, including inside a comment or a CDATA section, so an internal subset, a parameter entity, an external entity and an entity-expansion document are never parsed, and no entity is expanded or external resource opened. A byte order mark, processing instructions, comments and CDATA are otherwise read as XML. Model sources must be relative paths inside the archive or the SED-ML folder; .., absolute paths, URLs, URNs and links are refused. Limits: 16 MiB per XML document, 64 MiB per archive and for its declared uncompressed members, 256 members, 64 species, 4,096 parameters and compartments, 1,024 reactions, 256 events, and 16,777,216 returned values (paths × report times × species) per run.

Not supported: SBML Level 2 (the official L1V3_repeated-stochastic-runs.omex is refused), local functions, algebraic and rate rules, other KiSAO methods, seeds or other algorithm parameters, data generator expressions, setValue in repeated tasks, and report labels (the series are keyed by data set ID). Unsupported constructs and unresolved units produce a diagnostic during translation. SBML · SED-ML

Verification. validation/check_paper_capabilities.py runs offline in CI: a two-compartment pulse with a non-unit volume against the same native protocol (32 paths, identical counts), the pair hazard (survival 0.3535 at 4,000 paths against e^−1 = 0.3679), derived rates after an edit, event timing, and 33 refused inputs. CI also downloads the oscillator archive and SBML Test Suite stochastic case 00001 by commit and checks their SHA-256. Both oscillator tasks give 401 report times; the decay rate of R is 0.837758 and 0.209440 per archive time unit. validation/check_public_experiments.py compares the counts of A and R at times 0.5, 1 and 2 with a separately written direct SSA of the same reactions: at 512 paths per task the largest of 12 two-sample CDF gaps was 0.066 against a simultaneous bound of 0.123, which detects an edit that is not applied but not a 10% change of one rate. With --vilar-paths 8704 (about 3 minutes on one core) the largest gap was 0.023 against 0.030. In a review run with a reference SSA changed on purpose, 512 paths missed a 10% change of the R decay rate or of the promoter unbinding rate (largest gaps 0.086 and 0.057), and 8,704 paths caught both (0.062 and 0.038). Case 00001 at 5,000 paths and 50 report times gave |Z| ≤ 1.71 and |Y| ≤ 2.73, within the suite’s ranges of 3 and 5. These runs exercise the two 2-unit tasks of the deposited experiment, not the paper’s long oscillation.