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

Source Code for Module BIP.SDE.gillespie

  1  # -*- coding:utf-8 -*- 
  2  #----------------------------------------------------------------------------- 
  3  # Name:        gillespie.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  from numpy.random import uniform, multinomial, exponential,random 
 15  from numpy import arange, array, empty,zeros,log 
 16  #from math import log 
 17  import time 
 18  from multiprocessing import Pool 
 19   
 20   
 21   
 22  import psyco 
 23  psyco.full() 
 24   
 25  #global ini 
 26  #ini=[500,1,0] 
 27   
28 -class Model:
29 - def __init__(self,vnames,rates,inits, tmat,propensity):
30 ''' 31 * vnames: list of strings 32 * rates: list of fixed rate parameters 33 * inits: list of initial values of variables 34 * propensity: list of lambda functions of the form: 35 lambda r,ini: some function of rates ans inits. 36 ''' 37 self.vn = vnames 38 self.rates = rates 39 self.inits = inits 40 self.tm = tmat 41 self.pv = propensity#[compile(eq,'errmsg','eval') for eq in propensity] 42 self.pvl = len(self.pv) #length of propensity vector 43 self.nvars = len(self.inits) #number of variables 44 self.time=None 45 self.series=None 46 self.steps=0
47
48 - def getStats(self):
49 return self.time,self.series,self.steps
50
51 - def run(self,method='SSA', tmax=10, reps=1):
52 self.res = zeros((tmax,self.nvars,reps),dtype=float) 53 tvec = arange(tmax, dtype=int) 54 pool = Pool() 55 if method =='SSA': 56 #r = pool.imap(self.GSSA, ((tmax,i) for i in xrange(reps))) 57 #steps = r.get() 58 for i in xrange(reps): 59 steps = self.GSSA(tmax,i) 60 print steps,' steps' 61 elif method == 'SSAct': 62 pass 63 self.time=tvec 64 self.series=self.res 65 self.steps=steps
66 - def GSSA(self, tmax=50,round=0):
67 ''' 68 Gillespie Direct algorithm 69 ''' 70 ini = self.inits 71 r = self.rates 72 pvi = self.pv 73 l=self.pvl 74 pv = zeros(l,dtype=float) 75 tm = self.tm 76 77 tc = 0 78 steps = 0 79 self.res[0,:,round]= ini 80 a0=1 81 for tim in xrange(1,tmax): 82 while tc < tim: 83 for i in xrange(l): 84 pv[i] = pvi[i](r,ini) 85 #pv = abs(array([eq() for eq in pvi]))# #propensity vector 86 a0 = pv.sum() #sum of all transition probabilities 87 # print tim, pv, a0 88 tau = (-1/a0)*log(random()) 89 event = multinomial(1,pv/a0) # event which will happen on this iteration 90 ini += tm[:,event.nonzero()[0][0]] 91 #print tc, ini 92 tc += tau 93 steps +=1 94 if a0 == 0: break 95 self.res[tim,:,round] = ini 96 if a0 == 0: break 97 # tvec = tvec[:tim] 98 # self.res = res[:tim,:,round] 99 return steps
100
101 - def CR(self,pv):
102 """ 103 Composition reaction algorithm 104 """ 105 pass
106 107
108 -def main():
109 vars = ['s','i','r'] 110 ini= [500,1,0] 111 rates = [.001,.1] 112 tm = array([[-1,0],[1,-1],[0,1]]) 113 114 prop=[lambda r, ini:r[0]*ini[0]*ini[1],lambda r,ini:r[0]*ini[1]] 115 M = Model(vnames = vars,rates = rates,inits=ini, tmat=tm,propensity=prop) 116 t0=time.time() 117 M.run(tmax=80,reps=1000) 118 print 'total time: ',time.time()-t0
119 #print res 120 121 # from pylab import plot , show, legend 122 # plot(t,res,'-.') 123 # legend(M.vn,loc=0) 124 # show() 125 126 127 if __name__=="__main__": 128 import cProfile 129 cProfile.run('main()',sort=1) 130 main() 131