1
- # Not actually used: just for comparison
1
+ using DifferentialEquations
2
+
3
+ infection = Reaction (0.1 / 1000 ,[1 ,2 ],[(1 ,- 1 ),(2 ,1 )])
4
+ recovery = Reaction (0.01 ,[2 ],[(2 ,- 1 ),(3 ,1 )])
5
+ sir_prob = DiscreteProblem ([999 ,1 ,0 ],(0.0 ,250.0 ))
6
+ sir_jump_prob = GillespieProblem (sir_prob,Direct (),infection,recovery)
7
+
8
+ sir_sol = solve (sir_jump_prob,Discrete ())
9
+
10
+ using Plots; plot (sir_sol)
11
+
12
+ srand (1234 )
13
+ nums = Int[]
14
+ @time for i in 1 : 100000
15
+ sir_sol = solve (sir_jump_prob,Discrete ())
16
+ push! (nums,sir_sol[end ][3 ])
17
+ end
18
+ println (" Reaction DSL: $(mean (nums)) " )
19
+
20
+
21
+ using DiffEqJump, DiffEqBase, OrdinaryDiffEq
22
+ using Base. Test
23
+
24
+ rate = (t,u) -> (0.1 / 1000.0 )* u[1 ]* u[2 ]
25
+ affect! = function (integrator)
26
+ integrator. u[1 ] -= 1
27
+ integrator. u[2 ] += 1
28
+ end
29
+ jump = ConstantRateJump (rate,affect!;save_positions= (false ,true ))
30
+
31
+ rate = (t,u) -> 0.01 u[2 ]
32
+ affect! = function (integrator)
33
+ integrator. u[2 ] -= 1
34
+ integrator. u[3 ] += 1
35
+ end
36
+ jump2 = ConstantRateJump (rate,affect!;save_positions= (false ,true ))
37
+
38
+
39
+ prob = DiscreteProblem ([999.0 ,1.0 ,0.0 ],(0.0 ,250.0 ))
40
+ jump_prob = JumpProblem (prob,Direct (),jump,jump2)
41
+ sol = solve (jump_prob,Discrete (apply_map= false ))
42
+
43
+ using Plots; plot (sol)
44
+
45
+ nums = Int[]
46
+ @time for i in 1 : 100000
47
+ sol = solve (jump_prob,Discrete (apply_map= false ))
48
+ push! (nums,sol[end ][3 ])
49
+ end
50
+ println (" Direct Jumps: $(mean (nums)) " )
51
+
2
52
3
53
using Gillespie
4
- using Gadfly
5
54
6
55
function F (x,parms)
7
56
(S,I,R) = x
@@ -18,23 +67,9 @@ tf = 250.0
18
67
srand (1234 )
19
68
20
69
nums = Int[]
21
- @time for i in 1 : 1000
70
+ @time for i in 1 : 100000
22
71
result = ssa (x0,F,nu,parms,tf)
23
72
data = ssa_data (result)
24
73
push! (nums,data[:x3 ][end ])
25
74
end
26
- mean (nums)
27
-
28
- result = ssa (x0,F,nu,parms,tf)
29
- data = ssa_data (result)
30
-
31
- p= plot (data,
32
- layer (x= " time" ,y= " x1" ,Geom. step,Theme (default_color= color (" red" ))),
33
- layer (x= " time" ,y= " x2" ,Geom. step,Theme (default_color= color (" blue" ))),
34
- layer (x= " time" ,y= " x3" ,Geom. step,Theme (default_color= color (" green" ))),
35
- Guide. xlabel (" Time" ),
36
- Guide. ylabel (" Number" ),
37
- Guide. manual_color_key (" Population" ,
38
- [" S" , " I" , " R" ],
39
- [" red" , " blue" , " green" ]),
40
- Guide. title (" SIR epidemiological model" ))
75
+ println (" Gillespie: $(mean (nums)) " )
0 commit comments