コード例 #1
0
#
# Pyndamics provides a way to describe a dynamical system in terms of the differential equations, or the stock-flow formalism. It is a wrapper around the Scipy odeint function, with further functionality for time plots, phase plots, and vector fields.
#
# 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
コード例 #2
0
# 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[ ]:



コード例 #3
0
t = array([0, 2, 4, 6, 8, 10, 12])
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)
コード例 #4
0
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),
コード例 #5
0
from pyndamics import Simulation
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]:

コード例 #6
0
# -*- 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,
)
コード例 #7
0
# 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()
コード例 #8
0
t=array([0,2,4,6,8,10,12])
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[22]:


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):
    
コード例 #10
0
# 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[ ]:
コード例 #11
0
print numOfDays

# <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],
コード例 #12
0
# 
# Page for this package: [https://code.google.com/p/pyndamics/](https://code.google.com/p/pyndamics/)

# <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>
コード例 #13
0
# In[37]:

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
コード例 #14
0
5822434	,
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

コード例 #15
0
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]:
コード例 #16
0
from pylab import *

# 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]:
コード例 #17
0
#
# Page for this package: [https://code.google.com/p/pyndamics/](https://code.google.com/p/pyndamics/)

# <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>
コード例 #18
0
ファイル: Oscillations.py プロジェクト: bblais/pyndamics
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
コード例 #19
0
# 
# Page for this package: [https://code.google.com/p/pyndamics/](https://code.google.com/p/pyndamics/)

# <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>
コード例 #20
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-')
コード例 #21
0
# <codecell>

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)
コード例 #22
0
# 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]:
コード例 #23
0
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),
コード例 #24
0
# -*- 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) 
コード例 #25
0
# <codecell>

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()
コード例 #26
0
infected_data = np.array([1,2,5,6,7,8,9,11,13,15])
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 = 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)