Add a robust, faster fitting method to lagFitVelocity() - #89
Open
AstroEloy wants to merge 1 commit into
Open
Conversation
AstroEloy
force-pushed
the
lag-alpha-beta-robust-error
branch
from
July 23, 2026 11:06
f8975d3 to
fa9cfa9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
lagFitVelocity()(wmpl/Utils/AlphaBeta.py) previously had a single, undocumented fitting path:scipy.optimize.basinhopping(niter=200)+ Nelder-Mead on the exact L1 lag residual, seeded from hardcoded magic-number initial guesses, with dead commented-out code and no input validation. This PR:method='robust'—scipy.optimize.least_squares(loss='soft_l1')with a small multi-start overt0(the one parameter that makes the exponential→linear model's cost landscape multimodal), instead of 200 global basinhopping restarts.robustvs 1.89 m/s forbasinhopping; mean 0.96 vs 1.93;robustwon outright in 6/9 trials.basinhopping's fixed initial guess + finite iteration budget occasionally leaves it under-converged when the truea1is far from that guess —least_squaresdoesn't share that failure mode.brentqwas considered and confirmed not applicable: unlikealphaBetaVelocityNormed()(which inverts height↔velocity),expLinearLag()/expLinearVelocity()are already explicit closed-form functions oft— there's nothing to invert here.method='basinhopping', byte-for-byte unchanged behavior; the old 4-positional-argument call signature still works.method, too few points, non-finite/non-positivev0, and degenerate (zero-span)time_dataall raiseValueError; non-finite(time, lag, vel)points are dropped with a warning instead of silently propagating NaN.curve_fitfallback, a stale debug-plot block).-r/--lagrobustCLI flag into theif __name__ == "__main__":block, mirroring the existing-e/--errorspattern — when set, the lag-smoothing step usesmethod='robust'instead of the default.Testing
Added 5 new tests to
wmpl/Utils/Tests/test_AlphaBeta.py:testLagFitVelocityBackwardsCompatibleCall— old positional-only call still works, defaults tobasinhopping.testLagFitVelocityRobustRecoversParams—robustrecovers true params and is faster thanbasinhopping.testLagFitVelocityRobustFitQuality— deterministic multi-scenario Monte Carlo provingrobustmatches or beatsbasinhopping's fit quality (not just speed).testLagFitVelocityInputValidation— all new safeguards raiseValueError.testLagFitVelocityDropsNonFinitePoints— non-finite points are dropped, not propagated.Full suite: 49/49 passing (
python -m wmpl.Utils.Tests.test_AlphaBeta), ~2 min total.Files changed
wmpl/Utils/AlphaBeta.pywmpl/Utils/Tests/test_AlphaBeta.py