Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Regressive (AR) model using Maximum Likelihood Estimator in pandas dataframe: correlate() got an unexpected keyword argument 'old behavior'

I have a subset of a pandas dataframe that contains a time series I want to analyze with an AR or ARIMA model using statsmodel:

data_sci = H_Clinton_social_vector.Florida

The data looks like this:

Date
    2015-09-28     587
    2015-10-05     582
    2015-10-12     606
    2015-10-19     698

My AR model is created like this, aggregating the time series on a weekly basis:

ar_model = sm.tsa.AR(data_sci, freq='W')
ar_model
<statsmodels.tsa.ar_model.AR at 0x1178f5490>

Next, I want to do a Maximum Likelihood Estimator (MLE) fit of the AR parameters, with a half year lag:

ar_res = ar_model.fit(maxlag=26, method='mle')

I get the following results:

correlate() got an unexpected keyword argument 'old behavior'

I don't understand what the problem is, which I assume is related to the auto correlation of the data, due the correlate() argument. There is high autocorrelation in my data so this should be ok.

autocorrelation of dataset

I am not very familiar with stasmodels, and prefer avoding coding an AR or ARIMA model from scratch.

like image 719
Luis Miguel Avatar asked Mar 13 '23 20:03

Luis Miguel


2 Answers

After some research, the problem was incompatibility of statsmodel with numpy 1.10. Although I had the latest release of stasmodel, there was an internal problem with autocorrelation (incompatibility with latest version of numpy), that required the install of the master code at Github.

First, I found out what versions of stasmodels' dependencies I had:

Python >= 2.6, including Python 3.x
NumPy >= 1.5.1
SciPy >= 0.9.0
Pandas >= 0.7.1
Patsy >= 0.3.0

All of them were OK, so in order to install from source, I needed to have Cython >= 20.1, which I downloaded from here. Uncompress, navigate to that directory and do:

python setup.py install

After that is done, navigate to the downloaded copy of statsmodel from Github, and build stasmodel:

python setup.py install

You'll see:

Cythonizing sources
Processing statsmodels/nonparametric/_smoothers_lowess.pyx
Processing statsmodels/nonparametric/linbin.pyx
Processing statsmodels/tsa/kalmanf/kalman_loglike.pyx
Processing statsmodels/tsa/statespace/_statespace.pyx.in

etc. After a while, you'll have the latest build of statsmodel. Now my AR model works fine, although with some warnings that you can ignore or disable.

like image 137
Luis Miguel Avatar answered Apr 09 '23 09:04

Luis Miguel


You need to downgrade numpy to version 1.9.2 or below, just tested and did not get this error for your code anymore. It is due to a call to np.correlate() in _presample_varcov where statsmodels.tsa.armodel calculates the inverse of the presample variance-covariance.

Numpy has deprecated the use of multiarray.correlate() (the old behavior) with 1.10 around June 2015 (see docs), but statsmodels has not been updated in this respect (0.6.1 dates from December 2014).

like image 38
Stefan Avatar answered Apr 09 '23 11:04

Stefan