Skip to content

Commit 0460c8a

Browse files
committed
Added missing troubleshooting page
1 parent 940925e commit 0460c8a

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

docs/src/troubleshoot.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# [Troubleshooting](@id troubleshoot)
2+
3+
Solving the Chemical Master Equation numerically is a difficult task and errors are liable to crop up. The following section presents some ways to catch common errors.
4+
5+
## Ensure your state space has the right dimension
6+
7+
If your are solving an SIR model with three species, ``S``, ``I`` and ``R``, your state space will be 3-dimensional. FiniteStateProjection.jl computes probabilities for all states simultaneously and stores the results in a 3-dimensional array. In particular, `u0` must have type `Float64` (or similar).
8+
9+
```julia
10+
# correct
11+
u0 = zeros(101, 101, 101)
12+
u0[100,2,1] = 1.0 # start with 1 infected and 99 susceptible individuals
13+
14+
# incorrect
15+
u0 = [99,1,0] # wrong type (Int) and shape (1D)
16+
```
17+
18+
## Ensure your state space is big enough
19+
20+
A common reason for the solver returning nonsensical solutions is a state space that is too small. Since the Finite State Projection works with a finite-dimensional approximation of the system, the number of states considered can have a large impact on accuracy. The loss of accuracy due to using a smaller state space is the truncation error.
21+
22+
A good way to check whether your state space is large enough is to solve the CME until the required time ``t`` and to sum up the probabilities for each state - this gives the probability that the system will have remained in the truncated state space from start to end. If this quantity is noticeably less than 1, the state space is likely too small. As an informal rule of thumb, a value of less than ``95\%`` indicates that the solution will not be reliable.
23+
24+
```julia
25+
# always true
26+
sum(u0) == 1 # our initial condition lies in the truncated state space
27+
28+
# good
29+
sum(ut) >= 0.99 # our truncation covers most of the relevant states
30+
31+
# bad
32+
sum(ut) < 0.95 # we do not have enough coverage
33+
```
34+
35+
The above does not work directly when computing steady-state probabilities as the value will usually drop to 0 for large enough ``t``. In this case it is a good idea to redo the computation with a larger state space - the results should agree if the truncation error is small.
36+
37+
## Ensure your propensities are positive
38+
39+
This point might seem obvious, but errors in the rate functions, or an incorrectly chosen truncation, can lead to negative reaction propensities that will typically result in numerical instabilities. As an example, consider the following version of the SI model where the population size ``S + I = N`` is constant, allowing us to rewrite the system using only one species ``I`` (with ``S = N - I``):
40+
41+
```julia
42+
rn = @reaction_network begin
43+
σ * (N - I), I --> 2I
44+
ρ, I --> 0
45+
end σ ρ N
46+
47+
sys_fsp = FSPSystem(rn)
48+
```
49+
50+
Here the propensity function for the first reaction will negative if ``I > N``, so the following may result in numerical instabilities:
51+
52+
```julia
53+
u0 = zeros(30)
54+
u0[2] = 1
55+
56+
prob_fsp = convert(ODEProblem, sys_fsp, u0, (0, 100.), [ 1., 1., 20 ]) # N is too small for the state space!
57+
```
58+
59+
## Ensure you are using the right solver
60+
61+
The Chemical Master Equation is generally very stiff and requires a solver that can handle this stiffness, see [Tips & Tricks](@ref tips). If your solver fails, first check if any of the above points apply. You may be able to get a different solver to work; this requires some experimentation. Anecdotally some systems, particularly oscillatory ones such as the Schlögl model, can pose significant challenges to most solvers and take inordinate amounts of time to solve. I am not aware of any solutions for this issue and would appreciate any tips for getting such systems to work (please consider opening an issue on GitHub if you encounter such examples).

0 commit comments

Comments
 (0)