# Trends and cycles in unemployment¶

Here we consider three methods for separating a trend and cycle in economic data. Supposing we have a time series $$y_t$$, the basic idea is to decompose it into these two components:

$y_t = \mu_t + \eta_t$

where $$\mu_t$$ represents the trend or level and $$\eta_t$$ represents the cyclical component. In this case, we consider a stochastic trend, so that $$\mu_t$$ is a random variable and not a deterministic function of time. Two of methods fall under the heading of “unobserved components” models, and the third is the popular Hodrick-Prescott (HP) filter. Consistent with e.g. Harvey and Jaeger (1993), we find that these models all produce similar decompositions.

This notebook demonstrates applying these models to separate trend from cycle in the U.S. unemployment rate.

:

%matplotlib inline

:

import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

:

from pandas_datareader.data import DataReader
endog.index.freq = endog.index.inferred_freq


## Hodrick-Prescott (HP) filter¶

The first method is the Hodrick-Prescott filter, which can be applied to a data series in a very straightforward method. Here we specify the parameter $$\lambda=129600$$ because the unemployment rate is observed monthly.

:

hp_cycle, hp_trend = sm.tsa.filters.hpfilter(endog, lamb=129600)


## Unobserved components and ARIMA model (UC-ARIMA)¶

The next method is an unobserved components model, where the trend is modeled as a random walk and the cycle is modeled with an ARIMA model - in particular, here we use an AR(4) model. The process for the time series can be written as:

\begin{split}\begin{align} y_t & = \mu_t + \eta_t \\ \mu_{t+1} & = \mu_t + \epsilon_{t+1} \\ \phi(L) \eta_t & = \nu_t \end{align}\end{split}

where $$\phi(L)$$ is the AR(4) lag polynomial and $$\epsilon_t$$ and $$\nu_t$$ are white noise.

:

mod_ucarima = sm.tsa.UnobservedComponents(endog, 'rwalk', autoregressive=4)
# Here the powell method is used, since it achieves a
# higher loglikelihood than the default L-BFGS method
res_ucarima = mod_ucarima.fit(method='powell', disp=False)
print(res_ucarima.summary())

                        Unobserved Components Results
==============================================================================
Dep. Variable:                 UNRATE   No. Observations:                  832
Model:                    random walk   Log Likelihood                -459.064
+ AR(4)   AIC                            930.129
Date:                Wed, 17 May 2023   BIC                            958.464
Time:                        23:43:34   HQIC                           940.994
Sample:                    01-01-1954
- 04-01-2023
Covariance Type:                  opg
================================================================================
coef    std err          z      P>|z|      [0.025      0.975]
--------------------------------------------------------------------------------
sigma2.level     0.0772      0.166      0.466      0.641      -0.248       0.402
sigma2.ar        0.0978      0.168      0.581      0.561      -0.232       0.428
ar.L1            1.0620      0.111      9.570      0.000       0.844       1.279
ar.L2           -0.1765      0.317     -0.558      0.577      -0.797       0.444
ar.L3            0.0908      0.194      0.469      0.639      -0.289       0.470
ar.L4           -0.0219      0.081     -0.270      0.787      -0.180       0.137
===================================================================================
Ljung-Box (L1) (Q):                   0.00   Jarque-Bera (JB):           6237488.96
Prob(Q):                              0.95   Prob(JB):                         0.00
Heteroskedasticity (H):               9.31   Skew:                            17.19
Prob(H) (two-sided):                  0.00   Kurtosis:                       426.04
===================================================================================

Warnings:
 Covariance matrix calculated using the outer product of gradients (complex-step).


## Unobserved components with stochastic cycle (UC)¶

The final method is also an unobserved components model, but where the cycle is modeled explicitly.

\begin{split}\begin{align} y_t & = \mu_t + \eta_t \\ \mu_{t+1} & = \mu_t + \epsilon_{t+1} \\ \eta_{t+1} & = \eta_t \cos \lambda_\eta + \eta_t^* \sin \lambda_\eta + \tilde \omega_t \qquad & \tilde \omega_t \sim N(0, \sigma_{\tilde \omega}^2) \\ \eta_{t+1}^* & = -\eta_t \sin \lambda_\eta + \eta_t^* \cos \lambda_\eta + \tilde \omega_t^* & \tilde \omega_t^* \sim N(0, \sigma_{\tilde \omega}^2) \end{align}\end{split}
:

mod_uc = sm.tsa.UnobservedComponents(
endog, 'rwalk',
cycle=True, stochastic_cycle=True, damped_cycle=True,
)
# Here the powell method gets close to the optimum
res_uc = mod_uc.fit(method='powell', disp=False)
# but to get to the highest loglikelihood we do a
# second round using the L-BFGS method.
res_uc = mod_uc.fit(res_uc.params, disp=False)
print(res_uc.summary())

                            Unobserved Components Results
=====================================================================================
Dep. Variable:                        UNRATE   No. Observations:                  832
Model:                           random walk   Log Likelihood                -462.294
+ damped stochastic cycle   AIC                            932.587
Date:                       Wed, 17 May 2023   BIC                            951.468
Time:                               23:43:35   HQIC                           939.828
Sample:                           01-01-1954
- 04-01-2023
Covariance Type:                         opg
===================================================================================
coef    std err          z      P>|z|      [0.025      0.975]
-----------------------------------------------------------------------------------
sigma2.level        0.1403      0.022      6.392      0.000       0.097       0.183
sigma2.cycle        0.0280      0.021      1.366      0.172      -0.012       0.068
frequency.cycle     0.3490      0.199      1.757      0.079      -0.040       0.738
damping.cycle       0.7735      0.065     11.909      0.000       0.646       0.901
===================================================================================
Ljung-Box (L1) (Q):                   1.63   Jarque-Bera (JB):           6352841.10
Prob(Q):                              0.20   Prob(JB):                         0.00
Heteroskedasticity (H):               9.31   Skew:                            17.37
Prob(H) (two-sided):                  0.00   Kurtosis:                       430.45
===================================================================================

Warnings:
 Covariance matrix calculated using the outer product of gradients (complex-step).

/opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/site-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
warnings.warn("Maximum Likelihood optimization failed to "


## Graphical comparison¶

The output of each of these models is an estimate of the trend component $$\mu_t$$ and an estimate of the cyclical component $$\eta_t$$. Qualitatively the estimates of trend and cycle are very similar, although the trend component from the HP filter is somewhat more variable than those from the unobserved components models. This means that relatively mode of the movement in the unemployment rate is attributed to changes in the underlying trend rather than to temporary cyclical movements.

:

fig, axes = plt.subplots(2, figsize=(13,5));
axes.set(title='Level/trend component')
axes.plot(endog.index, res_uc.level.smoothed, label='UC')
axes.plot(endog.index, res_ucarima.level.smoothed, label='UC-ARIMA(2,0)')
axes.plot(hp_trend, label='HP Filter')
axes.legend(loc='upper left')
axes.grid()

axes.set(title='Cycle component')
axes.plot(endog.index, res_uc.cycle.smoothed, label='UC')
axes.plot(endog.index, res_ucarima.autoregressive.smoothed, label='UC-ARIMA(2,0)')
axes.plot(hp_cycle, label='HP Filter')
axes.legend(loc='upper left')
axes.grid()

fig.tight_layout(); Last update: May 17, 2023