Skip to content

O(T^2) complexity of stattools.acf #4135

Closed

Description

@nchopin

Hi,
Computing the acf of a vector of length T=10^6 (with nlags=100) takes more than 10 minutes on my machine. In addition, it seems impossible to stop the execution before completion (using Ctrl-C).

Looking at the code, I think the problem comes from this line:
>> acov = (np.correlate(xo, xo, 'full') / d)[n - 1:]
in function acovf

Correct me if I'm wrong, but this seems to compute the ACF for all the lags 1, ..., T-1, then keeping only the (n-1) first. This means the complexity is O(T^2), whereas computing the ACF at lags 1 to L
should have complexity O(TL). In most practical cases L << T.

Looking at the doc, I can see now that with the fft option I can compute the ACF faster. However, I think
it's problematic that the default implementation would be so inefficient. The acf command in R does not have such an option, and manages to compute and plot the ACF of a vector of length T=10^6 in one second.

Thank you for attention
Nicolas

Activity

  1. bashtage commented on Nov 23, 2017

    @bashtage
    Member

    use fft=True

  2. josef-pkt commented on Nov 23, 2017

    @josef-pkt
    Member

    I think most examples that we had in mind for acf will not have T >> L. We use explicit loops over lags in some other cases where we expect L to be small.

    I didn't follow the related recent discussions closely and don't have an overview anymore:
    numpy PR to add maxlag option to correlate has stalled numpy/numpy#5978 scipy/scipy#4940 https://mail.python.org/pipermail/numpy-discussion/2016-May/075398.html

    In scipy.signal there was also the discussion to combine convolve and fft_convolve with auto option, but I didn't pay attention.

    Besides using an explicit loop, it is also possible to adjust the correlate options and padding to limit maxlag, https://github.com/NeuralEnsemble/elephant/blob/master/elephant/spike_train_correlation.py#L535 (link was provided in numpy PR)

  3. added this to the 0.10 milestone on Nov 23, 2017
  4. nchopin commented on Nov 23, 2017

    @nchopin
    Author

    Never underestimate the ability of users to use your software in crazy ways... :-)
    More seriously, I was trying to compute the ACF of MCMC chains
    https://en.wikipedia.org/wiki/Markov_chain_Monte_Carlo
    as reporting the ACF is standard practice in MCMC simulation. In MCMC it is typical to have T = 10^6 - 10^8, and a number of lags L << T. So other MCMC ppl might run into the same issue. (plus also maybe ppl dealing with very long time series like in e.g. finance.)

    I was unaware that this was related to some numpy issues: looping over l, as josef-pkt suggests seems quite reasonable to me. At any rate, thanks for looking into this.

  5. added 5 commits that reference this issue on Aug 18, 2018
    c15f51f
    251e23e
    40ea515
    79a8585
    17b387a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions