# Modelagem SEIR
Neste notebook está implementado o modelo SEIR de Zhilan Feng. Que inclui quarentena, e hospitalizações

\begin{align}
\frac{dS}{dt}&=-\beta S (I+(1-\rho)H)\\
\frac{dE}{dt}&= \beta S (I+(1-\rho)H)-(\chi+\alpha)E\\
\frac{dQ}{dt}&=\chi E -\alpha Q\\
\frac{dI}{dt}&= \alpha E - (\phi+\delta)I\\
\frac{dH}{dt}&= \alpha Q +\phi I -\delta H\\
\frac{dR}{dt}&= \delta I +\delta H
\end{align}

Denotando o tamanho total da epidemia por $Y_e(t)=E(t)+Q(t)+I(t)+H(t)$, podemos escrever:

\begin{align}
\nonumber \frac{dY_e}{dt}&= \frac{dE}{dt}+\frac{dQ}{dt}+\frac{dI}{dt}+\frac{dH}{dt}\\
\label{ye}&=\beta S (I(1-\rho)H) -\delta(I+H)
\end{align}



```python
from scipy.integrate import odeint
from scipy.integrate import DOP853
import pandas as pd
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import warnings
import humanizer_portugues as hp
import seaborn as sns
from datetime import timedelta
warnings.filterwarnings("ignore")
%pylab inline
```

    Populating the interactive namespace from numpy and matplotlib



```python
def seqihr(y,t,*params):
    S,E,Q,I,H,R = y
    chi,phi,beta,rho,delta,alpha = params
    return[
        -beta*S*(I+(1-rho)*H), #dS/dt
        beta*S*(I+(1-rho)*H) - (chi+alpha)*E,#dE/dt
        chi*E -alpha*Q,#dQ/dt
        alpha*E - (phi+delta)*I,#dI/dt
        alpha*Q + phi*I -delta*H,#dH/dt
        delta*I + delta*H,#dR/dt
    ]
```


```python
chi=.05  # Taxa de quarentenação
phi=.01 # Taxa de Hospitalização
beta=.2 #taxa de Transmissão
rho=.6 # Atenuação da transmissão quando hospitalizado
delta=1/10 # Taxa de recuperação hospitalar
alpha=1/3 # Taxa de incubaçao
```


```python
inits = [0.9,.1,0,0,0,0]
trange = arange(0,100,.1)
res = odeint(seqihr,inits,trange,args=(chi,phi,beta,rho,delta,alpha))
```


