Skip to content

Note on pacf_ols: this is incorrect #3839

Closed

Description

@nikhase

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] + \
    1
print(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])

a plot of the pacf results.
pacf_ols_ywm

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.078

Afaics, 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

  1. josef-pkt commented on Jul 28, 2017

    @josef-pkt
    Member

    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 False might be left overs from a debugging session to trigger the python debugger.
    AFAICS, those test verify pacfols against matlab.

  2. nikhase commented on Jul 28, 2017

    @nikhase
    Author

    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 AR(1) process, according to https://en.wikipedia.org/wiki/Autoregressive_model#Definition
    # X[t] = c + phi * X[t-1] + epsilon_t
    np.random.seed(0)
    T = 15
    c = 2
    epsilon = np.random.randn(T)
    ar_process = np.repeat(1.0, T)
    phi = 0.85
    for t in range(T-1):
        ar_process[t+1] = phi*ar_process[t] + c  + epsilon[t]
    
    ar_process = pd.Series(ar_process)
    
    # Results from r builtin "pacf" (copy&paste the time series)
    pacf_coeffs_R =  [1, 0.694,-0.055,-0.133,-0.103,0.008,0.073,-0.151,-0.055,-0.082,-0.154,-0.081,-0.090,0.052,0.039]
    
    plt.scatter(range(len(ar_process)), ar_process)
    plt.plot(ar_process)
    plt.show()

    The process looks like:
    simple_ar_process

    Plot all results:

    # It seems like R does no bias correction.
    # To compare with statsmodels, use yw and ld without bias correction
    maxlag=len(ar_process)-1
    pacf_coeffs_ols = pacf(ar_process, method='ols', nlags=maxlag)
    pacf_coeffs_yw = pacf(ar_process, method='ywm', nlags=maxlag)
    pacf_coeffs_ld = pacf(ar_process, method='ldb', nlags=maxlag)
    plt.plot(pacf_coeffs_ols, 'p-')
    plt.plot(pacf_coeffs_yw, 'o-')
    plt.plot(pacf_coeffs_ld, '-')
    plt.plot(pacf_coeffs_R, '-')
    plt.legend(['ols', 'yw', 'ld', 'R builtin pacf'])
    plt.show()

    pacf_yw_ld_ols_vs_r

    The results from ywm, ldb and R are aligned; OLS does not deliver the same results. IMHO, this should be the case.

  3. josef-pkt commented on Jul 28, 2017

    @josef-pkt
    Member

    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

    • ols with demeaning of full series but without a constant in the regression
    • our current pacf_ols, which uses constant in the regression, which will differ by lag.

    (Note that your series still has a large influence of the initial observations on the estimate because of the initial trend)

  4. nikhase commented on Jul 28, 2017

    @nikhase
    Author

    So, based on the plot they all agree except for the statsmodels ols version, is that right?

    Yes, that's right.
    I'll have a look at your proposal.

  5. nikhase commented on Aug 7, 2017

    @nikhase
    Author

    I proposed a PR (#3862) as a fix.
    I evaluated the results on one wide-sense stationary process, and they look promising:

    • The process:
    # The AR(1) process, according to https://en.wikipedia.org/wiki/Autoregressive_model#Definition
    # X[t] = c + phi * X[t-1] + epsilon_t
    def return_ar_process(T=25):
        np.random.seed(0)
        c = 2
        epsilon = np.random.randn(T)
        ar_process = np.repeat(1.0, T)
        phi = 0.85
        for t in range(T-1):
            ar_process[t+1] = phi*ar_process[t] + c  + epsilon[t]
    
        ar_process = pd.Series(ar_process)
        
        return ar_process
    • Results
      sm_pacf_ols_len_25
      sm_pacf_ols_len_100
      sm_pacf_ols_len_250
      sm_pacf_ols_len_1000

    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

    The partial autocorrelations may be estimated by fitting successively autoregressive models of orders 1, 2, 3, ... by least squares and picking out the estimates $\phi_{11}$, $\phi_{22}$, $\phi_{33}$ ... of the last coefficient fitted at each stage.

  6. nikhase commented on Aug 7, 2017

    @nikhase
    Author

    Note: the series is demeaned, and OLS is done without intercept

  7. added this to the 0.10 milestone on Oct 7, 2017
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