You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: pacf needs to limit nlag to ensure that it is well defined. #4663
.../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
Reacted by Dmitry Murzin, Hongjoo Lee and Hiyam Ghannam
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.
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.
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).
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
Activity
utkarshnyu commentedon Jun 8, 2018
@elivil Same Issue!
utkarshnyu commentedon Jun 8, 2018
Specifying the
lagswill 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.varunxpatwardhan commentedon Jun 19, 2018
Same here!
josef-pkt commentedon Jun 19, 2018
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.iptag commentedon Jul 20, 2018
after trying a lot of ways, finally i find the solution, you should point out the method='ywm' or 'ywmle' in function plot_pacf()
BUG: Set default lags for acf/pacf plots
BUG: Set default lags for acf/pacf plots
BUG: Set default lags for acf/pacf plots
BUG: Set default lags for acf/pacf plots
BUG: Set default lags for acf/pacf plots
BUG: Set default lags for acf/pacf plots
BUG: Set default lags for acf/pacf plots
7 remaining items
bashtage commentedon Jan 24, 2020
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.
bashtage commentedon Jan 24, 2020
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).
[-]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.[/+]LacerdaPT commentedon Jan 24, 2020
Ah, great!
Thank you!
Maybe apply that rule as the default value for nlags :)
Again thanks a lot!
Cheers
BUG: Ensure PACF lag length is sensible
BUG: Ensure PACF lag length is sensible
BUG: Ensure PACF lag length is sensible
BUG: Ensure PACF lag length is sensible
mtorabirad commentedon Aug 11, 2021
@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
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.
bashtage commentedon Aug 12, 2021
You should use method="ywm":
plot_pacf(y,method="ywm"). This works well in master.bashtage commentedon Aug 12, 2021
This will also be the default after 0.13 is released.