import math
import numpy as np
import pandas as pd
import scipy.optimize as optim
import matplotlib.pyplot as plt
def my_logistic(t):
return 1000 / (1 + 999 * math.exp(-2 * t))
x = np.linspace(0, 10, 11)
y = [my_logistic(i) for i in x]
plt.plot(x, y)
pd.DataFrame({'Time':x, 'Infections':y})
def exponential(t):
return 1*2**t
plt.plot(x, y)
plt.plot(x, exponential(x))
plt.title('Logistic Growth vs Exponential Growth')
plt.legend(['Logistic', 'Exponential'])
plt.xlabel('Time')
plt.ylabel('Infections')
plt.show()
# Import the data
data = pd.read_csv('C://Users//jkorstan//Desktop//full_data_logistic.csv', sep=';')
data = data['total_cases']
data = data.reset_index(drop=False)
data.columns = ['Timestep', 'Total Cases']
data.head(10)
# Define funcion with the coefficients to estimate
def my_logistic(t, a, b, c):
return c / (1 + a * np.exp(-b*t))
# Randomly initialize the coefficients
p0 = np.random.exponential(size=3)
p0
# Set min bound 0 on all coefficients, and set different max bounds for each coefficient
bounds = (0, [100000., 3., 1000000000.])
# Convert pd.Series to np.Array and use Scipy's curve fit to find the best Nonlinear Least Squares coefficients
x = np.array(data['Timestep']) + 1
y = np.array(data['Total Cases'])
(a,b,c),cov = optim.curve_fit(my_logistic, x, y, bounds=bounds, p0=p0)
# Show the coefficients
a,b,c
# Redefine the function with the new a, b and c
def my_logistic(t):
return c / (1 + a * np.exp(-b*t))
plt.scatter(x, y)
plt.plot(x, my_logistic(x))
plt.title('Logistic Model vs Real Observations of China Coronavirus')
plt.legend([ 'Logistic Model', 'Real data'])
plt.xlabel('Time')
plt.ylabel('Infections')
# The time step at which the growth is fastest
t_fastest = np.log(a) / b
t_fastest
# First way to find the y of the fastest growth moment
y_fastest = c / 2
y_fastest
# Second way to find the y of the fastest growth moment
my_logistic(t_fastest)