def hpfilter(X, lamb=1600):
 _pandas_wrapper = _maybe_get_pandas_wrapper(X)
 X = np.asarray(X, float)
 if (X.ndim > 1):
  X = X.squeeze()
 nobs = len(X)
 I = sparse.eye(nobs, nobs)
 offsets = np.array([0, 1, 2])
 data = np.repeat([[1.0], [(-2.0)], [1.0]], nobs, axis=1)
 K = sparse.dia_matrix((data, offsets), shape=((nobs - 2), nobs))
 use_umfpack = True
 trend = spsolve((I + (lamb * K.T.dot(K))), X, use_umfpack=use_umfpack)
 cycle = (X - trend)
 if (_pandas_wrapper is not None):
  return (_pandas_wrapper(cycle), _pandas_wrapper(trend))
 return (cycle, trend)

def hpfilter ( X , lamb = 1600 ) : _pandas_wrapper = _maybe_get_pandas_wrapper ( X ) X = np . asarray ( X , float ) if ( X . ndim > 1 ) : X = X . squeeze ( ) nobs = len ( X ) I = sparse . eye ( nobs , nobs ) offsets = np . array ( [ 0 , 1 , 2 ] ) data = np . repeat ( [ [ 1.0 ] , [ ( - 2.0 ) ] , [ 1.0 ] ] , nobs , axis = 1 ) K = sparse . dia_matrix ( ( data , offsets ) , shape = ( ( nobs - 2 ) , nobs ) ) use_umfpack = True trend = spsolve ( ( I + ( lamb * K . T . dot ( K ) ) ) , X , use_umfpack = use_umfpack ) cycle = ( X - trend ) if ( _pandas_wrapper is not None ) : return ( _pandas_wrapper ( cycle ) , _pandas_wrapper ( trend ) ) return ( cycle , trend )
hodrick-prescott filter parameters x : array-like the 1d ndarray timeseries to filter of length or lamb : float the hodrick-prescott smoothing parameter .