# coding: utf-8 # In[1]: from pyndamics import Simulation from pyndamics.emcee import * # In[2]: sim=Simulation() sim.add("y'=a*y*(1-y/K)",.1,plot=True) sim.params(a=1,K=10) sim.run(0,10) # In[3]: model=MCMCModel(sim,a=Uniform(.001,5),K=Uniform(6,20),initial_h=Normal(4)) # In[ ]:
578759 ,] # In[99]: from pyndamics import Simulation # In[105]: state='Rhode Island' S0=populations[states.index(state)] sim=Simulation() sim.add("N=S+I+R+X") sim.add("S'=-β*S*I/N",S0) sim.add("I'=+β*S*I/N-γ*I-γ2*I",1,plot=True) sim.add("R'=+γ*I",0,plot=True) sim.add("X'=+γ2*I",0,plot=True) sim.params(β=.2,γ=4*24e-3,γ2=1*24e-3) sim.run(10*24) # In[111]: d['Rhode Island'].I # In[ ]:
get_ipython().magic('pylab inline') # 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),
# Page for this package: [https://code.google.com/p/pyndamics/](https://code.google.com/p/pyndamics/) # In[1]: from pyndamics import Simulation # ## Population of Mice - Exponential Growth # # ### Specifying the Differential Equation # In[2]: sim = Simulation() # get a simulation object sim.add( "mice'=b*mice - d*mice", # the equations 100, # initial value plot=True) # display a plot sim.params(b=1.1, d=0.08) sim.run(0, 4) # fig=sim.figures[0] # fig.savefig('mice.pdf') # fig.savefig('mice.png') # ### Specifying the Inflows/Outflows # In[3]: sim = Simulation() # get a simulation object
# -*- coding: utf-8 -*- # <nbformat>3.0</nbformat> # <codecell> from pyndamics import Simulation # <markdowncell> # From http://wiki.scipy.org/Cookbook/CoupledSpringMassSystem # <codecell> sim = Simulation() sim.add("x1'=y1", 0.5, plot=1) sim.add("y1'=(-b1 * y1 - k1 * (x1 - L1) + k2 * (x2 - x1 - L2)) / m1", 0.0, plot=False) sim.add("x2'=y2", 2.25, plot=1) sim.add("y2'=(-b2 * y2 - k2 * (x2 - x1 - L2)) / m2", 0.0, plot=False) sim.params( m1=1.0, # masses m2=1.5, # masses k1=8.0, # Spring constants k2=40.0, L1=0.5, # Natural lengths L2=1.0, b1=0.8, # Friction coefficients b2=0.5, )
h = array([1.0, 1.5, 2.2, 3.2, 4.3, 5.2, 5.6]) 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)
# In[34]: 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 ]) plot(t, h, '-o') xlabel('Days') ylabel('Height [cm]') # In[16]: sim = Simulation() sim.add(" y'=r*y*(1-y/K) ", 1, plot=True) sim.params(r=2, K=50) sim.run(0, 20) # In[20]: sim = Simulation() sim.add(" x'=v ", 3, plot=True) sim.add(" v'=-k*x/m ", 0, plot=False) sim.params(k=2, m=1) sim.run(0, 20) # In[23]: sim = Simulation() sim.add(" y'=r*y*(1-y/K) ", 1, plot=1)
from pyndamics.emcee import * # 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()
pip install "git+git://github.com/bblais/pyndamics" --upgrade # In[23]: from pyndamics import Simulation # In[28]: sim=Simulation() sim.add(" x' = v ",0,plot=True) sim.add(" v' = -k*x/m ",3,plot=True) sim.params(k=2,m=1) sim.run(100) # # infinite integers # In[29]: def factorial(N): value=1 for i in range(1,N+1):
# coding: utf-8 # In[1]: from pyndamics import Simulation from pyndamics.emcee import * # In[2]: sim = Simulation() sim.add("y'=a*y*(1-y/K)", .1, plot=True) sim.params(a=1, K=10) sim.run(0, 10) # In[3]: model = MCMCModel(sim, a=Uniform(.001, 5), K=Uniform(6, 20), initial_h=Normal(4)) # In[ ]:
# <codecell> def beta(t): if t >= tau: 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],
# <codecell> from pyndamics import Simulation # <markdowncell> # ## Population of Mice - Exponential Growth # # ### Specifying the Differential Equation # <codecell> sim=Simulation() # get a simulation object sim.add("mice'=b*mice - d*mice", # the equations 100, # initial value plot=True) # display a plot sim.params(b=1.1,d=0.08) sim.run(0,4) # <markdowncell> # ### Specifying the Inflows/Outflows # <codecell> sim=Simulation() # get a simulation object sim.stock("mice",100,plot=False)
from pyndamics import Simulation from pyndamics.emcee import * from numpy import array # ### Make the Data # In[38]: from numpy.random import randint, randn from numpy import array # In[39]: sim = Simulation() sim.add("M'=1/(1+E)-alpha", 1, plot=False) sim.add("E'=M-beta", 1, plot=False) sim.params(alpha=.4, beta=0.04) sim.run(0, 50) # In[40]: L = len(sim.t) N = 30 idx = randint(0, L, N) idx = sorted(idx) tt = array(sim.t[idx]) MM = array(sim.M[idx]) + randn(N) * .1 * (max(sim.M) - min(sim.M)) tt, MM # ## Run the Sim
# In[11]: from pyndamics import Simulation from pyndamics.emcee import * # ## 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()
get_ipython().magic('pylab inline') # In[2]: from pyndamics import Simulation from pyndamics.emcee import * # In[6]: t=linspace(0,10,100) h=3.4*exp(.3*t) sim=Simulation() sim.add("h'=a*h",1,plot=True) sim.add_data(t=t,h=h,plot=True) sim.params(a=1) sim.run(0,10) # In[14]: model=MCMCModel(sim, a=Normal(0,10), initial_h=Uniform(0,100), ) # In[17]:
from pyndamics.emcee import * from numpy import array # ### Make the Data # In[38]: from numpy.random import randint,randn from numpy import array # In[39]: sim=Simulation() sim.add("M'=1/(1+E)-alpha",1,plot=False) sim.add("E'=M-beta",1,plot=False) sim.params(alpha=.4,beta=0.04) sim.run(0,50) # In[40]: L=len(sim.t) N=30 idx=randint(0,L,N) idx=sorted(idx) tt=array(sim.t[idx]) MM=array(sim.M[idx])+randn(N)*.1*(max(sim.M)-min(sim.M)) tt,MM
# <codecell> from pyndamics import Simulation # <markdowncell> # ## Population of Mice - Exponential Growth # # ### Specifying the Differential Equation # <codecell> sim = Simulation() # get a simulation object sim.add( "mice'=b*mice - d*mice", # the equations 100, # initial value plot=True) # display a plot sim.params(b=1.1, d=0.08) sim.run(0, 4) # <markdowncell> # ### Specifying the Inflows/Outflows # <codecell> sim = Simulation() # get a simulation object sim.stock("mice", 100, plot=False)
#!/usr/bin/env python # coding: utf-8 # In[3]: get_ipython().run_line_magic('pylab', 'inline') from sci378 import * from pyndamics import Simulation from pyndamics.emcee import * # In[4]: sim = Simulation() sim.add("y'=r*y*(1-y/K)", 1, plot=True) sim.params(r=1, K=200) sim.run(0, 80) # ## another way to do it, with some more control # In[5]: sim = Simulation() sim.add("y'=r*y*(1-y/K)", 1) sim.params(r=1, K=200) sim.run(0, 80) # In[6]: x = sim.t y = sim.y plot(x, y, 'b-')
# <codecell> from pyndamics import Simulation # <markdowncell> # ## Population of Mice - Exponential Growth # # ### Specifying the Differential Equation # <codecell> sim=Simulation() # get a simulation object sim.add("mice'=b*mice - d*mice", # the equations 100, # initial value plot=True) # display a plot sim.params(b=1.1,d=0.08) sim.run(0,4) # fig=sim.figures[0] # fig.savefig('mice.pdf') # fig.savefig('mice.png') # <markdowncell> # ### Specifying the Inflows/Outflows # <codecell>
# In[1]: get_ipython().magic('pylab inline') # In[2]: from pyndamics import Simulation from pyndamics.emcee import * # In[6]: t = linspace(0, 10, 100) h = 3.4 * exp(.3 * t) sim = Simulation() sim.add("h'=a*h", 1, plot=True) sim.add_data(t=t, h=h, plot=True) sim.params(a=1) sim.run(0, 10) # In[14]: model = MCMCModel( sim, a=Normal(0, 10), initial_h=Uniform(0, 100), ) # In[17]: model.run_mcmc(500)
from pyndamics import Simulation from pyndamics.mcmc import MCMCModel # <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", # 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
# -*- coding: utf-8 -*- # <nbformat>3.0</nbformat> # <codecell> from pyndamics import Simulation # <markdowncell> # From http://wiki.scipy.org/Cookbook/CoupledSpringMassSystem # <codecell> sim=Simulation() sim.add("x1'=y1",0.5,plot=1) sim.add("y1'=(-b1 * y1 - k1 * (x1 - L1) + k2 * (x2 - x1 - L2)) / m1",0.0,plot=False) sim.add("x2'=y2",2.25,plot=1) sim.add("y2'=(-b2 * y2 - k2 * (x2 - x1 - L2)) / m2",0.0,plot=False) sim.params( m1 = 1.0, # masses m2 = 1.5, # masses k1 = 8.0, # Spring constants k2 = 40.0, L1 = 0.5, # Natural lengths L2 = 1.0, b1 = 0.8, # Friction coefficients b2 = 0.5, ) sim.run(0,10)
from pyndamics import Simulation 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>
t_data=np.array([0,1,2,3,4,5,6,7,8,9]) # $$ # \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
t = array(data['Year'] + (data['Week'] - 1) / 52) y = array(data['ALL_INF']) y = y[(t >= 2017.5) & (t <= 2018.5)] 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)