SandHouse
Pages and contents

State Space Models

Adapted from https://huggingface.co/blog/lbourdois/get-on-the-ssm-train,

State Space Models (SSM)

State space models (SSMs) are a class of machine learning algorithms used to make predictions about dynamic systems by modeling how their internal state evolves over time through differential equations. Historically used in control systems engineering, SSMs are a remarkably flexible mathematical platform adaptable to most sequence modeling tasks.

In general, state space models are built from three essential vectors.

1. x(t)βˆˆβ„‚nrepresents the n state variables
2. u(t)βˆˆβ„‚mrepresents the m state inputs
3. y(t)βˆˆβ„‚p represents the p state outputs

They are also composed of four learnable matrices

1. Aβˆˆβ„‚nΓ—nis the state matrix. It determines how the state evolves over time and what it forgets.
2. Bβˆˆβ„‚nΓ—mis the control matrix. It determines how the new input influences the state.
3. Cβˆˆβ„‚pΓ—nis the output matrix. It determines how the state translates to the predicted output.
4. Dβˆˆβ„‚pΓ—mis the command matrix. It acts as a skip connection directly from input to output.

Which we can reduce to the following system of equations

x'(t) = Ax(t) + Bu(t) \\ y(t) = Cx(t) + Du(t)

In deep learning, Du=0because it is an β€œeasily computable skip connection.”

[image: G7icfkYoxIqHZcJGHM7UD.png]

Discretization

Discretization allows us to pass from the continuous mathematics to recurrent and convolutional representations.

[image: 289609011-12bbe1cf-3911-4bad-9a3b-3f427bc6bc82.png]

Trapezoid Method

The trapezoid method is one of many methods to discretize an SSM. The principle of the trapezoid method is to approximate the region under a curve f defined on a segment [tn,tn+1] with a trapezoid and calculate its area T:T=(tn+1βˆ’tn)f(tn)+f(tn+1)2 .

We then have: xn+1βˆ’xn=12Ξ”(f(tn)+f(tn+1)) with Ξ”=tn+1βˆ’tn . If xβ€²n=Axn+Bun (first line of the SSM equation), corresponds to f , so:

x_{n+1} = x_n + \frac{\Delta}{2} (Ax_n + Bu_n + Ax_{n+1} + Bu_{n+1}) \\ x_{n+1} - \frac{\Delta}{2} Ax_{n+1} = x_n + \frac{\Delta}{2} Ax_n + \frac{\Delta}{2} B(u_{n+1} + u_n) \\ (I - \frac{\Delta}{2} A)x_{n+1} = (I + \frac{\Delta}{2} A)x_n + \Delta Bu_{n+1} \\ x_{n+1} = \left(I - \frac{\Delta}{2} A\right)^{-1}\left(I + \frac{\Delta}{2} A\right)x_n + \left(I - \frac{\Delta}{2} A\right)^{-1}\Delta Bu_{n+1}

This may be condensed into the following recursive formula

x_k= \bar{A}x_{k-1} + \bar{B}u_k \\ y_k = \bar{C}x_k

See the attached notes link for more in depth coverage of the convolutional view.

For the continuous view, the advantages and disadvantages are as follows:
βœ“ Automatically handles continuous data (audio signals, time series, for example). This represents a huge practical advantage when processing data with irregular or time-shifted sampling.
βœ“ Mathematically feasible analysis, e.g. by calculating exact trajectories or building memory systems (HiPPO).
βœ— Extremely slow for both training and inference.

For the recursive view these are the well-known advantages and disadvantages of recursive neural networks, namely:
βœ“ Natural inductive bias for sequential data, and in principle unbounded context.
βœ“ Efficient inference (constant-time state updates).
βœ— Slow learning (lack of parallelism).
βœ— Gradient disappearance or explosion when training too-long sequences.

