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
DifferenceEquations.AbstractStateSpaceProblem — Type
AbstractStateSpaceProblem <: AbstractDEProblemAbstract supertype for all discrete-time state-space problems in DifferenceEquations.jl.
Subtypes include LinearStateSpaceProblem, QuadraticStateSpaceProblem, PrunedQuadraticStateSpaceProblem, and StateSpaceProblem.
LinearStateSpaceProblem
DifferenceEquations.LinearStateSpaceProblem — Type
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), ornothingfor deterministic dynamics.u0: Initial state vector, or aDistributionfor 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). Ifnothing, no observations are computed.observables_noise: Observation noise covariance matrix (AbstractMatrix, e.g.Diagonal(d)orSymmetric(H * H')).observables: Observed data asVector{Vector{T}}for likelihood computation.noise: Fixed noise sequence asVector{Vector{T}}. Ifnothing, 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, andobservables_noise(withnoise = nothing) triggers automatic selection ofKalmanFilter. - The
observablestiming convention: observations correspond to $z_1, z_2, \ldots$ (starting from the second state), so passTobservations for atspanof(0, T).
See also: StateSpaceProblem, QuadraticStateSpaceProblem, DirectIteration, KalmanFilter.
QuadraticStateSpaceProblem
DifferenceEquations.QuadraticStateSpaceProblem — Type
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). EntryA_2[i,:,:]gives the matrix for thei-th element of the quadratic term.B: Noise input matrix (n×k), ornothing.u0: Initial state vector.tspan: Time span as(t0, t_end).
Keyword Arguments
C_0,C_1,C_2: Observation equation coefficients (analogous toA_0,A_1,A_2).observables_noise,observables,noise,syms,obs_syms: Same asLinearStateSpaceProblem.
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.
PrunedQuadraticStateSpaceProblem
DifferenceEquations.PrunedQuadraticStateSpaceProblem — Type
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.
StateSpaceProblem
DifferenceEquations.StateSpaceProblem — Type
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: Callbackf!!(x_next, x, w, p, t) -> x_next. For mutable arrays, mutatex_nextin place and return it; for immutable arrays (e.g.,SVector), return a new value.observation: Callbackg!!(y, x, p, t) -> y, ornothingfor no observations.u0: Initial state vector, or aDistributionfor 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)orSymmetric(H * H')).observables: Observed data asVector{Vector{T}}.noise: Fixed noise sequence asVector{Vector{T}}.syms: State variable names for symbolic indexing.obs_syms: Observation variable names for symbolic indexing.
See also: LinearStateSpaceProblem, DirectIteration.
Common Keyword Arguments
The following keywords are shared by all problem constructors:
| Keyword | Description | Default |
|---|---|---|
observables_noise | Observation noise covariance matrix (AbstractMatrix, e.g. Diagonal(d) or Symmetric(H * H')) | nothing |
observables | Observed data as Vector{Vector{T}} | nothing |
noise | Fixed noise as Vector{Vector{T}} | nothing (drawn randomly) |
syms | State variable names as a Tuple of Symbols, e.g. (:x, :y) | nothing |
obs_syms | Observation variable names as a Tuple of Symbols | nothing |
Linear-only keywords
These are accepted only by LinearStateSpaceProblem:
| Keyword | Description | Default |
|---|---|---|
C | Observation matrix | nothing |
u0_prior_mean | Prior mean for Kalman filtering | nothing |
u0_prior_var | Prior covariance for Kalman filtering | nothing |
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
observablesis not provided): observation noise with this covariance is added to the simulated observationssol.z. - During likelihood computation (when
observablesis provided): it defines the observation noise covariance used in the log-likelihood calculation.
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 condition2-element Vector{Float64}:
0.1
0.2