Skip to content

Preserve retained state coordinates in RBF inverse - #79

Merged
pswpswpsw merged 1 commit into
dynamicslab:masterfrom
AryanRG:fix/rbf-retained-state-inverse
Nov 15, 2026
Merged

pswpswpsw merged 1 commit into
dynamicslab:masterfrom
AryanRG:fix/rbf-retained-state-inverse

Conversation

@AryanRG

@AryanRG AryanRG commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #78.

RadialBasisFunction(include_state=True) already retains the input coordinates, but fits a least-squares inverse. With correlated training coordinates, that inverse can map an unseen [0, 1] state to [0.5, 0.5], despite the exact coordinates being present in the transformed data.

Use the identity block to recover retained state coordinates, as the other state-preserving observables do. Keep the learned inverse when include_state=False.

The regression covers all five kernels, concatenation, refitting and the no-state control. Validation on the project's core pins (Python 3.11.16, NumPy 1.26.0, SciPy 1.11.2, scikit-learn 1.1.3, Torch 2.1.2 and Lightning 2.0.9):

  • Full repository suite: 1,774 passed.
  • Combined observables/regression and independent-oracle checks: 1,408 passed. On the unchanged base, 31 cases fail and 1,377 pass.
  • The exact issue example fails on the base and preserves both unseen states after the fix.
  • Pinned Python style checks pass. Notebooks, docs and other platform/version combinations were not run.

The official Torch arm64 wheel has a mismatched internal platform tag, so uv pip check reports that metadata warning. Its published hash and native arm64 binaries were verified; the wheel was left unchanged and the normal imports and tests above pass.

Prepared with AI assistance.

@pswpswpsw pswpswpsw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the clear report and the focused fix.

I reproduced the issue on master (df56de2) and checked this branch (6af713e). With include_state=True, fit now sets measurement_matrix_ to [I 0], which is how Identity, Polynomial, TimeDelay, CustomObservables and RandomFourierFeatures(include_state=True) already treat the retained state. The least squares path for include_state=False is unchanged.

What I checked locally (Windows, Python 3.11, pinned dependencies):

  • The issue example and a case with fewer samples than lifted features (5 samples, 2 states, 10 centers) both fail on master and pass here. Your new test file gives 11 failures on master, with only the include_state=False control passing, and 12 passes on this branch.
  • I compared master and this branch over 4,130 configurations covering 5 seeds, all five rbf_type values, include_state True and False, 2, 10 and 30 centers, two kernel widths, and random or explicit centers. transform and the fitted centers are bit identical everywhere, and so is every output with include_state=False. With include_state=True, the round trip error on unseen states drops from up to 6.6 on master (collinear, planar or underdetermined training data) to exactly 0 wherever both branches run.
  • Concatenations whose first state carrying observable is an RBF with include_state=True, such as RadialBasisFunction() + Identity(), RadialBasisFunction() + TimeDelay(...) or RadialBasisFunction(include_state=False) + RadialBasisFunction(), are fixed too. The other combinations I tried, including those that start with Identity or TimeDelay, do not change.
  • Inside Koopman with EDMD trained on one step pairs, the eigenvalues are identical, C and W differ by at most about 1e-7 and 5e-11, and predict and simulate agree to a relative 3e-11 or better, even with 100 centers and only 51 samples. That seems expected, because C = measurement_matrix_ @ ur and the predictions only see the span of the lifted training data. The one visible difference came from a single trajectory near a slow manifold with 50 centers. In the cases I inspected there, the lifted data is numerically singular (condition number around 1e17), and the old lstsq matrix differs from [I 0] by 0.07 to 0.22 while still reproducing the training states. The one step prediction error changed by 12 to 15 percent in three of fifteen such cases, in both directions. Since [I 0] is exact on the training states, I would expect the new value to be the more reliable one there.
  • pre-commit passes on both files. test/observables passes, and the full suite passes (1,774 tests) with MPLBACKEND=Agg. With the default Tk backend one or two plotting tests fail in my environment because Tk cannot find init.tcl, and that happens on master as well.

One behavior change seems worth recording, although I do not think it should hold up this PR. The polyharmonic kernel evaluates to NaN when a sample coincides with a center (0 times log 0), and unlike thinplate it has no guard for that case. On master, such a point in the training data made fit raise LinAlgError. On this branch fit succeeds when include_state=True, and transform and inverse return NaN for those rows, since the inverse is a matrix product and NaN times 0 is still NaN. The centers from the issue show this: with rbf_type="polyharmonic" the row [0, 1] comes back as NaN on both branches. Giving the polyharmonic kernel the same r = 0 handling as thinplate would probably be the cleaner fix, and I would rather track it in a separate issue.

Two optional points on the tests:

  • Master also rebuilds the measurement matrix on every fit, so test_rbf_inverse_preserves_states_after_refit fails there for the same reason as the main test, not because of a stale matrix. The comment could say so, or the case could move into the parametrized test.
  • test_rbf_without_state_keeps_least_squares_inverse computes the expected matrix with the same lstsq call as the implementation, so it mainly pins the current formula. Keeping it as a guard on the unchanged branch is fine with me.

CI has not run on this PR yet, so I would like to see the checks pass before merging.

@pswpswpsw
pswpswpsw merged commit d4c00b1 into dynamicslab:master Nov 15, 2026
2 checks passed
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.

RBF inverse loses retained state coordinates after fitting correlated data

2 participants