Unobserved Components

Decompose every series into a stochastic trend, a cycle and noise with an unobserved components model: where the series is heading, how far it is above or below that path in its cycle, and how certain the trend is.

The structural time series model of Harvey (1989) writes the series as the sum of components that are not observed, each a simple stochastic process, and estimates them with the Kalman filter and smoother. Unlike the Hodrick-Prescott filter, the smoothness of the trend and the length of the cycle are estimated from the data, and the trend comes with a standard error, so a band of ±1.96 standard errors shows how uncertain it is. A business cycle of GDP or industrial production, or a long swing in an asset price, shows up as the cycle.

The formula is as follows:

\[y_{t} = \mu_{t} + \psi_{t} + \epsilon_{t}\] \[\mu_{t+1} = \mu_{t} + \eta_{t}\] \[\mu_{t+1} = \mu_{t} + \beta_{t} + \eta_{t}\] \[\mu_{t+1} = \mu_{t} + \beta_{t}\] \[\beta_{t+1} = \beta_{t} + \zeta_{t}\] \[\psi_{t+1} = \rho \cdot \left(\cos\left(\lambda\right) \cdot \psi_{t} + \sin\left(\lambda\right) \cdot \psi_{\text{star},t}\right) + \kappa_{t}\] \[\psi_{\text{star},t+1} = \rho \cdot \left(-\sin\left(\lambda\right) \cdot \psi_{t} + \cos\left(\lambda\right) \cdot \psi_{\text{star},t}\right) + \kappa_{\text{star},t}\] \[\text{Cycle Period} = \frac{2 \cdot \pi}{\lambda}\] \[\text{Irregular}_{t} = y_{t} - \mu_{t} - \psi_{t}\]

with \(y_{t}\) the column series (Adj Close by default), \(\mu_{t}\) the trend, \(\psi_{t}\) the cycle and \(\epsilon_{t}\) the irregular noise, normal with mean 0. The trend is the local level (mu(t+1) = \(\mu_{t} + \eta_{t}\)), the local linear trend (mu(t+1) = \(\mu_{t} + \beta_{t} + \eta_{t}\) with the slope \(\beta_{t}\), a level and a slope that drift) or the smooth trend (mu(t+1) = \(\mu_{t} + \beta_{t}\) with the same slope, an integrated random walk). The cycle is damped (\(0 < \rho < 1\)) and stochastic. The variances, rho and lambda are fitted by Maximum Likelihood with the Kalman filter, and the components are the smoothed estimates given the whole sample; the Trend Standard Error is the square root of the smoothed variance of \(\mu_{t}\).

Also known as: UCM, structural time series model, local level model, local linear trend, trend-cycle decomposition, output gap.

Example · 1990 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_unobserved_components:

import numpy as np

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="1990-01-01", end_date="2025-12-31")

production = np.log(economics.get_industrial_production_index())

econometrics = Econometrics(data=production)

econometrics.get_unobserved_components(period="quarterly")["United States"]

Which returns:

Using Your Own Data

get_unobserved_components runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2000-01-01")

unemployment = economics.get_unemployment_rate(
    countries=["United States", "Germany"], period="monthly"
)

econometrics = Econometrics(data=unemployment, changes="difference")

econometrics.get_unobserved_components()

Parameters

get_unobserved_components accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
columnstrThe historical data column to decompose. Defaults to “Adj Close”."Return"
trendstrThe trend. Options are: “local_level” (local level, a random walk), “local_linear_trend” (local linear trend), “smooth_trend” (smooth trend, an integrated random walk). Defaults to “local_linear_trend”."downtrend"
cycleboolWhether the model has a cycle. Defaults to True.True
cycle_period_boundslist[float] | NoneThe shortest and longest period of the cycle, in periods. Defaults to None, which takes the business cycle of 1.5 to 12 years for monthly, quarterly and yearly observations (by the spacing of the dates), and 2 periods without an upper bound for daily and weekly ones.
include_benchmarkboolWhether to include “Benchmark” among the assets. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None.2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Harvey, A.C. (1989). “Forecasting, Structural Time Series Models and the Kalman Filter.” Cambridge University Press.
  • Paper Harvey, A.C. and Jaeger, A. (1993). “Detrending, Stylized Facts and the Business Cycle.” Journal of Applied Econometrics, 8(3), 231-247.
  • Library Shares similar logic as statsmodels’ UnobservedComponents
Share