Package BIP :: Package SDE :: Module example
[hide private]
[frames] | no frames]

Source Code for Module BIP.SDE.example

 1  # -*- coding:utf-8 -*- 
 2  #----------------------------------------------------------------------------- 
 3  # Name:        example.py 
 4  # Project:  Bayesian-Inference 
 5  # Purpose:      
 6  # 
 7  # Author:      Flávio Codeço Coelho<fccoelho@gmail.com> 
 8  # 
 9  # Created:     2008-11-26 
10  # Copyright:   (c) 2008 by the Author 
11  # Licence:     GPL 
12  #----------------------------------------------------------------------------- 
13  __docformat__ = "restructuredtext en" 
14  """ 
15  Example of an SEIR model with two Infectious classes: subclinical(Is) and clinical(Ic) 
16          Is 
17         /  \ 
18  S -> E     R 
19         \  / 
20          Ic 
21           
22  States: 
23  S: Susceptible 
24  E: Exposed 
25  Is: Infectious subclinical 
26  Ic: Infectious clinical 
27  R: Recovered 
28   
29  Transition rates: 
30  b,ks,kc,rs,rc = (0.001, 0.1, 0.1, 0.01, .01) 
31  Transitions: 
32  S -> E : b*S*(Is+Ic) 
33  E -> Is : ks*E 
34  E -> Ic : kc*E 
35  Is -> R : rs*Is 
36  Ic -> R : rc*Ic 
37   
38  """ 
39  from gillespie import Model 
40  import time 
41  from numpy import array 
42  vars = ['S','E','Is','Ic','R'] 
43  #rates: b,ks,kc,rs,rc 
44   
45  r = (0.001, 0.1, 0.1, 0.01, .01) 
46  ini = (490,0,10,0,0) 
47  prop = (lambda r,ini:r[0]*ini[0]*(ini[2]+ini[3]), 
48          lambda r,ini:r[1]*ini[1], 
49          lambda r,ini:r[2]*ini[1], 
50          lambda r,ini:r[3]*ini[2], 
51          lambda r,ini:r[4]*ini[3] 
52          ) 
53   
54  tmat = array([[-1,0,0,0,0], 
55              [1,-1,-1,0,0], 
56              [0,1,0,-1,0], 
57              [0,0,1,0,-1], 
58              [0,0,0,1,1] 
59              ]) 
60  #for e in prop: 
61  #    print e() 
62  M=Model(vnames=vars,rates = r,inits=ini,tmat=tmat,propensity=prop) 
63  t0 = time.time() 
64  M.run(tmax=80,reps=100) 
65  print 'total time: ',time.time()-t0 
66  t,series,steps = M.getStats() 
67  print steps,'steps' 
68  from pylab import plot , show, legend, errorbar 
69  plot(t,series.mean(axis=2),'-o') 
70  legend(vars,loc=0) 
71  show() 
72