Filtering ========== This chapter covers the linear and nonlinear filtering machinery that ``filter``, ``estimate``, ``forecast``, and the historical decomposition route into. .. contents:: :local: :depth: 2 Linear filtration ------------------ Filtering with constant parameters can be done efficiently with the algorithms of De Jong et al. (2003), Koopman and Durbin (2000, 2003), and Durbin and Koopman (2012). These procedures cannot be used directly in a regime-switching context. The main references for the filtration of Markov-switching state-space models are Kim (1999) and Kim and Nelson (2001), which combine the Kalman and Hamilton filters. Similar algorithms appear in the engineering literature (Bar-Shalom et al., 2001, 2004). RISE uses an extension of the Durbin-Koopman (2012) algorithm except for the initialisation process, which in the non-stationary case has not been extended to regime-switching models. For non-stationarity, RISE applies an arbitrary variance for the initialisation. For efficiency, RISE deviates from Kim-Nelson (2001) when collapsing the regimes. Kim-Nelson collapse the *updates*; RISE collapses the *forecasts*. The two procedures yield numerically similar results but the RISE procedure is faster. Historical decomposition ~~~~~~~~~~~~~~~~~~~~~~~~~ .. warning:: Exact only for constant-parameter linear models. Observables decomposition ~~~~~~~~~~~~~~~~~~~~~~~~~~ .. important:: Available only for constant-parameter models. Conditional forecasting via filtering -------------------------------------- Conditional forecasting can be implemented using filtering algorithms. The smoother treats the conditioning values as observations on the forecast horizon and fills in everything else by least squares -- no :doc:`simulation plan ` is required, which makes this the most convenient route *when its assumptions hold*. The assumptions are restrictive: it works only for **linear** models, and the conditioning variables must be on the **observed** side (declared in ``@observables``). For anything else -- hard conditions on unobserved states, nonlinear solutions, regime switching, anticipated shocks, soft conditions enforced through shocks rather than measurement noise -- use a simulation plan through :doc:`Forecasting and simulation`. Hard conditions ~~~~~~~~~~~~~~~~ Extend the observation database to cover the forecast horizon. Numeric entries pin the observed variable at that date; ``NaN`` entries are free. Then call ``filter``:: db = struct(); db.PAI = [pai_history; pai_future_conditions]; % conditioned db.R = [r_history; nan(H, 1)]; % free db.DY = [dy_history; nan(H, 1)]; % free m = set(m, data = db); [filt, ll, ~, rc, m] = filter(m); The smoothed series in ``filt`` are the conditional forecast for every endogenous variable and shock. The implied shock path is the least-squares one that makes the conditioning observations consistent with the model. Soft conditions ~~~~~~~~~~~~~~~~ Relate the conditioning observation ``X0`` to a model variable ``X`` as :math:`X0_t = X_t + \sigma \varepsilon_t`. The scalar :math:`\sigma` controls the softness of the condition: * :math:`\sigma \to \infty` -- the condition is irrelevant. * :math:`\sigma \to 0` -- **hard conditions**. * :math:`\sigma` strictly positive and finite (small enough) -- **soft conditions**. When to choose this route ~~~~~~~~~~~~~~~~~~~~~~~~~~ * Model is linear (constant-parameter or regime-switching). * Conditions are on observed variables. * You want a least-squares decomposition of the implied shock path. * You do not need to control anticipation: the smoother treats the conditioning path as fully observed up to its date, which is closest to "unanticipated until realised" in spirit. When the route is the wrong tool ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Higher-order or strongly nonlinear solutions -- the least-squares interpretation breaks down. * Conditions on unobserved states (e.g. natural rate, output gap) -- declare them as ``@observables`` first or switch to a simulation plan. * You want to choose *which* shocks the solver may use to enforce a condition (the simplan identification rule). * You want to mix anticipated and unanticipated shocks in the same scenario. Nonlinear filtering ------------------- When a DSGE model is solved with a higher-order perturbation -- or otherwise has a nonlinear state-space representation -- the regime-switching Kalman/Hamilton filter no longer applies. RISE then uses a **sigma-point** filter for regime-switching nonlinear state-space models, following Binning and Maih (2015). When the nonlinear filter is used ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``filter`` -- and, through it, ``estimate``, ``forecast`` with conditioning, the historical decomposition, ... -- picks the algorithm automatically from the solution: * ``solve_order == 1`` -- the linear regime-switching Kalman/Hamilton filter (a fast constant-parameter variant when there is a single regime, no occasionally-binding constraint, and no real-time information). * ``solve_order >= 2`` -- a nonlinear sigma-point filter (the default is the *switching divided-difference filter*). Nonlinear filtering with real-time information is not supported (RISE raises an error). Choosing the filter ~~~~~~~~~~~~~~~~~~~~ Three sigma-point filters are available; the divided-difference filter is the default, the other two are selected by passing them as the filtering algorithm via ``kf_user_algo`` (they all share the standard filter signature, so they plug in directly):: % default: switching divided-difference filter [fkst, LogLik] = filter(m); % switching unscented Kalman filter [fkst, LogLik] = filter(m, kf_user_algo = 'switching_unscented_kalman_filter'); % switching cubature Kalman filter [fkst, LogLik] = filter(m, kf_user_algo = 'switching_cubature_kalman_filter'); The same option can be passed to ``estimate`` -- it changes which filter computes the likelihood that is maximized. Options -------- The filtering options apply equally to the linear and nonlinear cases (see also ``help dsge_model/filter``): .. list-table:: :header-rows: 1 :widths: 30 70 * - Option - Effect * - ``kf_filtering_level`` - ``0`` likelihood only; ``1`` + filtered (one-step-ahead) series; ``2`` + updated series; ``3`` + smoothed series. Default ``3``. * - ``kf_ergodic`` - Initialise regime probabilities at the ergodic distribution. Default ``true``. * - ``kf_init_variance`` - Initial state covariance (scalar = isotropic; matrix = explicit). Use ``1`` for non-stationary models that lack a finite unconditional variance. * - ``kf_user_init`` - User-supplied initial state and covariance, for fine control of the initialisation. * - ``kf_presample`` - Number of initial observations to discard before evaluating the likelihood. Default ``0``. * - ``kf_householder_chol`` - Return the Cholesky factor when taking the Householder transformation; primarily used by the divided-difference filter. Default ``false``. User-defined filter -------------------- A filter of your own can be supplied through ``kf_user_algo`` -- a function (or function name, or cell ``{name, extra-args...}``) with signature:: [LogLik, Incr, retcode, Filters] = myFilter(syst, y, U, z, options) If it needs information beyond what RISE passes, prefix the name with ``*`` so RISE first calls it as ``myFilter(modelObject, struct)`` to let it fetch what it needs. See :doc:`Extending RISE`. The available sigma-point filters ---------------------------------- Switching divided-difference filter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The default RISE nonlinear filter. Uses divided-difference approximations to propagate the state moments through the nonlinear transition and observation maps. Robust on regime-switching models where the unscented transform's sigma points can fall outside the admissible parameter region. Switching unscented Kalman filter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Uses the unscented transform to propagate sigma points through the nonlinear maps. Faster than the divided-difference filter on well-behaved problems; can be less robust on awkward switching specifications. Switching cubature Kalman filter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Uses a spherical-radial cubature rule to propagate the state moments. A modern alternative to the unscented Kalman filter with similar accuracy and somewhat better numerical behavior at high state dimension.