
"""2a_AR_EWMA.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1NrZUm2fc6SRC_NHrqtvPqLDqYchtCT0N
"""

import pandas as pd
import numpy as np
import math
from statistics import mean, median
from datetime import datetime as dt
import datetime
import matplotlib.pyplot as plt

def get_data(start, end, df_clean):
  MT_daily_cases = [int(df_clean['MT daily cases'][i]) for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  NC_daily_cases = [int(df_clean['NC daily cases'][i]) for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  MT_daily_death = [int(df_clean['MT daily death'][i]) for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  NC_daily_death = [int(df_clean['NC daily death'][i]) for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  date = [df_clean['Date'][i] for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  return date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death

# Get daily data for the 4 weeks of August, 2020

start = datetime.datetime(2020, 8, 1)
end = datetime.datetime(2020, 8, 28)
df_clean = pd.read_csv('../data/clean_organised.csv')

date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death =  get_data(start, end, df_clean)
MT_daily_cases=np.array(MT_daily_cases)
NC_daily_cases=np.array(NC_daily_cases)
MT_daily_death=np.array(MT_daily_death)
NC_daily_death=np.array(NC_daily_death)

def AR(cases, days, date):
  original = np.array(cases, copy=True)
  X = []
  Y = []
  index = days
  for day in original[days:21]:
    Y.append(day)
    Xs = []
    Xs.append(1)
    for i in range(1,days+1):
      Xs.append(original[index-i])
    X.append(Xs)
    index += 1
  X = np.array(X)
  Y = np.array(Y).reshape(len(Y),1)
  for i in range(21,28):
    betas = np.matmul(np.matmul(np.linalg.inv(np.matmul(np.transpose(X),X)),np.transpose(X)), Y)
    new_y = int(betas[0][0])
    count = 1
    for x in range(1, days+1):
      new_y += betas[x]*original[i-count]
      count += 1
    new_y = int(new_y)
    Y = np.append(Y, [[new_y]])
    Y = Y.reshape(len(Y), 1)
    original[i] = new_y
    new_x = []
    new_x.append(1)
    for k in range(1,days+1):
      new_x.append(original[i-k])
    new_x = np.array(new_x)
    X = np.vstack([X, new_x])

  plt.plot(pd.to_datetime(date), original)
  plt.xticks(rotation=45)
  plt.plot(pd.to_datetime(date), cases)
  plt.show()
  return original[-7:]

AR_MT_daily_cases_THREE = AR(MT_daily_cases, 3, date)
AR_NC_daily_cases_THREE = AR(NC_daily_cases, 3, date)
AR_MT_daily_death_THREE = AR(MT_daily_death, 3, date)
AR_NC_daily_death_THREE = AR(NC_daily_death, 3, date)

AR_MT_daily_cases_FIVE = AR(MT_daily_cases, 5, date)
AR_NC_daily_cases_FIVE = AR(NC_daily_cases, 5, date)
AR_MT_daily_death_FIVE = AR(MT_daily_death, 5, date)
AR_NC_daily_death_FIVE = AR(NC_daily_death, 5, date)

mse_mtcases_THREE = np.sum((AR_MT_daily_cases_THREE - MT_daily_cases[-7:])**2)/7
mse_nccases_THREE = np.sum((AR_NC_daily_cases_THREE - NC_daily_cases[-7:])**2)/7
mse_mtdeath_THREE = np.sum((AR_MT_daily_death_THREE - MT_daily_death[-7:])**2)/7
mse_ncdeath_THREE = np.sum((AR_NC_daily_death_THREE - NC_daily_death[-7:])**2)/7

mse_mtcases_FIVE = np.sum((AR_MT_daily_cases_FIVE - MT_daily_cases[-7:])**2)/7
mse_nccases_FIVE = np.sum((AR_NC_daily_cases_FIVE - NC_daily_cases[-7:])**2)/7
mse_mtdeath_FIVE = np.sum((AR_MT_daily_death_FIVE - MT_daily_death[-7:])**2)/7
mse_ncdeath_FIVE = np.sum((AR_NC_daily_death_FIVE - NC_daily_death[-7:])**2)/7

print("MSE")
print('MSE MT CASES (parameter=3) = ' + str(mse_mtcases_THREE))
print('MSE NC CASES (parameter=3) = ' + str(mse_nccases_THREE))
print('MSE MT DEATH (parameter=3) = ' + str(mse_mtdeath_THREE))
print('MSE NC DEATH (parameter=3) = ' + str(mse_ncdeath_THREE))
print('MSE MT CASES (parameter=5) = ' + str(mse_mtcases_FIVE))
print('MSE NC CASES (parameter=5) = ' + str(mse_nccases_FIVE))
print('MSE MT DEATH (parameter=5) = ' + str(mse_mtdeath_FIVE))
print('MSE NC DEATH (parameter=5) = ' + str(mse_ncdeath_FIVE))

mape_mtcases_THREE = np.sum(abs((MT_daily_cases[-7:] - AR_MT_daily_cases_THREE)/MT_daily_cases[-7:]))/7 * 100
mape_nccases_THREE = np.sum(abs((NC_daily_cases[-7:] - AR_NC_daily_cases_THREE)/NC_daily_cases[-7:]))/7 * 100
mape_mtdeath_THREE = np.sum(abs((MT_daily_death[-7:] - AR_MT_daily_death_THREE)/MT_daily_death[-7:]))/7 * 100
mape_ncdeath_THREE = np.sum(abs((NC_daily_death[-7:] - AR_NC_daily_death_THREE)/NC_daily_death[-7:]))/7 * 100

mape_mtcases_FIVE = np.sum(abs((MT_daily_cases[-7:] - AR_MT_daily_cases_FIVE)/MT_daily_cases[-7:]))/7 * 100
mape_nccases_FIVE = np.sum(abs((NC_daily_cases[-7:] - AR_NC_daily_cases_FIVE)/NC_daily_cases[-7:]))/7 * 100
mape_mtdeath_FIVE = np.sum(abs((MT_daily_death[-7:] - AR_MT_daily_death_FIVE)/MT_daily_death[-7:]))/7 * 100
mape_ncdeath_FIVE = np.sum(abs((NC_daily_death[-7:] - AR_NC_daily_death_FIVE)/NC_daily_death[-7:]))/7 * 100

print("MAPE in %")
print('MAPE MT CASES (parameter=3) = ' + str(mape_mtcases_THREE))
print('MAPE NC CASES (parameter=3) = ' + str(mape_nccases_THREE))
print('MAPE MT DEATH (parameter=3) = ' + str(mape_mtdeath_THREE))
print('MAPE NC DEATH (parameter=3) = ' + str(mape_ncdeath_THREE))
print('MAPE MT CASES (parameter=5) = ' + str(mape_mtcases_FIVE))
print('MAPE NC CASES (parameter=5) = ' + str(mape_nccases_FIVE))
print('MAPE MT DEATH (parameter=5) = ' + str(mape_mtdeath_FIVE))
print('MAPE NC DEATH (parameter=5) = ' + str(mape_ncdeath_FIVE))

AR(NC_daily_cases,3, date)

AR(MT_daily_cases,3, date)

AR(NC_daily_cases,5, date)

AR(MT_daily_cases,5, date)

AR(NC_daily_death,3, date)

AR(NC_daily_death,5, date)

AR(MT_daily_death,5, date)

AR(MT_daily_death,3, date)

def EWMA(original, alpha):
    predicted = [0] * len(original)
    predicted[0] = original[0]
    for i in range(1, len(original)):
        predicted[i] = int((1-alpha) * predicted[i - 1] + (alpha) * original[i - 1])
    # print(len(predicted))
    return np.array(predicted[-7:])

EWMA_MT_daily_cases_5 = EWMA(MT_daily_cases, 0.5)
EWMA_NC_daily_cases_5 = EWMA(NC_daily_cases, 0.5)
EWMA_MT_daily_death_5 = EWMA(MT_daily_death, 0.5)
EWMA_NC_daily_death_5 = EWMA(NC_daily_death, 0.5)

EWMA_MT_daily_cases_8 = EWMA(MT_daily_cases, 0.8)
EWMA_NC_daily_cases_8 = EWMA(NC_daily_cases, 0.8)
EWMA_MT_daily_death_8 = EWMA(MT_daily_death, 0.8)
EWMA_NC_daily_death_8 = EWMA(NC_daily_death, 0.8)

mse_mtcases_5 = np.sum((EWMA_MT_daily_cases_5 - MT_daily_cases[-7:])**2)/7
mse_nccases_5 = np.sum((EWMA_NC_daily_cases_5 - NC_daily_cases[-7:])**2)/7
mse_mtdeath_5 = np.sum((EWMA_MT_daily_death_5 - MT_daily_death[-7:])**2)/7
mse_ncdeath_5 = np.sum((EWMA_NC_daily_death_5 - NC_daily_death[-7:])**2)/7

mse_mtcases_8 = np.sum((EWMA_MT_daily_cases_8 - MT_daily_cases[-7:])**2)/7
mse_nccases_8 = np.sum((EWMA_NC_daily_cases_8 - NC_daily_cases[-7:])**2)/7
mse_mtdeath_8 = np.sum((EWMA_MT_daily_death_8 - MT_daily_death[-7:])**2)/7
mse_ncdeath_8 = np.sum((EWMA_NC_daily_death_8 - NC_daily_death[-7:])**2)/7

print("MSE")
print('MSE MT CASES (alpha=0.5) = ' + str(mse_mtcases_5))
print('MSE NC CASES (alpha=0.5) = ' + str(mse_nccases_5))
print('MSE MT DEATH (alpha=0.5) = ' + str(mse_mtdeath_5))
print('MSE NC DEATH (alpha=0.5) = ' + str(mse_ncdeath_5))
print('MSE MT CASES (alpha=0.8) = ' + str(mse_mtcases_8))
print('MSE NC CASES (alpha=0.8) = ' + str(mse_nccases_8))
print('MSE MT DEATH (alpha=0.8) = ' + str(mse_mtdeath_8))
print('MSE NC DEATH (alpha=0.8) = ' + str(mse_ncdeath_8))

mape_mtcases_5 = np.sum(abs((MT_daily_cases[-7:] - EWMA_MT_daily_cases_5)/MT_daily_cases[-7:]))/7 * 100
mape_nccases_5 = np.sum(abs((NC_daily_cases[-7:] - EWMA_NC_daily_cases_5)/NC_daily_cases[-7:]))/7 * 100
mape_mtdeath_5 = np.sum(abs((MT_daily_death[-7:] - EWMA_MT_daily_death_5)/MT_daily_death[-7:]))/7 * 100
mape_ncdeath_5 = np.sum(abs((NC_daily_death[-7:] - EWMA_NC_daily_death_5)/NC_daily_death[-7:]))/7 * 100

mape_mtcases_8 = np.sum(abs((MT_daily_cases[-7:] - EWMA_MT_daily_cases_8)/MT_daily_cases[-7:]))/7 * 100
mape_nccases_8 = np.sum(abs((NC_daily_cases[-7:] - EWMA_NC_daily_cases_8)/NC_daily_cases[-7:]))/7 * 100
mape_mtdeath_8 = np.sum(abs((MT_daily_death[-7:] - EWMA_MT_daily_death_8)/MT_daily_death[-7:]))/7 * 100
mape_ncdeath_8 = np.sum(abs((NC_daily_death[-7:] - EWMA_NC_daily_death_8)/NC_daily_death[-7:]))/7 * 100

print("MAPE in %")
print('MAPE MT CASES (alpha=0.5) = ' + str(mape_mtcases_5))
print('MAPE NC CASES (alpha=0.5) = ' + str(mape_nccases_5))
print('MAPE MT DEATH (alpha=0.5) = ' + str(mape_mtdeath_5))
print('MAPE NC DEATH (alpha=0.5) = ' + str(mape_ncdeath_5))
print('MAPE MT CASES (alpha=0.8) = ' + str(mape_mtcases_8))
print('MAPE NC CASES (alpha=0.8) = ' + str(mape_nccases_8))
print('MAPE MT DEATH (alpha=0.8) = ' + str(mape_mtdeath_8))
print('MAPE NC DEATH (alpha=0.8) = ' + str(mape_ncdeath_8))


cfd7f002a2eaa841edaeaf473013e715b75b9395
<mask token>


def get_data(start, end, df_clean):
    MT_daily_cases = [int(df_clean['MT daily cases'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    NC_daily_cases = [int(df_clean['NC daily cases'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    MT_daily_death = [int(df_clean['MT daily death'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    NC_daily_death = [int(df_clean['NC daily death'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    date = [df_clean['Date'][i] for i in range(0, len(df_clean['Date'])) if
        dt.strptime(df_clean['Date'][i], '%m/%d/%Y') >= start and dt.
        strptime(df_clean['Date'][i], '%m/%d/%Y') <= end]
    return date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death


<mask token>


def AR(cases, days, date):
    original = np.array(cases, copy=True)
    X = []
    Y = []
    index = days
    for day in original[days:21]:
        Y.append(day)
        Xs = []
        Xs.append(1)
        for i in range(1, days + 1):
            Xs.append(original[index - i])
        X.append(Xs)
        index += 1
    X = np.array(X)
    Y = np.array(Y).reshape(len(Y), 1)
    for i in range(21, 28):
        betas = np.matmul(np.matmul(np.linalg.inv(np.matmul(np.transpose(X),
            X)), np.transpose(X)), Y)
        new_y = int(betas[0][0])
        count = 1
        for x in range(1, days + 1):
            new_y += betas[x] * original[i - count]
            count += 1
        new_y = int(new_y)
        Y = np.append(Y, [[new_y]])
        Y = Y.reshape(len(Y), 1)
        original[i] = new_y
        new_x = []
        new_x.append(1)
        for k in range(1, days + 1):
            new_x.append(original[i - k])
        new_x = np.array(new_x)
        X = np.vstack([X, new_x])
    plt.plot(pd.to_datetime(date), original)
    plt.xticks(rotation=45)
    plt.plot(pd.to_datetime(date), cases)
    plt.show()
    return original[-7:]


<mask token>


def EWMA(original, alpha):
    predicted = [0] * len(original)
    predicted[0] = original[0]
    for i in range(1, len(original)):
        predicted[i] = int((1 - alpha) * predicted[i - 1] + alpha *
            original[i - 1])
    return np.array(predicted[-7:])


<mask token>

<mask token>


def get_data(start, end, df_clean):
    MT_daily_cases = [int(df_clean['MT daily cases'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    NC_daily_cases = [int(df_clean['NC daily cases'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    MT_daily_death = [int(df_clean['MT daily death'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    NC_daily_death = [int(df_clean['NC daily death'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    date = [df_clean['Date'][i] for i in range(0, len(df_clean['Date'])) if
        dt.strptime(df_clean['Date'][i], '%m/%d/%Y') >= start and dt.
        strptime(df_clean['Date'][i], '%m/%d/%Y') <= end]
    return date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death


<mask token>


def AR(cases, days, date):
    original = np.array(cases, copy=True)
    X = []
    Y = []
    index = days
    for day in original[days:21]:
        Y.append(day)
        Xs = []
        Xs.append(1)
        for i in range(1, days + 1):
            Xs.append(original[index - i])
        X.append(Xs)
        index += 1
    X = np.array(X)
    Y = np.array(Y).reshape(len(Y), 1)
    for i in range(21, 28):
        betas = np.matmul(np.matmul(np.linalg.inv(np.matmul(np.transpose(X),
            X)), np.transpose(X)), Y)
        new_y = int(betas[0][0])
        count = 1
        for x in range(1, days + 1):
            new_y += betas[x] * original[i - count]
            count += 1
        new_y = int(new_y)
        Y = np.append(Y, [[new_y]])
        Y = Y.reshape(len(Y), 1)
        original[i] = new_y
        new_x = []
        new_x.append(1)
        for k in range(1, days + 1):
            new_x.append(original[i - k])
        new_x = np.array(new_x)
        X = np.vstack([X, new_x])
    plt.plot(pd.to_datetime(date), original)
    plt.xticks(rotation=45)
    plt.plot(pd.to_datetime(date), cases)
    plt.show()
    return original[-7:]


<mask token>
print('MSE')
print('MSE MT CASES (parameter=3) = ' + str(mse_mtcases_THREE))
print('MSE NC CASES (parameter=3) = ' + str(mse_nccases_THREE))
print('MSE MT DEATH (parameter=3) = ' + str(mse_mtdeath_THREE))
print('MSE NC DEATH (parameter=3) = ' + str(mse_ncdeath_THREE))
print('MSE MT CASES (parameter=5) = ' + str(mse_mtcases_FIVE))
print('MSE NC CASES (parameter=5) = ' + str(mse_nccases_FIVE))
print('MSE MT DEATH (parameter=5) = ' + str(mse_mtdeath_FIVE))
print('MSE NC DEATH (parameter=5) = ' + str(mse_ncdeath_FIVE))
<mask token>
print('MAPE in %')
print('MAPE MT CASES (parameter=3) = ' + str(mape_mtcases_THREE))
print('MAPE NC CASES (parameter=3) = ' + str(mape_nccases_THREE))
print('MAPE MT DEATH (parameter=3) = ' + str(mape_mtdeath_THREE))
print('MAPE NC DEATH (parameter=3) = ' + str(mape_ncdeath_THREE))
print('MAPE MT CASES (parameter=5) = ' + str(mape_mtcases_FIVE))
print('MAPE NC CASES (parameter=5) = ' + str(mape_nccases_FIVE))
print('MAPE MT DEATH (parameter=5) = ' + str(mape_mtdeath_FIVE))
print('MAPE NC DEATH (parameter=5) = ' + str(mape_ncdeath_FIVE))
AR(NC_daily_cases, 3, date)
AR(MT_daily_cases, 3, date)
AR(NC_daily_cases, 5, date)
AR(MT_daily_cases, 5, date)
AR(NC_daily_death, 3, date)
AR(NC_daily_death, 5, date)
AR(MT_daily_death, 5, date)
AR(MT_daily_death, 3, date)


def EWMA(original, alpha):
    predicted = [0] * len(original)
    predicted[0] = original[0]
    for i in range(1, len(original)):
        predicted[i] = int((1 - alpha) * predicted[i - 1] + alpha *
            original[i - 1])
    return np.array(predicted[-7:])


<mask token>
print('MSE')
print('MSE MT CASES (alpha=0.5) = ' + str(mse_mtcases_5))
print('MSE NC CASES (alpha=0.5) = ' + str(mse_nccases_5))
print('MSE MT DEATH (alpha=0.5) = ' + str(mse_mtdeath_5))
print('MSE NC DEATH (alpha=0.5) = ' + str(mse_ncdeath_5))
print('MSE MT CASES (alpha=0.8) = ' + str(mse_mtcases_8))
print('MSE NC CASES (alpha=0.8) = ' + str(mse_nccases_8))
print('MSE MT DEATH (alpha=0.8) = ' + str(mse_mtdeath_8))
print('MSE NC DEATH (alpha=0.8) = ' + str(mse_ncdeath_8))
<mask token>
print('MAPE in %')
print('MAPE MT CASES (alpha=0.5) = ' + str(mape_mtcases_5))
print('MAPE NC CASES (alpha=0.5) = ' + str(mape_nccases_5))
print('MAPE MT DEATH (alpha=0.5) = ' + str(mape_mtdeath_5))
print('MAPE NC DEATH (alpha=0.5) = ' + str(mape_ncdeath_5))
print('MAPE MT CASES (alpha=0.8) = ' + str(mape_mtcases_8))
print('MAPE NC CASES (alpha=0.8) = ' + str(mape_nccases_8))
print('MAPE MT DEATH (alpha=0.8) = ' + str(mape_mtdeath_8))
print('MAPE NC DEATH (alpha=0.8) = ' + str(mape_ncdeath_8))

<mask token>


def get_data(start, end, df_clean):
    MT_daily_cases = [int(df_clean['MT daily cases'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    NC_daily_cases = [int(df_clean['NC daily cases'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    MT_daily_death = [int(df_clean['MT daily death'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    NC_daily_death = [int(df_clean['NC daily death'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    date = [df_clean['Date'][i] for i in range(0, len(df_clean['Date'])) if
        dt.strptime(df_clean['Date'][i], '%m/%d/%Y') >= start and dt.
        strptime(df_clean['Date'][i], '%m/%d/%Y') <= end]
    return date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death


start = datetime.datetime(2020, 8, 1)
end = datetime.datetime(2020, 8, 28)
df_clean = pd.read_csv('../data/clean_organised.csv')
date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death = (
    get_data(start, end, df_clean))
MT_daily_cases = np.array(MT_daily_cases)
NC_daily_cases = np.array(NC_daily_cases)
MT_daily_death = np.array(MT_daily_death)
NC_daily_death = np.array(NC_daily_death)


def AR(cases, days, date):
    original = np.array(cases, copy=True)
    X = []
    Y = []
    index = days
    for day in original[days:21]:
        Y.append(day)
        Xs = []
        Xs.append(1)
        for i in range(1, days + 1):
            Xs.append(original[index - i])
        X.append(Xs)
        index += 1
    X = np.array(X)
    Y = np.array(Y).reshape(len(Y), 1)
    for i in range(21, 28):
        betas = np.matmul(np.matmul(np.linalg.inv(np.matmul(np.transpose(X),
            X)), np.transpose(X)), Y)
        new_y = int(betas[0][0])
        count = 1
        for x in range(1, days + 1):
            new_y += betas[x] * original[i - count]
            count += 1
        new_y = int(new_y)
        Y = np.append(Y, [[new_y]])
        Y = Y.reshape(len(Y), 1)
        original[i] = new_y
        new_x = []
        new_x.append(1)
        for k in range(1, days + 1):
            new_x.append(original[i - k])
        new_x = np.array(new_x)
        X = np.vstack([X, new_x])
    plt.plot(pd.to_datetime(date), original)
    plt.xticks(rotation=45)
    plt.plot(pd.to_datetime(date), cases)
    plt.show()
    return original[-7:]


AR_MT_daily_cases_THREE = AR(MT_daily_cases, 3, date)
AR_NC_daily_cases_THREE = AR(NC_daily_cases, 3, date)
AR_MT_daily_death_THREE = AR(MT_daily_death, 3, date)
AR_NC_daily_death_THREE = AR(NC_daily_death, 3, date)
AR_MT_daily_cases_FIVE = AR(MT_daily_cases, 5, date)
AR_NC_daily_cases_FIVE = AR(NC_daily_cases, 5, date)
AR_MT_daily_death_FIVE = AR(MT_daily_death, 5, date)
AR_NC_daily_death_FIVE = AR(NC_daily_death, 5, date)
mse_mtcases_THREE = np.sum((AR_MT_daily_cases_THREE - MT_daily_cases[-7:]) ** 2
    ) / 7
mse_nccases_THREE = np.sum((AR_NC_daily_cases_THREE - NC_daily_cases[-7:]) ** 2
    ) / 7
mse_mtdeath_THREE = np.sum((AR_MT_daily_death_THREE - MT_daily_death[-7:]) ** 2
    ) / 7
mse_ncdeath_THREE = np.sum((AR_NC_daily_death_THREE - NC_daily_death[-7:]) ** 2
    ) / 7
mse_mtcases_FIVE = np.sum((AR_MT_daily_cases_FIVE - MT_daily_cases[-7:]) ** 2
    ) / 7
mse_nccases_FIVE = np.sum((AR_NC_daily_cases_FIVE - NC_daily_cases[-7:]) ** 2
    ) / 7
mse_mtdeath_FIVE = np.sum((AR_MT_daily_death_FIVE - MT_daily_death[-7:]) ** 2
    ) / 7
mse_ncdeath_FIVE = np.sum((AR_NC_daily_death_FIVE - NC_daily_death[-7:]) ** 2
    ) / 7
print('MSE')
print('MSE MT CASES (parameter=3) = ' + str(mse_mtcases_THREE))
print('MSE NC CASES (parameter=3) = ' + str(mse_nccases_THREE))
print('MSE MT DEATH (parameter=3) = ' + str(mse_mtdeath_THREE))
print('MSE NC DEATH (parameter=3) = ' + str(mse_ncdeath_THREE))
print('MSE MT CASES (parameter=5) = ' + str(mse_mtcases_FIVE))
print('MSE NC CASES (parameter=5) = ' + str(mse_nccases_FIVE))
print('MSE MT DEATH (parameter=5) = ' + str(mse_mtdeath_FIVE))
print('MSE NC DEATH (parameter=5) = ' + str(mse_ncdeath_FIVE))
mape_mtcases_THREE = np.sum(abs((MT_daily_cases[-7:] -
    AR_MT_daily_cases_THREE) / MT_daily_cases[-7:])) / 7 * 100
mape_nccases_THREE = np.sum(abs((NC_daily_cases[-7:] -
    AR_NC_daily_cases_THREE) / NC_daily_cases[-7:])) / 7 * 100
mape_mtdeath_THREE = np.sum(abs((MT_daily_death[-7:] -
    AR_MT_daily_death_THREE) / MT_daily_death[-7:])) / 7 * 100
mape_ncdeath_THREE = np.sum(abs((NC_daily_death[-7:] -
    AR_NC_daily_death_THREE) / NC_daily_death[-7:])) / 7 * 100
mape_mtcases_FIVE = np.sum(abs((MT_daily_cases[-7:] -
    AR_MT_daily_cases_FIVE) / MT_daily_cases[-7:])) / 7 * 100
mape_nccases_FIVE = np.sum(abs((NC_daily_cases[-7:] -
    AR_NC_daily_cases_FIVE) / NC_daily_cases[-7:])) / 7 * 100
mape_mtdeath_FIVE = np.sum(abs((MT_daily_death[-7:] -
    AR_MT_daily_death_FIVE) / MT_daily_death[-7:])) / 7 * 100
mape_ncdeath_FIVE = np.sum(abs((NC_daily_death[-7:] -
    AR_NC_daily_death_FIVE) / NC_daily_death[-7:])) / 7 * 100
print('MAPE in %')
print('MAPE MT CASES (parameter=3) = ' + str(mape_mtcases_THREE))
print('MAPE NC CASES (parameter=3) = ' + str(mape_nccases_THREE))
print('MAPE MT DEATH (parameter=3) = ' + str(mape_mtdeath_THREE))
print('MAPE NC DEATH (parameter=3) = ' + str(mape_ncdeath_THREE))
print('MAPE MT CASES (parameter=5) = ' + str(mape_mtcases_FIVE))
print('MAPE NC CASES (parameter=5) = ' + str(mape_nccases_FIVE))
print('MAPE MT DEATH (parameter=5) = ' + str(mape_mtdeath_FIVE))
print('MAPE NC DEATH (parameter=5) = ' + str(mape_ncdeath_FIVE))
AR(NC_daily_cases, 3, date)
AR(MT_daily_cases, 3, date)
AR(NC_daily_cases, 5, date)
AR(MT_daily_cases, 5, date)
AR(NC_daily_death, 3, date)
AR(NC_daily_death, 5, date)
AR(MT_daily_death, 5, date)
AR(MT_daily_death, 3, date)


def EWMA(original, alpha):
    predicted = [0] * len(original)
    predicted[0] = original[0]
    for i in range(1, len(original)):
        predicted[i] = int((1 - alpha) * predicted[i - 1] + alpha *
            original[i - 1])
    return np.array(predicted[-7:])


EWMA_MT_daily_cases_5 = EWMA(MT_daily_cases, 0.5)
EWMA_NC_daily_cases_5 = EWMA(NC_daily_cases, 0.5)
EWMA_MT_daily_death_5 = EWMA(MT_daily_death, 0.5)
EWMA_NC_daily_death_5 = EWMA(NC_daily_death, 0.5)
EWMA_MT_daily_cases_8 = EWMA(MT_daily_cases, 0.8)
EWMA_NC_daily_cases_8 = EWMA(NC_daily_cases, 0.8)
EWMA_MT_daily_death_8 = EWMA(MT_daily_death, 0.8)
EWMA_NC_daily_death_8 = EWMA(NC_daily_death, 0.8)
mse_mtcases_5 = np.sum((EWMA_MT_daily_cases_5 - MT_daily_cases[-7:]) ** 2) / 7
mse_nccases_5 = np.sum((EWMA_NC_daily_cases_5 - NC_daily_cases[-7:]) ** 2) / 7
mse_mtdeath_5 = np.sum((EWMA_MT_daily_death_5 - MT_daily_death[-7:]) ** 2) / 7
mse_ncdeath_5 = np.sum((EWMA_NC_daily_death_5 - NC_daily_death[-7:]) ** 2) / 7
mse_mtcases_8 = np.sum((EWMA_MT_daily_cases_8 - MT_daily_cases[-7:]) ** 2) / 7
mse_nccases_8 = np.sum((EWMA_NC_daily_cases_8 - NC_daily_cases[-7:]) ** 2) / 7
mse_mtdeath_8 = np.sum((EWMA_MT_daily_death_8 - MT_daily_death[-7:]) ** 2) / 7
mse_ncdeath_8 = np.sum((EWMA_NC_daily_death_8 - NC_daily_death[-7:]) ** 2) / 7
print('MSE')
print('MSE MT CASES (alpha=0.5) = ' + str(mse_mtcases_5))
print('MSE NC CASES (alpha=0.5) = ' + str(mse_nccases_5))
print('MSE MT DEATH (alpha=0.5) = ' + str(mse_mtdeath_5))
print('MSE NC DEATH (alpha=0.5) = ' + str(mse_ncdeath_5))
print('MSE MT CASES (alpha=0.8) = ' + str(mse_mtcases_8))
print('MSE NC CASES (alpha=0.8) = ' + str(mse_nccases_8))
print('MSE MT DEATH (alpha=0.8) = ' + str(mse_mtdeath_8))
print('MSE NC DEATH (alpha=0.8) = ' + str(mse_ncdeath_8))
mape_mtcases_5 = np.sum(abs((MT_daily_cases[-7:] - EWMA_MT_daily_cases_5) /
    MT_daily_cases[-7:])) / 7 * 100
mape_nccases_5 = np.sum(abs((NC_daily_cases[-7:] - EWMA_NC_daily_cases_5) /
    NC_daily_cases[-7:])) / 7 * 100
mape_mtdeath_5 = np.sum(abs((MT_daily_death[-7:] - EWMA_MT_daily_death_5) /
    MT_daily_death[-7:])) / 7 * 100
mape_ncdeath_5 = np.sum(abs((NC_daily_death[-7:] - EWMA_NC_daily_death_5) /
    NC_daily_death[-7:])) / 7 * 100
mape_mtcases_8 = np.sum(abs((MT_daily_cases[-7:] - EWMA_MT_daily_cases_8) /
    MT_daily_cases[-7:])) / 7 * 100
mape_nccases_8 = np.sum(abs((NC_daily_cases[-7:] - EWMA_NC_daily_cases_8) /
    NC_daily_cases[-7:])) / 7 * 100
mape_mtdeath_8 = np.sum(abs((MT_daily_death[-7:] - EWMA_MT_daily_death_8) /
    MT_daily_death[-7:])) / 7 * 100
mape_ncdeath_8 = np.sum(abs((NC_daily_death[-7:] - EWMA_NC_daily_death_8) /
    NC_daily_death[-7:])) / 7 * 100
print('MAPE in %')
print('MAPE MT CASES (alpha=0.5) = ' + str(mape_mtcases_5))
print('MAPE NC CASES (alpha=0.5) = ' + str(mape_nccases_5))
print('MAPE MT DEATH (alpha=0.5) = ' + str(mape_mtdeath_5))
print('MAPE NC DEATH (alpha=0.5) = ' + str(mape_ncdeath_5))
print('MAPE MT CASES (alpha=0.8) = ' + str(mape_mtcases_8))
print('MAPE NC CASES (alpha=0.8) = ' + str(mape_nccases_8))
print('MAPE MT DEATH (alpha=0.8) = ' + str(mape_mtdeath_8))
print('MAPE NC DEATH (alpha=0.8) = ' + str(mape_ncdeath_8))

<mask token>
import pandas as pd
import numpy as np
import math
from statistics import mean, median
from datetime import datetime as dt
import datetime
import matplotlib.pyplot as plt


def get_data(start, end, df_clean):
    MT_daily_cases = [int(df_clean['MT daily cases'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    NC_daily_cases = [int(df_clean['NC daily cases'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    MT_daily_death = [int(df_clean['MT daily death'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    NC_daily_death = [int(df_clean['NC daily death'][i]) for i in range(0,
        len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') >= start and dt.strptime(df_clean['Date'][i],
        '%m/%d/%Y') <= end]
    date = [df_clean['Date'][i] for i in range(0, len(df_clean['Date'])) if
        dt.strptime(df_clean['Date'][i], '%m/%d/%Y') >= start and dt.
        strptime(df_clean['Date'][i], '%m/%d/%Y') <= end]
    return date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death


start = datetime.datetime(2020, 8, 1)
end = datetime.datetime(2020, 8, 28)
df_clean = pd.read_csv('../data/clean_organised.csv')
date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death = (
    get_data(start, end, df_clean))
MT_daily_cases = np.array(MT_daily_cases)
NC_daily_cases = np.array(NC_daily_cases)
MT_daily_death = np.array(MT_daily_death)
NC_daily_death = np.array(NC_daily_death)


def AR(cases, days, date):
    original = np.array(cases, copy=True)
    X = []
    Y = []
    index = days
    for day in original[days:21]:
        Y.append(day)
        Xs = []
        Xs.append(1)
        for i in range(1, days + 1):
            Xs.append(original[index - i])
        X.append(Xs)
        index += 1
    X = np.array(X)
    Y = np.array(Y).reshape(len(Y), 1)
    for i in range(21, 28):
        betas = np.matmul(np.matmul(np.linalg.inv(np.matmul(np.transpose(X),
            X)), np.transpose(X)), Y)
        new_y = int(betas[0][0])
        count = 1
        for x in range(1, days + 1):
            new_y += betas[x] * original[i - count]
            count += 1
        new_y = int(new_y)
        Y = np.append(Y, [[new_y]])
        Y = Y.reshape(len(Y), 1)
        original[i] = new_y
        new_x = []
        new_x.append(1)
        for k in range(1, days + 1):
            new_x.append(original[i - k])
        new_x = np.array(new_x)
        X = np.vstack([X, new_x])
    plt.plot(pd.to_datetime(date), original)
    plt.xticks(rotation=45)
    plt.plot(pd.to_datetime(date), cases)
    plt.show()
    return original[-7:]


AR_MT_daily_cases_THREE = AR(MT_daily_cases, 3, date)
AR_NC_daily_cases_THREE = AR(NC_daily_cases, 3, date)
AR_MT_daily_death_THREE = AR(MT_daily_death, 3, date)
AR_NC_daily_death_THREE = AR(NC_daily_death, 3, date)
AR_MT_daily_cases_FIVE = AR(MT_daily_cases, 5, date)
AR_NC_daily_cases_FIVE = AR(NC_daily_cases, 5, date)
AR_MT_daily_death_FIVE = AR(MT_daily_death, 5, date)
AR_NC_daily_death_FIVE = AR(NC_daily_death, 5, date)
mse_mtcases_THREE = np.sum((AR_MT_daily_cases_THREE - MT_daily_cases[-7:]) ** 2
    ) / 7
mse_nccases_THREE = np.sum((AR_NC_daily_cases_THREE - NC_daily_cases[-7:]) ** 2
    ) / 7
mse_mtdeath_THREE = np.sum((AR_MT_daily_death_THREE - MT_daily_death[-7:]) ** 2
    ) / 7
mse_ncdeath_THREE = np.sum((AR_NC_daily_death_THREE - NC_daily_death[-7:]) ** 2
    ) / 7
mse_mtcases_FIVE = np.sum((AR_MT_daily_cases_FIVE - MT_daily_cases[-7:]) ** 2
    ) / 7
mse_nccases_FIVE = np.sum((AR_NC_daily_cases_FIVE - NC_daily_cases[-7:]) ** 2
    ) / 7
mse_mtdeath_FIVE = np.sum((AR_MT_daily_death_FIVE - MT_daily_death[-7:]) ** 2
    ) / 7
mse_ncdeath_FIVE = np.sum((AR_NC_daily_death_FIVE - NC_daily_death[-7:]) ** 2
    ) / 7
print('MSE')
print('MSE MT CASES (parameter=3) = ' + str(mse_mtcases_THREE))
print('MSE NC CASES (parameter=3) = ' + str(mse_nccases_THREE))
print('MSE MT DEATH (parameter=3) = ' + str(mse_mtdeath_THREE))
print('MSE NC DEATH (parameter=3) = ' + str(mse_ncdeath_THREE))
print('MSE MT CASES (parameter=5) = ' + str(mse_mtcases_FIVE))
print('MSE NC CASES (parameter=5) = ' + str(mse_nccases_FIVE))
print('MSE MT DEATH (parameter=5) = ' + str(mse_mtdeath_FIVE))
print('MSE NC DEATH (parameter=5) = ' + str(mse_ncdeath_FIVE))
mape_mtcases_THREE = np.sum(abs((MT_daily_cases[-7:] -
    AR_MT_daily_cases_THREE) / MT_daily_cases[-7:])) / 7 * 100
mape_nccases_THREE = np.sum(abs((NC_daily_cases[-7:] -
    AR_NC_daily_cases_THREE) / NC_daily_cases[-7:])) / 7 * 100
mape_mtdeath_THREE = np.sum(abs((MT_daily_death[-7:] -
    AR_MT_daily_death_THREE) / MT_daily_death[-7:])) / 7 * 100
mape_ncdeath_THREE = np.sum(abs((NC_daily_death[-7:] -
    AR_NC_daily_death_THREE) / NC_daily_death[-7:])) / 7 * 100
mape_mtcases_FIVE = np.sum(abs((MT_daily_cases[-7:] -
    AR_MT_daily_cases_FIVE) / MT_daily_cases[-7:])) / 7 * 100
mape_nccases_FIVE = np.sum(abs((NC_daily_cases[-7:] -
    AR_NC_daily_cases_FIVE) / NC_daily_cases[-7:])) / 7 * 100
mape_mtdeath_FIVE = np.sum(abs((MT_daily_death[-7:] -
    AR_MT_daily_death_FIVE) / MT_daily_death[-7:])) / 7 * 100
mape_ncdeath_FIVE = np.sum(abs((NC_daily_death[-7:] -
    AR_NC_daily_death_FIVE) / NC_daily_death[-7:])) / 7 * 100
print('MAPE in %')
print('MAPE MT CASES (parameter=3) = ' + str(mape_mtcases_THREE))
print('MAPE NC CASES (parameter=3) = ' + str(mape_nccases_THREE))
print('MAPE MT DEATH (parameter=3) = ' + str(mape_mtdeath_THREE))
print('MAPE NC DEATH (parameter=3) = ' + str(mape_ncdeath_THREE))
print('MAPE MT CASES (parameter=5) = ' + str(mape_mtcases_FIVE))
print('MAPE NC CASES (parameter=5) = ' + str(mape_nccases_FIVE))
print('MAPE MT DEATH (parameter=5) = ' + str(mape_mtdeath_FIVE))
print('MAPE NC DEATH (parameter=5) = ' + str(mape_ncdeath_FIVE))
AR(NC_daily_cases, 3, date)
AR(MT_daily_cases, 3, date)
AR(NC_daily_cases, 5, date)
AR(MT_daily_cases, 5, date)
AR(NC_daily_death, 3, date)
AR(NC_daily_death, 5, date)
AR(MT_daily_death, 5, date)
AR(MT_daily_death, 3, date)


def EWMA(original, alpha):
    predicted = [0] * len(original)
    predicted[0] = original[0]
    for i in range(1, len(original)):
        predicted[i] = int((1 - alpha) * predicted[i - 1] + alpha *
            original[i - 1])
    return np.array(predicted[-7:])


EWMA_MT_daily_cases_5 = EWMA(MT_daily_cases, 0.5)
EWMA_NC_daily_cases_5 = EWMA(NC_daily_cases, 0.5)
EWMA_MT_daily_death_5 = EWMA(MT_daily_death, 0.5)
EWMA_NC_daily_death_5 = EWMA(NC_daily_death, 0.5)
EWMA_MT_daily_cases_8 = EWMA(MT_daily_cases, 0.8)
EWMA_NC_daily_cases_8 = EWMA(NC_daily_cases, 0.8)
EWMA_MT_daily_death_8 = EWMA(MT_daily_death, 0.8)
EWMA_NC_daily_death_8 = EWMA(NC_daily_death, 0.8)
mse_mtcases_5 = np.sum((EWMA_MT_daily_cases_5 - MT_daily_cases[-7:]) ** 2) / 7
mse_nccases_5 = np.sum((EWMA_NC_daily_cases_5 - NC_daily_cases[-7:]) ** 2) / 7
mse_mtdeath_5 = np.sum((EWMA_MT_daily_death_5 - MT_daily_death[-7:]) ** 2) / 7
mse_ncdeath_5 = np.sum((EWMA_NC_daily_death_5 - NC_daily_death[-7:]) ** 2) / 7
mse_mtcases_8 = np.sum((EWMA_MT_daily_cases_8 - MT_daily_cases[-7:]) ** 2) / 7
mse_nccases_8 = np.sum((EWMA_NC_daily_cases_8 - NC_daily_cases[-7:]) ** 2) / 7
mse_mtdeath_8 = np.sum((EWMA_MT_daily_death_8 - MT_daily_death[-7:]) ** 2) / 7
mse_ncdeath_8 = np.sum((EWMA_NC_daily_death_8 - NC_daily_death[-7:]) ** 2) / 7
print('MSE')
print('MSE MT CASES (alpha=0.5) = ' + str(mse_mtcases_5))
print('MSE NC CASES (alpha=0.5) = ' + str(mse_nccases_5))
print('MSE MT DEATH (alpha=0.5) = ' + str(mse_mtdeath_5))
print('MSE NC DEATH (alpha=0.5) = ' + str(mse_ncdeath_5))
print('MSE MT CASES (alpha=0.8) = ' + str(mse_mtcases_8))
print('MSE NC CASES (alpha=0.8) = ' + str(mse_nccases_8))
print('MSE MT DEATH (alpha=0.8) = ' + str(mse_mtdeath_8))
print('MSE NC DEATH (alpha=0.8) = ' + str(mse_ncdeath_8))
mape_mtcases_5 = np.sum(abs((MT_daily_cases[-7:] - EWMA_MT_daily_cases_5) /
    MT_daily_cases[-7:])) / 7 * 100
mape_nccases_5 = np.sum(abs((NC_daily_cases[-7:] - EWMA_NC_daily_cases_5) /
    NC_daily_cases[-7:])) / 7 * 100
mape_mtdeath_5 = np.sum(abs((MT_daily_death[-7:] - EWMA_MT_daily_death_5) /
    MT_daily_death[-7:])) / 7 * 100
mape_ncdeath_5 = np.sum(abs((NC_daily_death[-7:] - EWMA_NC_daily_death_5) /
    NC_daily_death[-7:])) / 7 * 100
mape_mtcases_8 = np.sum(abs((MT_daily_cases[-7:] - EWMA_MT_daily_cases_8) /
    MT_daily_cases[-7:])) / 7 * 100
mape_nccases_8 = np.sum(abs((NC_daily_cases[-7:] - EWMA_NC_daily_cases_8) /
    NC_daily_cases[-7:])) / 7 * 100
mape_mtdeath_8 = np.sum(abs((MT_daily_death[-7:] - EWMA_MT_daily_death_8) /
    MT_daily_death[-7:])) / 7 * 100
mape_ncdeath_8 = np.sum(abs((NC_daily_death[-7:] - EWMA_NC_daily_death_8) /
    NC_daily_death[-7:])) / 7 * 100
print('MAPE in %')
print('MAPE MT CASES (alpha=0.5) = ' + str(mape_mtcases_5))
print('MAPE NC CASES (alpha=0.5) = ' + str(mape_nccases_5))
print('MAPE MT DEATH (alpha=0.5) = ' + str(mape_mtdeath_5))
print('MAPE NC DEATH (alpha=0.5) = ' + str(mape_ncdeath_5))
print('MAPE MT CASES (alpha=0.8) = ' + str(mape_mtcases_8))
print('MAPE NC CASES (alpha=0.8) = ' + str(mape_nccases_8))
print('MAPE MT DEATH (alpha=0.8) = ' + str(mape_mtdeath_8))
print('MAPE NC DEATH (alpha=0.8) = ' + str(mape_ncdeath_8))


"""2a_AR_EWMA.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1NrZUm2fc6SRC_NHrqtvPqLDqYchtCT0N
"""

import pandas as pd
import numpy as np
import math
from statistics import mean, median
from datetime import datetime as dt
import datetime
import matplotlib.pyplot as plt

def get_data(start, end, df_clean):
  MT_daily_cases = [int(df_clean['MT daily cases'][i]) for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  NC_daily_cases = [int(df_clean['NC daily cases'][i]) for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  MT_daily_death = [int(df_clean['MT daily death'][i]) for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  NC_daily_death = [int(df_clean['NC daily death'][i]) for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  date = [df_clean['Date'][i] for i in range(0, len(df_clean['Date'])) if dt.strptime(df_clean['Date'][i], "%m/%d/%Y")>=start and dt.strptime(df_clean['Date'][i], "%m/%d/%Y")<=end]
  return date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death

# Get daily data for the 4 weeks of August, 2020

start = datetime.datetime(2020, 8, 1)
end = datetime.datetime(2020, 8, 28)
df_clean = pd.read_csv('../data/clean_organised.csv')

date, MT_daily_cases, NC_daily_cases, MT_daily_death, NC_daily_death =  get_data(start, end, df_clean)
MT_daily_cases=np.array(MT_daily_cases)
NC_daily_cases=np.array(NC_daily_cases)
MT_daily_death=np.array(MT_daily_death)
NC_daily_death=np.array(NC_daily_death)

def AR(cases, days, date):
  original = np.array(cases, copy=True)
  X = []
  Y = []
  index = days
  for day in original[days:21]:
    Y.append(day)
    Xs = []
    Xs.append(1)
    for i in range(1,days+1):
      Xs.append(original[index-i])
    X.append(Xs)
    index += 1
  X = np.array(X)
  Y = np.array(Y).reshape(len(Y),1)
  for i in range(21,28):
    betas = np.matmul(np.matmul(np.linalg.inv(np.matmul(np.transpose(X),X)),np.transpose(X)), Y)
    new_y = int(betas[0][0])
    count = 1
    for x in range(1, days+1):
      new_y += betas[x]*original[i-count]
      count += 1
    new_y = int(new_y)
    Y = np.append(Y, [[new_y]])
    Y = Y.reshape(len(Y), 1)
    original[i] = new_y
    new_x = []
    new_x.append(1)
    for k in range(1,days+1):
      new_x.append(original[i-k])
    new_x = np.array(new_x)
    X = np.vstack([X, new_x])

  plt.plot(pd.to_datetime(date), original)
  plt.xticks(rotation=45)
  plt.plot(pd.to_datetime(date), cases)
  plt.show()
  return original[-7:]

AR_MT_daily_cases_THREE = AR(MT_daily_cases, 3, date)
AR_NC_daily_cases_THREE = AR(NC_daily_cases, 3, date)
AR_MT_daily_death_THREE = AR(MT_daily_death, 3, date)
AR_NC_daily_death_THREE = AR(NC_daily_death, 3, date)

AR_MT_daily_cases_FIVE = AR(MT_daily_cases, 5, date)
AR_NC_daily_cases_FIVE = AR(NC_daily_cases, 5, date)
AR_MT_daily_death_FIVE = AR(MT_daily_death, 5, date)
AR_NC_daily_death_FIVE = AR(NC_daily_death, 5, date)

mse_mtcases_THREE = np.sum((AR_MT_daily_cases_THREE - MT_daily_cases[-7:])**2)/7
mse_nccases_THREE = np.sum((AR_NC_daily_cases_THREE - NC_daily_cases[-7:])**2)/7
mse_mtdeath_THREE = np.sum((AR_MT_daily_death_THREE - MT_daily_death[-7:])**2)/7
mse_ncdeath_THREE = np.sum((AR_NC_daily_death_THREE - NC_daily_death[-7:])**2)/7

mse_mtcases_FIVE = np.sum((AR_MT_daily_cases_FIVE - MT_daily_cases[-7:])**2)/7
mse_nccases_FIVE = np.sum((AR_NC_daily_cases_FIVE - NC_daily_cases[-7:])**2)/7
mse_mtdeath_FIVE = np.sum((AR_MT_daily_death_FIVE - MT_daily_death[-7:])**2)/7
mse_ncdeath_FIVE = np.sum((AR_NC_daily_death_FIVE - NC_daily_death[-7:])**2)/7

print("MSE")
print('MSE MT CASES (parameter=3) = ' + str(mse_mtcases_THREE))
print('MSE NC CASES (parameter=3) = ' + str(mse_nccases_THREE))
print('MSE MT DEATH (parameter=3) = ' + str(mse_mtdeath_THREE))
print('MSE NC DEATH (parameter=3) = ' + str(mse_ncdeath_THREE))
print('MSE MT CASES (parameter=5) = ' + str(mse_mtcases_FIVE))
print('MSE NC CASES (parameter=5) = ' + str(mse_nccases_FIVE))
print('MSE MT DEATH (parameter=5) = ' + str(mse_mtdeath_FIVE))
print('MSE NC DEATH (parameter=5) = ' + str(mse_ncdeath_FIVE))

mape_mtcases_THREE = np.sum(abs((MT_daily_cases[-7:] - AR_MT_daily_cases_THREE)/MT_daily_cases[-7:]))/7 * 100
mape_nccases_THREE = np.sum(abs((NC_daily_cases[-7:] - AR_NC_daily_cases_THREE)/NC_daily_cases[-7:]))/7 * 100
mape_mtdeath_THREE = np.sum(abs((MT_daily_death[-7:] - AR_MT_daily_death_THREE)/MT_daily_death[-7:]))/7 * 100
mape_ncdeath_THREE = np.sum(abs((NC_daily_death[-7:] - AR_NC_daily_death_THREE)/NC_daily_death[-7:]))/7 * 100

mape_mtcases_FIVE = np.sum(abs((MT_daily_cases[-7:] - AR_MT_daily_cases_FIVE)/MT_daily_cases[-7:]))/7 * 100
mape_nccases_FIVE = np.sum(abs((NC_daily_cases[-7:] - AR_NC_daily_cases_FIVE)/NC_daily_cases[-7:]))/7 * 100
mape_mtdeath_FIVE = np.sum(abs((MT_daily_death[-7:] - AR_MT_daily_death_FIVE)/MT_daily_death[-7:]))/7 * 100
mape_ncdeath_FIVE = np.sum(abs((NC_daily_death[-7:] - AR_NC_daily_death_FIVE)/NC_daily_death[-7:]))/7 * 100

print("MAPE in %")
print('MAPE MT CASES (parameter=3) = ' + str(mape_mtcases_THREE))
print('MAPE NC CASES (parameter=3) = ' + str(mape_nccases_THREE))
print('MAPE MT DEATH (parameter=3) = ' + str(mape_mtdeath_THREE))
print('MAPE NC DEATH (parameter=3) = ' + str(mape_ncdeath_THREE))
print('MAPE MT CASES (parameter=5) = ' + str(mape_mtcases_FIVE))
print('MAPE NC CASES (parameter=5) = ' + str(mape_nccases_FIVE))
print('MAPE MT DEATH (parameter=5) = ' + str(mape_mtdeath_FIVE))
print('MAPE NC DEATH (parameter=5) = ' + str(mape_ncdeath_FIVE))

AR(NC_daily_cases,3, date)

AR(MT_daily_cases,3, date)

AR(NC_daily_cases,5, date)

AR(MT_daily_cases,5, date)

AR(NC_daily_death,3, date)

AR(NC_daily_death,5, date)

AR(MT_daily_death,5, date)

AR(MT_daily_death,3, date)

def EWMA(original, alpha):
    predicted = [0] * len(original)
    predicted[0] = original[0]
    for i in range(1, len(original)):
        predicted[i] = int((1-alpha) * predicted[i - 1] + (alpha) * original[i - 1])
    # print(len(predicted))
    return np.array(predicted[-7:])

EWMA_MT_daily_cases_5 = EWMA(MT_daily_cases, 0.5)
EWMA_NC_daily_cases_5 = EWMA(NC_daily_cases, 0.5)
EWMA_MT_daily_death_5 = EWMA(MT_daily_death, 0.5)
EWMA_NC_daily_death_5 = EWMA(NC_daily_death, 0.5)

EWMA_MT_daily_cases_8 = EWMA(MT_daily_cases, 0.8)
EWMA_NC_daily_cases_8 = EWMA(NC_daily_cases, 0.8)
EWMA_MT_daily_death_8 = EWMA(MT_daily_death, 0.8)
EWMA_NC_daily_death_8 = EWMA(NC_daily_death, 0.8)

mse_mtcases_5 = np.sum((EWMA_MT_daily_cases_5 - MT_daily_cases[-7:])**2)/7
mse_nccases_5 = np.sum((EWMA_NC_daily_cases_5 - NC_daily_cases[-7:])**2)/7
mse_mtdeath_5 = np.sum((EWMA_MT_daily_death_5 - MT_daily_death[-7:])**2)/7
mse_ncdeath_5 = np.sum((EWMA_NC_daily_death_5 - NC_daily_death[-7:])**2)/7

mse_mtcases_8 = np.sum((EWMA_MT_daily_cases_8 - MT_daily_cases[-7:])**2)/7
mse_nccases_8 = np.sum((EWMA_NC_daily_cases_8 - NC_daily_cases[-7:])**2)/7
mse_mtdeath_8 = np.sum((EWMA_MT_daily_death_8 - MT_daily_death[-7:])**2)/7
mse_ncdeath_8 = np.sum((EWMA_NC_daily_death_8 - NC_daily_death[-7:])**2)/7

print("MSE")
print('MSE MT CASES (alpha=0.5) = ' + str(mse_mtcases_5))
print('MSE NC CASES (alpha=0.5) = ' + str(mse_nccases_5))
print('MSE MT DEATH (alpha=0.5) = ' + str(mse_mtdeath_5))
print('MSE NC DEATH (alpha=0.5) = ' + str(mse_ncdeath_5))
print('MSE MT CASES (alpha=0.8) = ' + str(mse_mtcases_8))
print('MSE NC CASES (alpha=0.8) = ' + str(mse_nccases_8))
print('MSE MT DEATH (alpha=0.8) = ' + str(mse_mtdeath_8))
print('MSE NC DEATH (alpha=0.8) = ' + str(mse_ncdeath_8))

mape_mtcases_5 = np.sum(abs((MT_daily_cases[-7:] - EWMA_MT_daily_cases_5)/MT_daily_cases[-7:]))/7 * 100
mape_nccases_5 = np.sum(abs((NC_daily_cases[-7:] - EWMA_NC_daily_cases_5)/NC_daily_cases[-7:]))/7 * 100
mape_mtdeath_5 = np.sum(abs((MT_daily_death[-7:] - EWMA_MT_daily_death_5)/MT_daily_death[-7:]))/7 * 100
mape_ncdeath_5 = np.sum(abs((NC_daily_death[-7:] - EWMA_NC_daily_death_5)/NC_daily_death[-7:]))/7 * 100

mape_mtcases_8 = np.sum(abs((MT_daily_cases[-7:] - EWMA_MT_daily_cases_8)/MT_daily_cases[-7:]))/7 * 100
mape_nccases_8 = np.sum(abs((NC_daily_cases[-7:] - EWMA_NC_daily_cases_8)/NC_daily_cases[-7:]))/7 * 100
mape_mtdeath_8 = np.sum(abs((MT_daily_death[-7:] - EWMA_MT_daily_death_8)/MT_daily_death[-7:]))/7 * 100
mape_ncdeath_8 = np.sum(abs((NC_daily_death[-7:] - EWMA_NC_daily_death_8)/NC_daily_death[-7:]))/7 * 100

print("MAPE in %")
print('MAPE MT CASES (alpha=0.5) = ' + str(mape_mtcases_5))
print('MAPE NC CASES (alpha=0.5) = ' + str(mape_nccases_5))
print('MAPE MT DEATH (alpha=0.5) = ' + str(mape_mtdeath_5))
print('MAPE NC DEATH (alpha=0.5) = ' + str(mape_ncdeath_5))
print('MAPE MT CASES (alpha=0.8) = ' + str(mape_mtcases_8))
print('MAPE NC CASES (alpha=0.8) = ' + str(mape_nccases_8))
print('MAPE MT DEATH (alpha=0.8) = ' + str(mape_mtdeath_8))
print('MAPE NC DEATH (alpha=0.8) = ' + str(mape_ncdeath_8))

