import numpy as np
import matplotlib.pyplot as plt
import Pythonic_files as pf
import seaborn as sns
%config InlineBackend.figure_format='retina'
plt.figure(figsize=(13,6))
beta , gamma = 0.5,1/16
def sfunc(s,i):
return -beta*s*i
def ifunc(s,i):
return beta*s*i-gamma*i
I0=0.001
S0=1-I0
dt=0.01
t,s,i = pf.Ode_2D_solve(sfunc,ifunc,S0,I0,dt,100)
plt.plot(t,s,label='Susceptible')
plt.plot(t,i,label='Infectious')
plt.plot(t,1-s-i,label='Recovered')
plt.xlabel('time in days/months/mucci')
plt.ylabel('Proportion of Population')
plt.legend()
plt.show()
Days=[]
for x in range(len(t)):
if t[x]//1-t[x-1]//1>=1:
Days.append(x)
Betas = []
for x in Days[1:]:
b = -(s[x]-s[x-1])/dt/(s[x]*i[x])
Betas.append(b)
sns.distplot(Betas)
plt.show()
First lets create a "clipped" graph
plt.figure(figsize=(13,6))
beta , gamma = 0.5,1/16
def sfunc(s,i):
return -beta*s*i
def ifunc(s,i):
return beta*s*i-gamma*i
I0=0.001
S0=1-I0
dt=0.01
t,s,i = pf.Ode_2D_solve(sfunc,ifunc,S0,I0,dt,30)
plt.plot(t,s,label='Susceptible')
plt.plot(t,i,label='Infectious')
plt.plot(t,1-s-i,label='Recovered')
plt.xlabel('time in days/months/mucci')
plt.ylabel('Proportion of Population')
plt.xlim(0,100)
plt.legend()
plt.show()
Days=[]
for x in range(len(t)):
if t[x]//1-t[x-1]//1>=1:
Days.append(x)
Betas = []
for x in Days[1:]:
b = -(s[x]-s[x-1])/dt/(s[x]*i[x])
Betas.append(b)
sns.distplot(Betas)
plt.show()
estimate_beta=np.mean(Betas)
initial_s = s[-1]
initial_i = i[-1]
plt.figure(figsize=(13,6))
beta , gamma = 0.5,1/16
def sfunc(s,i):
return -beta*s*i
def ifunc(s,i):
return beta*s*i-gamma*i
I0=0.001
S0=1-I0
dt=0.01
t,s,i = pf.Ode_2D_solve(sfunc,ifunc,S0,I0,dt,35)
beta , gamma = estimate_beta,1/16
def sfunc(s,i):
return -beta*s*i
def ifunc(s,i):
return beta*s*i-gamma*i
t1,s1,i1 = pf.Ode_2D_solve(sfunc,ifunc,initial_s,initial_i,dt,5)
plt.plot(t,i,label='exact Infectious')
plt.plot(t1+30,i1,label = 'approximate Infectious')
plt.xlabel('time in days/months/mucci')
plt.ylabel('Proportion of Population')
plt.xlim(29,35)
plt.legend()
plt.show()
Obviously this is a great approximation, but its dis-honest, lets get some noisy data.
plt.figure(figsize=(13,6))
beta , gamma = 0.5,1/16
def sfunc(s,i):
return -beta*s*i
def ifunc(s,i):
return beta*s*i-gamma*i
I0=0.001
S0=1-I0
dt=0.01
noise=1
t,s,i = pf.Ode_2D_solve_noise(sfunc,ifunc,S0,I0,dt,22, noise)
plt.plot(t,s,label='Susceptible')
plt.plot(t,i,label='Infectious')
plt.plot(t,1-s-i,label='Recovered')
plt.xlabel('time in days/months/mucci')
plt.ylabel('Proportion of Population')
plt.xlim(0,20)
plt.legend()
plt.show()
print(s[-1])
Lets only look at the first 16 days
t
Days=[]
for x in range(len(t)):
if t[x]//1-t[x-1]//1>=1:
if t[x-1]<=16:
Days.append(x)
Betas = []
for x in Days[1:]:
b = -(s[x]-s[x-1])/dt/(s[x]*i[x])
Betas.append(b)
sns.distplot(Betas)
print(np.mean(Betas))
t[1600]
initial_s = s[1601]
initial_i = i[1601]
estimate_beta = np.mean(Betas)
plt.figure(figsize=(13,6))
beta , gamma = estimate_beta,1/16
def sfunc(s,i):
return -beta*s*i
def ifunc(s,i):
return beta*s*i-gamma*i
I0=0.001
S0=1-I0
dt=0.01
t1,s1,i1 = pf.Ode_2D_solve(sfunc,ifunc,initial_s,initial_i,dt,5)
plt.plot(t[:1600],i[:1600],label='exact Infectious')
plt.plot(t[1600:],i[1600:],c='r',label='exact Infectious')
plt.plot(t1+16,i1,label = 'approximate Infectious')
plt.xlabel('time in days/months/mucci')
plt.ylabel('Proportion of Population')
#plt.xlim(29,35)
plt.legend()
plt.show()
plt.figure(figsize=(13,6))
beta , gamma = estimate_beta,1/16
def sfunc(s,i):
return -beta*s*i
def ifunc(s,i):
return beta*s*i-gamma*i
I0=0.001
S0=1-I0
dt=0.01
t1,s1,i1 = pf.Ode_2D_solve(sfunc,ifunc,initial_s,initial_i,dt,5)
plt.plot(t[:1600],i[:1600],label='exact Infectious')
plt.plot(t[1600:],i[1600:],c='r',label='exact Infectious')
plt.plot(t1+16,i1,label = 'approximate Infectious')
plt.xlabel('time in days/months/mucci')
plt.ylabel('Proportion of Population')
#plt.xlim(29,35)
plt.legend()
plt.show()
def Ode_2D_solve_Sample(sfunc,ifunc,x0,y0,dt,T,Betas):
def sfunc(s,i):
return -beta*s*i
def ifunc(s,i):
return beta*s*i-gamma*i
Num = int(T//dt)
x=np.ones(Num+1)*x0
y=np.ones(Num+1)*y0
t=np.linspace(0,Num*dt,len(x))
for count in range(Num):
beta = np.random.choice(Betas)
x[count+1] = x[count]+sfunc(x[count],y[count])*dt
y[count+1] = y[count]+ifunc(x[count],y[count])*dt
return t,x,y
plt.figure(figsize=(13,6))
t1,s1,i1 = Ode_2D_solve_Sample(sfunc,ifunc,initial_s,initial_i,dt,5,Betas)
plt.plot(t[:1600],i[:1600],c='g',linewidth=0.6,label='exact Infectious')
plt.plot(t[1600:],i[1600:],c='r',linewidth=0.6,label='exact Infectious')
plt.plot(t1+16,i1,c='b',alpha = 0.3,linewidth=0.6,label = 'approximate Infectious')
plt.xlabel('time in days/months/mucci')
plt.ylabel('Proportion of Population')
#plt.xlim(29,35)
plt.legend()
plt.show()
plt.figure(figsize=(13,6))
t1,s1,i1 = Ode_2D_solve_Sample(sfunc,ifunc,initial_s,initial_i,dt,5,Betas)
plt.plot(t[:1600],i[:1600],c='g',linewidth=0.6,label='original data Infectious')
plt.plot(t[1600:],i[1600:],c='r',linewidth=0.6,label='original future data Infectious')
#plt.plot(t[:1600],s[:1600],c='g',linewidth=0.6,label='original data Susceptible')
#plt.plot(t[1600:],s[1600:],c='r',linewidth=0.6,label='original future data Suceptible')
for count in range(0,100):
t1,s1,i1 = Ode_2D_solve_Sample(sfunc,ifunc,initial_s,initial_i,dt,5,Betas)
plt.plot(t1+16,i1,c='b',alpha = 0.1,linewidth=0.3)
#plt.plot(t1+16,s1,c='b',alpha = 0.1,linewidth=0.3)
plt.xlabel('time in days/months/mucci')
plt.ylabel('Proportion of Population')
plt.xlim(15,20)
plt.ylim(0.3,0.6)
plt.legend()
plt.show()
Cases, Deaths, Populations = pf.Covid_Data("Platte County", "MO")
Counties = pf.Covid_Data_Keys('MO')
plt.plot(Cases[1],label='Confirmed Case')
plt.show()
len(Cases[1])
for C in Counties:
Cases, Deaths, Populations = pf.Covid_Data(C, "MO")
if C != "Statewide Unallocated":
plt.plot(Cases[1]/Populations, c='b',alpha=0.2)
plt.show()
3194*277