PyTimeSeries

Test for pytimeseries package

Importamos la librería


In [18]:
import pytimeseries
import pandas
import matplotlib

Preparación de los datos

pytimeseries recibe una serie de pandas para analizar. A continuación se muestra la preparación de los datos para una serie que inicialmente está en csv.

Elegir una serie de tiempo


In [9]:
tserie = pandas.read_csv('champagne.csv', index_col='Month')
print(tserie)


         Perrin
Month          
1964-01    2815
1964-02    2672
1964-03    2755
1964-04    2721
1964-05    2946
1964-06    3036
1964-07    2282
1964-08    2212
1964-09    2922
1964-10    4301
1964-11    5764
1964-12    7312
1965-01    2541
1965-02    2475
1965-03    3031
1965-04    3266
1965-05    3776
1965-06    3230
1965-07    3028
1965-08    1759
1965-09    3595
1965-10    4474
1965-11    6838
1965-12    8357
1966-01    3113
1966-02    3006
1966-03    4047
1966-04    3523
1966-05    3937
1966-06    3986
...         ...
1970-04    3740
1970-05    2927
1970-06    3986
1970-07    4217
1970-08    1738
1970-09    5221
1970-10    6424
1970-11    9842
1970-12   13076
1971-01    3934
1971-02    3162
1971-03    4286
1971-04    4676
1971-05    5010
1971-06    4874
1971-07    4633
1971-08    1659
1971-09    5951
1971-10    6981
1971-11    9851
1971-12   12670
1972-01    4348
1972-02    3564
1972-03    4577
1972-04    4788
1972-05    4618
1972-06    5312
1972-07    4298
1972-08    1413
1972-09    5877

[105 rows x 1 columns]

Convertir en fechas la columna que indica los meses


In [11]:
rng = pandas.to_datetime(tserie.index)
print(rng)


DatetimeIndex(['1964-01-01', '1964-02-01', '1964-03-01', '1964-04-01',
               '1964-05-01', '1964-06-01', '1964-07-01', '1964-08-01',
               '1964-09-01', '1964-10-01',
               ...
               '1971-12-01', '1972-01-01', '1972-02-01', '1972-03-01',
               '1972-04-01', '1972-05-01', '1972-06-01', '1972-07-01',
               '1972-08-01', '1972-09-01'],
              dtype='datetime64[ns]', name='Month', length=105, freq=None)

Asignar las fechas a los datos


In [12]:
tserie.reindex(rng)


Out[12]:
Perrin
Month
1964-01-01 2815
1964-02-01 2672
1964-03-01 2755
1964-04-01 2721
1964-05-01 2946
1964-06-01 3036
1964-07-01 2282
1964-08-01 2212
1964-09-01 2922
1964-10-01 4301
1964-11-01 5764
1964-12-01 7312
1965-01-01 2541
1965-02-01 2475
1965-03-01 3031
1965-04-01 3266
1965-05-01 3776
1965-06-01 3230
1965-07-01 3028
1965-08-01 1759
1965-09-01 3595
1965-10-01 4474
1965-11-01 6838
1965-12-01 8357
1966-01-01 3113
1966-02-01 3006
1966-03-01 4047
1966-04-01 3523
1966-05-01 3937
1966-06-01 3986
... ...
1970-04-01 3740
1970-05-01 2927
1970-06-01 3986
1970-07-01 4217
1970-08-01 1738
1970-09-01 5221
1970-10-01 6424
1970-11-01 9842
1970-12-01 13076
1971-01-01 3934
1971-02-01 3162
1971-03-01 4286
1971-04-01 4676
1971-05-01 5010
1971-06-01 4874
1971-07-01 4633
1971-08-01 1659
1971-09-01 5951
1971-10-01 6981
1971-11-01 9851
1971-12-01 12670
1972-01-01 4348
1972-02-01 3564
1972-03-01 4577
1972-04-01 4788
1972-05-01 4618
1972-06-01 5312
1972-07-01 4298
1972-08-01 1413
1972-09-01 5877

105 rows × 1 columns

Inferir la frecuencia de los datos automáticamente


In [14]:
frq = pandas.infer_freq(tserie.index)
print(frq)


MS

Visualización de la serie

Análisis exploratorio de los datos

Gráfica de la serie


In [20]:
pytimeseries.series_viewer(tserie).time_plot()
matplotlib.pyplot.show()


ACF


In [21]:
pytimeseries.series_viewer(tserie).ACF_plot()
matplotlib.pyplot.show()


PACF


In [22]:
pytimeseries.series_viewer(tserie).PACF_plot()
matplotlib.pyplot.show()


QQ Plot


In [23]:
pytimeseries.series_viewer(tserie).qq_plot()
matplotlib.pyplot.show()


Histograma


In [24]:
pytimeseries.series_viewer(tserie).histogram()
matplotlib.pyplot.show()


Gráfica de densidad


In [25]:
pytimeseries.series_viewer(tserie).density_plot()
matplotlib.pyplot.show()


Test de normalidad


In [29]:
nt = pytimeseries.series_viewer(tserie).normality()
print(nt)


                         Jarque Bera normality test
---------------------  ----------------------------
Test statistics value                  73.4391
p value                                 1.11022e-16

Especificación de la serie

El modelo puede recibir una serie de tiempo transformada de la siguiente manera:

trans = Transformaciones directas de la serie:

  • log
  • log10
  • sqrt
  • cbrt
  • boxcox

trend = Eliminando la tendencia:

  • linear
  • cuadratic
  • cubic
  • diff1
  • diff2

seasonal = Estacionalidad (de acuerdo a la frecuencia):

  • poly2
  • diff
  • (dummy)

También la combinación de ellas


In [33]:
matplotlib.pyplot.plot(tserie.values)
pytimeseries.base_model(ts = tserie).specify(trans = 'log')
matplotlib.pyplot.show()


Estimación (Modelo AR)

Utilizando el modelo de statsmodels

X = base_model(self.ts).specify(trans = self.trans, trend = self.trend, seasonal = self.seasonal)

model = statsmodels.tsa.ar_model.AR(X.residuals)
model_fit = model.fit()
estimation = model_fit.predict()
X.estimation = estimation

X.restore(trans = self.trans, trend = self.trend, seasonal = self.seasonal)

super().set_residuals(X.residuals)

In [40]:
model = pytimeseries.AR_p(ts = tserie, trans='sqrt', trend = 'linear', seasonal = 'poly2')
result = model.estimate()


matplotlib.pyplot.plot(tserie.values)
matplotlib.pyplot.plot(result.X.original)
matplotlib.pyplot.plot(result.X.residuals)
matplotlib.pyplot.plot(result.X.estimation)
matplotlib.pyplot.show()


Utilizando el modelo de pytimeseries


In [ ]: