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
48 changes: 48 additions & 0 deletions solution1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pandas
import scipy
import scipy.integrate as spint
from plotnine import *

def ddSim(y,t0,r,K):
N=y[0]
dNdt=r*(1-N/K)*N
return [dNdt]

N0 = [10]
K = 100
listr = (-0.1,0.1, 0.4, 0.8, 1.0)
times = range(0,600)
modelOutput=pandas.DataFrame({"time":times,"r1":0, "r2":0, "r3":0, "r4":0, "r5":0})

for i in range(len(listr)):
params = (listr[i], K)
modelSim=spint.odeint(func=ddSim,y0=N0,t=times,args=params)
modelOutput.iloc[:,i]=modelSim[:,0]

g1=ggplot(modelOutput,aes(x="time",y="r1"))+geom_line()+geom_line(aes(y="R2"))+geom_line(aes(y="R3"))+geom_line(aes(y="R4"))+geom_line(aes(y="R5"))
g1+theme_classic()

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.

This code looks good except R2 should be r1.

Also, for plotting, you can add color for example:
ggplot(modelOutput,aes(x="time",y="r1",color="1"))+geom_line()+geom_line(aes(x="time",y="r2",color="50"))


listK = (10,50,100)
r = 0.2
N0 = [1]
modelOutput=pandas.DataFrame({"time":times,"K1":0, "K2":0, "K3":0})
for i in range(len(listK)):
params = (r, listK[i])
modelSim=spint.odeint(func=ddSim,y0=N0,t=times,args=params)
modelOutput.iloc[:,i]=modelSim[:,0]

g2=ggplot(modelOutput,aes(x="time",y="K1"))+geom_line()+geom_line(aes(y="K2"))+geom_line(aes(y="K3"))
g2+theme_classic()

listN0 = (1, 50, 100)
params = (0.1, 50)
modelOutput=pandas.DataFrame({"time":times,"N1":0, "N2":0, "N3":0})
for i in range(len(listN0)):
modelSim=spint.odeint(func=ddSim,y0=listN0[i],t=times,args=params)
modelOutput.iloc[:,i]=modelSim[:,0]

g3=ggplot(modelOutput,aes(x="time",y="N1"))+geom_line()+geom_line(aes(y="N2"))+geom_line(aes(y="N3"))
g3+theme_classic()

#We dont know how to get all the plots outputted, but the code is there to make them all. We asked stuart but he didn't help us with this specifically

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.

Do you mean the ggplot command is not working? I added some comments above. Let me know if you still have any questions. In general, you guys did a good job on this question!

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

def ddSim(y,t0,b,a):
S=y[0]
I=y[1]
R=y[2]
dSdt= -b * I * S
dIdt = b * I * S - a * I
dRdt = a * I
return [dSdt, dIdt, dRdt]

listb = (0.0005, 0.005, 0.0001, 0.00005, 0.0001, 0.0002, 0.0001)
listy = (0.05, 0.5, 0.1, 0.1, 0.05, 0.05, 0.06)

days = range(0,500)
susceptible = 999
infected = 1
resistant = 0
initial = [susceptible, infected, resistant]

for i in range(len(listb)):
params = (listb[i], listy[i])
modelSim=spint.odeint(func=ddSim,y0=initial,t=days,args=params)
modelOutput = pandas.DataFrame({"Days":days,"S":modelSim[:,0], "I":modelSim[:,1], "R":modelSim[:,2]})
incidence = []
prevalence = []
for j in range(0,500):
if j != 0:
incidence.append(modelOutput.loc[j, "I"] - modelOutput.loc[j-1, "I"])

prevalence.append(modelOutput.loc[j, "I"] / (modelOutput.loc[j, "I"] + modelOutput.loc[j, "S"] + modelOutput.loc[j, "R"]))
incidence.sort()
prevalence.sort()

print("B: " + str(listb[i]) + " y: " + str(listy[i]))
print("Max incidence: " + str(incidence[0]))

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.

Maximum value should be incidence[-1]

print("Max prevalence: " + str(prevalence[0]))
pctAffected = (modelOutput.loc[499, "I"] + modelOutput.loc[499, "R"]) / (modelOutput.loc[499, "S"] + modelOutput.loc[499, "I"] + modelOutput.loc[499, "R"])
print("Percent affected: " + str(pctAffected))
bscReproduction = listb[i] * (modelOutput.loc[499, "S"] + modelOutput.loc[499, "I"] + modelOutput.loc[499, "R"]) / listy[i]
print("Basic reproduction number: " + str(bscReproduction))
print("############################################")

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.

Good job!