|
| 1 | +"""Nonlinear state-space model representation for EKF/UKF.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from typing import Callable |
| 7 | + |
| 8 | +import jax |
| 9 | +import jax.numpy as jnp |
| 10 | +from jax import Array |
| 11 | + |
| 12 | +from dynaris.core.types import GaussianState |
| 13 | + |
| 14 | +# Type aliases for transition and observation functions. |
| 15 | +# transition_fn: (state_vec,) -> predicted_state_vec |
| 16 | +# observation_fn: (state_vec,) -> predicted_observation_vec |
| 17 | +TransitionFn = Callable[[Array], Array] |
| 18 | +ObservationFn = Callable[[Array], Array] |
| 19 | + |
| 20 | + |
| 21 | +@dataclass(frozen=True) |
| 22 | +class NonlinearSSM: |
| 23 | + """Nonlinear state-space model for use with the Extended Kalman Filter. |
| 24 | +
|
| 25 | + State equation: theta_t = f(theta_{t-1}) + omega_t, omega_t ~ N(0, Q) |
| 26 | + Observation eq: Y_t = h(theta_t) + nu_t, nu_t ~ N(0, R) |
| 27 | +
|
| 28 | + The Jacobians of f and h are computed automatically via ``jax.jacfwd``, |
| 29 | + so no manual derivation is required. |
| 30 | +
|
| 31 | + Attributes: |
| 32 | + transition_fn: f, maps state (n,) -> state (n,). |
| 33 | + observation_fn: h, maps state (n,) -> observation (m,). |
| 34 | + transition_cov: Q, evolution noise covariance, shape (n, n). |
| 35 | + observation_cov: R, observation noise covariance, shape (m, m). |
| 36 | + state_dim: Dimension of the state vector. |
| 37 | + obs_dim: Dimension of the observation vector. |
| 38 | + """ |
| 39 | + |
| 40 | + transition_fn: TransitionFn |
| 41 | + observation_fn: ObservationFn |
| 42 | + transition_cov: Array # Q: (n, n) |
| 43 | + observation_cov: Array # R: (m, m) |
| 44 | + state_dim: int |
| 45 | + obs_dim: int |
| 46 | + |
| 47 | + # --- Short aliases --- |
| 48 | + |
| 49 | + @property |
| 50 | + def Q(self) -> Array: # noqa: N802 |
| 51 | + """Evolution / transition noise covariance.""" |
| 52 | + return self.transition_cov |
| 53 | + |
| 54 | + @property |
| 55 | + def R(self) -> Array: # noqa: N802 |
| 56 | + """Observation noise covariance.""" |
| 57 | + return self.observation_cov |
| 58 | + |
| 59 | + @property |
| 60 | + def f(self) -> TransitionFn: |
| 61 | + """Transition function alias.""" |
| 62 | + return self.transition_fn |
| 63 | + |
| 64 | + @property |
| 65 | + def h(self) -> ObservationFn: |
| 66 | + """Observation function alias.""" |
| 67 | + return self.observation_fn |
| 68 | + |
| 69 | + # --- Factory methods --- |
| 70 | + |
| 71 | + def initial_state( |
| 72 | + self, |
| 73 | + mean: Array | None = None, |
| 74 | + cov: Array | None = None, |
| 75 | + ) -> GaussianState: |
| 76 | + """Create a default initial GaussianState for this model. |
| 77 | +
|
| 78 | + Args: |
| 79 | + mean: Initial state mean. Defaults to zeros. |
| 80 | + cov: Initial state covariance. Defaults to 1e6 * I (diffuse prior). |
| 81 | +
|
| 82 | + Returns: |
| 83 | + GaussianState with the specified or default initial conditions. |
| 84 | + """ |
| 85 | + n = self.state_dim |
| 86 | + if mean is None: |
| 87 | + mean = jnp.zeros(n) |
| 88 | + if cov is None: |
| 89 | + cov = jnp.eye(n) * 1e6 |
| 90 | + return GaussianState(mean=mean, cov=cov) |
| 91 | + |
| 92 | + def __repr__(self) -> str: |
| 93 | + return f"NonlinearSSM(state_dim={self.state_dim}, obs_dim={self.obs_dim})" |
| 94 | + |
| 95 | + # --- JAX pytree registration --- |
| 96 | + |
| 97 | + def tree_flatten(self) -> tuple[list[Array], dict[str, object]]: |
| 98 | + """Flatten into JAX pytree leaves and auxiliary data.""" |
| 99 | + leaves = [self.transition_cov, self.observation_cov] |
| 100 | + aux = { |
| 101 | + "transition_fn": self.transition_fn, |
| 102 | + "observation_fn": self.observation_fn, |
| 103 | + "state_dim": self.state_dim, |
| 104 | + "obs_dim": self.obs_dim, |
| 105 | + } |
| 106 | + return leaves, aux |
| 107 | + |
| 108 | + @classmethod |
| 109 | + def tree_unflatten( |
| 110 | + cls, aux_data: dict[str, object], children: list[Array] |
| 111 | + ) -> NonlinearSSM: |
| 112 | + """Reconstruct from JAX pytree leaves.""" |
| 113 | + return cls( |
| 114 | + transition_fn=aux_data["transition_fn"], # type: ignore[arg-type] |
| 115 | + observation_fn=aux_data["observation_fn"], # type: ignore[arg-type] |
| 116 | + transition_cov=children[0], |
| 117 | + observation_cov=children[1], |
| 118 | + state_dim=aux_data["state_dim"], # type: ignore[arg-type] |
| 119 | + obs_dim=aux_data["obs_dim"], # type: ignore[arg-type] |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +jax.tree_util.register_pytree_node_class(NonlinearSSM) |
0 commit comments