-
Notifications
You must be signed in to change notification settings - Fork 361
Sample Weights introduction #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
To do: account for WEAK sindy
9ac839d to
9b7d2dd
Compare
|
Hey, thanks for this PR! Some quick thoughts:
|
|
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! |
Jacob-Stevens-Haas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the long delay! I can finally begin to prioritize this PR. As I mentioned, it's best to leave Weak for another day, and to put the sample array alignment in the same place as trajectories are already being flattened and combined (SampleConcatter). Other than that, there's a few tweaks to _expand... and to the tests
| from sklearn.pipeline import Pipeline | ||
| from sklearn.utils.validation import check_is_fitted | ||
|
|
||
| set_config(enable_metadata_routing=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave this up to the user. scikit-learn documentation explains that if they're using a pipeline or composite transform, they'll need this.
| self.set_fit_request(sample_weight=True) | ||
| self.set_score_request(sample_weight=True) | ||
| self.optimizer.set_fit_request(sample_weight=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, leave this up to the user
| x_dot=None, | ||
| u=None, | ||
| feature_names: Optional[list[str]] = None, | ||
| sample_weight=None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provide static type
| Names for the input features (e.g. :code:`['x', 'y', 'z']`). | ||
| If None, will use :code:`['x0', 'x1', ...]`. | ||
| sample_weight : float or array-like of shape (n_samples,), optional |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why allow a single float?
Also, shouldn't this be (*n_spatial, n_time)? Or a list of those arrays, for when multiple trajectories are set?
| x_dot = concat_sample_axis(x_dot) | ||
| self.model = Pipeline(steps) | ||
| self.model.fit(x, x_dot) | ||
| self.model.fit(x, x_dot, sample_weight=sample_weight) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove the Pipeline and directly call the components. That way, we don't need to rely on metadata routing internally
|
|
||
| # --- Fit without explicit sample weights --- | ||
| sindy.fit(X_trajs, t=0.1, x_dot=Xdot_trajs) | ||
| coef_unweighted = np.copy(sindy.model.named_steps["model"].coef_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just use sindy.optimizer.coef_
| # --- Fit with sample weights to emphasize trajectory 3 (different system) --- | ||
| sample_weight = [np.ones(len(x_a)), np.ones(len(x_a)), 10 * np.ones(len(x_b))] | ||
| sindy.fit(X_trajs, t=0.1, x_dot=Xdot_trajs, sample_weight=sample_weight) | ||
| coef_weighted = np.copy(sindy.model.named_steps["model"].coef_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
| # 3. Weighted model should bias toward system B coefficients | ||
| # since trajectory B had much higher weight | ||
| # True systems differ by factor of 2 | ||
| ratio = np.mean(np.abs(coef_weighted / coef_unweighted)) | ||
| assert ( | ||
| ratio > 1.05 | ||
| ), "Weighted coefficients should reflect stronger influence from system B" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need both an error message and description (prefer the error message). Also, make ratio > 1.5, since it should be substantially towards system B
| # 3. Weighted model should bias toward system B coefficients | |
| # since trajectory B had much higher weight | |
| # True systems differ by factor of 2 | |
| ratio = np.mean(np.abs(coef_weighted / coef_unweighted)) | |
| assert ( | |
| ratio > 1.05 | |
| ), "Weighted coefficients should reflect stronger influence from system B" | |
| # True systems differ by factor of 2 | |
| ratio = np.mean(np.abs(coef_weighted / coef_unweighted)) | |
| fail_msg = "Weighted coefficients should reflect stronger influence from system B" | |
| assert ratio > 1.5, fail_msg |
| # --- Assertions --- | ||
| # 1. Shapes are consistent | ||
| assert coef_weighted.shape == coef_unweighted.shape | ||
|
|
||
| # 2. The coefficients must differ when weighting is applied | ||
| assert not np.allclose(coef_weighted, coef_unweighted) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are implied by the following tests. That's justified if these are more specific. However, shape errors will show explicitly in the below code. Additionally, not np.allcolse is a less-specific version of the next assertion
| assert np.linalg.norm(coef_weighted - 2 * coef_unweighted) < np.linalg.norm( | ||
| coef_unweighted | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how this is correct? It would make more sense to explicitly form coef_a and coef_b, then check the equations that you added as comments.
|
As a heads up, we're changing the default branch from |
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_weightsn_samples × 1).weighted_weak_pde_libraryImplements 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:
Automated Tests