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
Binary file added 117109-1493-1vf424u.z0v8n9izfr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Exercise10_1(a)_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Exercise10_1(b)_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Exercise10_1(c)_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Exercise10_2_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions Paragraph
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#When there is only one person infected, and with a time frame of 500 days, the graphs
#even out to all peopel being susceptible, but recovered
#If half of the people are infected, the graph evens out to remain that 50% of people
#are still infected and 50% are susceptible after 500 days
#the different rates of beta and gamma do not significantly affect these observations
35 changes: 35 additions & 0 deletions Q1 Part C
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#Import Packages
import pandas
import numpy
from scipy.optimize import minimize
from scipy.stats import norm
import scipy.integrate as si
import re
import os
from plotnine import *

#Plot of population size with 3 populations with different initial populations
def ddSim(y,t0,r,K):
N=y[0]
dNdt=r*(1-N/K)*N
return [dNdt]

params=(0.3,10)
N0=[1,50,100]
times=range(0,50)

modelSim=si.odeint(func=ddSim,y0=N0,t=times,args=params)

rs=[0.1]
store_rs=pandas.DataFrame({"times":times,"r1":0,"r3":0,"r4":0,"r5":0})

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

modelOutput=pandas.DataFrame({"t":times,"rs":modelSim[:,0]})

#plot
ggplot(modelOutput,aes(x="t",y="rs"))+geom_line()+theme_classic()
305 changes: 305 additions & 0 deletions Question 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
##Question2
#Import Packages
import pandas
import numpy
from scipy.optimize import minimize
from scipy.stats import norm
import scipy.integrate as spi
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import re
import os
from plotnine import *

#Total Population=1000 (999 susceptible, 1 infected, 0 resistant)
N=1000
#Initial infected (I0) & Initial Resistant (R0)
I0=1
R0=0
#Everyone susceptible
S0=N-I0-R0
N=S0+I0+R0
#Recovery Rates
beta, gamma=0.2,1/10

#Time
t=0
t = np.linspace(0, 500, 500)
#number susceptible
sList=[]
#number infected
iList=[]
#number recovered
rList=[]
#new infections
newList=[]


#Time
t = np.linspace(0, 500, 500)

#SIR Differential Equations
def deriv(y, t, N, beta, gamma):
S = y
I = y
R = y
dSdt = -beta * S * I / N
dIdt = beta * S * I / N - gamma * I
dRdt = gamma * I
return dSdt, dIdt, dRdt

#Initial Conditions
y0 = S0, I0, R0

#Integrate
ret = odeint(deriv, y0, t, args=(N, beta, gamma))
S, I, R = ret.T

#Variable Equations
#Incidence
I(t) = I(t)-I(t-1)
#prevalence
P=I/(S+I+R)
#percent affected
A=(I+R)/(S+I+R)
#Basic Repduction
R0=beta*(S+I+R)/gamma

#values
beta=0.0005
gamma=0.05
#time (days)
t = np.linspace(0, 500, 500)

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.

Set t = 0 to 500?
t = np.linspace(0, 500, 501)

while I > 0:
newI = 0
for i in range(S):

if random.random() < b*(I/N):
newI += 1
recoverI = 0
for i in range(I):
if random.random() < g:
recoverI += 1

S -= newI
I += (newI - recoverI)
R += recoverI

#Append
sList.append(S)
iList.append(I)
rList.append(R)
newIList.append(newI)

print('t', t)
t += 1
#Print
print('sList', sList)
print('iList', iList)
print('rList', rList)
print('newIList', newIList)

#values
beta=0.005
gamma=0.5
#time (days)
t = np.linspace(0, 500, 500)
while I > 0:
newI = 0
for i in range(S):

if random.random() < b*(I/N):
newI += 1
recoverI = 0
for i in range(I):
if random.random() < g:
recoverI += 1

S -= newI
I += (newI - recoverI)
R += recoverI

#Append
sList.append(S)
iList.append(I)
rList.append(R)
newIList.append(newI)

print('t', t)
t += 1
#Print
print('sList', sList)
print('iList', iList)
print('rList', rList)
print('newIList', newIList)

#values
beta=0.0001
gamma=0.1
#time (days)
t = np.linspace(0, 500, 500)
while I > 0:
newI = 0
for i in range(S):

if random.random() < b*(I/N):
newI += 1
recoverI = 0
for i in range(I):
if random.random() < g:
recoverI += 1

S -= newI
I += (newI - recoverI)
R += recoverI

