Skip to content

Commit 3149f86

Browse files
committed
improve SDE simulation desc
1 parent 110bfa7 commit 3149f86

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

docs/src/model_simulation/simulation_introduction.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,34 @@ sol = solve(sprob, STrapezoid())
187187
sol = solve(sprob, STrapezoid(); seed = 123) # hide
188188
plot(sol)
189189
```
190-
we can see that while this simulation (unlike the ODE ones) exhibits some fluctuations. Next, we reduce species amounts (using [`remake`](@ref ref)), thereby also increasing the relative amount of noise, we encounter a problem when the model is simulated:
190+
we can see that while this simulation (unlike the ODE ones) exhibits some fluctuations.
191+
192+
!!! note
193+
Unlike for ODE and jump simulations, there are no good heuristics for automatically selecting suitable SDE solvers. Hence, for SDE simulations a solver must be provided. `STrapezoid` will work for a large number of cases. When this is not the case, however, please check the list of [available SDE solvers](https://docs.sciml.ai/DiffEqDocs/stable/solvers/sde_solve/) for a suitable alternative (making sure to select one compatible with non-diagonal noise and the [Ito interpretation]https://en.wikipedia.org/wiki/It%C3%B4_calculus).
194+
195+
### [Common SDE simulation pitfalls](@id simulation_intro_SDEs_pitfalls)
196+
Next, let us reduce species amounts (using [`remake`](@ref ref)), thereby also increasing the relative amount of noise, we encounter a problem when the model is simulated:
191197
```@example simulation_intro_sde
192198
sprob = remake(sprob; u0 = [:X1 => 0.33, :X2 => 0.66])
193199
sol = solve(sprob, STrapezoid())
194200
sol = solve(sprob, STrapezoid(); seed = 1234567) # hide
195201
plot(sol)
196202
```
197-
Here, we receive a warning that the simulation was terminated before the designated final time point, also suggesting that an instability might have been encountered. What has happened here is that the stochastic fluctuations have grown too large for the adaptive time-stepper to handle. This can be remedied by disabling adaptive time-stepping and setting a low `dt`:
203+
Here, we receive a warning that the simulation was aborted. In the plot, we also see that it is incomplete. In this case we also note that species concentrations are very low (and sometimes, due to the relatively high amount of noise, even negative). This, combined with the early termination, suggests that we are simulating our model for too low species concentration for the assumptions of the CLE to hold. Instead, [jump simulations](@ref simulation_intro_jumps) should be used.
204+
205+
Next, let us consider a simulation for another parameter set:
198206
```@example simulation_intro_sde
199-
sol = solve(sprob, STrapezoid(); adaptive = false, dt = 0.001)
200-
sol = solve(sprob, STrapezoid(); seed = 1234567, adaptive = false, dt = 0.001) # hide
207+
sprob = remake(sprob; u0 = [:X1 => 100.0, :X2 => 200.0], p = [:k1 => 200.0, :k2 => 500.0])
208+
sol = solve(sprob, STrapezoid())
209+
sol = solve(sprob, STrapezoid(); seed = 12345) # hide
210+
plot(sol)
211+
```
212+
Again, the simulation is aborted. This time, however, species concentrations are relatively large, so the CLE might still hold. What has happened this time is that the accuracy of the simulations has not reached its desired threshold. This can be deal with [by reducing simulation tolerances](@ref ref):
213+
```@example simulation_intro_sde
214+
sol = solve(sprob, STrapezoid(), abstol = 1e-1, reltol = 1e-1)
215+
sol = solve(sprob, STrapezoid(); seed = 12345, abstol = 1e-1, reltol = 1e-1) # hide
201216
plot(sol)
202217
```
203-
However, SDE solver instability (combined with the fact that the fluctuations push the solution to negative values!) suggests that species concentrations (for this combination of model, initial conditions, and parameter values) are too low for the CLE's approximating assumptions to hold. Instead, [jump simulations](@ref simulation_intro_jumps) should be used.
204-
205-
!!! note
206-
Unlike for ODE and jump simulations, there are no good heuristics for automatically selecting suitable SDE solvers. Hence, for SDE simulations a solver must be provided. `STrapezoid` will work for a large number of cases. When this is not the case, however, please check the list of [available SDE solvers](https://docs.sciml.ai/DiffEqDocs/stable/solvers/sde_solve/) for a suitable alternative (making sure to select one compatible with non-diagonal noise and the [Ito interpretation]https://en.wikipedia.org/wiki/It%C3%B4_calculus).
207218

208219
### [Scaling the noise in the chemical Langevin equation](@id simulation_intro_SDEs_noise_saling)
209220
When using the CLE to generate SDEs from a CRN, it can sometimes be desirable to scale the magnitude of the noise. This can be done by introducing a *noise scaling term*, with each noise term generated by the CLE being multiplied with this term. A noise scaling term can be set using the `@default_noise_scaling` option:
@@ -213,11 +224,11 @@ two_state_model = @reaction_network begin
213224
(k1,k2), X1 <--> X2
214225
end
215226
```
216-
Here, we set the noise scaling term to `0.1`, reducing the noise with a factor $10$ (noise scaling terms $>1.0$ increase the noise, while terms $<1.0$ reduce the noise). If we simulate the modified model using the low-concentration inputs used previously, we see that the noise has been reduced (in fact by so much that the model can now be simulated without issues):
227+
Here, we set the noise scaling term to `0.1`, reducing the noise with a factor $10$ (noise scaling terms $>1.0$ increase the noise, while terms $<1.0$ reduce the noise). If we re-simulate the model using the low-concentration settings used previously, we see that the noise has been reduced (in fact by so much that the model can now be simulated without issues):
217228
```@example simulation_intro_sde
218-
u0 = [:X1 => 0.33, :X2 => 0.66]
229+
u0 = [:X1 => 100.0, :X2 => 200.0]
219230
tspan = (0.0, 1.0)
220-
ps = [:k1 => 2.0, :k2 => 5.0]
231+
ps = [:k1 => 200.0, :k2 => 500.0]
221232
sprob = SDEProblem(two_state_model, u0, tspan, ps)
222233
sol = solve(sprob, STrapezoid())
223234
sol = solve(sprob, STrapezoid(); seed = 123) # hide

0 commit comments

Comments
 (0)