sg_data['Infected'] = sg_data['Confirmed']-sg_data['Recovered']-sg_data['Deaths'] # Adding
Infected data to the dataframe
sg_data['Date']=pd.to_datetime(sg_data['Date'])
# Plotting Confirmed,Deaths,Recovered and Infected '#
plt.figure(figsize=(12,7))
ax = plt.gca()
sg_data.plot(x ='Date', y='Confirmed', color='blue', kind = 'line',ax=ax)
sg_data.plot(x ='Date', y='Deaths', color='red', kind = 'line',ax=ax)
sg_data.plot(x ='Date', y='Recovered', color='green', kind = 'line',ax=ax)
sg_data.plot(x ='Date', y='Infected', color='black', kind = 'line',ax=ax)
plt.legend(['confirmed', 'deaths','recovered','infected'], loc='upper left')
plt.rcParams['figure.facecolor'] = 'xkcd:white'
dict_style_title = {'fontsize':20,'fontweight' : 'bold','color' : 'black','verticalalignment': 'baseline'}
plt.title('Singapore', fontdict = dict_style_title)
plt.show()
# Fitting graph for the confirmed cases
from scipy.optimize import curve_fit
import datetime as dt
def exp_func(x, a, b, c):
return a * np.exp(-b * x) + c
# return a * np.log(b * x) + c
x = sg_data[['Date']]
x = np.array(x['Date'].map(dt.datetime.toordinal), dtype=float) #transform your data in a numpy
array of floats
x_scale = x - x[0]
y = sg_data[['Confirmed']]
y = np.array(y['Confirmed'], dtype=float) #so the curve_fit can work
popt, pcov = curve_fit(exp_func, x_scale, y, p0= (0,0,0))
plt.figure(figsize=(15, 5))
plt.subplot()
plt.plot(x_scale, y, 'ko', label="Original Confirmed Cases")
plt.plot(x_scale, exp_func(x_scale, *popt), 'r-', label="Fitted Curve")
plt.legend()
plt.title('Actual Trend of Confirmed Cases')
plt.show()
#Caclulating beta for early cases (first 50)
first_fifty = np.arange(0, 50)
log_infected_early = np.log(early_infected)
coeffs = np.polyfit(t[early_phase], lxog_infected_early, 1)
t = np.arange(1, len(data))
plt.figure()
plt.plot(t, np.log(infected), 'b', linewidth=1)
plt.plot(t[early_phase], log_infected_early, 'black', linewidth=2, linestyle='-')
plt.xlabel('Time (days)')
plt.ylabel('log(I)')
plt.xlim([0, 500])
plt.ylim([0, 15])
slope = coeffs[0]
n = N / S[0]
beta_estimated = n * (slope + 0.07)