Skip to content

Macroeconomics

Output gap

The output gap from a published potential output, or by the Hodrick-Prescott, Hamilton, Baxter-King and Christiano-Fitzgerald filters, with the trend, the cycle and the latest gap.

Model id
macro.output_gap, version 1
Domain
Macroeconomics
Evidence
standard
Cost class
light: A short numerical method; a hosted run may take up to 10 seconds.

Formula

gapt=ytyˉt−1 (ratio)orln⁡yt−ln⁡yˉt (log)\text{gap}_t = \frac{y_t}{\bar y_t} - 1 \ \text{(ratio)} \quad\text{or}\quad \ln y_t - \ln \bar y_t \ \text{(log)}τHP=arg⁡min⁡τ∑t(xt−τt)2+λ∑t(τt+1−2τt+τt−1)2\begin{aligned} \tau^{\mathrm{HP}} &= \arg\min_\tau \sum_t (x_t - \tau_t)^2 \\ &\quad + \lambda \sum_t (\tau_{t+1} - 2\tau_t + \tau_{t-1})^2 \end{aligned}λ=1600(f/4)4\lambda = 1600 (f/4)^4xt+h=β0+β1xt+⋯+βpxt−p+1+vt+hx_{t+h} = \beta_0 + \beta_1 x_t + \cdots + \beta_p x_{t-p+1} + v_{t+h}cyclet+h=v^t+h\text{cycle}_{t+h} = \hat v_{t+h}ctBK=∑j=−KKajxt−jc^{\mathrm{BK}}_t = \sum_{j=-K}^{K} a_j x_{t-j}aj=bj−12K+1∑i=−KKbia_j = b_j - \frac{1}{2K+1}\sum_{i=-K}^{K} b_ibj=sin⁡(jω2)−sin⁡(jω1)πjb_j = \frac{\sin(j\omega_2) - \sin(j\omega_1)}{\pi j}

Where

gapt\text{gap}_t
The output gap in period t gap
yty_t
Actual output actual
yˉt\bar y_t
Potential output potential
τHP\tau^{\mathrm{HP}}
The Hodrick-Prescott trend trend
xtx_t
The series being filtered series
λ\lambda
The smoothing parameter smoothing
ff
Observations in a year frequency
hh
Hamilton's horizon: how many periods ahead the series is projected horizon
pp
Number of lags in Hamilton's regression lags
cyclet+h\text{cycle}_{t+h}
The cycle: the part of the series the projection misses cycle
ctBKc^{\mathrm{BK}}_t
The Baxter-King cycle cycle
KK
Leads and lags the Baxter-King filter uses on each side lead_lag
aja_j
The filter's weight on the observation j periods away
bjb_j
The ideal band-pass filter's weight, before the weights are made to sum to zero
ω1\omega_1
The lower frequency of the band, from its longest period high
ω2\omega_2
The upper frequency of the band, from its shortest period low

Each symbol's field is named beside it; the inputs and outputs, with their units and bounds, are under Variables, and the citations give each formula's source.

Variables

Rates, returns and yields are decimals: 0.05 is five percent. Bounds in brackets include their end; in parentheses they exclude it.

Inputs

