Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions tutorial10part1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pandas
import numpy
import scipy
import scipy.integrate as spint
from plotnine import *

r_list=[-0.1,0.1,0.4,0.8,1]

def growthRate(y,to,r,K):
N=y[0]
dNdt=r* (1-N/K)* N

return [dNdt]

for i in r_list:
params=(i,100)
N0=10
times=range(0,1000)

modelSim=spint.odeint(func=growthRate,y0=N0,t=times,args=params)

@lyy005 lyy005 Nov 24, 2017

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add modelSim and store the output in a dataframe into the loop. So you can store and print out:

rs=[-0.1,0.1,0.4,0.8,1]
store_rs=pandas.DataFrame({"time":times,"r1":0,"r2":0,"r3":0,"r4":0,"r5":0})

for i in range(0,len(rs)):
pars=(rs[i],K)
sim=si.odeint(func=ddSim,y0=y0,t=times,args=pars)
store_rs.iloc[:,i]=sim[:,0]

ggplot(store_rs,aes(x="time",y="r1",color="-0.1"))+geom_line()+geom_line(aes(x="time",y="r2",color="0.1"))+geom_line(aes(x="time",y="r3",color="0.4"))+geom_line(aes(x="time",y="r4",color="0.8"))+geom_line(aes(x="time",y="r5",color="1.0"))+theme_classic()+labs(x="time",y="N")

-0.1 pts

modelOutput=pandas.DataFrame({"t":times,"N":modelSim[:,0]})
a=ggplot(modelOutput,aes(x="t",y="N"))+geom_line()+theme_classic()
a.draw()

K_list=[10,50,100]
def carryingCapacity(y,to,r,K):
N=y[0]
dNdt=r* (1-N/K)* N

return [dNdt]

for i in K_list:
params=(0.2,i)
N0=1
times=range(0,1000)

modelSim=spint.odeint(func=carryingCapacity,y0=N0,t=times,args=params)
modelOutput=pandas.DataFrame({"t":times,"N":modelSim[:,0]})
b=ggplot(modelOutput,aes(x="t",y="N"))+geom_line()+theme_classic()
b.draw()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the last plot

N_list=[1,50,100]
def popSize(y,to,r,K):
N=y[0]
dNdt=r* (1-N/K)* N

return [dNdt]

for i in N_list:
params=(0.1,50)
N0=i
times=range(0,1000)

modelSim=spint.odeint(func=popSize,y0=N0,t=times,args=params)
modelOutput=pandas.DataFrame({"t":times,"N":modelSim[:,0]})
c=ggplot(modelOutput,aes(x="t",y="N"))+geom_line()+theme_classic()
c.draw

@lyy005 lyy005 Nov 24, 2017

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the last plot

54 changes: 54 additions & 0 deletions tutorial10part2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import pandas
import numpy
import scipy
import scipy.integrate as spint
from plotnine import *

beta=[0.0005,0.005,0.0001,0.00005,0.0001,0.0002,0.0001]
gamma=[0.05,0.5,0.1,0.1,0.05,0.05,0.06]
params_dict = {}
for i in range(len(beta)):
params_dict[beta[i]] = gamma[i]

def disTrans(y, t, beta, gamma):
S=y
I=y
R=y
N=1000
dSdt = -beta * S * I / N
dIdt = beta * S * I / N - gamma * I
dRdt = gamma * I

return dSdt
return dIdt
return dRdt

@lyy005 lyy005 Nov 26, 2017

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def disTrans(y,t,beta,gamma):
S=y[0]
I=y[1]
R=y[2]
dSdt=-betaIS
dIdt=betaIS-gammaI
dRdt=gamma
I
return [dSdt,dIdt,dRdt]

for i in params_dict.keys():
params=(i,params_dict[i])
S0=999
I0=1
R0=0
times=range(0,500)

modelSim=spint.odeint(func=disTrans,y0=S0,t=times,args=params)
modelSim2=spint.odeint(func=disTrans,y0=I0,t=times,args=params)
modelSim3=spint.odeint(func=disTrans,y0=R0,t=times,args=params)
modelOutput=pandas.DataFrame({"t":times,"S":modelSim[:,0],"I":modelSim2[:,0],"R":modelSim3[:,0]})

print modelOutput

a=ggplot(modelOutput,aes(x="t",y="y0"))+geom_line(aes(x="t",y="S"),color='blue')+geom_line(aes(x="t",y="I"),color='red')+geom_line(aes(x="t",y="R"),color='green')+theme_classic()
a.draw()

for i in I.modelOutput:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for i in modelOutput:

incidence=i-(i-1)

for i in modelOutput:
prevalence= I(i) / (S(i)+ I(i)+ R(i))

for i in modelOutput:
percentAffected= (I(i)+ S(i)) / (S(i)+ I(i)+ R(i))

for i in modelOutput:
reproductionNumber= beta[i]* (S(i)+ I(i)+ R(i)) / gamma[i]

@lyy005 lyy005 Nov 26, 2017

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-0.25 pts