Matrix Lie groups, invariant errors, the invariant EKF and the Deyst-Price conditions
DefinitionInvariantEKF_BasicObjects of Sections 2-3 and Appendices A-C of Barrau and Bonnabel, The invariant extended Kalman filter as a stable observer (arXiv:1410.1465v4).
- Matrices. is the Frobenius norm. means that is positive semidefinite. Derivatives of matrix-valued functions are taken entry by entry.
- Matrix Lie groups (Appendix A). A matrix Lie group is a set of invertible matrices that contains , is closed under inverses and products, and is a closed subgroup of . A linear map is a coordinate map of the Lie algebra if it is injective with range . The group exponential is .
- Systems and errors (Section 2). A trajectory of on a time set takes values in and has the derivative at every (one-sided at an endpoint). The left- and right-invariant errors are and (eqs. 5-6). An error is state-trajectory independent (Definition 1) if a single function gives for every pair of trajectories. The system is group affine (eq. 12) if for all and . The error fields are and (eqs. 8-9). A matrix is the linearization of if as . As a standing regularity condition, the field is locally Lipschitz in , uniformly for in each window .
- Simplified car (Section 2.1). For car states and with , the logarithmic error is with . The factor takes its limit value at .
- IEKF (Section 3, eqs. 36-37). The observations (left-invariant) or (right-invariant), , are taken at the times . Between observations the estimate follows , and follows . At the update is (LIEKF) or (RIEKF), with . Here or , the gain is with , and for the LIEKF and for the RIEKF. A filter started at is stored as one propagation segment per sampling interval. Its value at an observation time is the updated value.
- Linear Kalman filter and Deyst-Price conditions (Theorem 3). is the transition matrix of . Conditions (i)-(v) are: ; with ; ; ; and . The bounded transition condition is for in one sampling interval.
- Stability (Definition 2). The distance between the true state and the estimate is measured through the invariant error: for the LIEKF and for the RIEKF. The design matrices are comparable along the true trajectory if for every there is such that an error with gives , and the same for .
Formalization Note The paper does not fix the distance of Definition 2. The invariant-error distance is used because it is the quantity the proof controls. Section 3.3 prints with the opposite sign. With that sign, the linearized update is and the filter diverges. The sign used here is the one of the paper's worked examples (Sections 4.2.3, 4.2.4, 5.2.3). Condition (v) is printed with , , and . It is written here in the form of Deyst and Price.
import Mathlib
/-!
A. Barrau and S. Bonnabel, *The invariant extended Kalman filter as a stable observer*,
arXiv:1410.1465v4 (2015): matrix Lie groups and their exponential coordinates (Appendix A),
systems on matrix Lie groups and their invariant errors (Section 2.2, eqs. (4)-(9) and (12)),
the logarithmic error of the simplified car (Section 2.1, eq. (2)), the invariant extended Kalman
filter with discrete observations and its Riccati equation (Section 3, eqs. (14)-(37)), the
linear Kalman filter and the Deyst-Price conditions of Theorem 3, and the stability notion of
Definition 2 (Section 3.4).
-/
open MeasureTheory Matrix
noncomputable section
namespace InvariantEKF
/-! ### Matrix utilities -/
/-- Frobenius norm of a real matrix, used as the distance on matrices. -/
def frob {m n : Type*} [Fintype m] [Fintype n] (A : Matrix m n ℝ) : ℝ :=
Real.sqrt (∑ i, ∑ j, A i j ^ 2)
/-- The Loewner order `A ⪯ B` of Theorem 3 and Appendix C: `B - A` is positive semidefinite. -/
def LoewnerLE {m : Type*} [Fintype m] (A B : Matrix m m ℝ) : Prop :=
(B - A).PosSemidef
/-- The matrix-valued function `X` has derivative `D` at `t` within `S`, entry by entry. -/
def HasMatDerivWithinAt {m n : Type*} (X : ℝ → Matrix m n ℝ) (D : Matrix m n ℝ) (S : Set ℝ)
(t : ℝ) : Prop :=
∀ i j, HasDerivWithinAt (fun s => X s i j) (D i j) S t
/-- The matrix-valued function `X` has derivative `D` at `t`, entry by entry. -/
def HasMatDerivAt {m n : Type*} (X : ℝ → Matrix m n ℝ) (D : Matrix m n ℝ) (t : ℝ) : Prop :=
∀ i j, HasDerivAt (fun s => X s i j) (D i j) t
/-! ### Matrix Lie groups (Appendix A) -/
/-- Appendix A: a matrix Lie group `G ⊂ ℝ^{N×N}` is a set of invertible matrices containing `Id`
and closed under inversion and products; as a Lie group it is a closed subgroup of `GL_N(ℝ)`
(every limit of elements of `G` that is invertible lies in `G`). -/
def IsMatrixLieGroup {N : ℕ} (G : Set (Matrix (Fin N) (Fin N) ℝ)) : Prop :=
(1 : Matrix (Fin N) (Fin N) ℝ) ∈ G ∧ (∀ a ∈ G, IsUnit a.det ∧ a⁻¹ ∈ G) ∧
(∀ a ∈ G, ∀ b ∈ G, a * b ∈ G) ∧
∀ (x : ℕ → Matrix (Fin N) (Fin N) ℝ) (a : Matrix (Fin N) (Fin N) ℝ),
(∀ n, x n ∈ G) → Filter.Tendsto x Filter.atTop (nhds a) → IsUnit a.det → a ∈ G
/-- Appendix A: `L_𝔤 : ℝ^{dim 𝔤} → 𝔤` is a linear isomorphism onto the Lie algebra `𝔤` of `G`,
the set of matrices `X` with `exp_m(sX) ∈ G` for all real `s`. -/
def IsLieAlgebraCoords {N d : ℕ} (G : Set (Matrix (Fin N) (Fin N) ℝ))
(L : (Fin d → ℝ) →ₗ[ℝ] Matrix (Fin N) (Fin N) ℝ) : Prop :=
Function.Injective L ∧
∀ X : Matrix (Fin N) (Fin N) ℝ, X ∈ LinearMap.range L ↔ ∀ s : ℝ, NormedSpace.exp (s • X) ∈ G
/-- Appendix A: the exponential `exp(ξ) = exp_m(L_𝔤(ξ))` of `ξ ∈ ℝ^{dim 𝔤}`. -/
def expG {N d : ℕ} (L : (Fin d → ℝ) →ₗ[ℝ] Matrix (Fin N) (Fin N) ℝ) (ξ : Fin d → ℝ) :
Matrix (Fin N) (Fin N) ℝ :=
NormedSpace.exp (L ξ)
/-! ### Systems on matrix Lie groups and invariant errors (Section 2) -/
/-- Eq. (12) (and eq. (14)): the vector fields `f_u` satisfy
`f_u(ab) = f_u(a) b + a f_u(b) - a f_u(Id) b` for every input `u` and all `a, b ∈ G`. -/
def IsGroupAffine {N : ℕ} {U : Type*} (G : Set (Matrix (Fin N) (Fin N) ℝ))
(f : U → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin N) (Fin N) ℝ) : Prop :=
∀ v a b, a ∈ G → b ∈ G → f v (a * b) = f v a * b + a * f v b - a * f v 1 * b
/-- `X` solves `dX/dt = F_t(X_t)` at every time of `S` (derivatives within `S`). -/
def IsSolution {N : ℕ} (F : ℝ → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin N) (Fin N) ℝ)
(S : Set ℝ) (X : ℝ → Matrix (Fin N) (Fin N) ℝ) : Prop :=
∀ t ∈ S, HasMatDerivWithinAt X (F t (X t)) S t
/-- Eq. (4): a trajectory `χ_t` of `dχ_t/dt = f_{u_t}(χ_t)` on the time set `S`, with values
in `G`. -/
def IsTrajectory {N : ℕ} {U : Type*} (G : Set (Matrix (Fin N) (Fin N) ℝ))
(f : U → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin N) (Fin N) ℝ) (u : ℝ → U) (S : Set ℝ)
(χ : ℝ → Matrix (Fin N) (Fin N) ℝ) : Prop :=
(∀ t ∈ S, χ t ∈ G) ∧ IsSolution (fun t => f (u t)) S χ
/-- Eqs. (5)-(6): the left-invariant error `η^L = χ⁻¹ χ̄` (`left = true`) or the
right-invariant error `η^R = χ̄ χ⁻¹` (`left = false`) between `χ` and `χ̄`. -/
def invError {N : ℕ} (left : Bool) (χ χbar : Matrix (Fin N) (Fin N) ℝ) :
Matrix (Fin N) (Fin N) ℝ :=
if left then χ⁻¹ * χbar else χbar * χ⁻¹
/-- Definition 1 (Section 2.2): the left (`left = true`) or right invariant error has a
state-trajectory independent propagation: for one function `g`, the error between any two
trajectories of eq. (4) on `t > 0` satisfies `dη_t/dt = g_{u_t}(η_t)`. -/
def ErrorAutonomous {N : ℕ} {U : Type*} (left : Bool) (G : Set (Matrix (Fin N) (Fin N) ℝ))
(f : U → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin N) (Fin N) ℝ) (u : ℝ → U) : Prop :=
∃ g : U → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin N) (Fin N) ℝ,
∀ χ χbar : ℝ → Matrix (Fin N) (Fin N) ℝ,
IsTrajectory G f u (Set.Ioi 0) χ → IsTrajectory G f u (Set.Ioi 0) χbar →
∀ t, 0 < t → HasMatDerivAt (fun s => invError left (χ s) (χbar s))
(g (u t) (invError left (χ t) (χbar t))) t
/-- Eqs. (8)-(9): `g^L_u(η) = f_u(η) - f_u(Id) η` (`left = true`) and
`g^R_u(η) = f_u(η) - η f_u(Id)` (`left = false`). -/
def errField {N : ℕ} {U : Type*} (left : Bool)
(f : U → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin N) (Fin N) ℝ) (v : U)
(η : Matrix (Fin N) (Fin N) ℝ) : Matrix (Fin N) (Fin N) ℝ :=
if left then f v η - f v 1 * η else f v η - η * f v 1
/-- Standing regularity of the time-dependent vector field `F_t` (needed for "the flow" of
Appendix B to be well defined): on every time window `[0, T]` it is locally Lipschitz in the
state, uniformly in time, for the Frobenius distance. -/
def IsLocLipschitzField {N : ℕ}
(F : ℝ → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin N) (Fin N) ℝ) : Prop :=
∀ (T : ℝ) (X₀ : Matrix (Fin N) (Fin N) ℝ), ∃ r > 0, ∃ K : ℝ,
∀ t ∈ Set.Icc 0 T, ∀ X Y : Matrix (Fin N) (Fin N) ℝ, frob (X - X₀) < r → frob (Y - X₀) < r →
frob (F t X - F t Y) ≤ K * frob (X - Y)
/-- Theorem 2 / eq. (20): `A` is the matrix defined by `g(exp(ξ)) = L_𝔤(A ξ) + O(‖ξ‖²)`. -/
def IsLinearization {N d : ℕ} (L : (Fin d → ℝ) →ₗ[ℝ] Matrix (Fin N) (Fin N) ℝ)
(g : Matrix (Fin N) (Fin N) ℝ → Matrix (Fin N) (Fin N) ℝ)
(A : Matrix (Fin d) (Fin d) ℝ) : Prop :=
∃ C r : ℝ, 0 < r ∧ ∀ ξ : Fin d → ℝ, ‖ξ‖ < r →
∀ i j, |(g (expG L ξ) - L (A *ᵥ ξ)) i j| ≤ C * ‖ξ‖ ^ 2
/-! ### The simplified car of Section 2.1 -/
/-- Eq. (2): the factor `(s/2) a(s) = s sin s / (2(1 - cos s))`, extended by its limit `1`
at `s = 0`. -/
def carCoef (s : ℝ) : ℝ :=
if s = 0 then 1 else s * Real.sin s / (2 * (1 - Real.cos s))
/-- Eq. (2): the non-linear error `ξ_t = (Δθ, ½ Δθ [a(Δθ) I₂ - J₂] R(-θ̄)(X - X̄))` between the
car states `(θ, x¹, x²)` and `(θ̄, x̄¹, x̄²)`, with `Δθ = θ - θ̄`. -/
def carLogError (θ x1 x2 θbar xbar1 xbar2 : ℝ) : Fin 3 → ℝ :=
let s := θ - θbar
let p := Real.cos θbar * (x1 - xbar1) + Real.sin θbar * (x2 - xbar2)
let q := -Real.sin θbar * (x1 - xbar1) + Real.cos θbar * (x2 - xbar2)
![s, carCoef s * p + s / 2 * q, carCoef s * q - s / 2 * p]
/-- Eq. (3): the matrix of the linear error equation of Proposition 1, with the sign of the
`u_t` entry as the proof of Proposition 1 establishes it (see the statement). -/
def carErrorMatrix (uv ω : ℝ) : Matrix (Fin 3) (Fin 3) ℝ :=
!![0, 0, 0; 0, 0, ω; uv, -ω, 0]
/-! ### The invariant extended Kalman filter (Section 3) -/
/-- Eqs. (15) and (22): the observation `Y^i = χ d^i` (left-invariant, `left = true`) or
`Y^i = χ⁻¹ d^i` (right-invariant, `left = false`). -/
def obsY {N : ℕ} (left : Bool) (χ : Matrix (Fin N) (Fin N) ℝ) (v : Fin N → ℝ) : Fin N → ℝ :=
if left then χ *ᵥ v else χ⁻¹ *ᵥ v
/-- Eq. (36): the stacked innovation `(χ̂⁻¹ Y^i - d^i)_i` (LIEKF) or `(χ̂ Y^i - d^i)_i` (RIEKF)
in `ℝ^{kN}`, indexed by `(i, a) ∈ {1..k} × {1..N}`. -/
def innovation {N K : ℕ} (left : Bool) (dvec : Fin K → Fin N → ℝ)
(χhat : Matrix (Fin N) (Fin N) ℝ) (Y : Fin K → Fin N → ℝ) : Fin K × Fin N → ℝ :=
fun p => ((if left then χhat⁻¹ else χhat) *ᵥ Y p.1) p.2 - dvec p.1 p.2
/-- Eq. (36): the update `χ̂⁺ = χ̂ exp(ζ)` (LIEKF) or `χ̂⁺ = exp(ζ) χ̂` (RIEKF), with
`ζ = L_n (innovation)`. -/
def iekfUpdate {N d : ℕ} (left : Bool) (L : (Fin d → ℝ) →ₗ[ℝ] Matrix (Fin N) (Fin N) ℝ)
(χhat : Matrix (Fin N) (Fin N) ℝ) (ζ : Fin d → ℝ) : Matrix (Fin N) (Fin N) ℝ :=
if left then χhat * expG L ζ else expG L ζ * χhat
/-- Section 3.3: the observation matrix `H ∈ ℝ^{kN × dim 𝔤}` of the IEKF, with the sign for which
the linearised innovation is `-Hξ` (the convention of the worked examples of Sections 4.2.3,
4.2.4 and 5.2.3): `Hξ = (L_𝔤(ξ) d^i)_i` for the LIEKF and `Hξ = (-L_𝔤(ξ) d^i)_i` for the RIEKF. -/
def obsH {N d K : ℕ} (left : Bool) (L : (Fin d → ℝ) →ₗ[ℝ] Matrix (Fin N) (Fin N) ℝ)
(dvec : Fin K → Fin N → ℝ) : Matrix (Fin K × Fin N) (Fin d) ℝ :=
fun p j => (if left then (1 : ℝ) else -1) * (L (Pi.single j 1) *ᵥ dvec p.1) p.2
/-- Eq. (37): the Kalman gain `L_n = P H^T S⁻¹` with `S = H P H^T + N`. -/
def kalmanGain {d : ℕ} {ι : Type*} [Fintype ι] [DecidableEq ι] (P : Matrix (Fin d) (Fin d) ℝ)
(H : Matrix ι (Fin d) ℝ) (Nn : Matrix ι ι ℝ) : Matrix (Fin d) ι ℝ :=
P * Hᵀ * (H * P * Hᵀ + Nn)⁻¹
/-- The propagation interval `[max(t₀, t_n), t_{n+1}]` between the observation times
`t_n = n dt`, for a filter started at time `t₀`. -/
def segIcc (t0 dt : ℝ) (n : ℕ) : Set ℝ :=
Set.Icc (max t0 ((n : ℝ) * dt)) (((n : ℝ) + 1) * dt)
/-- The value at time `t` of a filter stored as one propagation segment per interval: the
segment `⌊t / dt⌋` (so the value at an observation time is the updated value). -/
def piecewiseRun {α : Type*} (xs : ℕ → ℝ → α) (dt t : ℝ) : α :=
xs ⌊t / dt⌋₊ t
/-- Eq. (37): `P` solves the Riccati equation `dP/dt = A_t P + P A_tᵀ + Q_t` on each propagation
interval, starts at `P₀` at time `t₀`, and jumps to `P⁺ = (I - L_n H) P` at each observation time
`t_{n+1} = (n+1) dt`, where `L_n` uses the matrix `Nend n` of that observation. -/
def IsRiccatiRun {d : ℕ} {ι : Type*} [Fintype ι] [DecidableEq ι]
(A : ℝ → Matrix (Fin d) (Fin d) ℝ) (H : Matrix ι (Fin d) ℝ)
(Q : ℕ → ℝ → Matrix (Fin d) (Fin d) ℝ) (Nend : ℕ → Matrix ι ι ℝ) (dt t0 : ℝ)
(P0 : Matrix (Fin d) (Fin d) ℝ) (Ps : ℕ → ℝ → Matrix (Fin d) (Fin d) ℝ) : Prop :=
∀ n : ℕ, t0 < ((n : ℝ) + 1) * dt →
((n : ℝ) * dt ≤ t0 → Ps n t0 = P0) ∧
(∀ t ∈ segIcc t0 dt n,
HasMatDerivWithinAt (Ps n) (A t * Ps n t + Ps n t * (A t)ᵀ + Q n t) (segIcc t0 dt n) t) ∧
Ps (n + 1) (((n : ℝ) + 1) * dt) =
(1 - kalmanGain (Ps n (((n : ℝ) + 1) * dt)) H (Nend n) * H) * Ps n (((n : ℝ) + 1) * dt)
/-- Eqs. (36)-(37): a run of the Left (`left = true`) or Right (`left = false`) Invariant EKF
started at time `t₀` from `(χ̂₀, P₀)`, observing the true trajectory `χ` at the times
`t_n = n dt` through eq. (15) or (22). Between observations `χ̂` follows eq. (14) and `P` the
Riccati equation with `A_{u_t}`; the design matrix `Q̂_t` is evaluated at the current estimate,
and `N̂_n` at the estimate just before the update. -/
def IsIEKFRun {N d K : ℕ} {U : Type*} (left : Bool)
(L : (Fin d → ℝ) →ₗ[ℝ] Matrix (Fin N) (Fin N) ℝ)
(f : U → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin N) (Fin N) ℝ) (u : ℝ → U)
(A : U → Matrix (Fin d) (Fin d) ℝ) (dvec : Fin K → Fin N → ℝ) (dt : ℝ)
(Qhat : ℝ → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin d) (Fin d) ℝ)
(Nhat : ℕ → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin K × Fin N) (Fin K × Fin N) ℝ)
(χ : ℝ → Matrix (Fin N) (Fin N) ℝ) (t0 : ℝ) (χhat0 : Matrix (Fin N) (Fin N) ℝ)
(P0 : Matrix (Fin d) (Fin d) ℝ) (xs : ℕ → ℝ → Matrix (Fin N) (Fin N) ℝ)
(Ps : ℕ → ℝ → Matrix (Fin d) (Fin d) ℝ) : Prop :=
IsRiccatiRun (fun t => A (u t)) (obsH left L dvec) (fun n t => Qhat t (xs n t))
(fun n => Nhat (n + 1) (xs n (((n : ℝ) + 1) * dt))) dt t0 P0 Ps ∧
∀ n : ℕ, t0 < ((n : ℝ) + 1) * dt →
((n : ℝ) * dt ≤ t0 → xs n t0 = χhat0) ∧
(∀ t ∈ segIcc t0 dt n, HasMatDerivWithinAt (xs n) (f (u t) (xs n t)) (segIcc t0 dt n) t) ∧
xs (n + 1) (((n : ℝ) + 1) * dt) =
iekfUpdate left L (xs n (((n : ℝ) + 1) * dt))
(kalmanGain (Ps n (((n : ℝ) + 1) * dt)) (obsH left L dvec)
(Nhat (n + 1) (xs n (((n : ℝ) + 1) * dt))) *ᵥ
innovation left dvec (xs n (((n : ℝ) + 1) * dt))
(fun i => obsY left (χ (((n : ℝ) + 1) * dt)) (dvec i)))
/-- Definition 2 for the IEKF: the distance between the true state `χ` and the estimate `χ̂`,
measured through the invariant error, `‖χ⁻¹χ̂ - Id‖` (LIEKF) or `‖χ̂χ⁻¹ - Id‖` (RIEKF). -/
def invErrDist {N : ℕ} (left : Bool) (χ χhat : Matrix (Fin N) (Fin N) ℝ) : ℝ :=
frob (invError left χ χhat - 1)
/-- The estimate `exp(ξ) χ` or `χ exp(ξ)` whose invariant error with `χ` is `exp(ξ)`. -/
def perturbed {N d : ℕ} (left : Bool) (L : (Fin d → ℝ) →ₗ[ℝ] Matrix (Fin N) (Fin N) ℝ)
(χ : Matrix (Fin N) (Fin N) ℝ) (ξ : Fin d → ℝ) : Matrix (Fin N) (Fin N) ℝ :=
if left then χ * expG L ξ else expG L ξ * χ
/-- Appendix C, proof of Lemma 4: the design matrices evaluated at estimates close to the true
trajectory are comparable with their values on the true trajectory: for every `κ > 1` there is
`ε > 0` such that an invariant error `exp(ξ)` with `‖ξ‖ < ε` gives
`κ⁻¹ Q̂(χ_t) ⪯ Q̂(χ̂_t) ⪯ κ Q̂(χ_t)` and `κ⁻¹ N̂_n(χ_{t_n}) ⪯ N̂_n(χ̂_{t_n}) ⪯ κ N̂_n(χ_{t_n})`. -/
def TuningComparable {N d K : ℕ} (left : Bool) (L : (Fin d → ℝ) →ₗ[ℝ] Matrix (Fin N) (Fin N) ℝ)
(χ : ℝ → Matrix (Fin N) (Fin N) ℝ) (dt : ℝ)
(Qhat : ℝ → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin d) (Fin d) ℝ)
(Nhat : ℕ → Matrix (Fin N) (Fin N) ℝ → Matrix (Fin K × Fin N) (Fin K × Fin N) ℝ) : Prop :=
∀ κ : ℝ, 1 < κ → ∃ ε > 0, ∀ ξ : Fin d → ℝ, ‖ξ‖ < ε →
(∀ t : ℝ, 0 ≤ t →
LoewnerLE (κ⁻¹ • Qhat t (χ t)) (Qhat t (perturbed left L (χ t) ξ)) ∧
LoewnerLE (Qhat t (perturbed left L (χ t) ξ)) (κ • Qhat t (χ t))) ∧
(∀ n : ℕ, 1 ≤ n →
LoewnerLE (κ⁻¹ • Nhat n (χ ((n : ℝ) * dt))) (Nhat n (perturbed left L (χ ((n : ℝ) * dt)) ξ)) ∧
LoewnerLE (Nhat n (perturbed left L (χ ((n : ℝ) * dt)) ξ)) (κ • Nhat n (χ ((n : ℝ) * dt))))
/-! ### The linear Kalman filter and the Deyst-Price conditions (Theorem 3) -/
/-- Theorem 3: `Phi s t = Phi_s^t` is the transition matrix of `dx/dt = A_t x`:
`Phi_s^s = I` and `d/dt Phi_s^t = A_t Phi_s^t`. -/
def IsTransitionMatrix {d : ℕ} (A : ℝ → Matrix (Fin d) (Fin d) ℝ)
(Phi : ℝ → ℝ → Matrix (Fin d) (Fin d) ℝ) : Prop :=
∀ s, Phi s s = 1 ∧ ∀ t, HasMatDerivAt (fun r => Phi s r) (A t * Phi s t) t
/-- Theorem 3 (iv): the controllability Gramian `∫_{t_{n-M}}^{t_n} Phi_s^{t_n} Q_s (Phi_s^{t_n})ᵀ ds`,
entry by entry, with `t_n = n dt`. -/
def ctrlGramian {d : ℕ} (dt : ℝ) (Phi : ℝ → ℝ → Matrix (Fin d) (Fin d) ℝ)
(Q : ℝ → Matrix (Fin d) (Fin d) ℝ) (M n : ℕ) : Matrix (Fin d) (Fin d) ℝ :=
fun i j => ∫ s in (((n - M : ℕ) : ℝ) * dt)..((n : ℝ) * dt),
(Phi s ((n : ℝ) * dt) * Q s * (Phi s ((n : ℝ) * dt))ᵀ) i j
/-- Theorem 3 (v), in the form of Deyst and Price (1968): the observability Gramian
`∑_{i=n-M}^{n} (Phi_{t_n}^{t_i})ᵀ Hᵀ N_i⁻¹ H Phi_{t_n}^{t_i}`, with `t_i = i dt`. -/
def obsGramian {d : ℕ} {ι : Type*} [Fintype ι] [DecidableEq ι] (dt : ℝ)
(Phi : ℝ → ℝ → Matrix (Fin d) (Fin d) ℝ) (H : Matrix ι (Fin d) ℝ) (Nm : ℕ → Matrix ι ι ℝ)
(M n : ℕ) : Matrix (Fin d) (Fin d) ℝ :=
∑ i ∈ Finset.Icc (n - M) n,
(Phi ((n : ℝ) * dt) ((i : ℝ) * dt))ᵀ * Hᵀ * (Nm i)⁻¹ * H * Phi ((n : ℝ) * dt) ((i : ℝ) * dt)
/-- Theorem 3 (i)-(v): the Deyst-Price conditions for the linear system with transition matrix
`Phi`, process-noise matrices `Q_s`, observation matrix `H` and measurement-noise matrices `N_n`
at the times `t_n = n dt` (with the observability sum in the form of Deyst and Price). -/
def DeystPriceConditions {d : ℕ} {ι : Type*} [Fintype ι] [DecidableEq ι] (dt : ℝ)
(Phi : ℝ → ℝ → Matrix (Fin d) (Fin d) ℝ) (Q : ℝ → Matrix (Fin d) (Fin d) ℝ)
(H : Matrix ι (Fin d) ℝ) (Nm : ℕ → Matrix ι ι ℝ) (α₁ α₂ β₁ β₂ δ₁ δ₂ δ₃ : ℝ) (M : ℕ) :
Prop :=
(∀ n : ℕ, LoewnerLE (δ₁ • (1 : Matrix (Fin d) (Fin d) ℝ))
((Phi ((n : ℝ) * dt) (((n : ℝ) + 1) * dt))ᵀ * Phi ((n : ℝ) * dt) (((n : ℝ) + 1) * dt))) ∧
(∃ q : ℕ, 0 < q ∧ ∃ Q' : Matrix (Fin q) (Fin q) ℝ, LoewnerLE (δ₂ • 1) Q' ∧
∀ s : ℝ, 0 < s → ∃ Gs : Matrix (Fin d) (Fin q) ℝ, Q s = Gs * Q' * Gsᵀ) ∧
(∀ n : ℕ, 1 ≤ n → LoewnerLE (δ₃ • (1 : Matrix ι ι ℝ)) (Nm n)) ∧
(∀ n : ℕ, M ≤ n →
(∀ i j, IntervalIntegrable (fun s => (Phi s ((n : ℝ) * dt) * Q s * (Phi s ((n : ℝ) * dt))ᵀ) i j)
volume (((n - M : ℕ) : ℝ) * dt) ((n : ℝ) * dt)) ∧
LoewnerLE (α₁ • 1) (ctrlGramian dt Phi Q M n) ∧ LoewnerLE (ctrlGramian dt Phi Q M n) (α₂ • 1)) ∧
(∀ n : ℕ, M + 1 ≤ n →
LoewnerLE (β₁ • 1) (obsGramian dt Phi H Nm M n) ∧ LoewnerLE (obsGramian dt Phi H Nm M n) (β₂ • 1))
/-- Theorem 3, conclusion "`γ_min I ⪯ P_t ⪯ γ_max I` for all `t`" (p. 13): the transition matrix
is bounded within each sampling interval, `(Phi_s^t)ᵀ Phi_s^t ⪯ c I` for `s, t ∈ [t_n, t_{n+1}]`.
The paper does not state this condition; the bound on `P_t` between observations needs it. -/
def BoundedTransition {d : ℕ} (dt : ℝ) (Phi : ℝ → ℝ → Matrix (Fin d) (Fin d) ℝ) (c : ℝ) :
Prop :=
∀ n : ℕ, ∀ s ∈ Set.Icc ((n : ℝ) * dt) (((n : ℝ) + 1) * dt),
∀ t ∈ Set.Icc ((n : ℝ) * dt) (((n : ℝ) + 1) * dt),
LoewnerLE ((Phi s t)ᵀ * Phi s t) (c • (1 : Matrix (Fin d) (Fin d) ℝ))
/-- Theorem 3: a run of the linear Kalman filter for `dx/dt = A_t x`, `y_{t_n} = H x_{t_n}`,
tuned with `Q` and `N`, started at time `t₀` from `(x̂₀, P₀)`: `x̂` follows `dx̂/dt = A_t x̂`
between the observation times `t_n = n dt`, where `x̂⁺ = x̂ + L_n (y_{t_n} - H x̂)`, and `P` is
a run of the Riccati equation (37). -/
def IsKalmanRun {d : ℕ} {ι : Type*} [Fintype ι] [DecidableEq ι]
(A : ℝ → Matrix (Fin d) (Fin d) ℝ) (H : Matrix ι (Fin d) ℝ)
(Q : ℝ → Matrix (Fin d) (Fin d) ℝ) (Nm : ℕ → Matrix ι ι ℝ) (dt : ℝ)
(x : ℝ → Fin d → ℝ) (t0 : ℝ) (xhat0 : Fin d → ℝ) (P0 : Matrix (Fin d) (Fin d) ℝ)
(xs : ℕ → ℝ → Fin d → ℝ) (Ps : ℕ → ℝ → Matrix (Fin d) (Fin d) ℝ) : Prop :=
IsRiccatiRun A H (fun _ t => Q t) (fun n => Nm (n + 1)) dt t0 P0 Ps ∧
∀ n : ℕ, t0 < ((n : ℝ) + 1) * dt →
((n : ℝ) * dt ≤ t0 → xs n t0 = xhat0) ∧
(∀ t ∈ segIcc t0 dt n, HasDerivWithinAt (xs n) (A t *ᵥ xs n t) (segIcc t0 dt n) t) ∧
xs (n + 1) (((n : ℝ) + 1) * dt) =
xs n (((n : ℝ) + 1) * dt) +
kalmanGain (Ps n (((n : ℝ) + 1) * dt)) H (Nm (n + 1)) *ᵥ
(H *ᵥ x (((n : ℝ) + 1) * dt) - H *ᵥ xs n (((n : ℝ) + 1) * dt))
end InvariantEKF
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
frob. For a real matrix with finitely many rows and columns , the Frobenius norm is
If there are no rows or no columns, the sum is empty and .
LoewnerLE. For square real matrices indexed by a finite set, means that is positive semidefinite. Positive semidefinite here means two things:
- is symmetric, and
- for every real vector .
Only the difference must be symmetric; and individually need not be. For empty index sets the relation always holds.
HasMatDerivWithinAt. Let , let be an matrix, let and let . The statement says: for every entry , the real function has derivative at relative to the set .
- The derivative is a limit taken only along points of .
- itself is not required to lie in .
- If is not an accumulation point of (for example, an isolated point of ), the condition holds for every .
HasMatDerivAt. For , a matrix and a time : for every entry , the function has ordinary two-sided derivative at .
IsMatrixLieGroup. A set of real matrices qualifies when all four conditions hold:
- The identity belongs to .
- Every has , and its inverse also lies in .
- whenever .
- Whenever a sequence of elements of converges to a matrix with , then .
Nothing is asserted about limits that are singular. For there is exactly one (empty) matrix, and must be the set containing it.
IsLieAlgebraCoords. Let be a set of real matrices and a linear map. The statement says two things:
- is injective.
- For every matrix : lies in the image of if and only if for every real , where is the matrix exponential.
So the image of is exactly the set . No hypothesis on is imposed.
expG. For a linear map and ,
the matrix exponential of .
IsGroupAffine. Let be a set of real matrices and let assign to each input a map from all matrices to matrices. The statement says that for every input and all ,
Nothing is required of off .
IsSolution. Let be a time-dependent map on matrices, , and . The statement says that for every , each entry of is differentiable at relative to , with
- The derivative is taken only along points of .
- At isolated points of the condition is automatic.
- Values of outside are unconstrained.
IsTrajectory. Let be a set of matrices, a family of matrix maps indexed by inputs , an input signal, , and . The statement has two parts:
- for every .
- For every , has derivative at relative to , entry by entry (derivative taken only along points of ).
invError. For a flag (left/right) and matrices :
If is singular, is the zero matrix, so the error is then the zero matrix.
ErrorAutonomous. Fix a flag (left/right), a set , a family of maps on matrices, and one fixed input signal . The statement says there exists a single family (maps on matrices, indexed by ) with the following property.
For any two functions that are both trajectories on the open half-line , meaning
- for all , and
- and for ,
and for every , the error is entrywise differentiable at with
Here in the left case or in the right case, with the inverse of a singular matrix taken to be .
- Only the given input is considered, not all inputs.
- If no such trajectories exist, the condition holds trivially.
errField. For a flag, a family , an input and an matrix :
IsLocLipschitzField. Let be a time-dependent map on real matrices. The statement says: for every real and every matrix , there exist a radius and a real constant such that
for every and all matrices with and .
- and may depend on and .
- For the interval is empty and the condition is vacuous.
- Negative times are never constrained.
- Nothing is required about continuity or measurability in .
IsLinearization. Let be linear, a map on matrices, and a matrix. The statement says there exist real numbers and such that for every with , every entry satisfies
- is the maximum norm.
- Taking forces .
carCoef. The function is defined by
At with a nonzero integer, the denominator is and division by zero yields , so there.
carLogError. Given reals , set
The output is the vector
where is the function from carCoef above (with and for nonzero integers ). The angle difference is not reduced modulo .
carErrorMatrix. For reals and :
obsY. For a flag, an matrix and a vector , the observation is in the left case and in the right case. If is singular, is the zero matrix.
innovation. Given a flag, vectors , an matrix and vectors , the result is the vector in , indexed by pairs , with entries
If is singular, is the zero matrix.
iekfUpdate. For a flag, a linear map , an matrix and , the updated estimate is
obsH. For a flag, a linear map and vectors , is the matrix with rows indexed by pairs and columns , given by
where is the -th standard basis vector and (left) or (right). Equivalently, by linearity, .
kalmanGain. For a matrix , an observation matrix (rows indexed by a finite set) and a square matrix on that index set:
If is singular, its inverse is the zero matrix and .
segIcc. For reals and and , the segment is the closed interval
It is empty whenever , for example when or . No sign condition on is imposed.
piecewiseRun. Given a family of segment functions (), and , the value is
where is the floor truncated at (negative arguments give index ). If , then and the index is .
IsRiccatiRun. The data are:
- , a matrix for each time ;
- an observation matrix (rows indexed by a finite set);
- matrices () for each segment and time ;
- square matrices on the observation index set;
- reals ;
- a matrix ;
- segment functions ().
The statement says: for every with , three conditions hold.
- Initial value: if , then .
- Riccati equation: for every in the segment , is entrywise differentiable at relative to (one-sided at the endpoints), with
- Jump at the observation time :
with the inverse of a singular matrix taken to be .
Edge cases:
- Segments with are unconstrained.
- Each is unconstrained outside its segment.
- If , then never holds, so no initial condition is imposed at all.
- No symmetry or positivity is required of , or , and no sign condition on .
IsIEKFRun. The data are:
- a flag (left/right);
- a linear map ;
- vector fields on matrices indexed by inputs , and an input signal ;
- matrices for each input ;
- vectors ;
- a real ;
- design maps (from matrices to matrices) and (from matrices to square matrices indexed by );
- a "true" function ;
- reals , matrices () and ();
- segment functions and .
Let be the matrix with , where (left) or (right). Write , , and . The statement asserts two things.
(I) The form a Riccati run in the sense above, with
- ,
- this ,
- (the design matrix at the current segment estimate),
- .
That run is:
- whenever and : ;
- on : , as a derivative relative to ;
- at : , with gain
(II) The estimate evolves accordingly: for every with ,
- if , then ;
- for every , has derivative relative to ;
- the update at is
The innovation has entries
Assumptions that are not imposed:
- Inverses of singular matrices are .
- is not required to be a trajectory or to lie in any group.
- Observations are noise-free, and only the values enter.
- No relation between and is assumed.
- The same edge cases as for the Riccati run apply: no initial condition if , segments ending at or before are unconstrained, and no sign condition on .
invErrDist. For a flag and matrices , the distance is
with the Frobenius norm. If is singular, is the zero matrix, and the distance is then .
perturbed. For a flag, a linear map , an matrix and , the perturbed matrix is
TuningComparable. Fix a flag, a linear map , a function , a real , and design maps and as in IsIEKFRun. Write (left) or (right).
The statement says: for every real there exists such that for every single fixed vector with , both of the following hold:
- for every :
- for every integer :
Here means is symmetric positive semidefinite. Further points:
- is uniform over all and all .
- The same is used at every time; the condition does not involve time-varying perturbations.
- Negative times and are not constrained.
IsTransitionMatrix. Let be matrices and a matrix for each pair of reals . The statement says that for every real :
- , and
- for every real , the map has ordinary entrywise derivative at .
ctrlGramian. For , , matrices , and , the matrix has entry
- is truncated subtraction.
- The integral is the oriented (signed) interval integral, so it is negated if the lower limit exceeds the upper one.
- A non-integrable integrand gives the value .
- If or , the interval is degenerate and the result is the zero matrix.
obsGramian. For , , an observation matrix , square matrices () and :
- is the transition matrix from time to time .
- The sum runs over integers from to inclusive.
- If is singular, is the zero matrix.
DeystPriceConditions. The data are , a transition matrix , matrices , an observation matrix with rows indexed by a finite set, square matrices , real constants , and . The statement is the conjunction of five conditions.
- For every (including ):
- There exist an integer and a matrix with such that for every there is a matrix with .
- For every : .
- For every , both of the following hold:
- every entry of is integrable on the interval between and ;
- with (entrywise, oriented integral),
- For every , with (singular inverse ):
Here means is symmetric positive semidefinite. Further points:
- No sign or ordering conditions are imposed on any of the constants , or on .
- If , then , so condition 4 requires when .
- for is constrained only through the integrals.
- is unconstrained.
- If the observation index set is empty, then and condition 3 holds trivially.
BoundedTransition. For , a transition matrix and a real , the statement says: for every and all ,
meaning is symmetric positive semidefinite.
- If , every interval is empty and the condition is vacuous.
- If , it only concerns .
IsKalmanRun. The data are:
- matrices ;
- an observation matrix (rows indexed by a finite set);
- matrices ;
- square matrices ;
- a real ;
- a "true" function ;
- reals , a vector , a matrix ;
- segment functions and .
Write , and
(singular inverse ). The statement asserts two things.
(I) The form a Riccati run: for every with ,
- if , then ;
- on : , as a derivative relative to ;
- .
(II) The state estimate evolves accordingly: for every with ,
- if , then ;
- for every , has derivative relative to ;
- the update at is
Assumptions that are not imposed:
- is not required to satisfy .
- Observations are noise-free, and only the values enter.
- No positivity or symmetry is required of .
- If , no initial condition is imposed.
- Segments ending at or before are unconstrained, and no sign condition on is imposed.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.