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.
Every concrete subtype supplies an initial state u0, an integer-step tspan, parameters p, and model-specific fields required by the solver. The public problem interface is consumed by solve, init, SciMLBase.remake, and StateSpaceSolution.
Interface
Callers may treat any subtype as an AbstractDEProblem and pass it to the standard SciML problem and solver functions. The time span must have an integer distance because the discrete trajectory contains one state for every integer step. Additional problem subtypes must preserve the callback and field semantics described by their concrete constructors.
The internal solver hooks used to implement additional problem types are documented on the developer API page. They are package-extension interfaces, not a requirement for ordinary users.
Examples
julia> using DifferenceEquations
julia> prob = LinearStateSpaceProblem([1.0;;], nothing, [0.0], (0, 2));
julia> prob isa AbstractStateSpaceProblem
trueSubtypes 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$.
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.
Fields
f: InternalSciMLBase.ODEFunctionbridge used for SciML interfaces and symbolic indexing; it does not define the state transition.A: State transition matrix.B: Noise input matrix ornothingfor deterministic dynamics.C: Observation matrix ornothingwhen observations are disabled.u0: Initial state or initial-state distribution.tspan: Integer-step time span.p: User parameters.observables_noise,observables,noise: Observation and simulation data.u0_prior_mean,u0_prior_var: Optional Gaussian prior forKalmanFilter.syms,obs_syms: Optional state and observation names for symbolic indexing.kwargs: Additional constructor keyword arguments retained for remaking.
Returns
LinearStateSpaceProblem: A SciML-compatible linear discrete-time problem.
Throws
ArgumentError: Iftspandoes not have an integer distance.
Examples
julia> using DifferenceEquations
julia> prob = LinearStateSpaceProblem([1.0;;], nothing, [1.0], (0, 2));
julia> length(solve(prob).u)
3QuadraticStateSpaceProblem
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$.
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)with integer distance.p: Parameters passed through the SciML problem interface (default:NullParameters()).
Keyword Arguments
C_0: Constant observation term, ornothingwhen observations are disabled.C_1: Linear observation matrix, ornothingwhen observations are disabled.C_2: Quadratic observation tensor, ornothingwhen observations are disabled.observables_noise: Observation covariance matrix, ornothing.observables: Observed data used for likelihood calculations, ornothing.noise: Fixed process-noise sequence, ornothingfor generated noise.syms: State variable names for symbolic indexing, ornothing.obs_syms: Observation variable names for symbolic indexing, ornothing.kwargs...: Additional constructor keywords retained in the problem.
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.
Fields
A_0,A_1,A_2: Constant, linear, and quadratic transition coefficients.f: InternalSciMLBase.ODEFunctionbridge used for SciML interfaces and symbolic indexing.B: Noise input matrix ornothing.C_0,C_1,C_2: Optional observation coefficients.u0: Initial state.tspan: Integer-step time span.p: User parameters.observables_noise,observables,noise: Observation and simulation data.syms,obs_syms: Optional state and observation names for symbolic indexing.kwargs: Additional constructor keyword arguments retained for remaking.
Returns
QuadraticStateSpaceProblem: A quadratic discrete-time state-space problem.
Throws
ArgumentError: Iftspandoes not have an integer distance.
Examples
julia> using DifferenceEquations
julia> prob = QuadraticStateSpaceProblem([0.0], [1.0;;], zeros(1, 1, 1), nothing, [1.0], (0, 2));
julia> length(solve(prob).u)
3PrunedQuadraticStateSpaceProblem
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
A_0: Constant drift vector.A_1: Linear transition matrix used by both the full and linear-part states.A_2: Quadratic transition tensor applied to the linear-part state.B: Noise input matrix, ornothing.u0: Initial full state and initial linear-part state.tspan: Integer-step time span.p: Parameters passed through the SciML problem interface (default:NullParameters()).
Keyword Arguments
C_0: Constant observation term, ornothing.C_1: Linear observation matrix, ornothing.C_2: Quadratic observation tensor, ornothing.observables_noise: Observation covariance matrix, ornothing.observables: Observed data used for likelihood calculations, ornothing.noise: Fixed process-noise sequence, ornothingfor generated noise.syms: State variable names for symbolic indexing, ornothing.obs_syms: Observation variable names for symbolic indexing, ornothing.kwargs...: Additional constructor keywords retained in the problem.
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.
Fields
A_0,A_1,A_2: Constant, linear, and quadratic transition coefficients.f: InternalSciMLBase.ODEFunctionbridge used for SciML interfaces and symbolic indexing.B: Noise input matrix ornothing.C_0,C_1,C_2: Optional observation coefficients.u0: Initial state and the initial value of the linear componentu_f.tspan: Integer-step time span.p: User parameters.observables_noise,observables,noise: Observation and simulation data.syms,obs_syms: Optional state and observation names for symbolic indexing.kwargs: Additional constructor keyword arguments retained for remaking.
Returns
PrunedQuadraticStateSpaceProblem: A pruned quadratic state-space problem.
Throws
ArgumentError: Iftspandoes not have an integer distance.
Examples
julia> using DifferenceEquations
julia> prob = PrunedQuadraticStateSpaceProblem([0.0], [1.0;;], zeros(1, 1, 1), nothing, [1.0], (0, 2));
julia> length(solve(prob).u)
3StateSpaceProblem
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)\]
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.
Fields
transition: In-place or out-of-place transition callback.observation: In-place or out-of-place observation callback, ornothing.u0: Initial state or initial-state distribution.tspan: Integer-step time span.p: Parameters passed to both callbacks.n_shocks,n_obs: Noise and observation dimensions.observables_noise,observables,noise: Observation and simulation data.syms,obs_syms: Optional state and observation names for symbolic indexing.f: InternalSciMLBase.ODEFunctionbridge used for SciML interfaces and symbolic indexing.kwargs: Additional constructor keyword arguments retained for remaking.
Returns
StateSpaceProblem: A SciML-compatible callback-based state-space problem.
Throws
ArgumentError: Iftspandoes not have an integer distance.
Examples
julia> using DifferenceEquations
julia> transition = (x_next, x, w, p, t) -> copyto!(x_next, x);
julia> prob = StateSpaceProblem(transition, nothing, [1.0], (0, 2); n_shocks = 0);
julia> solve(prob).u[end]
1-element Vector{Float64}:
1.0Common 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 SciMLBase.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