Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1 

2import numpy as np 

3from scipy import sparse 

4from scipy.sparse.linalg import spsolve 

5from statsmodels.tools.validation import array_like, PandasWrapper 

6 

7 

8def hpfilter(x, lamb=1600): 

9 """ 

10 Hodrick-Prescott filter. 

11 

12 Parameters 

13 ---------- 

14 x : array_like 

15 The time series to filter, 1-d. 

16 lamb : float 

17 The Hodrick-Prescott smoothing parameter. A value of 1600 is 

18 suggested for quarterly data. Ravn and Uhlig suggest using a value 

19 of 6.25 (1600/4**4) for annual data and 129600 (1600*3**4) for monthly 

20 data. 

21 

22 Returns 

23 ------- 

24 cycle : ndarray 

25 The estimated cycle in the data given lamb. 

26 trend : ndarray 

27 The estimated trend in the data given lamb. 

28 

29 See Also 

30 -------- 

31 statsmodels.tsa.filters.bk_filter.bkfilter 

32 Baxter-King filter. 

33 statsmodels.tsa.filters.cf_filter.cffilter 

34 The Christiano Fitzgerald asymmetric, random walk filter. 

35 statsmodels.tsa.seasonal.seasonal_decompose 

36 Decompose a time series using moving averages. 

37 statsmodels.tsa.seasonal.STL 

38 Season-Trend decomposition using LOESS. 

39 

40 Notes 

41 ----- 

42 The HP filter removes a smooth trend, `T`, from the data `x`. by solving 

43 

44 min sum((x[t] - T[t])**2 + lamb*((T[t+1] - T[t]) - (T[t] - T[t-1]))**2) 

45 T t 

46 

47 Here we implemented the HP filter as a ridge-regression rule using 

48 scipy.sparse. In this sense, the solution can be written as 

49 

50 T = inv(I + lamb*K'K)x 

51 

52 where I is a nobs x nobs identity matrix, and K is a (nobs-2) x nobs matrix 

53 such that 

54 

55 K[i,j] = 1 if i == j or i == j + 2 

56 K[i,j] = -2 if i == j + 1 

57 K[i,j] = 0 otherwise 

58 

59 References 

60 ---------- 

61 Hodrick, R.J, and E. C. Prescott. 1980. "Postwar U.S. Business Cycles: An 

62 Empirical Investigation." `Carnegie Mellon University discussion 

63 paper no. 451`. 

64 Ravn, M.O and H. Uhlig. 2002. "Notes On Adjusted the Hodrick-Prescott 

65 Filter for the Frequency of Observations." `The Review of Economics and 

66 Statistics`, 84(2), 371-80. 

67 

68 Examples 

69 -------- 

70 >>> import statsmodels.api as sm 

71 >>> import pandas as pd 

72 >>> dta = sm.datasets.macrodata.load_pandas().data 

73 >>> index = pd.DatetimeIndex(start='1959Q1', end='2009Q4', freq='Q') 

74 >>> dta.set_index(index, inplace=True) 

75 

76 >>> cycle, trend = sm.tsa.filters.hpfilter(dta.realgdp, 1600) 

77 >>> gdp_decomp = dta[['realgdp']] 

78 >>> gdp_decomp["cycle"] = cycle 

79 >>> gdp_decomp["trend"] = trend 

80 

81 >>> import matplotlib.pyplot as plt 

82 >>> fig, ax = plt.subplots() 

83 >>> gdp_decomp[["realgdp", "trend"]]["2000-03-31":].plot(ax=ax, 

84 ... fontsize=16) 

85 >>> plt.show() 

86 

87 .. plot:: plots/hpf_plot.py 

88 """ 

89 pw = PandasWrapper(x) 

90 x = array_like(x, 'x', ndim=1) 

91 nobs = len(x) 

92 I = sparse.eye(nobs, nobs) # noqa:E741 

93 offsets = np.array([0, 1, 2]) 

94 data = np.repeat([[1.], [-2.], [1.]], nobs, axis=1) 

95 K = sparse.dia_matrix((data, offsets), shape=(nobs - 2, nobs)) 

96 

97 use_umfpack = True 

98 trend = spsolve(I+lamb*K.T.dot(K), x, use_umfpack=use_umfpack) 

99 

100 cycle = x - trend 

101 return pw.wrap(cycle, append='cycle'), pw.wrap(trend, append='trend')