# In[6]: from pyndamics import Simulation from pyndamics.emcee import * # In[7]: t=array([7,14,21,28,35,42,49,56,63,70,77,84],float) h=array([17.93,36.36,67.76,98.10,131,169.5,205.5,228.3,247.1,250.5,253.8,254.5]) sim=Simulation() sim.add("h'=a*h*(1-h/K)",1,plot=True) sim.add_data(t=t,h=h,plot=True) sim.params(a=1,K=500) sim.run(0,90) # fig=sim.figures[0] # fig.savefig('sunflower_logistic1.pdf') # fig.savefig('sunflower_logistic1.png') # In[8]: model=MCMCModel(sim, a=Uniform(.001,5), K=Uniform(100,500), initial_h=Uniform(0,100), )
# \frac{dS}{dt} = - \beta S I /N # $$ # # $$ # \frac{dI}{dt} = + \beta S I /N - \gamma I # $$ # In[22]: sim=Simulation() sim.add("N=S+I+R") sim.add(" S' = -β*S*I/N",1000) sim.add(" I' = +β*S*I/N - γ*I",1) sim.add(" R' = +γ*I",0) sim.add_data(t=t_data,S=susceptible_data) sim.add_data(t=t_data,I=infected_data) sim.params(β=0.3,γ=0.1) sim.run(0,10) # In[23]: figure(figsize=(8,4)) t,S,I=sim.t,sim.S,sim.I subplot(2,1,1) plot(t,S,'-') # models plotted with solid line plot(t_data,susceptible_data,'o') # data with markers
64.06406406, 68.06806807, 72.07207207, 76.07607608 ]), array([ -16.52420711, 0.83399691, 26.29032381, 92.46966588, 192.82511069, 360.21219894, 472.59465177, 516.91547329, 480.05384666, 463.98953003, 501.72818401, 456.0508904, 468.98941972, 512.73730732, 479.49668258, 483.76056329, 466.88367256, 501.29703993, 501.26889691, 486.47837681 ])) # In[24]: sim = Simulation() sim.add("y'=r*y*(1-y/K)", 1) sim.params(r=1, K=200) sim.add_data(t=x_data, y=y_data) sim.run(0, 80) # In[25]: x = sim.t y = sim.y plot(x, y, 'b-') plot(x_data, y_data, 'bo') # ### make a statistical model and fit with MCMC...may take a while (2-5 min) to run when you do the run_mcmc line # In[26]: model = MCMCModel(sim, r=Uniform(0, 1), K=Uniform(300, 700))
plot(t, h, '-o') xlabel('Days') ylabel('Height [cm]') # <markdowncell> # ### Run an initial simulation # # Here, the constant value ($a=1$) is hand-picked, and doesn't fit the data particularly well. # <codecell> sim = Simulation() sim.add("h'=a", 1, plot=True) sim.add_data(t=t, h=h, plot=True) sim.params(a=1) sim.run(0, 12) # <markdowncell> # ### Fit the model parameter, $a$ # # Specifying the prior probability distribution for $a$ as uniform between -10 and 10. # <codecell> model = MCMCModel(sim, {'a': [-10, 10]}) model.fit(iter=25000) # <markdowncell>
44.54454454, 46.3963964 , 46.996997 , 47.2972973 , 48.14814815, 48.74874875]), array([ 1.14606519, 0.78658793, 0.15507708, 0.64491796, 0.3834882 , -0.65511548, -0.89478528, -0.24951861, 1.06321321, -0.04609502, 0.34452878, -0.18088143, -0.44538382, -0.72407121, -1.16075082, -0.45408912, 0.95732637, 0.83271397, 0.20881338, -0.03971536, -0.1518789 , -0.78984006, -0.83559018, -1.32878715, -0.99501354, -0.74628129, -0.24701692, -0.03410385, 0.95727806, 1.07165818])) # In[43]: sim=Simulation() sim.add("M'=1/(1+E)-alpha",1,plot=True) sim.add("E'=M-beta",1,plot=True) sim.add_data(t=t_data,M=M_data,plot=True) sim.params(alpha=.4,beta=0.04) sim.run(0,50) # In[45]: model=MCMCModel(sim, alpha=Uniform(0,5), beta=Uniform(0,5)) model.run_mcmc(500) # In[46]: model.plot_chains()
# In[3]: t=array([1,2,2.5,3,3.5,4,4.5,5.5,6,7.5,8.5,10.5]) y=array([50.6,47.3,45.8,43.9,42.2,40.4,38.3,35,33.5,29,26.3,21.6]) plot(t,y,'-o') # In[5]: sim=Simulation() sim.add("θ' = -Z*θ",53,plot=True) sim.params(Z=1) sim.add_data(t=t,θ=y,plot=True) sim.run(0,11) # In[ ]: # In[8]: sim=Simulation() sim.add("θ' = -Z*θ",53,plot=True) sim.params(Z=.01)
from pyndamics.emcee import * # <markdowncell> # ## Artificial Example with Mice Population # <codecell> data_t = [0, 1, 2, 3] data_mouse = [2, 5, 7, 19] sim = Simulation() # get a simulation object sim.add("mice'=b*mice - d*mice", 2, plot=True) # the equations # initial value # display a plot, which is the default sim.add_data(t=data_t, mice=data_mouse, plot=True) sim.params(b=1.1, d=0.08) # specify the parameters sim.run(5) # <codecell> model = MCMCModel(sim, b=Uniform(0, 10)) # <codecell> model.set_initial_values() model.plot_chains() # <codecell> model.run_mcmc(500)
# ## Artificial Example with Mice Population # In[12]: data_t = [0, 1, 2, 3] data_mouse = [2, 5, 7, 19] sim = Simulation() # get a simulation object sim.add( "mice'=b*mice - d*mice", # the equations 2, # initial value plot=True) # display a plot, which is the default sim.add_data(t=data_t, mice=data_mouse, plot=True) sim.params(b=1.1, d=0.08) # specify the parameters sim.run(5) # In[13]: model = MCMCModel(sim, b=Uniform(0, 10)) # In[14]: model.set_initial_values() model.plot_chains() # In[15]: model.run_mcmc(500)
# ## Artificial Example with Mice Population # <codecell> data_t = [0, 1, 2, 3] data_mouse = [2, 5, 7, 19] sim = Simulation() # get a simulation object sim.add( "mice'=b*mice - d*mice", # the equations 2, # initial value plot=True) # display a plot, which is the default sim.add_data(t=data_t, mice=data_mouse, plot=True) sim.params(b=1.1, d=0.08) # specify the parameters sim.run(5) # <codecell> model = MCMCModel(sim, {'b': [0, 10]}) model.fit(iter=25000) # <codecell> model.b # <codecell> sim.run(5)
# ## adding some data # In[31]: t_data = array([7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84], float) h_data = array([ 17.93, 36.36, 67.76, 98.10, 131, 169.5, 205.5, 228.3, 247.1, 250.5, 253.8, 254.5 ]) sim = Simulation() sim.figsize = (6, 4) sim.add(" y'=r*y*(1-y/K) ", 1, plot=True) sim.params(r=.2, K=270) sim.add_data(t=t_data, y=h_data, plot=True) sim.run(0, 100) # In[35]: model = MCMCModel(sim, r=Uniform(0, 1), K=Uniform(100, 400)) # In[36]: model.run_mcmc(500) # In[37]: model.plot_chains() # In[38]:
return beta1 + (beta0 - beta1)**(-q * (t - tau)) else: return beta0 # <codecell> N = 1000000 sim = Simulation() sim.add("S'=-beta(t)*(S*I)/N", N, plot=False) sim.add("E'=-beta(t)*(S*I)/N - (E/invk)", 135, plot=1) sim.add("I'=(E/invk) - (1/invGamma *I)", 136, plot=1) sim.add("R'=(1/invGamma*I)", 0, plot=False) sim.params(N=N, k=1 / 6.3, q=0.1000, invGamma=5.5000, invk=6.3) sim.functions(beta) sim.add_data(t=numOfDays, I=numOfCases, plot=1) sim.run(0, 350) # <codecell> model = MCMCModel( sim, { 'beta0': [0, 1], 'invGamma': [3.5, 10.7], 'beta1': [0, 1], 'q': [0, 100], 'tau': [100, 150], 'invk': [5, 22] }) #model = MCMCModel(sim,{'invGamma':[3.5,10.7],'q':[0,10]}) model.fit(iter=500)
1.14606519, 0.78658793, 0.15507708, 0.64491796, 0.3834882, -0.65511548, -0.89478528, -0.24951861, 1.06321321, -0.04609502, 0.34452878, -0.18088143, -0.44538382, -0.72407121, -1.16075082, -0.45408912, 0.95732637, 0.83271397, 0.20881338, -0.03971536, -0.1518789, -0.78984006, -0.83559018, -1.32878715, -0.99501354, -0.74628129, -0.24701692, -0.03410385, 0.95727806, 1.07165818 ])) # In[43]: sim = Simulation() sim.add("M'=1/(1+E)-alpha", 1, plot=True) sim.add("E'=M-beta", 1, plot=True) sim.add_data(t=t_data, M=M_data, plot=True) sim.params(alpha=.4, beta=0.04) sim.run(0, 50) # In[45]: model = MCMCModel(sim, alpha=Uniform(0, 5), beta=Uniform(0, 5)) model.run_mcmc(500) # In[46]: model.plot_chains() # In[49]: model.set_initial_values('samples')
t = t[(t >= 2017.5) & (t <= 2018.5)] t = (t - t[0]) * 365.25 # In[18]: plot(t, y, '-o') # In[19]: sim = Simulation() sim.add("N=S+I+R") sim.add(" S' = -β*S*I/N", 3e8) sim.add(" I' = +β*S*I/N - γ*I", 1, plot=True) sim.add(" R' = +γ*I", 0) sim.add_data(t=t, I=y, plot=True) sim.params(β=0.3, γ=0.1) sim.run(0, 365) # In[27]: sim = Simulation() sim.add("N=S+I+R") sim.add(" S' = -β*S*I/N", 3e8) sim.add(" I' = +β*S*I/N - γ*I", 1, plot=True) sim.add(" R' = +γ*I", 0) sim.add_data(t=t, I=y, plot=True) sim.params(β=0.08, γ=0.06) sim.run(0, 365) # In[29]: