コード例 #1
0
# 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)
sim.add_data(t=t,θ=y,plot=True)
コード例 #2
0
#!/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-')
コード例 #3
0
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>

# What is the best fit parameter value?
コード例 #4
0
ファイル: Oscillations.py プロジェクト: bblais/pyndamics
# ### 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
コード例 #5
0
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[41]:
コード例 #6
0
# 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)
model.plot_chains()
コード例 #7
0
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),
)

# In[11]:
コード例 #8
0
# 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),
                )

コード例 #9
0
# ## 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)
sim.inflow("mice", "b*mice")
sim.outflow("mice", "d*mice")

sim.params(b=1.1, d=0.08)
コード例 #10
0
# ## 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>

sim=Simulation()   # get a simulation object

sim.stock("mice",100,plot=False)
sim.inflow("mice","b*mice")
コード例 #11
0
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)
sim.add(" x'=r2*x*(1-x/K2) ", 1, plot=1)
sim.params(r=2, K=50, r2=3, K2=50)
# 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):
        value=value*i
        
    return value
コード例 #13
0
    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)
model.plot_distributions()
コード例 #14
0
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>

# What is the best fit parameter value?
コード例 #15
0
# <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)
model.plot_chains()
コード例 #16
0
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) 

# <codecell>

sim=Simulation()
sim.add("x1''=(-b1 * x1' - k1 * (x1 - L1) + k2 * (x2 - x1 - L2)) / m1",[0.5,0.0],plot=[1,2])
sim.add("x2''=(-b2 * x2' - k2 * (x2 - x1 - L2)) / m2",[2.25,0.0],plot=[1,2])
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,
        )
コード例 #17
0
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[ ]:




コード例 #18
0
# $$
# \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
ylabel('S')
gca().set_xticklabels([])
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]:

t1 = t[t < 200]