text_html_output_1.png
import itertools

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import statsmodels.api as sm

train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')

def impute(df):
    df['Province_State'] = df['Province_State'].mask(df['Province_State'].isnull(), df['Country_Region'])
    return df
train_imputed = impute(train)

train_cc = train_imputed.drop(['Id', 'Fatalities'], 1)
train_cc['Date'] = pd.to_datetime(train_cc['Date'])
train_cc.set_index(['Country_Region', 'Date'], inplace=True)
train_f = train_imputed.drop(['Id', 'ConfirmedCases'], 1)
train_f['Date'] = pd.to_datetime(train_f['Date'])
train_f.set_index(['Country_Region', 'Date'], inplace=True)

p = d = q = range(0, 2)
pdq = list(itertools.product(p, d, q))
seasonal_pdq = [(x[0], x[1], x[2], 7) for x in list(itertools.product(p, d, q))]
def param(df):
    liste = dict()
    for param in pdq:
        for param_seasonal in seasonal_pdq:
            try:
                mod = sm.tsa.statespace.SARIMAX(y, order=param, seasonal_order=param_seasonal, enforce_stationarity=False, enforce_invertibility=False)
                results = mod.fit()
                key = str(param) + ',' + str(param_seasonal)
                liste[key] = results.aic
            except:
                continue
    key_min = min(liste, key=liste.get)
    k = key_min.replace('(', '').replace(')', '').split(',')
    i = [int(x) for x in k]
    par = tuple(i[:3])
    par_seas = tuple(i[3:])
    return (par, par_seas)

liste_pays = train_cc.index.get_level_values(0).unique()

rmsle_cc_pays = dict()
mle_cc_retval = dict()
list_cc_results = dict()
predictions_cc = dict()
list_cc_y = dict()
for elmt in liste_pays:
    df = train_cc.loc[elmt]
    if len(df['Province_State'].unique()) == 1:
        y = df['ConfirmedCases'].resample('D').mean()
        list_cc_y[elmt + ' ' + elmt] = y
        par, par_seas = param(y)
        mod = sm.tsa.statespace.SARIMAX(y, order=par, seasonal_order=par_seas, enforce_stationarity=False, enforce_invertibility=False)
        results = mod.fit()
        list_cc_results[elmt + ' ' + elmt] = results
        pred = results.get_prediction(start=pd.to_datetime('2020-01-22'), dynamic=False)
        predictions_cc[elmt + ' ' + elmt] = pred
        y_forecasted = pred.predicted_mean
        y_truth = y.copy()
        rmsle = np.sqrt(np.square(np.log(y_forecasted + 1) - np.log(y_truth + 1)).mean())
        rmsle_cc_pays[elmt + ' ' + elmt] = round(rmsle, 2)
        mle_cc_retval[elmt + ' ' + elmt] = results.mle_retvals
    else:
        for elt in df['Province_State'].unique():
            d = df.loc[df['Province_State'] == elt]['ConfirmedCases'].resample('D').mean()
            list_cc_y[elmt + ' ' + elt] = d
            par, par_seas = param(d)
            mod = sm.tsa.statespace.SARIMAX(d, order=par, seasonal_order=par_seas, enforce_stationarity=False, enforce_invertibility=False)
            results = mod.fit()
            list_cc_results[elmt + ' ' + elt] = results
            pred = results.get_prediction(start=pd.to_datetime('2020-01-22'), dynamic=False)
            predictions_cc[elmt + ' ' + elt] = pred
            y_forecasted = pred.predicted_mean
            y_truth = d.copy()
            rmsle = np.sqrt(np.square(np.log(y_forecasted + 1) - np.log(y_truth + 1)).mean())
            rmsle_cc_pays[elmt + ' ' + elt] = round(rmsle, 2)
            mle_cc_retval[elmt + ' ' + elt] = results.mle_retvals