The gap from a published potential output (from_potential): 4 inputs
Inputs of the calculation from_potential
NameDescription
FormformtextHow the gap is stated: 'ratio' is output over trend less one; 'log' the difference of their logarithms
CalculationcalculationtextThe gap from a published potential output
Actualactualmoney (array) · length min 1, max 2000; each (0, 1e+15]Real output each period, positive
Potentialpotentialmoney (array) · length min 1, max 2000; each (0, 1e+15]Potential output each period, in the units of actual
The Hodrick-Prescott filter (hp): 5 inputs
Inputs of the calculation hp
NameDescription
FormformtextHow the gap is stated: 'ratio' is output over trend less one; 'log' the difference of their logarithms
Seriesseriesmoney (array) · length min 3, max 2000; each (0, 1e+15]Real output each period, positive; the filter runs on its log
FrequencyfrequencytextHow often the series is observed; it sets the filter's defaults
CalculationcalculationtextThe Hodrick-Prescott filter
Smoothingsmoothingratio · (0, 1e+08]Lambda, the penalty on the trend's curvature; left out, 1,600 for quarterly data, 6.25 annual and 129,600 monthly (Ravn and Uhlig)
Hamilton's (2018) regression filter (hamilton): 6 inputs
Inputs of the calculation hamilton
NameDescription
FormformtextHow the gap is stated: 'ratio' is output over trend less one; 'log' the difference of their logarithms
Seriesseriesmoney (array) · length min 3, max 2000; each (0, 1e+15]Real output each period, positive; the filter runs on its log
FrequencyfrequencytextHow often the series is observed; it sets the filter's defaults
CalculationcalculationtextHamilton's (2018) regression filter
Horizonhorizoncount · [1, 100]h, periods ahead the regression predicts; left out, two years
Lagslagscount · [1, 19]p, the most recent values it predicts from; left out, one year
The Baxter-King (1999) band-pass filter (baxter_king): 7 inputs
Inputs of the calculation baxter_king
NameDescription
FormformtextHow the gap is stated: 'ratio' is output over trend less one; 'log' the difference of their logarithms
Seriesseriesmoney (array) · length min 3, max 2000; each (0, 1e+15]Real output each period, positive; the filter runs on its log
FrequencyfrequencytextHow often the series is observed; it sets the filter's defaults
Lowlowperiods · [2, 1000]The shortest cycle kept, in periods; left out, 6 quarters (2 years, 18 months)
Highhighperiods · [2, 1000]The longest cycle kept, in periods; left out, 32 quarters (8 years, 96 months)
CalculationcalculationtextThe Baxter-King (1999) band-pass filter
Lead laglead_lagcount · [1, 1000]K, the leads and lags of the moving average; left out, 12 quarters (3 years, 36 months)
The Christiano-Fitzgerald (2003) band-pass filter (christiano_fitzgerald): 7 inputs
Inputs of the calculation christiano_fitzgerald
NameDescription
FormformtextHow the gap is stated: 'ratio' is output over trend less one; 'log' the difference of their logarithms
Seriesseriesmoney (array) · length min 3, max 2000; each (0, 1e+15]Real output each period, positive; the filter runs on its log
FrequencyfrequencytextHow often the series is observed; it sets the filter's defaults
Lowlowperiods · [2, 1000]The shortest cycle kept, in periods; left out, 6 quarters (2 years, 18 months)
Highhighperiods · [2, 1000]The longest cycle kept, in periods; left out, 32 quarters (8 years, 96 months)
CalculationcalculationtextThe Christiano-Fitzgerald (2003) band-pass filter
DriftdriftflagRemove the line from the first to the last observation before filtering (the random walk with drift)

Outputs

