dynamicslab / pysindy

A package for the sparse identification of nonlinear dynamical systems from data
https://pysindy.readthedocs.io/en/latest/
Other
1.36k stars 304 forks source link

Ensemble feature in SINDy-PI #479

Closed anur2203 closed 3 months ago

anur2203 commented 4 months ago

I am unable to use ensemble feature in SINDy-PI because SINDyPI optimizer is not included in the base optimizer. Any solution for this.

Jacob-Stevens-Haas commented 4 months ago

Ensembling is handled by the EnsembleOptimizer

anur2203 commented 3 months ago

feature_library = ps.PolynomialLibrary(degree=2) differentiation_method = ps.SINDyDerivative(kind="finite_difference", k=4) optimizer = ps.STLSQ(alpha=0.1, threshold=0.001, max_iter=1000) ensemble_optimizer = ps.EnsembleOptimizer( optimizer, bagging=True, n_models=5, replace=True, n_subset=int(0.7 * x_train.shape[0]), ensemble_aggregator= lambda x: np.mean(x, axis=0) ) model = ps.SINDy(differentiation_method=differentiation_method, feature_library= feature_library, optimizer=ensemble_optimizer, feature_names=["x", "y"] ) model.fit(x_train, t=t_train) model.print() ensemble_coefs = ensemble_optimizer.coef_list mean_ensemble = np.mean(ensemble_coefs, axis=0) print(mean_ensemble.T)

Output: (x)' = -0.596 1 + 0.554 x + 0.158 y + -0.047 x^2 + -0.149 x y + 0.004 y^2 (y)' = -0.532 1 + 0.098 x + 0.111 y + 0.010 x^2 + 0.028 x y + -0.023 y^2 [[ 1.07310567 -0.16638743] [-0.3351258 -0.0230943 ] [-0.45537466 -0.19060713] [ 0.05809299 0.02597279] [ 0.02279691 0.05569571] [ 0.04866902 0.03735311]]

Model parameter values are different from mean coefficient values. Why? Also SINDyPI optimizer is not supporting with ensemble optimizer. What I can do ?

Jacob-Stevens-Haas commented 3 months ago

It's three backticks to format a code block.

Model parameter values are different from mean coefficient values. Why?

Because of unbiasing. Check the unbias argument in optimizers

Also SINDyPI optimizer is not supporting with ensemble optimizer. What I can do ?

By rearranging the terms in the equation, SINDy-PI does more than an optimizer, even though its implemented by inheriting BaseOptimizer and assigning it to the kwarg SINDy(optimizer=...). This means it can't be ensembled with an EnsembleOptimizer.