Skip to content

Instantly share code, notes, and snippets.

@arivero
Last active April 14, 2020 10:50
Show Gist options
  • Save arivero/a6bd5359fe17241b64efafded2bd2165 to your computer and use it in GitHub Desktop.
Save arivero/a6bd5359fe17241b64efafded2bd2165 to your computer and use it in GitHub Desktop.
Analysis en modelo SI (sin R) de los datos Covid oficiales de España
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
dataframe=pd.read_csv("https://covid19.isciii.es/resources/serie_historica_acumulados.csv", skipfooter=3, engine="python", encoding='iso8859')
# In[2]:
dataframe['date']=pd.to_datetime(dataframe['FECHA'],format='%d/%m/%Y')
# In[3]:
data=dataframe.drop(columns='FECHA').set_index(['CCAA','date']).sort_index().unstack(level=0).fillna(0)
data
# In[4]:
data.plot(x=['CASOS'],y=['Fallecidos'],kind='scatter')
# In[5]:
data.plot(x=[('CASOS','MD')],y=[('Fallecidos','MD')],kind='scatter')
# In[6]:
#y a jugar
fallRoll=pd.concat([data[('Fallecidos','MD')].rolling(4).mean(),data[('Fallecidos','MD')].rolling(4).mean().diff()],axis=1,keys=['f','diff'])
fallRoll.plot(x=['f'],y=['diff'],kind='scatter')
# In[7]:
fallRoll=pd.concat([data[('Fallecidos','MD')].rolling(4).mean(),data[('Fallecidos','MD')].diff()],axis=1,keys=['f','diff'])
fallRoll.plot(x=['f'],y=['diff'],kind='scatter')
# In[8]:
fallRoll=pd.concat([data[('Fallecidos','MD')].rolling(4).median(),data[('Fallecidos','MD')].diff()],axis=1,keys=['f','diff'])
fallRoll.plot(x=['f'],y=['diff'],kind='scatter')
# In[9]:
fallRoll=pd.concat([data[('Fallecidos','MD')].rolling(4).median(),data[('Fallecidos','MD')].rolling(4).mean().diff()],axis=1,keys=['f','diff'])
fallRoll.plot(x=['f'],y=['diff'],kind='scatter')
# In[10]:
fallRoll=pd.concat([data[('Fallecidos','MD')].rolling(4).median(),data[('Fallecidos','MD')].rolling(4).median().diff()],axis=1,keys=['f','diff'])
fallRoll.plot(x=['f'],y=['diff'],kind='scatter')
# In[11]:
fallRoll=pd.concat([data[('Fallecidos','MD')],data[('Fallecidos','MD')].rolling(4).median().diff()],axis=1,keys=['f','diff'])
fallRoll.plot(x=['f'],y=['diff'],kind='scatter')
# In[12]:
fallRoll=pd.concat([data[('Fallecidos','MD')],data[('Fallecidos','MD')].rolling(4).mean().diff()],axis=1,keys=['f','diff'])
fallRoll.plot(x=['f'],y=['diff'],kind='scatter')
# In[13]:
fallRoll=pd.concat([data[('Fallecidos','MD')],data[('Fallecidos','MD')].diff()],axis=1,keys=['f','diff'])
fallRoll.plot(x=['f'],y=['diff'],kind='scatter')
# In[14]:
import numpy as np
for n in range(50,2,-1):
f=data['CASOS'].sum(axis=1).tail(n)
r=np.polyfit(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],2, full=True)
x=r[0]
print(n,x[0],(-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0]))
# In[17]:
import matplotlib.pyplot as plt
import numpy as np
# In[24]:
f=data['CASOS'].sum(axis=1)
a=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.')
d=f
x=np.polyfit(d[~np.isnan(d.diff())],d.diff()[~np.isnan(d.diff())],2)
print((-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0]))
p=np.poly1d(x)
plt.xlim(0,250000)
plt.ylim(0,10000)
xp=np.linspace(0, 300000, 300)
b=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.',xp,p(xp),'-')
d=f.tail(15)
x=np.polyfit(d[~np.isnan(d.diff())],d.diff()[~np.isnan(d.diff())],2)
print((-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0]))
p2=np.poly1d(x)
b=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.',xp,p(xp),'-',xp,p2(xp),'-')
# In[15]:
import numpy as np
for n in range(50,2,-1):
f=data['Fallecidos'].sum(axis=1).tail(n)
r=np.polyfit(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],2, full=True)
x=r[0]
print(n,x[0],(-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0]))
# In[16]:
for n in range(50,2,-1):
try:
fit=data['Fallecidos'].tail(n).apply(lambda x: np.polyfit(x[~np.isnan(x.diff())],x.diff()[~np.isnan(x.diff())],2))
r=fit.apply(lambda x: (-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0])).sum()
print(n,r)
except np.linalg.LinAlgError:
pass
# In[26]:
f=data['Fallecidos'].sum(axis=1)
a=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.')
d=f
x=np.polyfit(d[~np.isnan(d.diff())],d.diff()[~np.isnan(d.diff())],4)
p=np.poly1d(x)
print(p.roots)
x=np.polyfit(d[~np.isnan(d.diff())],d.diff()[~np.isnan(d.diff())],2)
print((-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0]))
p=np.poly1d(x)
print(p.roots)
plt.xlim(0,30000)
plt.ylim(0,1000)
xp=np.linspace(0, 300000, 300)
b=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.',xp,p(xp),'-')
d=f.tail(12)
x=np.polyfit(d[~np.isnan(d.diff())],d.diff()[~np.isnan(d.diff())],2)
print((-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0]))
p2=np.poly1d(x)
b=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.',xp,p(xp),'-',xp,p2(xp),'-')
# In[20]:
f0=data['CASOS']
for name in f0:
f=f0[name]
a=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.')
d=f
x=np.polyfit(d[~np.isnan(d.diff())],d.diff()[~np.isnan(d.diff())],2)
print(name,(-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0]))
p=np.poly1d(x)
plt.xlim(0,60000)
plt.ylim(0,3000)
xp=np.linspace(0, 300000, 300)
b=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.',xp,p(xp),'-')
# In[21]:
f0=data['Fallecidos']
for name in f0:
f=f0[name]
a=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.')
d=f
x=np.polyfit(d[~np.isnan(d.diff())],d.diff()[~np.isnan(d.diff())],2)
print(name,(-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0]))
p=np.poly1d(x)
plt.xlim(0,7000)
plt.ylim(0,400)
xp=np.linspace(0, 7000, 70)
b=plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.',xp,p(xp),'-')
# In[22]:
###%matplotlib notebook
import matplotlib.pyplot as plt
plt.ioff()
f0=data['Fallecidos']
for name in f0:
d=f0[name]
f=d
x=np.polyfit(d[~np.isnan(d.diff())],d.diff()[~np.isnan(d.diff())],2)
print(name,(-x[1]- np.sqrt(x[1]*x[1]-4*x[0]*x[2]))/(2*x[0]))
p=np.poly1d(x)
xp=np.linspace(0, int(d.max())*1.5, int(d.max()))
plt.plot(f[~np.isnan(f.diff())],f.diff()[~np.isnan(f.diff())],'.',xp,p(xp),'-')
plt.axis("auto")
plt.ylim(bottom=0)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment