def pacf_ols(x, nlags=40):
    'Calculate partial autocorrelations\n\n    Parameters\n    ----------\n    x : 1d array\n        observations of time series for which pacf is calculated\n    nlags : int\n        Number of lags for which pacf is returned.  Lag 0 is not returned.\n\n    Returns\n    -------\n    pacf : 1d array\n        partial autocorrelations, maxlag+1 elements\n\n    Notes\n    -----\n    This solves a separate OLS estimation for each desired lag.\n    '
    (xlags, x0) = lagmat(x, nlags, original='sep')
    xlags = add_constant(xlags)
    pacf = [1.0]
    for k in range(1, (nlags + 1)):
        res = OLS(x0[k:], xlags[k:, :(k + 1)]).fit()
        pacf.append(res.params[(- 1)])
    return np.array(pacf)
Calculate partial autocorrelations

Parameters
----------
x : 1d array
    observations of time series for which pacf is calculated
nlags : int
    Number of lags for which pacf is returned.  Lag 0 is not returned.

Returns
-------
pacf : 1d array
    partial autocorrelations, maxlag+1 elements

Notes
-----
This solves a separate OLS estimation for each desired lag.
def pacf_ols(x, nlags=40):
    'Calculate partial autocorrelations\n\n    Parameters\n    ----------\n    x : 1d array\n        observations of time series for which pacf is calculated\n    nlags : int\n        Number of lags for which pacf is returned.  Lag 0 is not returned.\n\n    Returns\n    -------\n    pacf : 1d array\n        partial autocorrelations, maxlag+1 elements\n\n    Notes\n    -----\n    This solves a separate OLS estimation for each desired lag.\n    '
    (xlags, x0) = lagmat(x, nlags, original='sep')
    xlags = add_constant(xlags)
    pacf = [1.0]
    for k in range(1, (nlags + 1)):
        res = OLS(x0[k:], xlags[k:, :(k + 1)]).fit()
        pacf.append(res.params[(- 1)])
    return np.array(pacf)