Repository navigation
Description
sm version 0.8.0
Hi everyone,
I am confused by the note on the pacf_ols method. It says:
#NOTE: this is incorrect.
And indeed, when I compare method="ols" to method="ywm", I get quite different results. IMHO, these should be the same (almost, might depend on whether bias is corrected or not).
My example (a non-stationary AR process):
ar_process = [1] + [3.5] + [8.25] + 12 * [0] # 15 data points, first 3 calculated by hand
for i in range(3, len(ar_process)):
ar_process[i] = \
2.5 * ar_process[i - 1] + \
-1.5 * ar_process[i - 2] + \
0.5 * ar_process[i - 3] + \
1print(ar_process)
# Out
[1, 3.5, 8.25, 16.875, 32.5625, 61.21875, 113.640625, 209.5546875, 385.03515625, 706.076171875, 1293.4150390625, 2367.94091796875, 4333.767822265625, 7930.2156982421875, 14509.857971191406]from statsmodels.tsa.stattools import pacf
pacf(ar_process, nlags=10, method="ols")
# Out
array([ 1. , 1.8294777 , -0.83245889, 0.5 , 0.27906977,
-0.02710134, -0.19004152, -0.20583946, -0.17431542, -0.13670252,
-0.10660277])
pacf(ar_process, nlags=10, method='ywm')
# Out
array([ 1. , 0.52702984, -0.025083 , -0.0310201 , -0.03713103,
-0.04344407, -0.04996822, -0.05666608, -0.06339867, -0.06981546,
-0.07513756])Btw, the last result coincides with the result in R, using the builtin stats.pacf function:
data = c(1, 3.5, 8.25, 16.875, 32.5625, 61.21875, 113.640625, 209.5546875, 385.03515625, 706.076171875, 1293.4150390625, 2367.94091796875, 4333.767822265625, 7930.2156982421875, 14509.857971191406)
res = pacf(data)
res
# Out
Partial autocorrelations of series ‘data’, by lag
1 2 3 4 5 6 7 8 9 10 11
0.527 -0.025 -0.031 -0.037 -0.043 -0.050 -0.057 -0.063 -0.070 -0.075 -0.078Afaics, R stats (see the details and Matlab do it the "OLS-way" by
The partial correlation coefficient is estimated by fitting autoregressive models of successively higher orders up to lag.max. [R stats]
There are also tests for the pacf_ols function, but with the comment # assert False
It seems like the developer knew that the result is wrong. I suggest to deactivate this option or to correct it (but I don't know how to do this right now).
Please correct me if I am mistaken.

Activity
josef-pkt commentedon Jul 28, 2017
AFAIR, the previous fix was how the constant is handled, and that we have unit tests against Stata.
0f963da
I don't think the non-stationary trend example satisfies the assumption for the acf, pacf function. Specifically, the mean is not constant. It make a large difference whether the mean includes all observations, or drops one or several initial observations.
The assumption in my initial implementation and what I always assumed is that the demeaned variable is zero mean stationary.
So I'm not sure there is a bug and not just implementation details about initial conditions that have a large effect in the "non-applicable" non-stationary case.
(I haven't looked at all the details of the question yet.)
The
assert Falsemight be left overs from a debugging session to trigger the python debugger.AFAICS, those test verify pacfols against matlab.
nikhase commentedon Jul 28, 2017
This is a simple stationary AR(1) process which should match your assumptions. I cross checked again with R, because I do not have access to Matlab. Good to know: The R builtin PACF function does no bias correction.
The process looks like:

Plot all results:
The results from
ywm,ldband R are aligned; OLS does not deliver the same results. IMHO, this should be the case.josef-pkt commentedon Jul 28, 2017
So, based on the plot they all agree except for the statsmodels ols version, is that right?
Can you try to reverse engineer and check the computation for, e.g., the 3rd pacf lag, comparing
(Note that your series still has a large influence of the initial observations on the estimate because of the initial trend)
nikhase commentedon Jul 28, 2017
Yes, that's right.
I'll have a look at your proposal.
Bugfix for pacf_ols (issue statsmodels#3839)
nikhase commentedon Aug 7, 2017
I proposed a PR (#3862) as a fix.
I evaluated the results on one wide-sense stationary process, and they look promising:
It is said in
[1] Box, G. E., Jenkins, G. M., Reinsel, G. C., & Ljung, G. M. (2015).
Time series analysis: forecasting and control.
John Wiley & Sons, p. 66
nikhase commentedon Aug 7, 2017
Note: the series is demeaned, and OLS is done without intercept
Bugfix for pacf_ols (issue statsmodels#3839)
ENH: Add additional options to pacf_ols
Bugfix for pacf_ols (issue statsmodels#3839)
ENH: Add additional options to pacf_ols
ENH: Add additional options to pacf_ols
Bugfix for pacf_ols (issue statsmodels#3839)
ENH: Add additional options to pacf_ols
Bugfix for pacf_ols (issue statsmodels#3839)
ENH: Add additional options to pacf_ols
Bugfix for pacf_ols (issue statsmodels#3839)
ENH: Add additional options to pacf_ols