-
Notifications
You must be signed in to change notification settings - Fork 10
AMidlige Tutorial 10 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| 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() | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the last plot |
||
| 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 | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. def disTrans(y,t,beta,gamma): |
||
| 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: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -0.25 pts |
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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