Skip to content

ENH: pacf needs to limit nlag to ensure that it is well defined. #4663

Description

@elivil
plot_pacf(np.array([ -6.,   8.,   4.,  -2.,   4.,   1.,  -2.,  -3.,  -5.,   2.,   4.,
        12.,  24., -17.,  -3.,  -4.,  -8.,  -2.,   4.,  -1.,   3.,   5.,
         7.,  -6., -26.,  17.,  -4.,  -5.,   8.,   4., -10.,  -3.,  -1.,
        -2.,  -7.,  18.,  -6.,  14.,   6.,  -4.,  13.,  -9.,  11.,   2.,
         0.,  -3.,  10., -17.,  -4.,  -6.,   3.,  -3.,  -7.,  13.,  -7.,
        -5.,   0.,  -5.,  -5.,  -6.,  25., -16.,  -1.,   9., -12.,   7.,
         2.,   2.,  -1.,  -2.,  -4.,   4., -23.,  16.,  -1.,   4.,   6.,
        -1.,   6.,   0.,  -7.,  -4.,  10.,  -7.,   1.,   5.,  -3.,  -4.,
        -1.,   4.,   1.,  -4.,   9.,  -3.,  -4.,  37.,  -5.,   4.,   1.,
         2.,  -2.,  -4.,   1.,  -1.,   3.,  -1.,  -3., -27.,  21., -13.,
        -8.,  -3.,   3.,   3.,  -4.,  -2.,  -9.,  -7.,   2., -10., -21.,
        19.,   5.,  -2.,  -7.,  -2.,  -5.,  -7.,   6.,  13.,   3.,  10.,
        -2.,   1.,  -2.,   8.,  11.,   4.,   3.,   1., -10.,  -8.,  -2.,
        -5.,  -2.,  -1.,  -2.,  -4.,  -7.,  -2.,   3.,  11.,  13.,   9.,
         6.])) 

returns

.../python3.6/site-packages/statsmodels/regression/linear_model.py:1283: RuntimeWarning: invalid value encountered in sqrt
return rho, np.sqrt(sigmasq)

plot_acf(np.array(...)) - on the same data - works as expected.
Digging into the source code, it appears that statsmodels.tsa.stattools.pacf(np.array(...)) works for the default nlags=40 but results in the same error for nlags returned by _prepare_data_corr_plot(x, lags, zero) in statsmodels.graphics.tsaplots

Activity

  1. utkarshnyu commented on Jun 8, 2018

    @utkarshnyu

    @elivil Same Issue!

  2. utkarshnyu commented on Jun 8, 2018

    @utkarshnyu

    Specifying the lags will solve the problem!

    lags : int or array_like, optional
    int or Array of lag values, used on horizontal axis. Uses
    np.arange(lags) when lags is an int. If not provided,
    lags=np.arange(len(corr)) is used.

  3. varunxpatwardhan commented on Jun 19, 2018

    @varunxpatwardhan

    Same here!

  4. josef-pkt commented on Jun 19, 2018

    @josef-pkt
    Member

    I think we should change the default lags

    lags= len(x) - 2 works, but for pacf the lags towards the end are very noisy, and it's not so cheap to compute all pacf.

    maybe lags=max(len(x) // 3, 100) just as a rough idea, I have no idea or don't remember what other packages are doing.

  5. iptag commented on Jul 20, 2018

    @iptag

    after trying a lot of ways, finally i find the solution, you should point out the method='ywm' or 'ywmle' in function plot_pacf()

  6. added 5 commits that reference this issue on Aug 21, 2018
    4934db8
    75725cd
    2e9b4cd
    14e41f0
    30f2e86
  7. added 2 commits that reference this issue on Sep 5, 2018
    184d58e
    47e410f
  8. 7 remaining items

  9. bashtage commented on Jan 24, 2020

    @bashtage
    Member

    The issue here is that you are trying to some something that is impossible. You cannot compute partial ac's for lags larger than nobs // 2 (or perhaps (nobs-1)//2). The is because computing the ith partial autocorrelation requires inverting an (i-1 by i-1) matrix*.

    In practice, you shouldn't estimate autocorrelations or partial autocorrelations for lags larger than about sqrt(nobs).

    What is missing here is an error message telling users that this is impossible.

    * This isn't done in the code, but this matrix must be invertible for the ac/pac to be well defined.

  10. bashtage commented on Jan 24, 2020

    @bashtage
    Member

    That is happens at 42 is just a numerical issue when it becomes numerically 0, which then creates a nan, and the warning (note that partial acf value 42 is -1.19, which makes no sense either).

  11. changed the title [-]0.9.0 -> plot_pacf gives Runtime Warning and returns nonsensical plot[/-] [+]ENH: pacf needs to limit nlag to ensure that it is well defined.[/+] on Jan 24, 2020
  12. modified the milestones: 0.10, 0.12 on Jan 24, 2020
  13. LacerdaPT commented on Jan 24, 2020

    @LacerdaPT

    Ah, great!
    Thank you!

    Maybe apply that rule as the default value for nlags :)

    Again thanks a lot!
    Cheers

  14. added 4 commits that reference this issue on Jul 2, 2020
    19bbe06
    ac3203f
    f6c6a0c
    68867f9
  15. mtorabirad commented on Aug 11, 2021

    @mtorabirad

    @bashtage

    I am getting getting this error even with a reasonable number of lags. Here is my data

    [-1.8524766893333435, -1.9515665767885366, -0.3209117167880322, 2.407873267082323, 5.071849933288831, 7.5527565729844355, 9.649909066854889, 10.889130504394416, 10.655957794408096, 9.617804108838754,
    8.354302884650679, 7.515155214090555, 7.726621246754803, 9.458973121443705, 11.965949884841393, 13.346026708159116, 11.797553938258716, 7.40107773980526, 2.504561277182802, -1.180118720712926,
    -3.5878073378353292, -4.447022049447696, -4.200273047468727, -3.077997752609008, -2.1836852739888855, -1.1971292168964283, 0.06770300111929828, 1.28729314480006, 1.487806340406678, 1.248491641072178,
    1.003939328920751, 0.3577572457943461, -1.398676764942138, -4.047311222344433, -6.365069684718037, -7.03437374637906, -6.134351743114166, -2.9555568544165, 1.4295024196180748, 4.808764312949279,
    5.491303185835932, 2.9609092110404163, 0.3578579549157439, -1.752953437377073, -2.7943253220510087, -2.6014502078978694, -1.713765602948115, -0.39304393686506955, 0.45552949369994167]

    sm.graphics.tsa.plot_pacf(curr_data, lags=6) gives me the warning

    (i.e., lib/python3.8/site-packages/statsmodels/regression/linear_model.py:1434: RuntimeWarning: invalid value encountered in sqrt return rho, np.sqrt(sigmasq))

    and the attached figure

    Figure_2

    As you can see, some of the values are greater than one!

    More information: I noticed that "ywm" and "ols" methods give values that are all less than unity.

  16. bashtage commented on Aug 12, 2021

    @bashtage
    Member

    You should use method="ywm": plot_pacf(y,method="ywm") . This works well in master.

    image

  17. bashtage commented on Aug 12, 2021

    @bashtage
    Member

    This will also be the default after 0.13 is released.

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