<jupyter_start><jupyter_text>Required Import<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Data Exploration<jupyter_code>datasetCvdTs = pd.read_csv('time-series-19-covid-combined.csv')
datasetCvdTs.shape
datasetCvdTs.head()
np.unique(datasetCvdTs['Country/Region'])
datasetCvdWorld = pd.read_csv('worldwide-aggregated.csv')
datasetCvdWorld.shape
datasetCvdWorld.head()
np.unique(datasetCvdWorld.Date)
plt.figure(figsize=(14,9))
datasetCvdWorld.Confirmed.plot(kind='bar')
plt.figure(figsize=(14,9))
datasetCvdWorld.Confirmed.plot(kind='bar', color='blue')
datasetCvdWorld.Deaths.plot(kind='bar', color='red')
datasetCvdWorld['Increase rate'].plot()
datasetCvdWorld.isnull().sum()
sns.pairplot(data=datasetCvdWorld)
datasetCvdTs.columns
usaDataset = datasetCvdTs[datasetCvdTs['Country/Region'] =='US']
italyDatset = datasetCvdTs[datasetCvdTs['Country/Region'] =='Italy']
usaDataset.Confirmed.plot(color='green', marker='o')
italyDatset.Confirmed.plot(color='blue', marker='*')
italyDataset
#Lets run adfuller test on Italy dataset
italyDataset.index = np.arange(len(italyDataset.Confirmed))
from statsmodels.tsa.stattools import adfuller
from numpy import log
import pandas as pd
result = adfuller(italyDataset.Confirmed.dropna())
print('ADF Statistic: %f' % result[0])
print('p-value: %f' % result[1])
#Adfuller test check if the dataset is staionary or moving
#dataset us stationary here as pvalue is < 0.05 and we dont need differencing
#HENCE no differencing required, d=0
#Lets find autiorgression terms<jupyter_output><empty_output><jupyter_text>How to find the order of the AR term (p) The next step is to identify if the model needs any AR terms. You can find out the required number of AR terms by inspecting the Partial Autocorrelation (PACF) plot.But what is PACF?Partial autocorrelation can be imagined as the correlation between the series and its lag, after excluding the contributions from the intermediate lags. So, PACF sort of conveys the pure correlation between a lag and the series. That way, you will know if that lag is needed in the AR term or not.<jupyter_code>italyDataset.index
df = pd.DataFrame(italyDataset.Confirmed)
df.index = np.arange(len(italyDataset.Confirmed))
df.index
# PACF plot of 1st differenced series
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plt.rcParams.update({'figure.figsize':(9,3), 'figure.dpi':120})

fig, axes = plt.subplots(1, 2, sharex=True)
axes[0].plot(italyDataset.Confirmed); axes[0].set_title('Series')
axes[1].set(ylim=(0,5))
plot_pacf(italyDataset.Confirmed.dropna(), ax=axes[1])

plt.show()
# We got p =2
#Lets find q term for MA
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plt.rcParams.update({'figure.figsize':(9,3), 'figure.dpi':120})

fig, axes = plt.subplots(1, 2, sharex=True)
axes[0].plot(italyDataset.Confirmed); axes[0].set_title('2nd Differencing')
axes[1].set(ylim=(0,1.2))
plot_acf(italyDataset.Confirmed.dropna(), ax=axes[1])

plt.show()
#q = 5 or 6
#Lets take q = 5
from statsmodels.tsa.arima_model import ARIMA

# 2,1,5 ARIMA Model
model = ARIMA(italyDataset.Confirmed, order=(1,2,1))
model_fit = model.fit(disp=0)
print(model_fit.summary())
# Actual vs Fitted
model_fit.plot_predict(dynamic=False)
plt.show()
# Plot residual errors
residuals = pd.DataFrame(model_fit.resid)
fig, ax = plt.subplots(1,2)
residuals.plot(title="Residuals", ax=ax[0])
residuals.plot(kind='kde', title='Density', ax=ax[1])
plt.show()
italyDataset.shape
from statsmodels.tsa.stattools import acf

# Create Training and Test
train = italyDataset.Confirmed[:52]
test = italyDataset.Confirmed[52:]
test.shape, train.shape
# Build Model
model = ARIMA(train, order=(1, 1, 1))  
fitted = model.fit(disp=-1)  
print(fitted.summary())

# Forecast
fc, se, conf = fitted.forecast(15, alpha=0.05)  # 95% conf

# Make as pandas series
fc_series = pd.Series(fc, index=test.index)
lower_series = pd.Series(conf[:, 0], index=test.index)
upper_series = pd.Series(conf[:, 1], index=test.index)

# Plot
plt.figure(figsize=(12,5), dpi=100)
plt.plot(train, label='training')
plt.plot(test, label='actual')
plt.plot(fc_series, label='forecast')
plt.fill_between(lower_series.index, lower_series, upper_series, 
                 color='k', alpha=.15)
plt.title('Forecast vs Actuals')
plt.legend(loc='upper left', fontsize=8)
plt.show()
#AUTO ARIMA
from statsmodels.tsa.arima_model import ARIMA
import pmdarima as pm

model = pm.auto_arima(italyDataset.Confirmed, start_p=1, start_q=1,
                      test='adf',       # use adftest to find optimal 'd'
                      max_p=4, max_q=4, # maximum p and q
                      m=1,              # frequency of series
                      d=None,           # let model determine 'd'
                      seasonal=False,   # No Seasonality
                      start_P=0, 
                      D=0, 
                      trace=True,
                      error_action='ignore',  
                      suppress_warnings=True, 
                      stepwise=True)

print(model.summary())
# Forecast
n_periods = 24
fc, confint = model.predict(n_periods=n_periods, return_conf_int=True)
index_of_fc = np.arange(len(italyDataset.Confirmed), len(italyDataset.Confirmed)+n_periods)

# make series for plotting purpose
fc_series = pd.Series(fc, index=index_of_fc)
lower_series = pd.Series(confint[:, 0], index=index_of_fc)
upper_series = pd.Series(confint[:, 1], index=index_of_fc)

# Plot
plt.plot(italyDataset.Confirmed)
plt.plot(fc_series, color='darkgreen')
plt.fill_between(lower_series.index, 
                 lower_series, 
                 upper_series, 
                 color='k', alpha=.15)

plt.title("Final ITALY NEXT 24 DAYS FORCAST ON CONFIRMED CASES")
plt.show()<jupyter_output><empty_output><jupyter_text>Deaths<jupyter_code>#AUTO ARIMA
from statsmodels.tsa.arima_model import ARIMA
import pmdarima as pm

model = pm.auto_arima(italyDataset.Deaths, start_p=1, start_q=1,
                      test='adf',       # use adftest to find optimal 'd'
                      max_p=4, max_q=4, # maximum p and q
                      m=1,              # frequency of series
                      d=None,           # let model determine 'd'
                      seasonal=False,   # No Seasonality
                      start_P=0, 
                      D=0, 
                      trace=True,
                      error_action='ignore',  
                      suppress_warnings=True, 
                      stepwise=True)

print(model.summary())
# Forecast
n_periods = 24
fc, confint = model.predict(n_periods=n_periods, return_conf_int=True)
index_of_fc = np.arange(len(italyDataset.Deaths), len(italyDataset.Confirmed)+n_periods)

# make series for plotting purpose
fc_series = pd.Series(fc, index=index_of_fc)
lower_series = pd.Series(confint[:, 0], index=index_of_fc)
upper_series = pd.Series(confint[:, 1], index=index_of_fc)

# Plot
plt.plot(italyDataset.Deaths)
plt.plot(fc_series, color='darkgreen')
plt.fill_between(lower_series.index, 
                 lower_series, 
                 upper_series, 
                 color='k', alpha=.15)

plt.title("Final ITALY NEXT 24 DAYS FORCAST ON DEATH CASES")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12024623803009576, 'fraction_numerical': 0.015595075239398085, 'mean_word_length': 3.8837675350701404, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '532212', 'license': 'Apache-2.0', 'n_tokens_mistral': 2611, 'n_tokens_neox': 2492, 'n_words': 555, 'path': 'Covid 19 Damage Prediction/CovidAnalysisWithPGPBatch.ipynb', 'repo_name': 'Saaimurugan/PGP-AI---Python-for-Data-Science', 'size': '7310'}