application_vnd.jupyter.stderr_output_1.png
from ipywidgets import interact, interact_manual, fixed

from pathlib import Path

from sklearn.linear_model import LinearRegression

from sklearn.metrics import r2_score, mean_squared_error

import arviz as az

import cufflinks as cf

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import pymc3 as pm

pd.set_option('display.max_rows', 500)
pd.set_option('use_inf_as_na', True)
cf.set_config_file(offline=True, theme='solar')
path = Path('../input/novel-corona-virus-2019-dataset/')

master_df = pd.read_csv(path / 'covid_19_data.csv')

recovered_df = pd.read_csv(path / 'time_series_covid_19_recovered.csv').drop(columns=['Lat', 'Long']).groupby('Country/Region').sum()
deaths_df = pd.read_csv(path / 'time_series_covid_19_deaths.csv').drop(columns=['Lat', 'Long']).groupby('Country/Region').sum()
confirmed_df = pd.read_csv(path / 'time_series_covid_19_confirmed.csv').drop(columns=['Lat', 'Long']).groupby('Country/Region').sum()

sorted_country_list = confirmed_df.sort_values(by=confirmed_df.columns[-1], ascending=False).index.to_list()

@interact(country=sorted_country_list, threshold=(0, 1000, 10))
def log_lin_visualise(country, threshold=100):
    y = confirmed_df.filter(items=[country], axis=0).values.squeeze(0)
    y = np.log(y[y > threshold])
    x = np.arange(1, y.shape[0] + 1)
    lr = LinearRegression(normalize=True)
    lr.fit(X=x.reshape(-1, 1), y=y)
    y_fitted = lr.predict(X=x.reshape(-1, 1))
    plt.close()

country = 'US'
threshold = 100
y = confirmed_df.filter(items=[country], axis=0).values.squeeze(0)
y = np.log(y[y > threshold])
x = np.arange(1, y.shape[0] + 1)
with pm.Model() as unpooled_model:
    α = pm.Normal(name='α', mu=int(np.log(threshold)), sd=10)
    β = pm.Normal(name='β')
    σ = pm.HalfNormal(name='σ', sd=10)
    μ = pm.Deterministic(name='μ', var=α + β * x)
    pm.Normal(name=country, mu=μ, sd=σ, observed=y)
pm.model_to_graphviz(unpooled_model)

with unpooled_model:
    prior = pm.sample_prior_predictive()
    trace = pm.sample()
    pred = pm.sample_posterior_predictive(trace)

unpooled = az.from_pymc3(trace=trace, prior=prior, posterior_predictive=pred)
prior_vars = ['α', 'β', 'σ']
unpooled

az.summary(unpooled)