Problem Types

DifferenceEquations.jl provides a hierarchy of problem types for defining discrete-time state-space models. All concrete problem types inherit from AbstractStateSpaceProblem and share a common interface for specifying dynamics, observations, and noise.

Abstract Type

LinearStateSpaceProblem

DifferenceEquations.LinearStateSpaceProblemType
LinearStateSpaceProblem(A, B, u0, tspan[, p]; kwargs...)

Define a linear time-invariant state-space model:

\[u_{n+1} = A \, u_n + B \, w_{n+1}\]

with optional observation equation $z_n = C \, u_n + v_n$.

Positional Arguments

  • A: Transition matrix (n×n).
  • B: Noise input matrix (n×k), or nothing for deterministic dynamics.
  • u0: Initial state vector, or a Distribution for random initial conditions.
  • tspan: Time span as (t0, t_end) with integer distance (e.g., (0, T)).
  • p: Parameters (default: NullParameters()).

Keyword Arguments

  • C: Observation matrix (m×n). If nothing, no observations are computed.
  • observables_noise: Observation noise covariance matrix (AbstractMatrix, e.g. Diagonal(d) or Symmetric(H * H')).
  • observables: Observed data as Vector{Vector{T}} for likelihood computation.
  • noise: Fixed noise sequence as Vector{Vector{T}}. If nothing, noise is drawn randomly.
  • u0_prior_mean: Prior mean for Kalman filtering.
  • u0_prior_var: Prior covariance matrix for Kalman filtering.
  • syms: State variable names (e.g., (:x, :y)) for symbolic indexing.
  • obs_syms: Observation variable names for symbolic indexing.

Notes

  • Providing u0_prior_mean, u0_prior_var, observables, and observables_noise (with noise = nothing) triggers automatic selection of KalmanFilter.
  • The observables timing convention: observations correspond to $z_1, z_2, \ldots$ (starting from the second state), so pass T observations for a tspan of (0, T).

See also: StateSpaceProblem, QuadraticStateSpaceProblem, DirectIteration, KalmanFilter.

source

QuadraticStateSpaceProblem

DifferenceEquations.QuadraticStateSpaceProblemType
QuadraticStateSpaceProblem(A_0, A_1, A_2, B, u0, tspan[, p]; kwargs...)

Define a second-order (quadratic) state-space model:

\[u_{n+1} = A_0 + A_1 \, u_n + u_n^\top A_2 \, u_n + B \, w_{n+1}\]

with optional observation equation $z_n = C_0 + C_1 \, u_n + u_n^\top C_2 \, u_n + v_n$.

Positional Arguments

  • A_0: Constant drift vector (length n).
  • A_1: Linear transition matrix (n×n).
  • A_2: Quadratic transition tensor (n×n×n). Entry A_2[i,:,:] gives the matrix for the i-th element of the quadratic term.
  • B: Noise input matrix (n×k), or nothing.
  • u0: Initial state vector.
  • tspan: Time span as (t0, t_end).

Keyword Arguments

  • C_0, C_1, C_2: Observation equation coefficients (analogous to A_0, A_1, A_2).
  • observables_noise, observables, noise, syms, obs_syms: Same as LinearStateSpaceProblem.

References

  • Andreasen, Fernandez-Villaverde, and Rubio-Ramirez (2017), "The Pruned State-Space System for Non-Linear DSGE Models: Theory and Empirical Applications."

See also: PrunedQuadraticStateSpaceProblem, LinearStateSpaceProblem.

source

PrunedQuadraticStateSpaceProblem

DifferenceEquations.PrunedQuadraticStateSpaceProblemType
PrunedQuadraticStateSpaceProblem(A_0, A_1, A_2, B, u0, tspan[, p]; kwargs...)

Define a pruned second-order state-space model. Unlike QuadraticStateSpaceProblem, the quadratic terms operate on a separate linear-part state $u_f$ rather than the full state:

\[u_f^{n+1} = A_1 \, u_f^n + B \, w_{n+1}\]

\[u_{n+1} = A_0 + A_1 \, u_n + (u_f^n)^\top A_2 \, u_f^n + B \, w_{n+1}\]

The observation equation similarly uses $u_f$: $z_n = C_0 + C_1 \, u_n + (u_f^n)^\top C_2 \, u_f^n + v_n$.

This pruning approach prevents explosive dynamics in higher-order perturbation solutions. Arguments are identical to QuadraticStateSpaceProblem.

References

  • Andreasen, Fernandez-Villaverde, and Rubio-Ramirez (2017), "The Pruned State-Space System for Non-Linear DSGE Models: Theory and Empirical Applications."

See also: QuadraticStateSpaceProblem.

source

StateSpaceProblem

DifferenceEquations.StateSpaceProblemType
StateSpaceProblem(transition, observation, u0, tspan[, p]; n_shocks, kwargs...)

Define a generic state-space model with user-provided callback functions:

\[u_{n+1} = f(u_n, w_{n+1}, p, t_n), \quad z_n = g(u_n, p, t_n)\]

Positional Arguments

  • transition: Callback f!!(x_next, x, w, p, t) -> x_next. For mutable arrays, mutate x_next in place and return it; for immutable arrays (e.g., SVector), return a new value.
  • observation: Callback g!!(y, x, p, t) -> y, or nothing for no observations.
  • u0: Initial state vector, or a Distribution for random initial conditions.
  • tspan: Time span as (t0, t_end) with integer distance.
  • p: Parameters passed to the callbacks (default: NullParameters()).

Keyword Arguments

  • n_shocks::Int: Number of noise dimensions (required).
  • n_obs::Int: Number of observation dimensions (default: 0).
  • observables_noise: Observation noise covariance matrix (AbstractMatrix, e.g. Diagonal(d) or Symmetric(H * H')).
  • observables: Observed data as Vector{Vector{T}}.
  • noise: Fixed noise sequence as Vector{Vector{T}}.
  • syms: State variable names for symbolic indexing.
  • obs_syms: Observation variable names for symbolic indexing.

See also: LinearStateSpaceProblem, DirectIteration.

source

Common Keyword Arguments

The following keywords are shared by all problem constructors:

KeywordDescriptionDefault
observables_noiseObservation noise covariance matrix (AbstractMatrix, e.g. Diagonal(d) or Symmetric(H * H'))nothing
observablesObserved data as Vector{Vector{T}}nothing
noiseFixed noise as Vector{Vector{T}}nothing (drawn randomly)
symsState variable names as a Tuple of Symbols, e.g. (:x, :y)nothing
obs_symsObservation variable names as a Tuple of Symbolsnothing

Linear-only keywords

These are accepted only by LinearStateSpaceProblem:

KeywordDescriptionDefault
CObservation matrixnothing
u0_prior_meanPrior mean for Kalman filteringnothing
u0_prior_varPrior covariance for Kalman filteringnothing

Quadratic-only keywords

QuadraticStateSpaceProblem and PrunedQuadraticStateSpaceProblem accept C_0, C_1, C_2 instead of C.

Generic-only keywords

StateSpaceProblem requires the additional positional/keyword arguments n_shocks and n_obs to specify dimensions.

Dual role of observables_noise

The observables_noise keyword has a dual role:

  • During simulation (when observables is not provided): observation noise with this covariance is added to the simulated observations sol.z.
  • During likelihood computation (when observables is provided): it defines the observation noise covariance used in the log-likelihood calculation.
Note

observables_noise must be an AbstractMatrix. For diagonal noise, use Diagonal([σ₁², σ₂², …]) where the entries are variances (not standard deviations). For a general covariance, use a full Matrix or Symmetric(H * H').

Remaking Problems

Use remake to create a modified copy of a problem, changing specific fields while keeping everything else. This is useful for parameter sweeps and optimization loops.

using DifferenceEquations, LinearAlgebra
A = [0.95 6.2; 0.0 0.2]
B = [0.0; 0.01;;]
prob = LinearStateSpaceProblem(A, B, zeros(2), (0, 5))
prob2 = remake(prob; u0 = [0.1, 0.2])
sol2 = solve(prob2)
sol2.u[1]  # new initial condition
2-element Vector{Float64}:
 0.1
 0.2