LTI identification: canonical reparameterization of linear Gaussian systems¶
A linear time-invariant (LTI) Gaussian state-space model (SSM) is the most classical SSM for Bayesian inference, and its state estimation tasks can be solved exactly with a Kalman filter. Despite state estimation being "easy," it turns out that parameter estimation can still be quite hard when the dimension of the hidden state, $d_x$, is larger than the dimension of the observed state, $d_y$. This is because there are many equivalent representations of the hidden state --- for example, swapping two entries of $x$ and the corresponding rows of its response matrix --- that imply a curved, disconnected parameter posterior that can be difficult to explore via Monte Carlo.
Canonical Bayesian Linear System Identification addresses this difficulty by using controller canonical coordinates; this is a reparameterization of the LTI system which directly parameterizes an equivalence class of linear responses. In a controllable single-input single-output (SISO) system, for example, the parameterization drops from $d_x^2 + 2d$ degrees of freedom to just $2d$. Bryutkin et al. use this form to develop identifiability argument and its consequences for inference and invariant quantities of interest.
Here we qualitatively reproduce experiments of Bryutkin et al. in NumPyro.
import os
import time
from functools import partial
from pathlib import Path
import numpyro
numpyro.set_host_device_count(
8
) # Allow up to eight chains to run on separate CPU devices.
import arviz as az
import jax
import jax.numpy as jnp
import jax.random as jr
import matplotlib.pyplot as plt
import numpy as np
import numpyro.distributions as dist
import pandas as pd
import seaborn as sns
from IPython.display import display
from matplotlib.lines import Line2D
from numpyro.handlers import block
from numpyro.infer import Predictive
from numpyro.infer.util import log_density
import dynestyx as dsx
from dynestyx.inference.configs.filter import KFConfig
from dynestyx.inference.configs.mcmc import AdaptiveMetropolisConfig, NUTSConfig
from dynestyx.inference.mcmc import MCMCInference
SMOKE = os.environ.get("LTI_SMOKE", "0") == "1"
CHAINS, WARMUP, DRAWS = (2, 100, 100) if SMOKE else (8, 500, 1_000)
COLORS = {"full": "#2463a6", "canonical": "#d77536"}
sns.set_theme(style="whitegrid", context="notebook", font_scale=0.9)
plt.rcParams.update(
{
"figure.dpi": 120,
"axes.spines.top": False,
"axes.spines.right": False,
"grid.alpha": 0.18,
}
)
A true system and observations¶
We use $$x_{t+1}=Ax_t+Bu_t+w_t,\qquad y_t=Cx_t+v_t,$$ with $x_0\sim N(0,I)$, $w_t\sim N(0,0.09I)$, and $v_t\sim N(0,0.25)$. Controls are known throughout the experiment; $u_t$ drives the transition from time $t$.
We simulate one trajectory and split it into three consecutive segments:
- Training, steps 0–499: for fitting the system parameters;
- Filtering test, steps 500–699: to test filtering performance using the learned parameters; and
- Forecasting test, steps 700–799: to test forecasting performance using the learned parameters.
Change A_TRUE, B_TRUE, and C_TRUE to study a different system, or the segment lengths below to change the split. Process and observation noise covariances are treated as known in both fits.
A_TRUE = jnp.array([[0.5, -0.93], [0.7, 0.3]])
B_TRUE = jnp.array([[0.2], [1.0]])
C_TRUE = jnp.array([[1.0, 0.5]])
STATE_DIM = A_TRUE.shape[0]
TRUE = {"A": A_TRUE, "B": B_TRUE, "C": C_TRUE}
def companion(alpha):
"""Last-row companion matrix, including batched coefficients."""
d = alpha.shape[-1]
A = jnp.zeros(alpha.shape[:-1] + (d, d), dtype=alpha.dtype)
A = A.at[..., jnp.arange(d - 1), jnp.arange(1, d)].set(1.0)
return A.at[..., -1, :].set(-alpha)
def matrices(parameters, form):
if form == "full":
return parameters["A"], parameters["B"], parameters["C"]
d = parameters["alpha"].shape[-1]
return companion(parameters["alpha"]), jnp.eye(d)[:, -1:], parameters["C"]
def system(parameters, form="full"):
A, B, C = matrices(parameters, form)
return dsx.LTI_discrete(
A=A, B=B, H=C, Q=0.09 * jnp.eye(A.shape[-1]), R=jnp.array([[0.25]])
)
def generate_data(key, length):
input_key, simulation_key = jr.split(key)
t = jnp.arange(length, dtype=A_TRUE.dtype)
u = jr.normal(input_key, (length, 1))
simulated = dsx.simulate(
system(TRUE),
rng_key=simulation_key,
predict_times=t,
ctrl_times=t,
ctrl_values=u,
)
x, y = simulated.states[0], simulated.observations[0]
return {"t": t, "u": u, "y": y, "signal": (x @ C_TRUE.T)[:, 0]}
N_TRAIN, N_FILTER_TEST, N_FORECAST_TEST = 500, 200, 100
FORECAST_START = N_TRAIN + N_FILTER_TEST
data = generate_data(jr.PRNGKey(47), FORECAST_START + N_FORECAST_TEST)
train = {name: value[:N_TRAIN] for name, value in data.items()}
filtering_test = {name: value[N_TRAIN:FORECAST_START] for name, value in data.items()}
forecasting_test = {name: value[FORECAST_START:] for name, value in data.items()}
fig, axes = plt.subplots(2, 1, figsize=(12, 5), sharex=True)
axes[0].plot(data["t"], data["u"][:, 0], color="#65778b", lw=0.9)
axes[0].set(
ylabel="control $u_t$",
title="One trajectory: training, filtering test, forecasting test",
)
axes[1].plot(
data["t"], data["signal"], "--", color="#222222", lw=1.1, label="true signal"
)
for segment, label, color in (
(train, "training observations", "#65778b"),
(filtering_test, "filtering test", "#2463a6"),
(forecasting_test, "forecasting test", "#d77536"),
):
axes[1].scatter(
segment["t"], segment["y"][:, 0], s=7, alpha=0.45, color=color, label=label
)
for ax in axes:
ax.axvspan(N_TRAIN - 0.5, FORECAST_START - 0.5, color="#edf4fa", zorder=0)
ax.axvspan(FORECAST_START - 0.5, len(data["t"]) - 0.5, color="#faf0e7", zorder=0)
for boundary in (N_TRAIN, FORECAST_START):
ax.axvline(boundary - 0.5, color="#687482", ls="--", lw=0.8)
axes[1].set(xlabel="time step", ylabel="observation $y_t$")
axes[1].legend(ncol=4, loc="upper left", frameon=False)
fig.tight_layout()
plt.show()
Full and canonical parameterizations¶
We now provide two different priors based on different parameterizations parameterizations, a "full" parameterization and a "canonical" one.
Full: we place independent standard Gaussian priors on all entries of $A,B,C$, restricted to $|\operatorname{Re}\lambda_i(A)|<1$. Note that this constraint is related but weaker than discrete-time stability, which requires $|\lambda_i|<1$.
Canonical: we place a prior directly in the canonical parameterization. In particular, $A_c$ is the companion matrix with last row $-\alpha$, $B_c=e_d$, and $C_c$ is free. The coefficient prior below retains the source's real/complex root mixture, unit-disk support, and positive log-Vandermonde convention. It is the experiment's coefficient-space prior: the paper's eigenvalue-to-coefficient change of variables instead uses the reciprocal Vandermonde (equations 15–16). The first entry of $C_c$ has the typical flat prior; all remaining entries have $\mathcal{N}(0.5,1)$ priors.
Both dynamics-prior formulas extend to arbitrary fixed $d$. The corresponding priors are prior for $A_c$ and $B_c$, though the flat prior on $C_c$ is improper in this example.
We hold $Q$ and $P_0$ isotropic in both parameterizations, though this is not necessary in general.
def polar_coefficient_logprior(alpha):
eigenvalues = jnp.linalg.eigvals(companion(alpha))
radius = jnp.abs(eigenvalues)
real = jnp.abs(eigenvalues.imag) < 1e-12
n_real, n_complex = real.sum(), (~real).sum()
n_pairs = n_complex // 2
real_lp = jnp.where(
real, jnp.where((radius > 0) & (radius <= 1), jnp.log(radius), -jnp.inf), 0.0
).sum()
valid_complex = (n_complex % 2 == 0) & jnp.all(real | (radius <= 1))
complex_lp = jnp.where(valid_complex, -n_pairs * jnp.log(jnp.pi), -jnp.inf)
differences = jnp.abs(eigenvalues[:, None] - eigenvalues[None, :])
upper = jnp.triu(jnp.ones(differences.shape, dtype=bool), k=1)
vandermonde = jnp.prod(jnp.where(upper, differences, 1.0))
return (
real_lp
+ complex_lp
+ n_real * jnp.log(0.707)
+ n_pairs * jnp.log(0.293)
+ jnp.log(vandermonde + 1e-12)
)
def model(
obs_times=None,
obs_values=None,
ctrl_times=None,
ctrl_values=None,
predict_times=None,
*,
form="full",
state_dim=STATE_DIM,
):
d = state_dim
if form == "full":
parameters = {
name: numpyro.sample(name, dist.Normal(0, 1).expand(shape).to_event(2))
for name, shape in {"A": (d, d), "B": (d, 1), "C": (1, d)}.items()
}
valid = jnp.all(jnp.abs(jnp.linalg.eigvals(parameters["A"]).real) < 1)
numpyro.factor("real_part_support", jnp.where(valid, 0.0, -jnp.inf))
else:
reference = dist.Normal(0, 1).expand((d,)).to_event(1)
alpha = numpyro.sample("alpha", reference)
C = numpyro.sample("C", dist.Normal(0.5, 1).expand((1, d)).to_event(2))
numpyro.factor(
"coefficient_prior",
polar_coefficient_logprior(alpha)
- reference.log_prob(alpha)
- jnp.log(2.0),
)
numpyro.factor("flat_first_C", -dist.Normal(0.5, 1).log_prob(C[0, 0]))
parameters = {"alpha": alpha, "C": C}
return dsx.sample(
"system",
system(parameters, form),
obs_times=obs_times,
obs_values=obs_values,
ctrl_times=ctrl_times,
ctrl_values=ctrl_values,
predict_times=predict_times,
)
def inference_model(*args, **kwargs):
# Keep the likelihood factor, but do not collect state histories for every MCMC draw.
with block(hide_fn=lambda message: message["type"] == "deterministic"):
with dsx.Filter(KFConfig(filter_source="cd_dynamax")):
return model(*args, **kwargs)
Fitting the models¶
Next, we fit the models using NUTS. All timings below include compilation, warmup, and every attempted chain. R-hat, ESS, and sampler diagnostics are reported for assessing convergence.
For the canonical fit, we initialize from a least-squares ARX model fitted to the training segment: $$ y_{t+d}=-\sum_{k=0}^{d-1}\alpha_k y_{t+k} +\sum_{k=0}^{d-1}(C_c)_k u_{t+k}+e_t. $$ The estimated denominator gives the initial poles. We move poles outside radius 0.98 inward and refit the numerator $C_c$. Each chain starts with a small independent perturbation of the pole radii, complex angles, and $C_c$; conjugate pairing and stability are preserved. This works for any fixed state dimension with enough training data and control excitation. The ARX estimate is only a starting point: measurement and process noise can bias it, and the prior and likelihood used by NUTS are unchanged.
Initialization uses neither the true parameters nor held-out observations nor the full-model fit. Its cost is included in the canonical fit timing. All chains still contribute to the results; initialization does not replace convergence diagnostics.
Chain starting values are generated before MCMC and passed via init_params. Keeping pole construction outside the parallel chain compiler avoids a native JAX compiler crash observed with a custom initialization strategy.
def arx_initial_values(training, state_dim, *, max_radius=0.98):
"""SISO ARX estimate in increasing lag order, using training observations only."""
d = state_dim
y, u = np.asarray(training["y"]), np.asarray(training["u"])
if y.ndim != 2 or u.ndim != 2 or y.shape[1] != 1 or u.shape[1] != 1:
raise ValueError("This initializer expects scalar observations and controls")
if d < 1 or len(y) != len(u) or len(y) < 3 * d:
raise ValueError(
"ARX initialization requires at least 3 * state_dim observations"
)
if not (np.isfinite(y).all() and np.isfinite(u).all()):
raise ValueError("ARX initialization requires finite observations and controls")
if not 0 < max_radius < 1:
raise ValueError("max_radius must be between zero and one")
# y[t+d] = -sum_k alpha[k] y[t+k] + sum_k C[k] u[t+k] + error.
y_windows = np.lib.stride_tricks.sliding_window_view(y[:, 0], d + 1)
u_windows = np.lib.stride_tricks.sliding_window_view(u[:, 0], d)[:-1]
design = np.concatenate([-y_windows[:, :d], u_windows], axis=1)
coefficients, _, rank, _ = np.linalg.lstsq(design, y_windows[:, d], rcond=None)
if rank < 2 * d:
raise ValueError(
"ARX design is rank deficient; use more data or richer controls"
)
raw_poles = np.roots(np.r_[1.0, coefficients[:d][::-1]])
radii = np.abs(raw_poles)
# Clip unstable poles radially; keep conjugate pairs and nonzero real roots.
directions = np.ones_like(raw_poles)
np.divide(raw_poles, radii, out=directions, where=radii > 0)
poles = directions * np.clip(radii, min(0.01, max_radius / 2), max_radius)
alpha = np.poly(poles).real[:0:-1]
# Refit the numerator after stabilizing the denominator.
C = np.linalg.lstsq(
u_windows, y_windows[:, d] + y_windows[:, :d] @ alpha, rcond=None
)[0][None, :]
return {
"alpha": alpha,
"C": C,
"raw_poles": raw_poles,
"poles": poles,
}
def arx_initial_parameters(initial, key, chains, *, jitter=0.03):
"""Prepare stable per-chain starts on the host, outside parallel compilation."""
if chains < 1:
raise ValueError("chains must be positive")
poles = initial["poles"]
real = np.abs(poles.imag) < 1e-12
real_poles, upper_poles = poles[real].real, poles[poles.imag > 1e-12]
if len(real_poles) + 2 * len(upper_poles) != len(poles):
raise ValueError("Expected real poles and complete complex-conjugate pairs")
radii = np.r_[np.abs(real_poles), np.abs(upper_poles)]
radius_logits = np.log(radii) - np.log1p(-radii)
fractions = np.clip(np.angle(upper_poles) / np.pi, 1e-6, 1 - 1e-6)
angle_logits = np.log(fractions) - np.log1p(-fractions)
signs = np.where(real_poles < 0, -1.0, 1.0)
n_real = len(real_poles)
C_center = initial["C"]
rng = np.random.default_rng(int(jax.device_get(jr.bits(key, dtype=jnp.uint32))))
starts = {"alpha": [], "C": []}
for _ in range(chains):
radius = 1 / (
1 + np.exp(-(radius_logits + jitter * rng.normal(size=radius_logits.shape)))
)
angle = np.pi / (
1 + np.exp(-(angle_logits + jitter * rng.normal(size=angle_logits.shape)))
)
upper = radius[n_real:] * np.exp(1j * angle)
roots = np.r_[signs * radius[:n_real], upper, upper.conj()]
starts["alpha"].append(np.poly(roots).real[:0:-1])
starts["C"].append(
C_center + jitter * (1 + np.abs(C_center)) * rng.normal(size=C_center.shape)
)
dtype = jnp.asarray(initial["alpha"]).dtype
starts = {
name: jnp.asarray(np.stack(values), dtype=dtype)
for name, values in starts.items()
}
# NumPyro expects a leading chain axis only when num_chains > 1.
return starts if chains > 1 else {name: value[0] for name, value in starts.items()}
def fit(form, key, *, chains=CHAINS, warmup=WARMUP, draws=DRAWS):
common = dict(num_chains=chains, num_warmup=warmup, num_samples=draws)
started = time.perf_counter()
initial, run_kwargs = None, {}
if form == "canonical":
key, initialization_key = jr.split(key)
initial = arx_initial_values(train, STATE_DIM)
starts = arx_initial_parameters(initial, initialization_key, chains)
# alpha and C both have unconstrained real support in the NumPyro model.
run_kwargs["init_params"] = starts
initial["chain_starts"] = {
name: np.asarray(value) for name, value in starts.items()
}
config = NUTSConfig(**common, mcmc_source="numpyro")
inference = MCMCInference(config, partial(inference_model, form=form))
samples = inference.run(
key,
train["t"],
train["y"],
ctrl_times=train["t"],
ctrl_values=train["u"],
**run_kwargs,
)
jax.block_until_ready(samples)
seconds = time.perf_counter() - started
samples = {
name: value.reshape((chains, draws) + value.shape[1:])
for name, value in samples.items()
}
print(
f"{form}: {chains} chains × {draws:,} draws; total fit {seconds / 60:.1f} min",
flush=True,
)
return {
"samples": {k: np.asarray(v) for k, v in samples.items()},
"diagnostics": {
k: np.asarray(v) for k, v in inference.get_diagnostics().items()
},
"seconds": seconds,
"initialization": initial,
}
results = {}
results["full"] = fit("full", jr.PRNGKey(2026))
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
full: 8 chains × 1,000 draws; total fit 27.2 min
results["canonical"] = fit("canonical", jr.PRNGKey(2027))
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
0%| | 0/1500 [00:00<?, ?it/s]
canonical: 8 chains × 1,000 draws; total fit 1.1 min
Visualizing the results¶
We now visualize the resulting fitted parameters, including full corner plots, as well as plots projected into canonical coordinates. The markers called MAP below are the highest-density draws across all chains in each model's native coordinates, i.e., the Monte Carloo MAP estimate.
def flatten_chains(samples):
return {
name: value.reshape((-1,) + value.shape[2:]) for name, value in samples.items()
}
def parameter_array(samples, form):
names = ("A", "B", "C") if form == "full" else ("alpha", "C")
leading = samples[names[0]].shape[:2]
values = np.concatenate(
[samples[name].reshape(leading + (-1,)) for name in names], axis=-1
)
d = samples["C"].shape[-1]
labels = (
[f"A{i + 1}{j + 1}" for i in range(d) for j in range(d)]
+ [f"B{i + 1}" for i in range(d)]
if form == "full"
else [rf"$\alpha_{i}$" for i in range(d)]
)
return values, labels + [f"C{i + 1}" for i in range(d)]
def eigen_quantities(parameters, form):
A, _, _ = matrices(parameters, form)
A = np.asarray(A)
eigenvalues = np.linalg.eigvals(A)
return {
"Re_min": eigenvalues.real.min(axis=-1),
"Re_max": eigenvalues.real.max(axis=-1),
"Im_abs_max": np.abs(eigenvalues.imag).max(axis=-1),
"spectral_radius": np.abs(eigenvalues).max(axis=-1),
"trace_A": np.trace(A, axis1=-2, axis2=-1),
"det_A": np.linalg.det(A),
}
def canonical_projection(parameters):
"""Deterministic SISO projection; batch axes are preserved."""
A, B, C = (np.asarray(parameters[name]) for name in ("A", "B", "C"))
d = A.shape[-1]
roots = np.linalg.eigvals(A)
coefficients = np.ones(roots.shape[:-1] + (1,), dtype=complex)
for i in range(d):
coefficients = np.pad(
coefficients, [(0, 0)] * (coefficients.ndim - 1) + [(0, 1)]
) - roots[..., i, None] * np.pad(
coefficients, [(0, 0)] * (coefficients.ndim - 1) + [(1, 0)]
)
alpha = coefficients[..., 1:][..., ::-1].real
Ac = np.asarray(companion(jnp.asarray(alpha)))
W, Wc = [], []
b, bc = B, np.broadcast_to(np.eye(d)[:, -1:], B.shape).copy()
for _ in range(d):
W.append(b)
Wc.append(bc)
b, bc = A @ b, Ac @ bc
W, Wc = np.concatenate(W, axis=-1), np.concatenate(Wc, axis=-1)
if np.any(np.linalg.cond(W) > 1e10):
raise ValueError(
"Canonical projection requires a well-conditioned controllable realization"
)
T = np.linalg.solve(Wc.swapaxes(-1, -2), W.swapaxes(-1, -2)).swapaxes(-1, -2)
return {"alpha": alpha, "C": C @ T}
def posterior_logdensities(samples, form):
flat = {name: jnp.asarray(value) for name, value in flatten_chains(samples).items()}
density = lambda p: log_density(
partial(inference_model, form=form),
(train["t"], train["y"]),
dict(ctrl_times=train["t"], ctrl_values=train["u"]),
p,
)[0]
return np.asarray(
jax.jit(lambda p: jax.lax.map(density, p, batch_size=32))(flat)
).reshape(samples["C"].shape[:2])
posterior_samples = {form: result["samples"] for form, result in results.items()}
maps = {}
for form, result in results.items():
result["logdensity"] = posterior_logdensities(posterior_samples[form], form)
density = result["logdensity"]
best = np.unravel_index(density.argmax(), density.shape)
maps[form] = {name: value[best] for name, value in posterior_samples[form].items()}
print(form, result["diagnostics"])
full {'mean_acceptance_rate': array([0.9464783 , 0.9164947 , 0.9459474 , 0.8944106 , 0.9435442 ,
0.8471197 , 0.92991567, 0.8882415 ], dtype=float32), 'num_divergences': array([0, 0, 0, 0, 0, 0, 0, 0], dtype=int32)}
canonical {'mean_acceptance_rate': array([0.9183586 , 0.9226955 , 0.91948766, 0.931427 , 0.9221092 ,
0.9178776 , 0.9354148 , 0.9134937 ], dtype=float32), 'num_divergences': array([0, 0, 0, 0, 0, 0, 0, 0], dtype=int32)}
TRUTH_CANONICAL = canonical_projection(TRUE)
PROJECTED_FULL_MAP = canonical_projection(maps["full"])
MARKERS = {
"truth": ("#182b3a", "*"),
"MAP": ("#c03a74", "X"),
"projected full MAP": ("#20866f", "P"),
}
def pairplot(values, labels, markers, *, title, color, columns=None):
"""Compact lower-triangle density plot; select columns for larger systems."""
columns = list(range(len(labels))) if columns is None else list(columns)
data = values.reshape(-1, values.shape[-1])
data = data[np.linspace(0, len(data) - 1, min(3500, len(data)), dtype=int)][
:, columns
]
labels = [labels[i] for i in columns]
n = len(labels)
fig, axes = plt.subplots(
n, n, figsize=(max(5, 1.6 * n), max(5, 1.6 * n)), squeeze=False
)
for i in range(n):
for j in range(n):
ax = axes[i, j]
if j > i:
ax.set_visible(False)
continue
if i == j:
sns.kdeplot(
x=data[:, i], fill=True, color=color, ax=ax, warn_singular=False
)
else:
sns.kdeplot(
x=data[:, j],
y=data[:, i],
fill=True,
cmap="mako",
levels=12,
thresh=0.04,
gridsize=90,
ax=ax,
warn_singular=False,
)
for name, point in markers.items():
hue, symbol = MARKERS[name]
point = np.asarray(point)[columns]
if i == j:
ax.axvline(point[i], color=hue, ls="--", lw=1.1)
else:
ax.scatter(
point[j],
point[i],
c=hue,
marker=symbol,
s=58,
edgecolor="white",
linewidth=0.5,
zorder=5,
)
ax.set(
xlabel=labels[j] if i == n - 1 else "",
ylabel=labels[i] if j == 0 and i != j else "",
)
ax.tick_params(labelsize=7, labelbottom=i == n - 1, labelleft=j == 0)
legend = [
Line2D(
[],
[],
marker=MARKERS[name][1],
color=MARKERS[name][0],
linestyle="None",
markersize=8,
label=name,
)
for name in markers
]
fig.legend(
handles=legend, loc="upper right", bbox_to_anchor=(0.96, 0.94), frameon=False
)
fig.suptitle(title, y=1.01)
fig.tight_layout()
plt.show()
for form in ("full", "canonical"):
values, labels = parameter_array(posterior_samples[form], form)
point_array = lambda p: parameter_array(
{k: np.asarray(v)[None, None] for k, v in p.items()}, form
)[0][0, 0]
markers = {
"truth": point_array(TRUE if form == "full" else TRUTH_CANONICAL),
"MAP": point_array(maps[form]),
}
if form == "canonical":
markers["projected full MAP"] = point_array(PROJECTED_FULL_MAP)
pairplot(
values,
labels,
markers,
title=f"{form.capitalize()} parameter posterior",
color=COLORS[form],
columns=range(min(8, len(labels))),
)
# Raw draws confirm that disconnected KDE regions are present in the sampled geometry.
if STATE_DIM >= 2:
values, labels = parameter_array(posterior_samples["full"], "full")
pairs = [
(1, STATE_DIM),
(STATE_DIM**2, STATE_DIM**2 + 1),
(STATE_DIM**2 + STATE_DIM, STATE_DIM**2 + STATE_DIM + 1),
]
fig, axes = plt.subplots(1, 3, figsize=(12, 3.4))
for ax, (i, j) in zip(axes, pairs):
for c, chain in enumerate(values):
ax.scatter(
chain[::10, i], chain[::10, j], s=3, alpha=0.2, label=f"chain {c}"
)
ax.set(xlabel=labels[i], ylabel=labels[j])
axes[0].legend(markerscale=3, fontsize=8)
fig.suptitle("Full posterior geometry · draws colored by chain")
fig.tight_layout()
plt.show()
common = {
"full": canonical_projection(posterior_samples["full"]),
"canonical": posterior_samples["canonical"],
}
common_labels = parameter_array(common["canonical"], "canonical")[1]
fig, axes = plt.subplots(
1, len(common_labels), figsize=(3.3 * len(common_labels), 3.2), squeeze=False
)
for i, ax in enumerate(axes[0]):
for form in common:
values, _ = parameter_array(common[form], "canonical")
sns.kdeplot(x=values[..., i].ravel(), color=COLORS[form], label=form, ax=ax)
for name, point in {
"truth": TRUTH_CANONICAL,
"MAP": maps["canonical"],
"projected full MAP": PROJECTED_FULL_MAP,
}.items():
v = parameter_array(
{k: np.asarray(x)[None, None] for k, x in point.items()}, "canonical"
)[0][0, 0, i]
ax.axvline(v, color=MARKERS[name][0], ls="--", lw=1, label=name)
ax.set(xlabel=common_labels[i], ylabel="density")
axes[0, 0].legend(fontsize=7)
fig.suptitle(
"Full posterior projected to canonical coordinates versus the canonical fit"
)
fig.tight_layout()
plt.show()
quantities = {form: eigen_quantities(posterior_samples[form], form) for form in posterior_samples}
truth_qoi = eigen_quantities(TRUE, "full")
map_qoi = {form: eigen_quantities(maps[form], form) for form in maps}
fig, axes = plt.subplots(2, 3, figsize=(12, 6))
for ax, name in zip(axes.flat, truth_qoi):
for form in quantities:
sns.kdeplot(
x=quantities[form][name].ravel(), color=COLORS[form], label=form, ax=ax
)
ax.axvline(
map_qoi[form][name], color=COLORS[form], ls=":", lw=1.2, label=f"{form} MAP"
)
ax.axvline(truth_qoi[name], color=MARKERS["truth"][0], ls="--", label="truth")
ax.set(xlabel=name, ylabel="density")
axes[0, 0].legend(fontsize=8)
fig.suptitle(
"Eigenvalue quantities of interest · computed separately for every posterior draw"
)
fig.tight_layout()
plt.show()
rows = []
for form in posterior_samples:
values, labels = parameter_array(posterior_samples[form], form)
variables = dict(zip(labels, np.moveaxis(values, -1, 0))) | quantities[form]
for name, value in variables.items():
ess = float(az.ess(value, method="bulk"))
rows.append(
dict(
form=form,
quantity=name,
mean=value.mean(),
sd=value.std(),
rhat=float(az.rhat(value, method="rank")),
ess_bulk=ess,
ess_per_second=ess / results[form]["seconds"],
)
)
summary = pd.DataFrame(rows)
display(summary.round(4))
display(
summary[summary.quantity.isin(truth_qoi)]
.pivot(index="quantity", columns="form", values="ess_per_second")
.round(3)
)
| form | quantity | mean | sd | rhat | ess_bulk | ess_per_second | |
|---|---|---|---|---|---|---|---|
| 0 | full | A11 | 0.3931 | 0.4047 | 1.0045 | 1750.3611 | 1.0717 |
| 1 | full | A12 | 0.2393 | 1.0174 | 1.6447 | 13.0124 | 0.0080 |
| 2 | full | A21 | -0.2407 | 1.0350 | 1.6435 | 13.0141 | 0.0080 |
| 3 | full | A22 | 0.3889 | 0.4046 | 1.0046 | 1753.5189 | 1.0737 |
| 4 | full | B1 | 0.0343 | 0.8964 | 1.0114 | 709.4968 | 0.4344 |
| 5 | full | B2 | -0.0941 | 0.9037 | 1.0052 | 724.4487 | 0.4436 |
| 6 | full | C1 | 0.0486 | 0.7023 | 1.0092 | 751.7685 | 0.4603 |
| 7 | full | C2 | -0.0539 | 0.6967 | 1.0096 | 755.3878 | 0.4625 |
| 8 | full | Re_min | 0.3910 | 0.0064 | 1.0003 | 8429.0648 | 5.1610 |
| 9 | full | Re_max | 0.3910 | 0.0064 | 1.0003 | 8429.0648 | 5.1610 |
| 10 | full | Im_abs_max | 0.7869 | 0.0072 | 1.0005 | 8112.7100 | 4.9673 |
| 11 | full | spectral_radius | 0.8787 | 0.0071 | 1.0009 | 8222.0497 | 5.0343 |
| 12 | full | trace_A | 0.7820 | 0.0129 | 1.0003 | 8429.0702 | 5.1610 |
| 13 | full | det_A | 0.7721 | 0.0125 | 1.0009 | 8222.0521 | 5.0343 |
| 14 | canonical | $\alpha_0$ | 0.7710 | 0.0128 | 1.0010 | 6699.3247 | 101.3222 |
| 15 | canonical | $\alpha_1$ | -0.7869 | 0.0130 | 1.0008 | 7253.6895 | 109.7065 |
| 16 | canonical | C1 | -1.1750 | 0.0305 | 1.0014 | 6204.5280 | 93.8387 |
| 17 | canonical | C2 | 0.6990 | 0.0323 | 1.0005 | 6208.7205 | 93.9022 |
| 18 | canonical | Re_min | 0.3935 | 0.0065 | 1.0008 | 7253.6900 | 109.7065 |
| 19 | canonical | Re_max | 0.3935 | 0.0065 | 1.0008 | 7253.6900 | 109.7065 |
| 20 | canonical | Im_abs_max | 0.7849 | 0.0075 | 1.0004 | 7801.4945 | 117.9916 |
| 21 | canonical | spectral_radius | 0.8780 | 0.0073 | 1.0010 | 6699.2932 | 101.3217 |
| 22 | canonical | trace_A | 0.7869 | 0.0130 | 1.0008 | 7253.6900 | 109.7065 |
| 23 | canonical | det_A | 0.7710 | 0.0128 | 1.0010 | 6699.3247 | 101.3222 |
| form | canonical | full |
|---|---|---|
| quantity | ||
| Im_abs_max | 117.992 | 4.967 |
| Re_max | 109.707 | 5.161 |
| Re_min | 109.707 | 5.161 |
| det_A | 101.322 | 5.034 |
| spectral_radius | 101.322 | 5.034 |
| trace_A | 109.707 | 5.161 |
Testing the learned parameters on unseen data¶
We now test both fitted models on the rest of the data, using the learned parameters. This includes a test on filtering performance, as well as a forecasting test.
The rows compare three ways to use the learned parameters: their posterior mean, the approximate MAP draw, and an ensemble of 100 posterior draws sampled across all chains. Predicting with complete draws preserves each system; averaging full-model matrices can cancel equivalent internal coordinate representations.
Both columns predict noisy observations $y_t$, so their uncertainty bands have the same meaning. Shading shows pointwise 90% and 50% predictive intervals. The score table evaluates the two test segments separately.
def test_predictions(parameters, form, key, *, n_simulations):
"""Filter the observed prefix and let Dynestyx simulate the forecast."""
prediction_key, filtering_key = jr.split(key)
predictive = Predictive(
partial(model, form=form, state_dim=parameters["C"].shape[-1]),
posterior_samples=parameters,
return_sites=(
"system_filtered_states_mean",
"system_filtered_states_cov",
"system_predicted_observations",
),
)
with dsx.DiscreteTimeSimulator(n_simulations=n_simulations):
with dsx.Filter(
KFConfig(
filter_source="cd_dynamax",
record_filtered_states_mean=True,
record_filtered_states_cov=True,
)
):
output = predictive(
prediction_key,
obs_times=data["t"][:FORECAST_START],
obs_values=data["y"][:FORECAST_START],
ctrl_times=data["t"],
ctrl_values=data["u"],
predict_times=data["t"][FORECAST_START - 1 :],
)
# Predict y_t from the state filtered at t-1, BEFORE revealing y_t.
previous = slice(N_TRAIN - 1, FORECAST_START - 1)
means = output["system_filtered_states_mean"][:, previous]
covariances = output["system_filtered_states_cov"][:, previous]
A, B, _ = matrices(parameters, form)
C = parameters["C"][:, 0, :]
controls = data["u"][previous]
state_mean = jnp.einsum("nij,ntj->nti", A, means) + jnp.einsum(
"...ij,tj->...ti", B, controls
)
observation_mean = jnp.einsum("ni,nti->nt", C, state_mean)
CA = jnp.einsum("ni,nij->nj", C, A)
observation_variance = (
jnp.einsum("ni,ntij,nj->nt", CA, covariances, CA)
+ 0.09 * (C**2).sum(axis=-1)[:, None]
+ 0.25
)
# Independent marginal draws suffice for pointwise intervals and scores.
noise = jr.normal(filtering_key, (len(C), n_simulations, N_FILTER_TEST))
filtering_draws = observation_mean[:, None, :] + noise * jnp.sqrt(
jnp.maximum(observation_variance[:, None, :], 0)
)
# The simulator includes the last observed time as its initial anchor.
forecast_draws = output["system_predicted_observations"][:, :, 1:, 0]
draws = jnp.concatenate([filtering_draws, forecast_draws], axis=-1)
return np.asarray(draws).reshape(-1, N_FILTER_TEST + N_FORECAST_TEST)
predictions = {}
for form in posterior_samples:
flat = flatten_chains(posterior_samples[form])
count = len(flat["C"])
# Spread ensemble members across every chain.
index = np.linspace(0, count - 1, min(100, count), dtype=int)
ensembles = {
"Posterior-mean parameters": {
k: v.mean(axis=0, keepdims=True) for k, v in flat.items()
},
"Approximate MAP parameters": {k: v[None] for k, v in maps[form].items()},
"Posterior ensemble": {k: v[index] for k, v in flat.items()},
}
for panel, parameters in ensembles.items():
predictions[panel, form] = test_predictions(
{k: jnp.asarray(v) for k, v in parameters.items()},
form,
jr.PRNGKey(3000 + len(predictions)),
n_simulations=10 if panel == "Posterior ensemble" else 1000,
)
test_segments = {
"Filtering test": (filtering_test, slice(0, N_FILTER_TEST)),
"Forecasting test": (forecasting_test, slice(N_FILTER_TEST, None)),
}
fig, axes = plt.subplots(3, 2, figsize=(13, 8), sharex="col", sharey=True)
for row, panel in enumerate(ensembles):
for column, (stage, (segment, selection)) in enumerate(test_segments.items()):
ax = axes[row, column]
ax.plot(
segment["t"],
segment["signal"],
"--",
color="#222222",
lw=1.2,
label="true signal",
)
ax.scatter(
segment["t"],
segment["y"][:, 0],
s=7,
alpha=0.4,
color="#5f5f5f",
label="observations" if column == 0 else "withheld observations",
)
for form in posterior_samples:
draws = predictions[panel, form][:, selection]
lo90, lo50, hi50, hi90 = np.quantile(
draws, [0.05, 0.25, 0.75, 0.95], axis=0
)
ax.fill_between(
segment["t"], lo90, hi90, color=COLORS[form], alpha=0.12, linewidth=0
)
ax.fill_between(
segment["t"], lo50, hi50, color=COLORS[form], alpha=0.22, linewidth=0
)
ax.plot(
segment["t"], draws.mean(axis=0), color=COLORS[form], lw=1.5, label=form
)
ax.grid(False)
ax.tick_params(direction="out")
if row == 0:
ax.set_title(stage)
if column == 0:
ax.set_ylabel(f"{panel}\nobservation $y_t$")
if row == 2:
ax.set_xlabel("time step")
handles, labels = axes[0, 0].get_legend_handles_labels()
fig.legend(handles, labels, loc="upper center", ncol=4, frameon=False)
fig.tight_layout(rect=(0, 0, 1, 0.96))
figure_dir = Path("figures")
figure_dir.mkdir(exist_ok=True)
fig.savefig(figure_dir / "lti_unseen_data.png", dpi=300, bbox_inches="tight")
fig.savefig(figure_dir / "lti_unseen_data.pdf", bbox_inches="tight")
plt.show()
def prediction_scores(draws, observed):
mean = draws.mean(axis=0)
lo, hi = np.quantile(draws, [0.05, 0.95], axis=0)
ordered = np.sort(draws, axis=0)
n = len(draws)
# Empirical CRPS: E|Y-y| - 1/2 E|Y-Y'|, evaluated without a quadratic pair matrix.
weights = (2 * np.arange(1, n + 1) - n - 1)[:, None]
crps = (
np.abs(draws - observed).mean(axis=0) - (weights * ordered).sum(axis=0) / n**2
)
return dict(
RMSE=np.sqrt(np.mean((mean - observed) ** 2)),
CRPS=crps.mean(),
coverage_90=np.mean((observed >= lo) & (observed <= hi)),
interval_width=(hi - lo).mean(),
)
scores = pd.DataFrame(
[
dict(
stage=stage,
panel=panel,
form=form,
**prediction_scores(draws[:, selection], np.asarray(segment["y"])[:, 0]),
)
for stage, (segment, selection) in test_segments.items()
for (panel, form), draws in predictions.items()
]
)
display(scores.round(4))
| stage | panel | form | RMSE | CRPS | coverage_90 | interval_width | |
|---|---|---|---|---|---|---|---|
| 0 | Filtering test | Posterior-mean parameters | full | 2.3573 | 1.6218 | 0.305 | 1.6429 |
| 1 | Filtering test | Approximate MAP parameters | full | 0.7931 | 0.4514 | 0.880 | 2.4319 |
| 2 | Filtering test | Posterior ensemble | full | 0.7917 | 0.4507 | 0.895 | 2.4410 |
| 3 | Filtering test | Posterior-mean parameters | canonical | 0.7948 | 0.4516 | 0.885 | 2.5310 |
| 4 | Filtering test | Approximate MAP parameters | canonical | 0.7940 | 0.4511 | 0.885 | 2.5186 |
| 5 | Filtering test | Posterior ensemble | canonical | 0.7895 | 0.4488 | 0.885 | 2.5359 |
| 6 | Forecasting test | Posterior-mean parameters | full | 2.3565 | 1.7134 | 0.240 | 1.6409 |
| 7 | Forecasting test | Approximate MAP parameters | full | 1.0784 | 0.6191 | 0.820 | 2.8944 |
| 8 | Forecasting test | Posterior ensemble | full | 1.0817 | 0.6195 | 0.840 | 2.9609 |
| 9 | Forecasting test | Posterior-mean parameters | canonical | 1.0686 | 0.6098 | 0.850 | 3.0230 |
| 10 | Forecasting test | Approximate MAP parameters | canonical | 1.0709 | 0.6124 | 0.830 | 3.0473 |
| 11 | Forecasting test | Posterior ensemble | canonical | 1.0582 | 0.6041 | 0.870 | 3.0838 |