Repository navigation
Preserve retained state coordinates in RBF inverse - #79
Conversation
pswpswpsw
left a comment
There was a problem hiding this comment.
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=Falsecontrol passing, and 12 passes on this branch. - I compared master and this branch over 4,130 configurations covering 5 seeds, all five
rbf_typevalues,include_stateTrue and False, 2, 10 and 30 centers, two kernel widths, and random or explicit centers.transformand the fitted centers are bit identical everywhere, and so is every output withinclude_state=False. Withinclude_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 asRadialBasisFunction() + Identity(),RadialBasisFunction() + TimeDelay(...)orRadialBasisFunction(include_state=False) + RadialBasisFunction(), are fixed too. The other combinations I tried, including those that start withIdentityorTimeDelay, do not change. - Inside
KoopmanwithEDMDtrained on one step pairs, the eigenvalues are identical,CandWdiffer by at most about 1e-7 and 5e-11, andpredictandsimulateagree to a relative 3e-11 or better, even with 100 centers and only 51 samples. That seems expected, becauseC = measurement_matrix_ @ urand 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 oldlstsqmatrix 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/observablespasses, and the full suite passes (1,774 tests) withMPLBACKEND=Agg. With the default Tk backend one or two plotting tests fail in my environment because Tk cannot findinit.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, sotest_rbf_inverse_preserves_states_after_refitfails 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_inversecomputes the expected matrix with the samelstsqcall 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.
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):
The official Torch arm64 wheel has a mismatched internal platform tag, so
uv pip checkreports 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.