EC9D5 · Working with Dynare · Part 2

Linking Data to a Macroeconomic Model for Bayesian Estimation in Dynare

Bayesian estimation compares what the model predicts with what the data show. Observation equations make that comparison possible: they turn each model variable into exactly the object stored in the data file, in the same units. This page shows how to write them with the conventions used on this course (variables in logs, shocks in percent, data as HP-filtered cycles in percent) and how to check them before estimating. It builds on Part 1, which explains log deviations and the units of Dynare’s output.

1. What an Observation Equation Does

The model describes theoretical quantities such as productivity, capital, consumption and output. The data contain particular statistical measures of some of them. The two are rarely the same object.

The model contains log output, \(y_t=\log(Y_t^{\text{level}})\), a log level (around 4.6 if output is 100). The data contain the cyclical component of output, in percent, which fluctuates around zero. They cannot be compared directly.

An observation equation (or measurement equation) defines a new model variable that reproduces the observed series. In Dynare it is an ordinary endogenous variable, defined by an equation in the model block and declared as observed with varobs. It takes the same name as the series in the data file:

Dynare .mod file
var y      // log output (model)
    Y;     // observed output: HP cycle, in percent

On this page \(Y_t^{\text{data}}\) denotes the observed series and Y its model counterpart. What the observation equation looks like depends entirely on how the data were constructed.

2. Three Questions About Your Data

Before writing an observation equation, establish exactly what each series in the data file represents.

  1. Which economic object? A deviation from trend (cyclical component), a growth rate, a level, or a rate such as the interest rate or inflation.
  2. Which transformation? Was the series logged before filtering or differencing? Was it detrended with the Hodrick–Prescott (HP) filter, and with which smoothing parameter? Was it demeaned?
  3. Which units? Decimals or percent? For rates, quarterly or annualised?

For example, “output as a percentage deviation from its HP trend” and “output growth from one quarter to the next, in percent” are both in percent, but they are different economic objects and need different observation equations.

3. HP-Filtered Cycles

3.1. What the cyclical component measures

The HP filter splits a series into a trend and a cyclical component. Applied to log output,

\[ \log\!\left(Y_t^{\text{level}}\right)=\log\!\left(Y_t^{\text{trend}}\right)+y_t^{\text{cycle}}, \]

so the cyclical component is the log deviation of output from its HP trend:

HP cycle of a logged series \[ y_t^{\text{cycle}} =\log\!\left(Y_t^{\text{level}}\right)-\log\!\left(Y_t^{\text{trend}}\right) =\log\!\left(\frac{Y_t^{\text{level}}}{Y_t^{\text{trend}}}\right). \]

This is the data counterpart of the log deviation from the steady state in Part 1, with one difference. The steady state is a fixed reference point implied by the model; the HP trend is a statistical estimate that moves over time. Matching the two amounts to assuming that the stationary model describes fluctuations around the trend the filter has removed.

3.2. The data: cycles in percent, mean zero

On this course the data are HP-filtered cyclical components of quarterly series (smoothing parameter \(\lambda=1600\)), multiplied by 100 so that they are in percent:

\[ Y_t^{\text{data}}=100\left[\log\!\left(Y_t^{\text{level}}\right)-\log\!\left(Y_t^{\text{trend}}\right)\right]. \]

A cycle of 0.02 is recorded as \(Y_t^{\text{data}}=2\): output about 2% above its trend. By construction the series has a mean of zero. The data are saved in a .mat file whose variable names are the observables, for example data_estim_phi.mat containing Y and I.

3.3. The observation equation

The shocks are in percent, so at order = 1 the model’s deviations from the steady state are already in percent (Part 1, section 5.1). The observation equation is therefore the deviation from the steady state, without a factor of 100:

Observation equation, model block
Y = y - STEADY_STATE(y);   // percent deviation from steady state
  • y is log output and STEADY_STATE(y) steady-state log output;
  • with shocks in percent, their difference is in percent, the units of the data;
  • Y has a steady state of zero, like the mean-zero data, so no constant and no prefilter option are needed.
Worked example

A shock raises output 2% above its steady state. Dynare reports y - STEADY_STATE(y) = 2, so the observation equation gives \(Y_t=2\), directly comparable with an observation of 2 in the data.

In other papers and code. When shocks are set in log units (stderr 0.0089), the model’s deviations are log deviations (0.02 for 2%), and the observation equation must restore the percent scale: Y = 100*(y - STEADY_STATE(y));. At order = 1 the two set-ups are equivalent; what matters is that exactly one of them carries the factor of 100.

3.4. Observing a log level directly

