def pacf_ols(x, nlags=40):
 (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)

def pacf_ols ( x , nlags = 40 ) : ( 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 .