```python

@interact(chi=(0,.1, 0.01),phi=(0,.1,.005),beta=(0,.5,.05),rho=(0,1,.1),delta=(0,1,.05),alpha=(0,1,0.01))
def plota_simulação(chi=0.05,phi=.01,beta=.2,rho=.6,delta=.1,alpha=.33):
    res = odeint(seqihr,inits,trange,args=(chi,phi,beta,rho,delta,alpha))
    fig, ax = subplots(1,1, figsize=(15,10))
    ax.plot(trange,res[:,1:-1])
    ax.set_ylabel('Fração da População')
    ax.set_xlabel('Tempo (dias)')
    ax.grid()
    ax.legend(['Exposto','Quar','Inf','Hosp']);
```


    interactive(children=(FloatSlider(value=0.05, description='chi', max=0.1, step=0.01), FloatSlider(value=0.01, …


## Adicionando Assintomáticos

O modelo de Feng não é apropriado para representar a epidemia da COVID-19. Vamos Modificá-lo para incluir entre outras coisas, os assintomáticos. Para simplificar a notação vamos denotar por $\lambda=\beta(I\color{red}{+A}+(1-\rho)H)$, já incluindo os assintomáticos, $A$.

\begin{align}
\frac{dS}{dt}&=-\lambda[\color{red}{(1-\chi)} S] \\
\frac{dE}{dt}&= \lambda [\color{red}{(1-\chi)} S] -\alpha E\\
\frac{dI}{dt}&= \color{red}{(1-p)}\alpha E - (\phi+\delta)I\\
\color{red}{\frac{dA}{dt}}&= \color{red}{p\alpha E -\delta A}\\
\frac{dH}{dt}&= \phi I -\delta H\\
\frac{dR}{dt}&= \delta I +\delta H \color{red}{+ \delta A}
\end{align}

Neste novo modelo alteramos mais algumas coisinhas ressaltadas em vermelho acima. Em primeiro lugar, fizemos com que a quarentena seja aplicada aos suscetíveis, efetivamente removendo-os da cadeia de transmissão, para simplificar removemos o compartimento $Q$. Além disso, para alimentar a classe dos Assintomáticos, definimos $p$ como a fração de assintomáticos na população. $R_0\approx$

### Adicionando ou removendo a quarentena
Para permitir que a quarentena seja acionada no modelo em uma data definida por um parâmetro $q$, precisamos expressá-la como uma função diferenciável que varie de zero a um rapidamente. Esta função é então multiplicada pelo parâmetro de quarentena $\chi$.
\begin{equation}
    \chi^+(t)=\chi\frac{1+tanh(t-q)}{2}
\end{equation}

Para remover a quarentena podemos usar a função com o sinal trocado na tangente hiperbólica.
\begin{equation}
    \chi^-(t)=\chi\frac{1-tanh(t-q)}{2}
\end{equation}
Abaixo temos uma exemplificação grafica de $\chi^+(t)$ e $\chi^-(t)$


```python
t = arange(-3,10,.01)
d=1
q = 0.3 #Nivel de quarentena desejado
addq = lambda t,d: (1+tanh(t-d))/2
remq = lambda t,d: (1-tanh(t-d))/2
f,(ax1,ax2)=subplots(1,2, figsize=(15,8))
ax1.plot(t,q*addq(t,d), label=f"liga a quarent. no dia {d}")
ax1.plot(t,q*remq(t,d), label=f"desliga a quarent. no dia {d}")
ax2.plot(t,q*addq(t,d)*remq(t,d+5))
ax1.vlines(d,0,q)
ax2.vlines(d,0,q,'g')
ax2.vlines(d+5,0,q, 'r')
ax1.grid()
ax2.grid()
ax1.legend();
```


```python
f,ax=subplots(1,1, figsize=(15,8))
ax.plot(t,q*addq(t,d)*remq(t,d+5))
ax.vlines(d,0,q,'g')
ax.vlines(d+5,0,q, 'r')
ax.set_ylabel('Fraction Quarantined')
ax.set_xlabel('Time(days)')
ax.grid()
savefig('chi_of_t.png')
```


```python
def seqiahr(y,t,*params):
    S,E,I,A,H,R,C,M = y
    chi,phi,beta,rho,delta,alpha,mu,p,q,r = params
    lamb = beta*(I+A)
    chi *= ((1+np.tanh(t-q))/2) * ((1 - np.tanh(t - (q+r))) / 2 )
    return[
        -lamb*((1-chi)*S), #dS/dt
        lamb*((1-chi)*S) - alpha*E,#dE/dt
        (1-p)*alpha*E - delta*I,#dI/dt
        p*alpha*E - delta*A,
        phi*delta*I -(rho+mu)*H,#dH/dt
        (1-phi)*delta*I + rho*H+delta*A ,#dR/dt
        phi*delta*I,#(1-p)*alpha*E+ p*alpha*E # Casos acumulados
        mu*H  #Mortes acumuladas
    ]
```


```python
trange = arange(0, 365, 1)
inits = [0.99, 0, 1.0277e-6, 0.0, 0, 0, 0, 0]
N = 97.3e6
fat = 0.035  # case fatality rate
# sumario = open('cenarios.csv','w')
# sumario.write('R0,Quarentena,tamanho_total,hosp_total,hosp_pico,morte_total\n')



def r0(chi, phi, beta, rho, delta, p, S=1):
    "R0 for seqiahr2"
    return -(S*beta*chi - S*beta)/delta


@interact(χ=(0, 1, 0.05),
          φ=(0, .5, .01),
          β=(0, 1, .02),
          ρ=(0, 1, .1),
          δ=(0, 1, .05),
          α=(0, 10, 0.01),
          mu=(0,1,.01),
          p=(0, 1, .05),
          q=(0, 120, 10),
          r=(0,100,10))
def plota_simulação(χ=0.7, φ=.01, β=.5, ρ=0.05, δ=.1, α=.33, mu=0.03, p=.75, q=30, r=10):
    res = odeint(seqiahr, inits, trange, args=(χ, φ, β, ρ, δ, α, mu, p, q,r))
    rzero = r0(χ, φ, β, ρ, δ, p)
#     et = 1 / rzero
#     idx = np.argwhere(np.abs(res[:, 0] - et) == min(np.abs(res[:, 0] -
#                                                            et)))[0, 0]
    fig, ax = subplots(1, 1, figsize=(15, 10))
    ax.plot(trange, res[:, 1])#E
#     ax.plot(trange, res[:, 2:4])
    ax.plot(trange, res[:, 4]) #H
    ax.plot(trange, res[:,-1]) #M
    ax.set_ylim([0, 0.02])
    Ye = res[:, 1:4].sum(axis=1)
    Imax = (1 - res[-1, 0]) * N  # N-S(inf)
    Hosp_p = res[:, 4].max() * N  # pico de Hospitalizações
    Hosp_tot = (res[-1, -2]) * N
    casos = (res[:, 2] + res[:, 4])  # Casos notificados H+I
    casosT = Hosp_tot + ((1 - p) * α * res[:, 1]).sum() * N  # N*sum(p*α*E)
    M = res[-1,-1]*N
    #     secax = ax.twinx()
    #     secax.plot(trange, Ye,'k:')
    #     secax.set_ylabel('Total prevalence fraction')
    ax.text(0,
            0.005,
            f"$R_0~{rzero:.1f}$\n$p={p}$\n$\\rho={ρ}$\n$\\chi={χ*100}$%",
            fontsize=16)
    ax.text(
        110,
        .01,
        f"Tamanho total: {hp.intword(Imax)} infectados\nHosp. pico: {hp.intword(Hosp_p)}\nHosp. totais: {hp.intword(Hosp_tot)}\nMortes: {hp.intword(M)}",
        fontsize=16)
#     ax.vlines(trange[idx], 0, 0.02,'b')  #res[:,1:-1].max())
    ax.vlines(q, 0, .02)
    ax.vlines(q+r, 0, .02,'r')
    ax.set_ylabel('Fração da População')
    ax.set_xlabel('Tempo (dias)')
    ax.grid()
    #         sumario.write(f'{rzero},{χ*100},{Imax},{Hosp_tot},{Hosp_p},{M}\n')
#     ax.legend(['Exposto', 'Inf', 'Assintomático', 'Hosp', 'Mortes'])
    ax.legend(['Exposto', 'Hospitalizado', 'Mortes'])
#     plt.savefig(f"", dpi=300)
```


    interactive(children=(FloatSlider(value=0.7, description='χ', max=1.0, step=0.05), FloatSlider(value=0.01, des…



```python
# sumario.close()
# suma = pd.read_csv('cenarios.csv')
# suma.drop_duplicates(inplace=True)
# suma
```


```python
def save_data():
    fig,ax = subplots(1,1, figsize=(15,10), constrained_layout=True)
    χ=0.05;φ=.01;β=.25;ρ=.6;δ=.1;α=10; p=.75; q=0
    i=1
    dff = pd.DataFrame(data={'Sem controle':range(365),'Com controle':range(365)})
    for q,R0,c in [(0,3.6,'Sem controle'),(10,1.7,'Com controle')]:
        res = odeint(seqiahr2,inits,trange,args=(q/100,φ,R0/10,ρ,δ,α,p,q))
        df = pd.DataFrame(data=res*N, columns=['S','E','I','A','H','R','C'],index=trange)
        ax = df[['H']].plot(ax=ax, grid=True)
        dff[c] =df.H
    plt.legend(['Sem Controles','Com controles'])
    plt.savefig(f'export/achatando a curva.png', dpi=300)
    dff.to_csv(f'export/achatando a curva.csv')

save_data()
```

## Carregando os dados do Brasil


```python
# Pegando os casos do Brasil.io
cases = pd.read_csv('https://brasil.io/dataset/covid19/caso?format=csv')
cases.date = pd.to_datetime(cases.date)
```


```python
df_states = cases[cases.place_type!='state'].groupby(['date','state']).sum()
df_states.reset_index(inplace=True)
df_states.set_index('date', inplace=True)
```


```python
# df_states.set_index('date', inplace=True)
fig,ax = subplots(1,1,figsize=(15,8))
for uf in ['SP','RJ','MG','CE','RS']:
    df_states[df_states.state==uf].confirmed.plot(style='-o', label=uf)

ax.legend()
plt.savefig('Casos_confirmados_estados.png',dpi=200)
```


```python
# Pegando os dados do Ministério
casos = pd.read_csv('COVID19_20200411.csv',sep=';')
casos.data = pd.to_datetime(casos.data, dayfirst=True)
casos_estados = casos.groupby(['data','estado']).sum()
casos_estados
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th></th>
      <th>casosNovos</th>
      <th>casosAcumulados</th>
      <th>obitosNovos</th>
      <th>obitosAcumulados</th>
    </tr>
    <tr>
      <th>data</th>
      <th>estado</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th rowspan="5" valign="top">2020-01-30</th>
      <th>AC</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>AL</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>AM</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>AP</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>BA</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>...</th>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th rowspan="5" valign="top">2020-04-11</th>
      <th>RS</th>
      <td>4</td>
      <td>640</td>
      <td>1</td>
      <td>15</td>
    </tr>
    <tr>
      <th>SC</th>
      <td>39</td>
      <td>732</td>
      <td>3</td>
      <td>21</td>
    </tr>
    <tr>
      <th>SE</th>
      <td>0</td>
      <td>42</td>
      <td>0</td>
      <td>4</td>
    </tr>
    <tr>
      <th>SP</th>
      <td>203</td>
      <td>8419</td>
      <td>20</td>
      <td>560</td>
    </tr>
    <tr>
      <th>TO</th>
      <td>0</td>
      <td>23</td>
      <td>0</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
<p>1971 rows × 4 columns</p>
</div>




```python
casos.columns
```




    Index(['regiao', 'estado', 'data', 'casosNovos', 'casosAcumulados',
           'obitosNovos', 'obitosAcumulados'],
          dtype='object')




```python
fig,ax = subplots(1,1,figsize=(15,8))
casos_estados.reset_index(inplace=True)
casos_estados.set_index('data', inplace=True)
for uf in ['SP','RJ','MG','CE','RS', 'BA']:
    casos_estados[casos_estados.estado==uf].casosAcumulados.plot(style='-o', label=uf)

ax.legend()
plt.savefig('Casos_Acumulados_estados.png',dpi=200);
```


```python
trange = arange(0,1095,1)
χ=0.0;φ=.1;β=.5;ρ=.6;δ=.1;α=.53; p=.75; q=-2; mu=0.02; r=1000
inits = [0.99,0,1.0277e-8, 0.0,0,0,0,0]
res = odeint(seqiahr,inits,trange,args=(χ,φ,β,ρ,δ,α,mu,p,q,r))
# Com controle
χ=0.1;β=.17
res_c = odeint(seqiahr,inits,trange,args=(χ,φ,β,ρ,δ,α,mu,p,q, r))
plt.plot(trange,res[:,-1],label='no control');
plt.plot(trange,res_c[:,-1],label='with control');
plt.legend();
```


```python
df_brasil = casos_estados.groupby('data').sum()
# df_brasil['casos_simulados']=res[:dias,-1]*N
# dfcp = df_brasil[['confirmed']]
# dfcp.to_csv('export/dados_brasil.csv')
df_brasil
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>casosNovos</th>
      <th>casosAcumulados</th>
      <th>obitosNovos</th>
      <th>obitosAcumulados</th>
    </tr>
    <tr>
      <th>data</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2020-01-30</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>2020-01-31</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>2020-02-01</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>2020-02-02</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>2020-02-03</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>2020-04-07</th>
      <td>1661</td>
      <td>13717</td>
      <td>114</td>
      <td>667</td>
    </tr>
    <tr>
      <th>2020-04-08</th>
      <td>2210</td>
      <td>15927</td>
      <td>133</td>
      <td>800</td>
    </tr>
    <tr>
      <th>2020-04-09</th>
      <td>1930</td>
      <td>17857</td>
      <td>141</td>
      <td>941</td>
    </tr>
    <tr>
      <th>2020-04-10</th>
      <td>1781</td>
      <td>19638</td>
      <td>115</td>
      <td>1056</td>
    </tr>
    <tr>
      <th>2020-04-11</th>
      <td>1089</td>
      <td>20727</td>
      <td>68</td>
      <td>1124</td>
    </tr>
  </tbody>
</table>
<p>73 rows × 4 columns</p>
</div>




```python
df_brasil.casosAcumulados.plot();
```


```python
dias=400
offset = 35 # quantos dias antes do primeiro caso notificado devemos começar a simulaçao
drange = pd.date_range(df_brasil[df_brasil.casosAcumulados>0].index.min()-timedelta(offset),periods=dias,freq='D')
# df_states.reset_index(inplace=True)
# df_brasil = df_states.groupby('date').sum()
fig,ax = subplots(1,1,figsize=(15,8))
ax.plot(drange,res[:dias,-1]*N,'-v', label='Sem controle')
# ax.plot(drange,res[:dias,-3]*N,'-v', label='Hosp')
# ax.vlines('2020-05-5',0,1e6)
ax.plot(drange,res_c[:dias,-1]*N,'-v', label='Com controle (Simulados)')
# ax.plot(drange,res[:dias,2]*N,'-^', label='Prevalência')
df_brasil[df_brasil.casosAcumulados>0].casosAcumulados.plot(ax=ax, style='-o', 
                                                            label='Dados oficiais',
                                                            grid=True, 
                                                            logy=True)
# Sem controle
ax.text('2020-05-15',0.6e5, f'Casos Totais: {res[dias,-1]*N:.0f}\nMortes: {res[dias,-1]*N*fat:.0f}', 
        fontsize=16)
# com 10% quarentena e R0=1.7
ax.text('2020-11-15',1e3, f'Casos Totais: {res_c[dias,-1]*N:.0f}\nMortes:{res_c[dias,-1]*N*fat:.0f}', 
        fontsize=16)
ax.legend();
# plt.savefig('export/Casos_vs_Projeções_log.png',dpi=200)
```


```python
df_sim = pd.DataFrame(data={'sem controle':res[:dias,-1]*N,'com controle':res_c[:dias,-1]*N}, index=drange)
df_sim.to_csv('simulação_brasil_com_e_sem_controle.csv')
```

## Comparando a série do Brasil com a de outros países


```python
confirmed = pd.read_csv('https://github.com/CSSEGISandData/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv')
confirmed = confirmed.groupby('Country/Region').sum()
```


```python
confirmed
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Lat</th>
      <th>Long</th>
      <th>1/22/20</th>
      <th>1/23/20</th>
      <th>1/24/20</th>
      <th>1/25/20</th>
      <th>1/26/20</th>
      <th>1/27/20</th>
      <th>1/28/20</th>
      <th>1/29/20</th>
      <th>...</th>
      <th>3/25/20</th>
      <th>3/26/20</th>
      <th>3/27/20</th>
      <th>3/28/20</th>
      <th>3/29/20</th>
      <th>3/30/20</th>
      <th>3/31/20</th>
      <th>4/1/20</th>
      <th>4/2/20</th>
      <th>4/3/20</th>
    </tr>
    <tr>
      <th>Country/Region</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Afghanistan</td>
      <td>33.0000</td>
      <td>65.0000</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>84</td>
      <td>94</td>
      <td>110</td>
      <td>110</td>
      <td>120</td>
      <td>170</td>
      <td>174</td>
      <td>237</td>
      <td>273</td>
      <td>281</td>
    </tr>
    <tr>
      <td>Albania</td>
      <td>41.1533</td>
      <td>20.1683</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>146</td>
      <td>174</td>
      <td>186</td>
      <td>197</td>
      <td>212</td>
      <td>223</td>
      <td>243</td>
      <td>259</td>
      <td>277</td>
      <td>304</td>
    </tr>
    <tr>
      <td>Algeria</td>
      <td>28.0339</td>
      <td>1.6596</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>302</td>
      <td>367</td>
      <td>409</td>
      <td>454</td>
      <td>511</td>
      <td>584</td>
      <td>716</td>
      <td>847</td>
      <td>986</td>
      <td>1171</td>
    </tr>
    <tr>
      <td>Andorra</td>
      <td>42.5063</td>
      <td>1.5218</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>188</td>
      <td>224</td>
      <td>267</td>
      <td>308</td>
      <td>334</td>
      <td>370</td>
      <td>376</td>
      <td>390</td>
      <td>428</td>
      <td>439</td>
    </tr>
    <tr>
      <td>Angola</td>
      <td>-11.2027</td>
      <td>17.8739</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>3</td>
      <td>4</td>
      <td>4</td>
      <td>5</td>
      <td>7</td>
      <td>7</td>
      <td>7</td>
      <td>8</td>
      <td>8</td>
      <td>8</td>
    </tr>
    <tr>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <td>Venezuela</td>
      <td>6.4238</td>
      <td>-66.5897</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>91</td>
      <td>107</td>
      <td>107</td>
      <td>119</td>
      <td>119</td>
      <td>135</td>
      <td>135</td>
      <td>143</td>
      <td>146</td>
      <td>153</td>
    </tr>
    <tr>
      <td>Vietnam</td>
      <td>16.0000</td>
      <td>108.0000</td>
      <td>0</td>
      <td>2</td>
      <td>2</td>
      <td>2</td>
      <td>2</td>
      <td>2</td>
      <td>2</td>
      <td>2</td>
      <td>...</td>
      <td>141</td>
      <td>153</td>
      <td>163</td>
      <td>174</td>
      <td>188</td>
      <td>203</td>
      <td>212</td>
      <td>218</td>
      <td>233</td>
      <td>237</td>
    </tr>
    <tr>
      <td>West Bank and Gaza</td>
      <td>31.9522</td>
      <td>35.2332</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>59</td>
      <td>84</td>
      <td>91</td>
      <td>98</td>
      <td>109</td>
      <td>116</td>
      <td>119</td>
      <td>134</td>
      <td>161</td>
      <td>194</td>
    </tr>
    <tr>
      <td>Zambia</td>
      <td>-15.4167</td>
      <td>28.2833</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>12</td>
      <td>16</td>
      <td>22</td>
      <td>28</td>
      <td>29</td>
      <td>35</td>
      <td>35</td>
      <td>36</td>
      <td>39</td>
      <td>39</td>
    </tr>
    <tr>
      <td>Zimbabwe</td>
      <td>-20.0000</td>
      <td>30.0000</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>3</td>
      <td>3</td>
      <td>5</td>
      <td>7</td>
      <td>7</td>
      <td>7</td>
      <td>8</td>
      <td>8</td>
      <td>9</td>
      <td>9</td>
    </tr>
  </tbody>
</table>
<p>181 rows × 75 columns</p>
</div>




```python
conf_US = pd.read_csv('https://github.com/CSSEGISandData/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv')
```


```python
conf_US
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>UID</th>
      <th>iso2</th>
      <th>iso3</th>
      <th>code3</th>
      <th>FIPS</th>
      <th>Admin2</th>
      <th>Province_State</th>
      <th>Country_Region</th>
      <th>Lat</th>
      <th>Long_</th>
      <th>...</th>
      <th>3/25/20</th>
      <th>3/26/20</th>
      <th>3/27/20</th>
      <th>3/28/20</th>
      <th>3/29/20</th>
      <th>3/30/20</th>
      <th>3/31/20</th>
      <th>4/1/20</th>
      <th>4/2/20</th>
      <th>4/3/20</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>16</td>
      <td>AS</td>
      <td>ASM</td>
      <td>16</td>
      <td>60.0</td>
      <td>NaN</td>
      <td>American Samoa</td>
      <td>US</td>
      <td>-14.2710</td>
      <td>-170.1320</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>1</td>
      <td>316</td>
      <td>GU</td>
      <td>GUM</td>
      <td>316</td>
      <td>66.0</td>
      <td>NaN</td>
      <td>Guam</td>
      <td>US</td>
      <td>13.4443</td>
      <td>144.7937</td>
      <td>...</td>
      <td>37</td>
      <td>45</td>
      <td>51</td>
      <td>55</td>
      <td>56</td>
      <td>58</td>
      <td>69</td>
      <td>77</td>
      <td>82</td>
      <td>84</td>
    </tr>
    <tr>
      <td>2</td>
      <td>580</td>
      <td>MP</td>
      <td>MNP</td>
      <td>580</td>
      <td>69.0</td>
      <td>NaN</td>
      <td>Northern Mariana Islands</td>
      <td>US</td>
      <td>15.0979</td>
      <td>145.6739</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>2</td>
      <td>6</td>
      <td>6</td>
      <td>6</td>
    </tr>
    <tr>
      <td>3</td>
      <td>630</td>
      <td>PR</td>
      <td>PRI</td>
      <td>630</td>
      <td>72.0</td>
      <td>NaN</td>
      <td>Puerto Rico</td>
      <td>US</td>
      <td>18.2208</td>
      <td>-66.5901</td>
      <td>...</td>
      <td>51</td>
      <td>64</td>
      <td>79</td>
      <td>100</td>
      <td>127</td>
      <td>174</td>
      <td>239</td>
      <td>286</td>
      <td>316</td>
      <td>316</td>
    </tr>
    <tr>
      <td>4</td>
      <td>850</td>
      <td>VI</td>
      <td>VIR</td>
      <td>850</td>
      <td>78.0</td>
      <td>NaN</td>
      <td>Virgin Islands</td>
      <td>US</td>
      <td>18.3358</td>
      <td>-64.8963</td>
      <td>...</td>
      <td>17</td>
      <td>17</td>
      <td>19</td>
      <td>22</td>
      <td>0</td>
      <td>0</td>
      <td>30</td>
      <td>30</td>
      <td>30</td>
      <td>37</td>
    </tr>
    <tr>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <td>3248</td>
      <td>84090053</td>
      <td>US</td>
      <td>USA</td>
      <td>840</td>
      <td>90053.0</td>
      <td>Unassigned</td>
      <td>Washington</td>
      <td>US</td>
      <td>0.0000</td>
      <td>0.0000</td>
      <td>...</td>
      <td>51</td>
      <td>69</td>
      <td>67</td>
      <td>0</td>
      <td>125</td>
      <td>274</td>
      <td>274</td>
      <td>303</td>
      <td>344</td>
      <td>501</td>
    </tr>
    <tr>
      <td>3249</td>
      <td>84090054</td>
      <td>US</td>
      <td>USA</td>
      <td>840</td>
      <td>90054.0</td>
      <td>Unassigned</td>
      <td>West Virginia</td>
      <td>US</td>
      <td>0.0000</td>
      <td>0.0000</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>3250</td>
      <td>84090055</td>
      <td>US</td>
      <td>USA</td>
      <td>840</td>
      <td>90055.0</td>
      <td>Unassigned</td>
      <td>Wisconsin</td>
      <td>US</td>
      <td>0.0000</td>
      <td>0.0000</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>61</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>3251</td>
      <td>84090056</td>
      <td>US</td>
      <td>USA</td>
      <td>840</td>
      <td>90056.0</td>
      <td>Unassigned</td>
      <td>Wyoming</td>
      <td>US</td>
      <td>0.0000</td>
      <td>0.0000</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>3252</td>
      <td>84099999</td>
      <td>US</td>
      <td>USA</td>
      <td>840</td>
      <td>99999.0</td>
      <td>NaN</td>
      <td>Grand Princess</td>
      <td>US</td>
      <td>0.0000</td>
      <td>0.0000</td>
      <td>...</td>
      <td>28</td>
      <td>28</td>
      <td>28</td>
      <td>103</td>
      <td>103</td>
      <td>103</td>
      <td>103</td>
      <td>103</td>
      <td>103</td>
      <td>103</td>
    </tr>
  </tbody>
</table>
<p>3253 rows × 84 columns</p>
</div>




```python
conf_US.groupby('Country_Region').sum()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>UID</th>
      <th>code3</th>
      <th>FIPS</th>
      <th>Lat</th>
      <th>Long_</th>
      <th>1/22/20</th>
      <th>1/23/20</th>
      <th>1/24/20</th>
      <th>1/25/20</th>
      <th>1/26/20</th>
      <th>...</th>
      <th>3/25/20</th>
      <th>3/26/20</th>
      <th>3/27/20</th>
      <th>3/28/20</th>
      <th>3/29/20</th>
      <th>3/30/20</th>
      <th>3/31/20</th>
      <th>4/1/20</th>
      <th>4/2/20</th>
      <th>4/3/20</th>
    </tr>
    <tr>
      <th>Country_Region</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>US</td>
      <td>272936469664</td>
      <td>2730712</td>
      <td>104327612.0</td>
      <td>120958.868944</td>
      <td>-290083.749251</td>
      <td>1</td>
      <td>1</td>
      <td>2</td>
      <td>2</td>
      <td>5</td>
      <td>...</td>
      <td>65778</td>
      <td>83836</td>
      <td>101657</td>
      <td>121478</td>
      <td>140886</td>
      <td>161807</td>
      <td>188172</td>
      <td>213362</td>
      <td>243453</td>
      <td>275582</td>
    </tr>
  </tbody>
</table>
<p>1 rows × 78 columns</p>
</div>




```python
serie_US = conf_US.groupby('Country_Region').sum().T
serie_US = serie_US.iloc[5:]
serie_US.index  = pd.to_datetime(serie_US.index)
serie_US.index.name = 'data'
# serie_US['acumulados'] = np.cumsum(serie_US.US)
serie_US
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>Country_Region</th>
      <th>US</th>
    </tr>
    <tr>
      <th>data</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>2020-01-22</td>
      <td>1.0</td>
    </tr>
    <tr>
      <td>2020-01-23</td>
      <td>1.0</td>
    </tr>
    <tr>
      <td>2020-01-24</td>
      <td>2.0</td>
    </tr>
    <tr>
      <td>2020-01-25</td>
      <td>2.0</td>
    </tr>
    <tr>
      <td>2020-01-26</td>
      <td>5.0</td>
    </tr>
    <tr>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <td>2020-03-30</td>
      <td>161807.0</td>
    </tr>
    <tr>
      <td>2020-03-31</td>
      <td>188172.0</td>
    </tr>
    <tr>
      <td>2020-04-01</td>
      <td>213362.0</td>
    </tr>
    <tr>
      <td>2020-04-02</td>
      <td>243453.0</td>
    </tr>
    <tr>
      <td>2020-04-03</td>
      <td>275582.0</td>
    </tr>
  </tbody>
</table>
<p>73 rows × 1 columns</p>
</div>




```python
# outros = pd.DataFrame(data=)
suecia = confirmed.loc['Sweden'][2:]
alemanha = confirmed.loc['Germany'][2:]
espanha = confirmed.loc['Spain'][2:]
italia = confirmed.loc['Italy'][2:]
outros = pd.concat([suecia,alemanha,espanha,italia], axis=1)
outros.index = pd.to_datetime(outros.index)
# outros['Sweden_acc'] = np.cumsum(outros.Sweden)
# outros['Germany_acc'] = np.cumsum(outros.Germany)
# outros['Spain_acc'] = np.cumsum(outros.Spain)
# outros['Italy_acc'] = np.cumsum(outros.Italy)
outros
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Sweden</th>
      <th>Germany</th>
      <th>Spain</th>
      <th>Italy</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>2020-01-22</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
    <tr>
      <td>2020-01-23</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
    <tr>
      <td>2020-01-24</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
    <tr>
      <td>2020-01-25</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
    <tr>
      <td>2020-01-26</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
    <tr>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <td>2020-03-30</td>
      <td>4028.0</td>
      <td>66885.0</td>
      <td>87956.0</td>
      <td>101739.0</td>
    </tr>
    <tr>
      <td>2020-03-31</td>
      <td>4435.0</td>
      <td>71808.0</td>
      <td>95923.0</td>
      <td>105792.0</td>
    </tr>
    <tr>
      <td>2020-04-01</td>
      <td>4947.0</td>
      <td>77872.0</td>
      <td>104118.0</td>
      <td>110574.0</td>
    </tr>
    <tr>
      <td>2020-04-02</td>
      <td>5568.0</td>
      <td>84794.0</td>
      <td>112065.0</td>
      <td>115242.0</td>
    </tr>
    <tr>
      <td>2020-04-03</td>
      <td>6131.0</td>
      <td>91159.0</td>
      <td>119199.0</td>
      <td>119827.0</td>
    </tr>
  </tbody>
</table>
<p>73 rows × 4 columns</p>
</div>




```python
fig,ax = subplots(1,1,figsize=(15,8))
df_brasil[df_brasil.casosAcumulados>0].casosAcumulados.plot(ax=ax, style='-o', 
                                                            label='Brasil',
                                                            grid=True, 
                                                            logy=True)
serie_US.US.plot(ax=ax, style='-v', grid=True,
                        label='EUA',);
outros.Sweden.plot(ax=ax, style='-v', grid=True,
                        label='Suécia')
outros.Germany.plot(ax=ax, style='-v', grid=True,
                        label='Alemanha')
outros.Spain.plot(ax=ax, style='-v', grid=True,
                        label='Espanha')
outros.Italy.plot(ax=ax, style='-v', grid=True,
                        label='Italia')
ax.legend();
```


```python
fig,ax = subplots(1,1,figsize=(15,8))
alinhados = pd.concat([suecia[suecia>100].reset_index(),
           alemanha[alemanha>100].reset_index(),
           espanha[espanha>100].reset_index(),
           italia[italia>100].reset_index()], 
          axis=1)
alinhados['EUA'] = serie_US[serie_US.US>100].reset_index().US
alinhados['Brasil'] = df_brasil[df_brasil.casosAcumulados>100].reset_index().casosAcumulados
alinhados.plot(ax=ax,logy=True, grid=True);
plt.savefig('export/Brasil_vs_outros.png', dpi=300)
alinhados.to_csv('export/Brasil_vs_outros.csv')
```


```python
serie_US[serie_US.acumulados>100].reset_index().acumulados
```




    0         109.0
    1         120.0
    2         131.0
    3         142.0
    4         154.0
    5         166.0
    6         179.0
    7         192.0
    8         205.0
    9         218.0
    10        231.0
    11        244.0
    12        257.0
    13        270.0
    14        285.0
    15        300.0
    16        315.0
    17        330.0
    18        345.0
    19        360.0
    20        376.0
    21        392.0
    22        416.0
    23        446.0
    24        499.0
    25        572.0
    26        676.0
    27        848.0
    28       1065.0
    29       1401.0
    30       1851.0
    31       2365.0
    32       3073.0
    33       4178.0
    34       5735.0
    35       7882.0
    36      10739.0
    37      13657.0
    38      17964.0
    39      24060.0
    40      32933.0
    41      46957.0
    42      66187.0
    43      91814.0
    44     125435.0
    45     169098.0
    46     222834.0
    47     288612.0
    48     372448.0
    49     474105.0
    50     595583.0
    51     736469.0
    52     898276.0
    53    1086448.0
    54    1299810.0
    55    1543263.0
    Name: acumulados, dtype: float64




```python
1-timedelta(32)
```


```python
pd.date_range(df_brasil[df_brasil.casosAcumulados>0].index.min()-timedelta(offset),periods=dias,freq='D')
```




    DatetimeIndex(['2020-01-22', '2020-01-23', '2020-01-24', '2020-01-25',
                   '2020-01-26', '2020-01-27', '2020-01-28', '2020-01-29',
                   '2020-01-30', '2020-01-31',
                   ...
                   '2021-02-15', '2021-02-16', '2021-02-17', '2021-02-18',
                   '2021-02-19', '2021-02-20', '2021-02-21', '2021-02-22',
                   '2021-02-23', '2021-02-24'],
                  dtype='datetime64[ns]', length=400, freq='D')




```python

```

48a66629d29f134bf7b9d8fd9a00c21b6a087ade
ipynb
Jupyter Notebook
notebooks/Modelo SEIR.ipynb
nahumsa/covidash
24f3fdabb41ceeaadc4582ed2820f6f7f1a392a1
MIT
notebooks/Modelo SEIR.ipynb
nahumsa/covidash
24f3fdabb41ceeaadc4582ed2820f6f7f1a392a1
MIT
notebooks/Modelo SEIR.ipynb
nahumsa/covidash
24f3fdabb41ceeaadc4582ed2820f6f7f1a392a1
MIT
Qwen/Qwen-72B
1. YES
2. YES