Some older files observe a log-level model variable directly, for example varobs c d Ia; where c is log consumption. Dynare then uses the steady state of c, a log level, as the mean of the observed series, which does not match mean-zero data. In that case, add prefilter = 1 to estimation(...), so that Dynare demeans the data and drops the steady state from the observation equation. Defining an observation variable with a zero steady state, as in section 3.3, avoids the issue altogether.

3.5. Log cycles or exact percentage cycles?

Some datasets report the exact percentage deviation from trend, \(100\left[Y_t^{\text{level}}/Y_t^{\text{trend}}-1\right]\), instead of 100 times the log cycle. For movements of a few percent the two are almost identical (Part 1, section 4). A first-order model cannot tell them apart: Dynare linearises every equation, so an “exact” observation equation would give the same numbers. Build the data as 100 times the log cycle, as in section 3.2.

3.6. Other variables

The same equation applies to every variable in logs: consumption, investment, hours, wages. If log investment is \(inv_t=\log(I_t^{\text{level}})\) and the data contain its HP cycle in percent,

Observation equation, model block
I = inv - STEADY_STATE(inv);   // investment, percent deviation from steady state

4. Growth Rates

Many estimations in the literature use growth rates instead of HP-filtered cycles, which avoids filtering the data. The same units logic applies, with one extra step: the mean.

If the data are quarterly growth rates in percent, \(Y_t^{\text{data}}=100\left[\log(Y_t^{\text{level}})-\log(Y_{t-1}^{\text{level}})\right]\), the model counterpart is the change in log output. With shocks in percent, it is already in percent:

Observation equation, model block
dY = y - y(-1);   // output growth from t-1 to t, in percent

Note the timing: y - y(-1) is growth from \(t-1\) to \(t\), as in the data.

In a stationary (detrended) model, y - y(-1) fluctuates around zero. Observed growth does not: US real GDP, for instance, has grown by around 0.5% per quarter on average in recent decades. Either demean the data, or add a trend-growth parameter and estimate it with the other parameters:

Observation equation with a trend-growth parameter
parameters gamma_bar;        // mean quarterly growth, in percent

model;
  // ...
  dY = gamma_bar + y - y(-1);
end;

HP cycles do not need this step, because the filter removes the trend and the cycle has a mean of zero.

5. Units and Scale

Even when the model and the data measure the same economic object, their numbers may be on different scales. This is one of the most common sources of error in Bayesian estimation. The scale of the model is set by the units of the shocks, and the observation equation must match it to the data:

Shock units, observation equations and resulting scale
Shock units Observation equation A 2% deviation gives Against data in percent
Percent (stderr 0.89) Y = y - STEADY_STATE(y); 2 Match (this course)
Log units (stderr 0.0089) Y = 100*(y - STEADY_STATE(y)); 2 Match
Log units (stderr 0.0089) Y = y - STEADY_STATE(y); 0.02 100 times too small
Percent (stderr 0.89) Y = 100*(y - STEADY_STATE(y)); 200 100 times too large
A factor-of-100 mismatch is not a minor error. The estimation then moves the shock standard deviations by a factor of about 100 to fit the data, far from their priors (inv_gamma_pdf, 1, 2, in percent), and every other parameter estimate is distorted.

5.1. Interest rates and inflation

Rates need the same care. A quarterly model has the log gross interest rate \(r_t=\log(1+R_t)\); with shocks in percent, its deviation is the change in the quarterly net rate in percentage points (Part 1, section 5.2). If the data are the annualised rate in percent, demeaned, multiply by 4:

Observation equation, model block
R = 4*(r - STEADY_STATE(r));   // annualised percentage points, demeaned

Inflation works the same way.

5.2. A practical scale check

Before estimating, solve the model at the calibrated parameters with stoch_simul(order = 1, hp_filter = 1600), which reports the standard deviations of the HP-filtered model variables, including Y. Compare them with the standard deviations of the data. The HP cycle of quarterly real GDP in percent typically has a standard deviation between 1 and 2. If the model reports about 0.015 instead, the shocks are in log units and a factor of 100 is missing; if it reports more than 100, the factor has been applied twice. Do this check for every observed variable.

6. Putting It Together in Dynare

The skeleton below estimates a model with a TFP shock and an investment shock on the HP cycles of output and investment. Sections follow the same order in every file: variables, calibration, model, steady state, shocks, steady; check;, then observables, priors and estimation.

Dynare .mod file (Dynare 6.x)
// =========================================================
// Model name: short description (estimation)
// Author, month year. MATLAB / Dynare 6.x
// Variables in logs; equations in levels with exp().
// Shocks in percent (stderr 1 = 1%): at order = 1, all
// deviations are in percent. Observables: HP cycles
// (lambda = 1600), in percent, mean zero.
// =========================================================