#Append
sList.append(S)
iList.append(I)
rList.append(R)
newIList.append(newI)

print('t', t)
t += 1
#Print
print('sList', sList)
print('iList', iList)
print('rList', rList)
print('newIList', newIList)

#values
beta=0.00005
gamma=0.1
#time (days)
t = np.linspace(0, 500, 500)
while I > 0:
newI = 0
for i in range(S):

if random.random() < b*(I/N):
newI += 1
recoverI = 0
for i in range(I):
if random.random() < g:
recoverI += 1

S -= newI
I += (newI - recoverI)
R += recoverI

#Append
sList.append(S)
iList.append(I)
rList.append(R)
newIList.append(newI)

print('t', t)
t += 1
#Print
print('sList', sList)
print('iList', iList)
print('rList', rList)
print('newIList', newIList)

#values
beta=0.0001
gamma=0.05
#time (days)
t = np.linspace(0, 500, 500)
while I > 0:
newI = 0
for i in range(S):

if random.random() < b*(I/N):
newI += 1
recoverI = 0
for i in range(I):
if random.random() < g:
recoverI += 1

S -= newI
I += (newI - recoverI)
R += recoverI

#Append
sList.append(S)
iList.append(I)
rList.append(R)
newIList.append(newI)

print('t', t)
t += 1
#Print
print('sList', sList)
print('iList', iList)
print('rList', rList)
print('newIList', newIList)

#values
beta=0.0002
gamma=0.05
#time (days)
t = np.linspace(0, 500, 500)
while I > 0:
newI = 0
for i in range(S):

if random.random() < b*(I/N):
newI += 1
recoverI = 0
for i in range(I):
if random.random() < g:
recoverI += 1

S -= newI
I += (newI - recoverI)
R += recoverI

#Append
sList.append(S)
iList.append(I)
rList.append(R)
newIList.append(newI)

print('t', t)
t += 1
#Print
print('sList', sList)
print('iList', iList)
print('rList', rList)
print('newIList', newIList)

#values
beta=0.0001
gamma=0.06
#time (days)
t = np.linspace(0, 500, 500)
while I > 0:
newI = 0
for i in range(S):

if random.random() < b*(I/N):
newI += 1
recoverI = 0
for i in range(I):
if random.random() < g:
recoverI += 1

S -= newI
I += (newI - recoverI)
R += recoverI

#Append
sList.append(S)
iList.append(I)
rList.append(R)
newIList.append(newI)

print('t', t)
t += 1
#Print
print('sList', sList)
print('iList', iList)
print('rList', rList)
print('newIList', newIList)

@lyy005 lyy005 Nov 28, 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.

Define the function:
def simSIR(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]

Define the parameters:
y0=[999,1,0]
times=range(0,500)

betas=numpy.array([0.0005,0.005,0.0001,0.00005,0.0001,0.0002,0.0001])
gammas=numpy.array([0.05,0.5,0.1,0.1,0.05,0.05,0.06])

Make a new dataframe to store the parameters:
storeSIR=pandas.DataFrame({"beta":betas,"gamma":gammas,"R0":betas*sum(y0)/gammas,"maxIncidence":0,"maxPrevalence":0,"percentAffected":0})

Using the for loop to simulate:
for i in range(0,len(betas)):
pars=(betas[i],gammas[i])
sim=si.odeint(func=simSIR,y0=y0,t=times,args=pars)
storeSIR.iloc[i,4]=numpy.max(sim[:,1]/numpy.sum(sim,axis=1))
storeSIR.iloc[i,3]=numpy.max(sim[1:len(sim),1]-sim[0:(len(sim)-1),1])
storeSIR.iloc[i,5]=numpy.sum(sim[len(sim)-1,1:3])/numpy.sum(sim[len(sim)-1,:])*100

-0.5 pts

35 changes: 35 additions & 0 deletions Tutorial 10 Part A
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#Import Packages
import pandas
import numpy
from scipy.optimize import minimize
from scipy.stats import norm
import scipy.integrate as si
import re
import os
from plotnine import *

#Plot of five population sizes
def ddSim(y,t0,r,K):
N=y[0]
dNdt=r*(1-N/K)*N
return [dNdt]

params=(0.3,10)
N0=[10]
times=range(0,100)

modelSim=si.odeint(func=ddSim,y0=N0,t=times,args=params)

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

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

modelOutput=pandas.DataFrame({"t":times,"rs":modelSim[:,0]})

#plot
ggplot(modelOutput,aes(x="t",y="rs"))+geom_line()+theme_classic()
Loading