For the convolutional view, we're talking here about the well-known advantages and disadvantages of convolutional neural networks (we're here in the context of their one-dimensional version), namely:
βœ“ Local, interpretable features.
βœ“ Efficient (parallelizable) training.
βœ— Slowness in online or autoregressive contexts (must recalculate entire input for each new data point).
βœ— Fixed context size.

Examples

Mamba

Mamba SSM rivals the performance of transformers at sequence modeling, using an efficient GPU algorithm to model attention based token updates.

DreamerV3's RSSM

The DreamerV3 RSSM is a latent dynamics model used to learn a representation of the world from sequence data (inputs, actions, rewards, and continuation flags). It partitions its latent state into two parts to handle both memory and uncertainty: a deterministic state ht (usually a Recurrent Neural Network hidden state) and a stochastic state zt .

The model consists of several neural network components (parameterized by Ο• ):

β€’ Sequence model: ht=fΟ•(htβˆ’1,ztβˆ’1,atβˆ’1) updates the deterministic memory using the previous state and action.
β€’ Encoder: zt∼qΟ•(zt|ht,xt) forms the "posterior" representation by compressing the current observation xt and the deterministic state into a stochastic representation.
β€’ Dynamics predictor: z^t∼pΟ•(z^t|ht) forms the "prior" representation. It guesses what the stochastic state will be without seeing the current observation. This is critical for "imagining" future trajectories during reinforcement learning.
β€’ Predictors (Reward, Continue, Decoder): These reconstruct the current environment signals (reward r^t , episode continuation c^t , and observation x^t ) strictly from the joint latent state (ht,zt) .

The entire system is optimized end-to-end using a combined loss function containing a prediction loss (how well it reconstructs outputs), a dynamics loss (forcing the prior to match the posterior), and a representation loss.


Mapping RSSM to Standard Linear SSM Equations

The standard discrete-time linear SSM equations you provided are:

1. State Equation: xk=Aβ€Ύxkβˆ’1+Bβ€Ύuk
2. Observation Equation: yk=Cβ€Ύxk

DreamerV3's RSSM is essentially a deeply non-linear, probabilistic extension of this exact framework. Here is how the variables and concepts align:

1\. The Latent State xk

β€’ Linear SSM: Uses a single, continuous vector xk .
β€’ DreamerV3: Splits the state into xkβ‰ˆ(ht,zt) . The state is represented jointly by a deterministic vector ht and a stochastic vector zt (which the text notes is sampled from a vector of softmax distributions).

2\. The Control Inputuk

β€’ Linear SSM: The control variable uk drives the system.
β€’ DreamerV3: The action taken by the agent, atβˆ’1 , serves as the control input.

3\. The State Transition / Dynamics d xk=Aβ€Ύxkβˆ’1+Bβ€Ύuk

β€’ Linear SSM: Transitions are handled by linear matrices Aβ€Ύ andBβ€Ύ. It is entirely deterministic.
β€’ DreamerV3: Replaces the linear matrices with non-linear neural networks (MLPs). The transition happens in two steps: a deterministic update ht=fΟ•(htβˆ’1,ztβˆ’1,atβˆ’1) and a stochastic transition (prior) z^t∼pΟ•(z^t|ht).
β€’ Why the difference? Environments are highly complex and unpredictable. The linear SSM struggles with multimodal uncertainty. Dreamer uses non-linear neural networks to capture complex dynamics and the stochastic sampling zt to prevent the model from failing when multiple future outcomes are possible.

4\. The Observation / Output Model yk=Cβ€Ύxk

β€’ Linear SSM: The output yk is projected linearly from the state using matrix Cβ€Ύ .
β€’ DreamerV3: The output consists of the observation xt , reward rt , and continuation ct . It replaces the linear matrix Cβ€Ύ with deep neural networks. The decoder predicts x^t∼pΟ•(x^t|ht,zt) (using CNNs for images), and auxiliary predictors handle r^t and c^t via MLPs.