// ---------------------------------------------------------
// Variables
// ---------------------------------------------------------
var y      // log output
    inv    // log investment
    k      // log capital (end of period: k = log K_{t+1})
    h      // log hours
    A      // log TFP
    phi    // log investment-specific shock
    // ... other model variables
    Y      // observed output (percent deviation)
    I;     // observed investment (percent deviation)
varexo eps_A eps_phi;   // at least as many shocks as observables
parameters alpha beta delta rhoA rhophi phiss;   // ...

// ---------------------------------------------------------
// Calibration
// ---------------------------------------------------------
beta  = 0.988;
alpha = 0.333;
delta = 0.025;
rhoA  = 0.976;
// ...

// ---------------------------------------------------------
// Model
// ---------------------------------------------------------
model;
  // Production
  exp(y) = exp(A)*exp(k(-1))^alpha*exp(h)^(1-alpha);
  // ... other equations, each commented
  // Exogenous processes: log AR(1)
  A   = rhoA*A(-1) + eps_A;
  phi = (1-rhophi)*log(phiss) + rhophi*phi(-1) + eps_phi;
  // Observation equations (no x100: shocks are in percent)
  Y = y - STEADY_STATE(y);
  I = inv - STEADY_STATE(inv);
end;

// ---------------------------------------------------------
// Steady state (closed form)
// ---------------------------------------------------------
steady_state_model;
  // ... levels with temporary variables ending in _, then logs
  Y = 0;
  I = 0;
end;

// ---------------------------------------------------------
// Shocks (standard deviations in percent)
// ---------------------------------------------------------
shocks;
  var eps_A;   stderr 0.89;
  var eps_phi; stderr 1;
end;

steady;
check;

// ---------------------------------------------------------
// Observables, priors and estimation
// ---------------------------------------------------------
varobs Y I;

estimated_params;
  rhoA,            beta_pdf,      0.95, 0.02;
  rhophi,          beta_pdf,      0.7,  0.1;
  stderr eps_A,    inv_gamma_pdf, 1,    2;   // in percent
  stderr eps_phi,  inv_gamma_pdf, 1,    2;
end;

estimation(
    datafile     = data_estim_phi,
    first_obs    = 1,
    mode_compute = 6,
    mh_replic    = 20000,
    mh_nblocks   = 2,
    mh_jscale    = 0.3,
    mh_drop      = 0.3,
    bayesian_irf,
    smoother
) y inv h k A phi;
shock_decomposition y inv;
  • Declare the observation variables with var, like any endogenous variable, and give them a zero steady state in steady_state_model.
  • Name the data series exactly as in varobs: data_estim_phi.mat must contain variables Y and I, in percent.
  • Avoid stochastic singularity: the number of observed variables cannot exceed the number of shocks, counting any measurement errors. With fewer shocks than observables, the likelihood cannot be computed.

For growth-rate data, replace the two observation equations with those of section 4; nothing else changes.

7. Checklist Before Estimating

  1. Identify the economic object. Cyclical component, growth rate, level or rate: these are different objects.
  2. Identify the transformation. Was the series logged before the HP filter or differencing? Which smoothing parameter? Was it demeaned?
  3. Check the units. Shocks in percent (stderr 0.89, not 0.0089) and data in percent: then no factor of 100 in the observation equations. Rates: quarterly or annualised?
  4. Deal with the mean. Observation variables have a zero steady state, so the data must have a zero mean: demean growth rates and rates, or add a parameter for their mean.
  5. Write the observation equation so that the model variable represents exactly the same object as the observed series, with the same name as in the data file.
  6. Check the scale. Compare the standard deviations from stoch_simul(order = 1, hp_filter = 1600) with those of the data, to catch a missing or doubled factor of 100.
  7. Check the timing. For growth rates, y - y(-1) measures growth from \(t-1\) to \(t\), as in the data.
  8. Count shocks and observables. At least as many shocks (including measurement errors) as observed variables.

8. Key Principle

Observation equations are not arbitrary transformations: they define how the theoretical model relates to the observed data. A correct observation equation matches three things.

  • Economic meaning: the model variable and the data measure the same object.
  • Statistical construction: the model reproduces the transformation applied to the data, such as logging, HP filtering, differencing or demeaning.
  • Units: the model-implied and actual observations are on the same numerical scale, which with shocks in percent means percent on both sides.

When all three hold, the model and the data are correctly linked for Bayesian estimation.