Skip to content

[kalman] Fix errors, modernize code, and unify notation - #908

Merged
jstac merged 2 commits into
mainfrom
kaled
Jun 17, 2026
Merged

jstac merged 2 commits into
mainfrom
kaled

Conversation

@jstac

@jstac jstac commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Overview

A review-driven pass over A First Look at the Kalman Filter (lectures/kalman.md). The changes fall into four groups: correctness/consistency fixes, code modernization, a notation overhaul for flow, and math-layout tweaks. Every code cell was executed end-to-end after each change (via jupytext --to py); the lecture runs clean and figure output is unchanged except for the intended colormap swap.

1. Correctness & consistency fixes

  • Unterminated math span. $Y_{t+1}. in the recursive-procedure section was missing its closing $, which would render the rest of the sentence as math. Now $Y_{t+1}$.
  • Measurement-equation subscript. Y_t = G X_t + v_t, \text{ where } v \sim N(0,R) → v_t \sim N(0,R), so the subscript is consistent within the equation.
  • Figure label. The forecast-step figure labelled the observation point "$y$" while the other two figures use "$Y_t$"; unified to "$Y_t$".

2. Code modernization

  • Library density. Removed the hand-rolled bivariate_normal function (~37 lines, a copy of the long-removed matplotlib.mlab.bivariate_normal) and replaced it with scipy.stats.multivariate_normal, fed the covariance matrix directly. Verified numerically identical to the old implementation (max abs difference ≈ 2.8e-16).
  • Colormap. Switched all cmap=cm.jet to cmap="viridis" (perceptually uniform; jet is discouraged) and dropped the now-unused import matplotlib.cm as cm.
  • Redundant conversions. Removed float(...) wrapping around .item() calls in five places (.item() already returns a Python float), including simplifying two m, v = [float(z) for z in (...)] comprehensions.

3. Notation overhaul: \hat x → \mu

To improve flow and logical consistency, the Gaussian is now parameterized throughout as $N(\mu, \Sigma)$ — the canonical mean/covariance pairing — matching the lecture's distributional narrative.

  • Every \hat x (and derived forms \hat x^F, \hat x_t, \hat x_{new}, \hat x_0, …) replaced by \mu in math and prose.
  • The overview's generic "point prediction" was reworded so the symbol is introduced only at the density, where $\mu$ is described as "the (vector) mean of the distribution—a natural point prediction."
  • Code variables renamed to match the math: x_hat → μ, x_hat_F → μ_F, new_x_hat → new_μ, x_hat_0 → μ_0. The QuantEcon Kalman attributes (kalman.x_hat, kn.x_hat) are left intact, since they are library API.
  • Two bridging notes added to manage the divergence from convention:
    • After the recursion equations: a note that $\mu_t$ is the filter's prediction of $X_t$, written $\hat x_t$ in much of the Kalman filter literature (so readers consulting Ljungqvist–Sargent / Anderson–Moore aren't disoriented).
    • In the Implementation section: clarifies that $(\mu_t, \Sigma_t)$ are stored as the .x_hat and .Sigma attributes, explaining the attribute name.

4. Math layout

  • Upright subscript. _{new} → _{\mathrm{new}} so "new" renders upright rather than as italic $n\cdot e\cdot w$.
  • Reflowed wide derivations. The two chained equalities after "Standard calculations and the expressions…" were very wide. Each is now an aligned block with one = step per line, and the longest right-hand side (the variance expansion) is split so the subtracted regression term wraps to its own line.

Verification

  • All seven code cells run to completion (exit 0), only the expected non-interactive-backend warnings.
  • bivariate_normal → multivariate_normal confirmed equivalent to machine precision.
  • Post-edit checks confirm no stray \hat x outside the two intentional notes, no orphaned renamed identifiers, and library attributes preserved.

🤖 Generated with Claude Code

Review-driven pass over the Kalman filter lecture covering correctness
fixes, code modernization, a notation overhaul, and math layout.

- Fix an unterminated math span ($Y_{t+1}.), a measurement-equation
  subscript (v -> v_t), and a stray figure label ($y$ -> $Y_t$).
- Replace the hand-rolled bivariate_normal density with
  scipy.stats.multivariate_normal; switch the jet colormap to viridis;
  drop redundant float(...item()) conversions.
- Reparameterize the Gaussian throughout as N(mu, Sigma) (xhat -> mu),
  in math, prose, and code variables, with bridging notes tying mu_t to
  the literature's xhat and the QuantEcon `x_hat` attribute.
- Set "new" subscripts upright (\mathrm{new}) and reflow two wide
  derivations into aligned blocks.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Jun 16, 2026 •

Copy link
Copy Markdown

📖 Netlify Preview Ready!

Preview URL: https://pr-908--sunny-cactus-210e3e.netlify.app

Commit: cd2f096

📚 Changed Lectures


Build Info

@jstac
jstac merged commit 8e4836d into main Jun 17, 2026
1 check passed
@jstac
jstac deleted the kaled branch June 17, 2026 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant