geossm.ssm.StateSpaceModel¶
- class geossm.ssm.StateSpaceModel(H=None, R=None, F=None, Q=None, x0=None, Sigma0=None, Xbeta=None, beta=None, xbeta_names=None, backend='auto', dtype=<class 'jax.numpy.float32'>)[source]¶
Bases:
objectA linear-Gaussian state-space model with Kalman filtering, smoothing and simulation capabilities.
Implements
\[\begin{split}y_t &= H x_t + X_t \beta + e_t, \qquad e_t \sim N(0, R) \\ x_t &= F x_{t-1} + \eta_t, \qquad \eta_t \sim N(0, Q) \\ x_0 &\sim N(x_0, \Sigma_0)\end{split}\]where
y_tis the \(p\)-dimensional observation at timet,x_tis the \(q\)-dimensional latent state, andX_t \betais a regression/fixed-effect term with \(b\) coefficients. The class stores the system matrices and dimensions, and exposes filter, smoother, estimate (filter + smoother), sim (simulation) and predict on top of the JIT-compiled JAX kernels defined at module level (_filter_kernelJAX, _smoother_kernelJAX, _sim_kernelJAX, …).This class is meant to be reusable/subclassable: for example
LRStateSpaceModel(ingeossm.stmodel.lrssm) subclasses it to specialize the model for large-scale spatial data via a low-rank state covariance, while reusing the filter/smoother machinery defined here.Notes
FandRare restricted to diagonal matrices in this implementation (stored internally as their 1D diagonal - see _prepare_diag_array);HandQare general dense matrices.Methods
Compute predicted observations and EM sufficient statistics from a set of state means/covariances (filtered or smoothed).
Run the Kalman filter and smoother (filter + backward pass) and return the smoothed results, including the sufficient statistics (
S11,S10,S00) needed by an EM M-step.Run the Kalman filter forward pass and return the filtered results (state estimates, log-likelihood, and one-step-ahead predicted observations/sufficient statistics computed from the filtered - not smoothed - states).
Build the two small key/value tables (model identity/config on the left, shape and mean system-matrix diagonals on the right) used by summary.
Compute predicted observations and their state-driven predictive covariance from (typically smoothed) states.
Update any subset of the model's system matrices/metadata in place.
Simulate a time series from the state-space model.
Run the Kalman filter followed by the RTS backward smoother, and return the smoothed results together with the EM sufficient statistics computed from the smoothed states.
Compute (and optionally print) a compact summary comparing theoretical (model-implied) and empirical (simulation-based) variances of the latent effect, observation noise and response.
Build a
statsmodels-style structured summary of the model configuration (not of fitted results - see StateSpaceResults.summary for that).Attributes
Diagonal of the state transition matrix
F.Observation/design matrix mapping the latent state to the mean of
y_t.Process noise covariance matrix (covariance of
eta_tin the state equation).Diagonal of the observation noise covariance matrix
R(variance ofe_tper observed location/variable).Covariance of the initial state \(x_0\).
Number of observed time steps.
Exogenous regressors entering the observation equation as
X_t @ beta.Number of regression coefficients (length of
beta).The JAX compute device this model's arrays and kernels are pinned to (resolved from the
backendconstructor argument via geossm.utils._select_device).Regression coefficients for Xbeta.
Human-readable model order label (used in summaries), e.g. an ARMA-style
"(p, q)"tuple string.Dimension of the measurement/observation equation (number of observed locations/variables).
Free/estimated model parameters; currently an alias for beta (kept as a distinct attribute so subclasses, e.g. an EM/optimization-fitted model, can extend it with additional parameters).
Dimension of params; currently an alias for b.
Names corresponding to params; currently an alias for xbeta_names.
Dimension of the latent state equation (state rank).
(p, q, T)- observation dimension, state dimension and number of time steps.Human-readable model type/family label (used in summaries), e.g.
"Linear (Gaussian)".Mean of the initial state \(x_0\).
Human-readable names for the
bregression terms in Xbeta/beta.Currently-set observed data (may contain
NaNfor missing entries).Name of the dependent variable
y, used in summaries.- __call__(y_t)[source]¶
Shorthand for smoother, i.e. run the Kalman filter followed by the RTS smoother (“estimation” of the state).
- Parameters:
y_t (
array_like,shape (p,T)) – Observed data (may containNaNfor missing entries).- Returns:
StateSpaceResults– Smoothed results; see smoother.
- set(H=None, R=None, F=None, Q=None, x0=None, Sigma0=None, y_t=None, Xbeta=None, beta=None, xbeta_names=None, yname=None)[source]¶
Update any subset of the model’s system matrices/metadata in place.
Only arguments that are not
Noneare updated; omitted ones keep their current value. This is the method the constructor and every public entry point (filter, smoother, sim, …) use internally to merge caller-supplied overrides into the model before running a kernel. No shape/consistency validation is performed here - see _check_parameters for that.- Parameters:
H (
array_like,shape (p,q), optional) – Observation/design matrix.R (
array_like,shape (p,p)or(p,), optional) – Observation noise covariance (diagonal).F (
array_like,shape (q,q)or(q,), optional) – State transition matrix (diagonal).Q (
array_like,shape (q,q), optional) – Process noise covariance.x0 (
array_like,shape (q,), optional) – Initial state mean.Sigma0 (
array_like,shape (q,q), optional) – Initial state covariance.y_t (
array_like,shape (p,T), optional) – Observed data.Xbeta (
array_like,shape (p,b,T), optional) – Exogenous regressors.beta (
array_like,shape (b,), optional) – Regression coefficients forXbeta.xbeta_names (
listofstr, optional) – Names for the exogenous regression terms.yname (
str, optional) – Name for the dependent variable (used in summaries).
- Returns:
None
- property backend¶
The JAX compute device this model’s arrays and kernels are pinned to (resolved from the
backendconstructor argument via geossm.utils._select_device).- Type:
jax.Device
- estimate(y_t, yname=None, H=None, R=None, F=None, Q=None, x0=None, Sigma0=None, Xbeta=None, beta=None, xbeta_names=None, light=False)[source]¶
Run the Kalman filter and smoother (filter + backward pass) and return the smoothed results, including the sufficient statistics (
S11,S10,S00) needed by an EM M-step.This is equivalent to smoother - kept as a distinct, named entry point for API compatibility - and does not recompute
S11/S10/S00/y_hata second time on top of what smoother already computes.- Parameters:
y_t (
array_like,shape (p,T)) – Observed data (may containNaNfor missing entries).yname (
str, optional) – Name for the dependent variable (used in summaries).H (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
R (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
F (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Q (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
x0 (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Sigma0 (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Xbeta (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
beta (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
xbeta_names (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
light (
bool, defaultFalse) – Forwarded to smoother; if True, the returned StateSpaceResults omits the filter-stage arrays (x_filtered,P_filtered,K,x_pred,P_pred).
- Returns:
StateSpaceResults– Smoothed results, including filtered arrays (unlesslight=True), smoothed arrays, the log-likelihood and the EM sufficient statistics.
- predict(H, x_T, P_T, Xbeta=None, beta=None)[source]¶
Compute predicted observations and their state-driven predictive covariance from (typically smoothed) states.
Thin wrapper around _compute_predict_kernel_JAX; see its docstring for the exact equations. Unlike filter/smoother, this does not update the model’s own state (
H,Xbeta,betaare used as passed, not read fromself).- Parameters:
H (
array_like,shape (p,q)) – Observation matrix.x_T (
array_like,shape (q,T+1)) – State means (index 0 is the prior/initial state and is excluded from the output).P_T (
array_like,shape (q,q,T+1)) – State covariances (index 0 excluded from the output).Xbeta (
array_like,shape (p,b,T), optional) – Exogenous regressors.beta (
array_like,shape (b,), optional) – Regression coefficients forXbeta.
- Returns:
y_hat (
jax.numpy.ndarray,shape (p,T)) – Predicted observation means.Sigma_y_hat (
jax.numpy.ndarray,shape (p,p,T)) – State-driven predictive covariance \(H P_{t} H^T\) (excludes the observation noise covarianceR).
- filter(y_t, yname=None, H=None, R=None, F=None, Q=None, x0=None, Sigma0=None, Xbeta=None, beta=None, xbeta_names=None)[source]¶
Run the Kalman filter forward pass and return the filtered results (state estimates, log-likelihood, and one-step-ahead predicted observations/sufficient statistics computed from the filtered - not smoothed - states).
See _filter_kernelJAX for the exact filter recursion (predict/update, Woodbury identity, missing-data handling).
- Parameters:
y_t (
array_like,shape (p,T)) – Observed data (may containNaNfor missing entries).yname (
str, optional) – Name for the dependent variable (used in summaries).H (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
R (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
F (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Q (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
x0 (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Sigma0 (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Xbeta (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
beta (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
xbeta_names (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
- Returns:
StateSpaceResults– Filtered results: filtered/predicted state means and covariances, the Kalman gain, the log-likelihood, and the EM sufficient statistics computed from the filtered states.- Return type:
tuple
References
Durbin, J., & Koopman, S. J. (2012). Time Series Analysis by State Space Methods. Oxford University Press.
- smoother(y_t, yname=None, H=None, R=None, F=None, Q=None, x0=None, Sigma0=None, Xbeta=None, beta=None, xbeta_names=None, light=False)[source]¶
Run the Kalman filter followed by the RTS backward smoother, and return the smoothed results together with the EM sufficient statistics computed from the smoothed states.
This is the main “estimation” entry point (filtering and smoothing algorithm for linear-Gaussian state-space models); see _filter_kernelJAX and _smoother_kernelJAX for the exact recursions.
- Parameters:
y_t (
array_like,shape (p,T)) – Observed data (may containNaNfor missing entries).yname (
str, optional) – Name for the dependent variable (used in summaries).H (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
R (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
F (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Q (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
x0 (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Sigma0 (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Xbeta (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
beta (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
xbeta_names (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
light (
bool, defaultFalse) – If True, the returned StateSpaceResults omits the filter-stage arrays (x_filtered,P_filtered,K,x_pred,P_pred) instead of keeping numpy copies of them alive for the whole lifetime of the results object. They are only used internally by this method (as smoother-kernel inputs); once the smoothed states are computed they serve no further purpose for callers - such as an EM loop - that never read them back. The defaultFalsepreserves the previous behaviour for callers (e.g.model.smoother(y)used directly) that do want to inspect filtered vs. smoothed state.
- Returns:
StateSpaceResults– Smoothed results: smoothed state means/covariances and lag-one covariances, the log-likelihood, predicted observations and the EM sufficient statistics (S11,S10,S00) computed from the smoothed states, plus (unlesslight=True) the filter-stage arrays.- Return type:
tuple
References
Durbin, J., & Koopman, S. J. (2012). Time Series Analysis by State Space Methods. Oxford University Press.
- computeExpectedValues(x_T, P_T, P_T_1)[source]¶
Compute predicted observations and EM sufficient statistics from a set of state means/covariances (filtered or smoothed).
Thin, timed wrapper around _compute_expected_values_kernelJAX; see its docstring for the exact equations for
y_hat,S11,S10andS00. Called internally by both filter (on the filtered states) and smoother (on the smoothed states).- Parameters:
x_T (
array_like,shape (q,T+1)) – State means (index 0 is the initial prior).P_T (
array_like,shape (q,q,T+1)) – State covariances.P_T_1 (
array_like,shape (q,q,T+1)) – Lag-one covariances \(\mathrm{Cov}(x_t, x_{t-1})\) (as produced by _smoother_kernelJAX; when called on filtered-only states this argument is typically not meaningful/available in the same sense).
- Returns:
y_hat (
jax.numpy.ndarray,shape (p,T)) – Predicted observation means.S11, S10, S00 (
jax.numpy.ndarray,shape (q,q)) – EM sufficient statistics.tDelta (
float) – Wall-clock time (seconds) spent computing these quantities.
- Return type:
tuple
- sim(seed=1234, R=None, F=None, H=None, Q=None, x0=None, Sigma0=None, Xbeta=None, beta=None, block_p=None, block_q=None, stats=True, verbose=False)[source]¶
Simulate a time series from the state-space model.
Draws \(x_0 \sim N(x_0, \Sigma_0)\) and then propagates the observation/state equations forward in time via _sim_kernelJAX (see its docstring for the exact equations). Uses a Python
for-loop under the hood, so this is not JIT-compiled and is slower than the filter/smoother kernels.- Parameters:
seed (
intorgeossm.utils.KeyStream, default1234) – Either an integer seed used to build a new KeyStream, or an existing KeyStream to continue drawing from.R (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
F (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
H (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Q (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
x0 (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Sigma0 (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
Xbeta (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
beta (optional) – Overrides for the corresponding model parameters; see set. If omitted, the model’s currently-set values are used.
block_p (
listofint, optional) – Block boundaries along the observation dimensionp, forwarded to summarize_ssm_variances whenstats=True(e.g. to separate multiple co-simulated processes). Defaults to a single block[0, p].block_q (
listofint, optional) – Block boundaries along the latent dimensionq, forwarded to summarize_ssm_variances. Defaults to a single block[0, q].stats (
bool, defaultTrue) – If True, additionally compute and return a theoretical-vs- empirical variance summary via summarize_ssm_variances.verbose (
bool, defaultFalse) – If True (andstats=True), print the variance summary.
- Returns:
y_t (
jax.numpy.ndarray,shape (p,T)) – Simulated observations.x_t (
jax.numpy.ndarray,shape (q,T+1)) – Simulated state vectors \([x_0, \dots, x_T]\).stats (
dictorNone) – Output of summarize_ssm_variances ifstats=True, otherwiseNone.tdelta (
float) – Wall-clock time (seconds) spent in the simulation kernel.
- Return type:
Array
- summarize_ssm_variances(x_sim, y_sim, block_p, block_q, decimals=4, verbose=True)[source]¶
Compute (and optionally print) a compact summary comparing theoretical (model-implied) and empirical (simulation-based) variances of the latent effect, observation noise and response.
The theoretical stationary state covariance is obtained by solving the discrete Lyapunov equation \(P = F P F^T + Q\) for \(P\); since
Fis diagonal here, this has the closed form \(P_{ij} = Q_{ij} / (1 - f_i f_j)\) elementwise, avoiding a dense-matrix Lyapunov solve. The theoretical variance of the latent effectH @ x_tis thendiag(H @ P @ H.T), and the theoretical response variance is the latent variance plus the observation noise variance (R); these are compared against the corresponding sample variances ofx_sim/y_sim, averaged within each block ofblock_p/block_q(useful when several processes are stacked/simulated together).- Parameters:
x_sim (
array_like,shape (q,T+1)or(q,T)) – Simulated latent states, as returned by sim.y_sim (
array_like,shape (p,T)) – Simulated observations with the fixed-effect termXbeta @ betaalready removed (i.e. theH @ x_t + e_tpart).block_p (
listofint) – Block boundaries along the observation dimensionp(e.g.[0, p1, p1+p2, ...]) delimiting separate processes for the purpose of averaging.block_q (
listofint) – Block boundaries along the latent dimensionq.decimals (
int, default4) – Currently unused by this implementation.verbose (
bool, defaultTrue) – If True, print a formatted summary table.
- Returns:
dict– Dictionary with keysvar_latent_theoretical,var_latent_empirical,var_noise,var_y_theoretical,var_y_empirical,ratios(a dict of empirical/theoretical ratios) andP(the theoretical stationary state covariance).- Raises:
AttributeError – If the model is missing any of
F,Q,H,R.- Return type:
dict
- property T¶
Number of observed time steps.
- Type:
int
- property p¶
Dimension of the measurement/observation equation (number of observed locations/variables).
- Type:
int
- property q¶
Dimension of the latent state equation (state rank).
- Type:
int
- property b¶
Number of regression coefficients (length of
beta).- Type:
int
- property y_t¶
Currently-set observed data (may contain
NaNfor missing entries).
- property params¶
Free/estimated model parameters; currently an alias for beta (kept as a distinct attribute so subclasses, e.g. an EM/optimization-fitted model, can extend it with additional parameters).
- Type:
array_like
- property params_names¶
Names corresponding to params; currently an alias for xbeta_names.
- Type:
list of str
- property params_dim¶
Dimension of params; currently an alias for b.
- Type:
int
- property xbeta_names¶
Human-readable names for the
bregression terms in Xbeta/beta.- Type:
list of str
- property yname¶
Name of the dependent variable
y, used in summaries.- Type:
str
- property type¶
Human-readable model type/family label (used in summaries), e.g.
"Linear (Gaussian)".- Type:
str
- property order¶
Human-readable model order label (used in summaries), e.g. an ARMA-style
"(p, q)"tuple string.- Type:
str
- property shape¶
(p, q, T)- observation dimension, state dimension and number of time steps.- Type:
tuple of int
- property Xbeta¶
Exogenous regressors entering the observation equation as
X_t @ beta.
- property H¶
Observation/design matrix mapping the latent state to the mean of
y_t.
- property R¶
Diagonal of the observation noise covariance matrix
R(variance ofe_tper observed location/variable).
- property Q¶
Process noise covariance matrix (covariance of
eta_tin the state equation).
- __getstate__()[source]¶
Return a serializable state for pickling.
Convert JAX arrays to NumPy arrays and store basic metadata.
- __setstate__(state)[source]¶
Restore object state from pickled state.
Arrays are converted back to JAX arrays with the original dtype.
- generate_summary()[source]¶
Build the two small key/value tables (model identity/config on the left, shape and mean system-matrix diagonals on the right) used by summary.
- Returns:
tupleoflist–(gen_top_left, gen_top_right), each a list of(label, [value])pairs suitable forstatsmodels.iolib.summary.Summary.add_table_2cols.
- summary()[source]¶
Build a
statsmodels-style structured summary of the model configuration (not of fitted results - see StateSpaceResults.summary for that).- Returns:
statsmodels.iolib.summary.Summary– Printable summary table with model identity/type/order, shape(p, q, T)and mean system-matrix diagonals.- Return type:
Summary