Skip to content

Conversation

filippozacchei
Copy link

Introduction of Weighted Samples in SINDy.fit()

This pull request introduces support for weighted samples in both standard and weak formulations of SINDy.
It enables users to provide per-sample or per-trajectory weights that influence model fitting, improving control over data importance and uncertainty propagation.

Main Changes

_core._expand_sample_weights

  • Introduces a unified weight-expansion utility to validate and broadcast user-provided sample weights.
  • In standard SINDy, weights are expanded to match the total number of samples (n_samples × 1).
  • In weak SINDy, each trajectory’s scalar weight is repeated according to the number of weak test functions (`(
  • (n_trajectories \cdot K) × 1`).
  • Strict validation ensures that:
    • Weak mode accepts exactly one scalar weight per trajectory.
    • Standard mode allows either scalars or per-sample arrays.

weighted_weak_pde_library

  • Implements Generalized Least Squares (GLS) whitening for weak-form regression.

  • When non-uniform spatiotemporal weights are provided, the feature matrix (Θ) and right-hand side (V) are prewhitened using the Cholesky factor of the covariance matrix: $\mathrm{Cov}[V] = V' \Sigma (V')^\top$

    where:

    • Σ represents the pointwise sample weights.
    • V′ are the weak integration matrices for the right-hand side.

Automated Tests

  • Added two new test suites to validate the weighting behavior.

@Jacob-Stevens-Haas Jacob-Stevens-Haas linked an issue Oct 14, 2025 that may be closed by this pull request
@Jacob-Stevens-Haas
Copy link
Member

Jacob-Stevens-Haas commented Oct 14, 2025

Hey, thanks for this PR! Some quick thoughts:

  • Weak form is going through a big refactor, and the WeakPDELibrary will be deprecated in the next release. In it's place, WeakSINDy will be a subclass of _BaseSINDy. This will allow removing a lot of if weak: that is sprinkled throughout the package, as well as enable prediction and simulation of weak models and pave the way for people to implement different basis functions. IDK how you want to proceed - wait to see what weak branch comes up with in a couple weeks, or just continue with strict "1-weight-per-trajectory" for now so it's the same for weak and regular?
  • On a related note, why is GLS whitening desired?
  • I know I steered you away from SampleConcatter before, but maybe calls to _expand_sample_weights should go there, rather than in fit() and score. It might be able to reduce redundant code. It helps if the logic to handle flattening axes only occurs in one place, since otherwise, developers need to keep multiple axis layout conventions in mind.
  • Is STLSQ the only optimizer that supports sample weight?
  • Add type annotations wherever you touch a function signature

@filippozacchei
Copy link
Author

Thanks for your comment.

I would say then to just use sample weights for only standard Sindy then. The GLS whitening is needed for Weak SINDy if we have samples of the same trajectory of different importance, but I would avoid for the time being.

Will look into SampleConcatter and update!

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.

Introduce sample_weights inside fit function

2 participants