Detecting Recessions in Germany’s GDP data with a Hidden Markov Model

Reading Time: 3 minutes

There are several ways to define a recession. Julius Shishkin suggested in 1974 that two consecutive quarters of negative GDP (gross domestic product) growth indicate a ongoing recession. Since then, that has been the standard definition. In this article, we will present a Hidden-Markov-Model (HMM) which will be used to predict recession probabilities based on German quarterly GDP data (source: OECD.)

A Hidden-Markov-Model is used to detect hidden states in the underlying data. In this example we will estimate a HMM with two states (0: No Recession, 1: Recession)

The above image shows the time evolution of a hidden markov model. x(t-1), x(t) and x(t+1) describe the hidden-states at times t, t-1 and t+1 while y(t-1), y(t) and y(t+1) are the observations we make (in this short example, y(t) denotes the quarterly GDP data at time t).

As usual for a Markov Model, the state x(t) depends only on the previous state x(t-1) and the transition matrix M. In the following, two more general assumption will be made. There are two time-discrete random processes {X_t} and {Y_t}, of which only the latter is observable. Through it, conclusions are to be drawn about the course of the first process.

First Markov characteristic:

The current value of the first process depends only on its last value

    \[P(X_t = x_t | X_1 = x_1, … X_{t-1} = x_{t-1}; Y_1 = y_1, …, Y_{t-1} = y_{t-1}) = P(X_t = x_t | X_{t-1} = x_{t-1})\]

Second Markov characteristic:

The current value of the second process depends only on its last value

    \[P(Y_t = y_t | X_1 = x_1, … X_{t-1} = x_{t-1}; Y_1 = y_1, …, Y_{t-1} = y_{t-1}) = P(Y_t = y_t | X_{t-1} = x_{t-1})\]

First off, we will take a look at the data used to estimate the model.

1960-02-01 98.168533 NaN
1960-03-01 98.552486 0.390353
2018-11-01 99.439948 -0.147800
2018-12-01 99.297610 -0.143242

In the following figure the (log) GDP Change

    \[\mathrm{GDP_{Change}} = \log(GDP_t / GDP_{t-1}) = \log(GDP_t) - \log(GDP_{t-1})\]

is illustrated. Notice the data during the 2008 financial crisis. Here, a change of GDP of approx. -1% between two quarters can be seen.

We will use quarterly GDP data from 1960-05-01 to 2018-12-01 to fit our model. The important question is: What are the most likely transition probabilities A and emissions (hidden states) B for a given time series?

The model parameters are estimated by the Viterbi algorithm. The probability that a series of states X_t given an observed series of outputs Y_t can be denoted as:

    \[\arg \max P(X_t|Y_t; M, B) = \arg \max_{X_t} \frac{P(Y_t,X_t;M,B))}{\sum_{X_t}\,P(Y_t, X_t;M,B)} = \arg \max_{X_t} P(Y_t,X_t;M,B))\]

where matrix B encodes the probability of our hidden state generating output, given that the state at the corresponding time was x(t).

In the above figure, we have two data sets. Both use the same time series, but the recession states (indicated by red and blue dots) in the bottom figure are predicted by our HMM-model. As you can see, there is a large overlap between prediction and the real data.

The next figure shows the probabilities p with 0 \le p \le 1 of whether the model predicts state 1 or 0 at time t.

We can now predict the next three months based on the last time t using our Hidden Markov Model and its fitted transition matrix.

    \[X_{t_{end}+1} = M\,X_{t_{end}}\]

Since our model was in state 0 at t_{end} the probability of staying in state 0 is p_0 = 0.901 and p_1 = 1 - p_0 \approx 0.098.


MacDonald, I and Zucchini, W (1997) Hidden Markov and Other Models
for Discrete-Valued Time Series: Chapman & Hall/CRC.

Ramage, Daniel, (2007), Hidden Markov Models Fundamentals, CS229 Section Notes

Hamilton, James D. (1989), “A New Approach to the Economic Analysis of Nonstationary Time Series and the Business Cycle

This blog post was highly inspired by:

Fulton, Chad (2016), “Markov switching autoregression models”,

Piger, Jeremy (2019), “Smoothed U.S. Recession Probabilites”,

Print Friendly, PDF & Email
+ posts