The gap from a published potential output (from_potential): 5 outputs
Outputs of the calculation from_potential
NameDescription
Trendtrendmoney (array) · length max 2000; each (0, 1e+18]The trend (potential output) in each period the cycle covers, in the series' units
Cyclecyclerate (array) · length max 2000; each [-100, 1e+06]The gap in each period, in the stated form
Gapgaprate · [-100, 1e+06]The gap in the last period the cycle covers
Startstartcount · [0, 2000]The position in the series (from 0) of the first period the trend and cycle cover
CalculationcalculationtextThe gap from a published potential output
The Hodrick-Prescott filter (hp): 6 outputs
Outputs of the calculation hp
NameDescription
Trendtrendmoney (array) · length max 2000; each (0, 1e+18]The trend (potential output) in each period the cycle covers, in the series' units
Cyclecyclerate (array) · length max 2000; each [-100, 1e+06]The gap in each period, in the stated form
Gapgaprate · [-100, 1e+06]The gap in the last period the cycle covers
Startstartcount · [0, 2000]The position in the series (from 0) of the first period the trend and cycle cover
CalculationcalculationtextThe Hodrick-Prescott filter
Smoothingsmoothingratio · (0, 1e+08]The lambda the filter used
Hamilton's (2018) regression filter (hamilton): 7 outputs
Outputs of the calculation hamilton
NameDescription
Trendtrendmoney (array) · length max 2000; each (0, 1e+18]The trend (potential output) in each period the cycle covers, in the series' units
Cyclecyclerate (array) · length max 2000; each [-100, 1e+06]The gap in each period, in the stated form
Gapgaprate · [-100, 1e+06]The gap in the last period the cycle covers
Startstartcount · [0, 2000]The position in the series (from 0) of the first period the trend and cycle cover
CalculationcalculationtextHamilton's (2018) regression filter
Horizonhorizoncount · [1, 100]The h it used
Lagslagscount · [1, 19]The p it used
The Baxter-King (1999) band-pass filter (baxter_king): 8 outputs
Outputs of the calculation baxter_king
NameDescription
Trendtrendmoney (array) · length max 2000; each (0, 1e+18]The trend (potential output) in each period the cycle covers, in the series' units
Cyclecyclerate (array) · length max 2000; each [-100, 1e+06]The gap in each period, in the stated form
Gapgaprate · [-100, 1e+06]The gap in the last period the cycle covers
Startstartcount · [0, 2000]The position in the series (from 0) of the first period the trend and cycle cover
CalculationcalculationtextThe Baxter-King (1999) band-pass filter
Lowlowperiods · [2, 1000]The band's low end
Highhighperiods · [2, 1000]The band's high end
Lead laglead_lagcount · [1, 1000]The K it used
The Christiano-Fitzgerald (2003) band-pass filter (christiano_fitzgerald): 7 outputs
Outputs of the calculation christiano_fitzgerald
NameDescription
Trendtrendmoney (array) · length max 2000; each (0, 1e+18]The trend (potential output) in each period the cycle covers, in the series' units
Cyclecyclerate (array) · length max 2000; each [-100, 1e+06]The gap in each period, in the stated form
Gapgaprate · [-100, 1e+06]The gap in the last period the cycle covers
Startstartcount · [0, 2000]The position in the series (from 0) of the first period the trend and cycle cover
CalculationcalculationtextThe Christiano-Fitzgerald (2003) band-pass filter
Lowlowperiods · [2, 1000]The band's low end
Highhighperiods · [2, 1000]The band's high end

Calculator

Run the model on your own inputs, starting from its example. Rates are decimals: 0.05 is five percent. The run goes to the pyeconomics API on this site: your values travel only in the request, and nothing is stored.

Loading the calculator…

Worked example

Chain the gap into a policy rule: `gap = pyeconomics.run(pyeconomics.bind("macro.output_gap", calculation="hp", frequency="quarterly")).outputs.gap`, then `pyeconomics.run(pyeconomics.bind("macro.policy_rule", profile="us_output_gap", calculation="taylor_1993", output_gap=gap))`. The manifest records output_gap as user-supplied beside the rule's bound data.

Example hp_quarterly. Inputs:

{
  "calculation": "hp",
  "form": "ratio",
  "frequency": "quarterly",
  "series": [
    19000, 19150, 19310, 19440,
    19590, 19700, 19760, 19650,
    19420, 19350, 19480, 19660,
    19850, 20030, 20190, 20340,
    20480, 20610, 20760, 20900,
    21050, 21180, 21330, 21470
  ],
  "smoothing": null
}

Outputs:

{
  "calculation": "hp",
  "cycle": [
    -0.0017360038963747245,
    0.002013752512396258,
    0.006238074304680906,
    0.008854879360317564,
    0.012463731293158089,
    0.013954215053535484,
    0.012821940964063739,
    0.002948054742175226,
    -0.013048566919718854,
    -0.020952853091736424,
    -0.01888230548573976,
    -0.014509587544549337,
    -0.009869812524568306,
    -0.005958021877103549,
    -0.0032487826408688354,
    -0.0012195974995857408,
    0.00015582227395168833,
    0.0009049778338017336,
    0.0025043003936256734,
    0.0035159364884555155,
    0.004920522767925986,
    0.005301341122001761,
    0.006574932608538533,
    0.007326518837910103
  ],
  "gap": 0.007326518837910103,
  "smoothing": 1600,
  "start": 0,
  "trend": [
    19033.04143408943,
    19111.51414038409,
    19190.28954787204,
    19269.372035278535,
    19348.84124192666,
    19428.88515825131,
    19509.84590755536,
    19592.24100100713,
    19676.753433945643,
    19764.114589481662,
    19854.906408190207,
    19949.458413314318,
    20047.868705641857,
    20150.054465329264,
    20255.80671322672,
    20364.8369041678,
    20476.809257018303,
    20591.36527086214,
    20708.1405953558,
    20826.774384006447,
    20946.930153262718,
    21068.309703398314,
    21190.672754708205,
    21313.843722459118
  ]
}

Assumptions

  • The filters run on the natural log of output; the trend is reported in the series' units and the gap as a ratio (CBO's definition, the default) or a log difference.
  • Defaults follow the papers and scale with the frequency: lambda 1,600 quarterly (Ravn and Uhlig: 6.25 annual, 129,600 monthly); Hamilton's h of two years and p of one; a band of 1.5 to 8 years with K of three years (two to eight years for annual data). Each can be overridden.
  • The Christiano-Fitzgerald filter is the asymmetric random-walk form, on the series less the line from its first to its last value (as statsmodels' cffilter computes it).
  • Profiles: default binds 120 quarters of US real GDP and CBO's real potential GDP; ca, de, fr, gb, it and jp bind 40 years of the Penn World Table's real GDP (rgdpna) for a filter, with frequency='annual'.

Limitations

  • Potential output is not observed: the filters' trends are statistical and revised as data arrive, and the HP and Christiano-Fitzgerald trends are least reliable at the ends of the sample (Hamilton 2018).
  • Hamilton's cycle starts h + p - 1 periods into the series, and Baxter-King's starts K periods in and ends K periods early: its gap is the one K periods before the last observation, and it warns.
  • A series whose recent values are collinear (an exactly log-linear one) has no Hamilton regression, and a series that moves by factors no output series does leaves the trend or gap bounds: both raise DomainError.
  • Chain into macro.policy_rule by passing the gap as its output_gap override; the manifest records it as user-supplied, apart from a published gap.
  • In hosted mode, where FRED is refused, the originators serve too short a history for the default windows (BLS about three years, CBO's potential and natural rate from 2021), so the bind raises: supply the series instead.

Evidence

Status: standard. Textbook consensus, such as bond duration or the CAPM formula.

Every model declares one status: standard (textbook consensus), practitioner (a widely used convention), contested (under active debate), rejected (failed out of sample) or historical (a superseded methodology).

Syllabus

The 2027 CFA® Program's topic areas this model relates to, as factual tags. Each links to CFA Institute's page for its level; pyeconomics holds none of the curriculum's text.

Data

This model can read its data for you: each bound field names a data series, and the sources listed can fill it, in the order they are tried. This website reads only the sources whose terms let it serve their data (marked "hosted"); every source is available when you run pyeconomics on your own computer.

Profile: Default
FieldConceptRead asSources (licence class; hosted)
actualReal GDPus.output.real_gdpthe last 120 observations on or before the date; real time: as published by the date
  • US Bureau of Economic Analysis (hosted-safe; hosted)
  • FRED and ALFRED, Federal Reserve Bank of St. Louis (library-only; not hosted)
potentialReal potential GDPus.output.potential_gdpthe last 120 observations on or before the date; real time: as published by the date
  • Congressional Budget Office (hosted-safe; hosted)
  • FRED and ALFRED, Federal Reserve Bank of St. Louis (library-only; not hosted)
seriesReal GDPus.output.real_gdpthe last 120 observations on or before the date; real time: as published by the date
  • US Bureau of Economic Analysis (hosted-safe; hosted)
  • FRED and ALFRED, Federal Reserve Bank of St. Louis (library-only; not hosted)
Profile: Canada (ca)
FieldConceptRead asSources (licence class; hosted)
seriesPWT real GDP, Canadapwt.rgdpna.ca.annualthe last 40 observations on or before the date; real time: as published by the date
  • Penn World Table (hosted-safe; hosted)
Profile: Germany (de)
FieldConceptRead asSources (licence class; hosted)
seriesPWT real GDP, Germanypwt.rgdpna.de.annualthe last 40 observations on or before the date; real time: as published by the date
  • Penn World Table (hosted-safe; hosted)
Profile: France (fr)
FieldConceptRead asSources (licence class; hosted)
seriesPWT real GDP, Francepwt.rgdpna.fr.annualthe last 40 observations on or before the date; real time: as published by the date
  • Penn World Table (hosted-safe; hosted)
Profile: United Kingdom (gb)
FieldConceptRead asSources (licence class; hosted)
seriesPWT real GDP, the United Kingdompwt.rgdpna.gb.annualthe last 40 observations on or before the date; real time: as published by the date
  • Penn World Table (hosted-safe; hosted)
Profile: Italy (it)
FieldConceptRead asSources (licence class; hosted)
seriesPWT real GDP, Italypwt.rgdpna.it.annualthe last 40 observations on or before the date; real time: as published by the date
  • Penn World Table (hosted-safe; hosted)
Profile: Japan (jp)
FieldConceptRead asSources (licence class; hosted)
seriesPWT real GDP, Japanpwt.rgdpna.jp.annualthe last 40 observations on or before the date; real time: as published by the date
  • Penn World Table (hosted-safe; hosted)

Citations

  1. Hodrick, R. J. and Prescott, E. C. (1997). Postwar U.S. Business Cycles: An Empirical Investigation. Journal of Money, Credit and Banking, 29(1), 1-16. doi:10.2307/2953682 (Section 2: the trend that minimizes the penalized sum of squares)
  2. Ravn, M. O. and Uhlig, H. (2002). On Adjusting the Hodrick-Prescott Filter for the Frequency of Observations. Review of Economics and Statistics, 84(2), 371-376. doi:10.1162/003465302317411604 (Lambda scales with the fourth power of the observation frequency)
  3. Hamilton, J. D. (2018). Why You Should Never Use the Hodrick-Prescott Filter. Review of Economics and Statistics, 100(5), 831-843. doi:10.1162/rest_a_00706 (Section 4: the regression of y(t+h) on y(t), ..., y(t-p+1))
  4. Baxter, M. and King, R. G. (1999). Measuring Business Cycles: Approximate Band-Pass Filters for Economic Time Series. Review of Economics and Statistics, 81(4), 575-593. doi:10.1162/003465399558454 (The truncated ideal filter with weights summing to zero; K = 12)
  5. Christiano, L. J. and Fitzgerald, T. J. (2003). The Band Pass Filter. International Economic Review, 44(2), 435-465. doi:10.1111/1468-2354.t01-1-00076 (The random-walk filter with end weights that sum the rest to zero)
  6. Congressional Budget Office. Budget and Economic Data: Potential GDP and Underlying Inputs. https://www.cbo.gov/data/budget-economic-data (Quarterly real GDP, real potential GDP and the output gap)
  7. Seabold, S. and Perktold, J. (2010). statsmodels: Econometric and statistical modeling with Python. Proceedings of the 9th Python in Science Conference, 92-96. doi:10.25080/Majora-92bf1922-011 (statsmodels.tsa.filters: hpfilter, bkfilter and cffilter (oracles))

Changelog

  • Version 1: First version.

Use it elsewhere

Documentation
The model's reference page
REST API
GET or POST /v1/models/macro.output_gap/run
MCP tool
run_model with the model's id, macro.output_gap. Connect an assistant
Command line
pyeconomics models run macro.output_gap --help

The worked example, from Python and from the command line:

Python
import pyeconomics

inputs = {
    "calculation": "hp",
    "form": "ratio",
    "frequency": "quarterly",
    "series": [
        19000, 19150, 19310, 19440,
        19590, 19700, 19760, 19650,
        19420, 19350, 19480, 19660,
        19850, 20030, 20190, 20340,
        20480, 20610, 20760, 20900,
        21050, 21180, 21330, 21470,
    ],
    "smoothing": None,
}
result = pyeconomics.run("macro.output_gap", inputs)
print(result.to_json())
Command line
pyeconomics models run macro.output_gap --inputs - <<'JSON'
{
  "calculation": "hp",
  "form": "ratio",
  "frequency": "quarterly",
  "series": [
    19000, 19150, 19310, 19440,
    19590, 19700, 19760, 19650,
    19420, 19350, 19480, 19660,
    19850, 20030, 20190, 20340,
    20480, 20610, 20760, 20900,
    21050, 21180, 21330, 21470
  ],
  "smoothing": null
}
JSON