Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
331 changes: 170 additions & 161 deletions examples/1_basic_tutorial.ipynb

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pynumdiff/kalman_smooth/_kalman_smooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def _kalman_forward_filter(xhat0, P0, y, A, C, Q, R, u=None, B=None):
xhat_pre.append(xhat_) # the [n]th index holds _{n|n-1} info
P_pre.append(P_)

xhat = xhat_ # handle missing data
P = P_
if not np.isnan(y[n]):
xhat = xhat_.copy()
P = P_.copy()
Copy link
Collaborator Author

@pavelkomarov pavelkomarov Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one was bad. Subtle. Took me a long time to find, because the old code that worked properly was so different from this updated code, so winnowing and ablating down to the issue was complicated.

if not np.isnan(y[n]): # handle missing data
K = P_ @ C.T @ np.linalg.inv(C @ P_ @ C.T + R)
xhat += K @ (y[n] - C @ xhat_)
P -= K @ C @ P_
Expand Down Expand Up @@ -89,7 +89,7 @@ def _constant_derivative(x, P0, A, C, R, Q, forwardbackward):
x_hat_forward = xhat_smooth[:, 0] # first dimension is time, so slice first element at all times
dxdt_hat_forward = xhat_smooth[:, 1]

if not forwardbackward: # bound out here if not doing the same in reverse and then combining
if not forwardbackward: # bounce out here if not doing the same in reverse and then combining
return x_hat_forward, dxdt_hat_forward

xhat0[0] = x[-1] # starting from the other end of the signal
Expand Down
26 changes: 13 additions & 13 deletions pynumdiff/tests/test_diff_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,22 @@ def iterated_first_order(*args, **kwargs): return first_order(*args, **kwargs)
[(1, 0), (2, 2), (1, 0), (2, 2)],
[(1, 0), (3, 3), (1, 0), (3, 3)]],
constant_velocity: [[(-25, -25), (-25, -25), (0, -1), (0, 0)],
[(-5, -6), (-4, -4), (0, -1), (0, 0)],
[(-1, -2), (0, 0), (0, 0), (1, 0)],
[(0, -1), (1, 0), (0, -1), (1, 1)],
[(-6, -6), (-5, -5), (0, -1), (0, 0)],
[(-1, -2), (0, 0), (0, -1), (0, 0)],
[(-1, -1), (1, 0), (0, -1), (1, 0)],
[(1, 1), (2, 2), (1, 1), (2, 2)],
[(1, 1), (3, 3), (1, 1), (3, 3)]],
constant_acceleration: [[(-25, -25), (-25, -25), (0, 0), (1, 0)],
[(-5, -5), (-3, -3), (0, 0), (1, 0)],
[(-4, -4), (-2, -2), (0, 0), (1, 0)],
[(0, -1), (1, 0), (0, 0), (1, 0)],
[(1, 1), (3, 2), (1, 1), (3, 2)],
constant_acceleration: [[(-25, -25), (-25, -25), (0, -1), (0, 0)],
[(-5, -6), (-4, -5), (0, -1), (0, 0)],
[(-5, -5), (-4, -4), (0, -1), (0, 0)],
[(-1, -1), (0, 0), (0, -1), (0, 0)],
[(1, 1), (2, 2), (1, 1), (2, 2)],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bounds get a lot tighter across the board with the bug fixed.

[(1, 1), (3, 3), (1, 1), (3, 3)]],
constant_jerk: [[(-25, -25), (-25, -25), (0, 0), (1, 0)],
[(-5, -5), (-3, -3), (0, 0), (1, 0)],
[(-4, -4), (-2, -2), (0, 0), (1, 0)],
[(-1, -2), (1, 0), (0, 0), (1, 0)],
[(1, 0), (2, 2), (1, 0), (2, 2)],
constant_jerk: [[(-25, -25), (-25, -25), (0, -1), (0, 0)],
[(-5, -5), (-4, -5), (0, -1), (0, 0)],
[(-4, -5), (-3, -4), (0, -1), (0, 0)],
[(-1, -2), (0, 0), (0, -1), (0, 0)],
[(1, 0), (2, 1), (1, 0), (2, 1)],
[(1, 1), (3, 3), (1, 1), (3, 3)]],
velocity: [[(-25, -25), (-18, -19), (0, -1), (1, 0)],
[(-12, -12), (-11, -12), (-1, -1), (-1, -2)],
Expand Down