You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the example code in the documentation, the mean or median calculated from the optimizer.coef_list is different from the value by optimizer.coef_ or model.coefficients(). Is this a bug?
Reproducing code example:
importpysindyimportmatplotlib.pyplotaspltfrommpl_toolkits.mplot3dimportAxes3Dimportnumpyasnpfromscipy.integrateimportsolve_ivpfromsklearn.linear_modelimportLassofrompysindy.utilsimportenzymefrompysindy.utilsimportlorenzfrompysindy.utilsimportlorenz_controlimportpysindyasps# bad code but allows us to ignore warningsimportwarningswarnings.filterwarnings("ignore", category=UserWarning)
# Seed the random number generators for reproducibilitynp.random.seed(100)
# Initialize integrator keywords for solve_ivp to replicate the odeint defaultsintegrator_keywords= {}
integrator_keywords['rtol'] =1e-12integrator_keywords['method'] ='LSODA'integrator_keywords['atol'] =1e-12# Generate measurement datadt=.002t_train=np.arange(0, 10, dt)
x0_train= [-8, 8, 27]
t_train_span= (t_train[0], t_train[-1])
x_train=solve_ivp(lorenz, t_train_span, x0_train,
t_eval=t_train, **integrator_keywords).y.T# Default is to sample the entire time series with replacement, generating 10 models on roughly# 60% of the total data, with duplicates.# Custom feature namesnp.random.seed(100)
feature_names= ['x', 'y', 'z']
ensemble_optimizer=ps.EnsembleOptimizer(ps.STLSQ(threshold=1e-3,normalize_columns=False),
bagging=True,n_subset=int(1.0*x_train.shape[0]), replace=True)
model=ps.SINDy(optimizer=ensemble_optimizer, feature_names=feature_names)
model.fit(x_train, t=dt)
ensemble_coefs=ensemble_optimizer.coef_listmean_ensemble=np.mean(ensemble_coefs, axis=0)
std_ensemble=np.std(ensemble_coefs, axis=0)
ensemble_optimizer.coef_==model.coefficients()
np.abs(np.median(ensemble_coefs, axis=0)-model.coefficients())<1e-10np.abs(np.mean(ensemble_coefs, axis=0)-model.coefficients())<1e-10
Error message:
PySINDy/Python version information:
The text was updated successfully, but these errors were encountered:
Very interesting - thank you for finding a difficult numerical issue, and thank you for submitting a complete working example! I can confirm this in master.
tl;dr: yes, this is by design. The final coefficients are "unbiased" by the choice of optimizer.
My first thought is it's in LinearRegression._set_intercept()
That was correct, but SINDyOptimizer.unbias is desired - fit the coefficients one more time, omitting all terms the (regularized) optimizer chose to omit but using un-regularized linear regression. It defaults to True in SINDy.__init__().
There are some things to think about design-wise here, however. @akaptano, @znicolaou , thoughts?
Currently, BaseOptimizer has old docstring with unbias parameter but doesn't use it - should be removed if nothing else done.
Currently, SINDy.fit wraps all optimizers in a SINDyOptimizer to which it gives it's own unbias initialization parameter.
SINDyOptimizer, as a class, seems to exist solely to run _unbias() (maybe some simple reshaping on 1-d coeff matrices).
It appears that SINDyOptimizer is never used in the examples, except as a markdown comment to suggest unbiasing.
Is there a reason to keep SINDyOptimizer? We could return _unbias() to the BaseOptimizer class, combine their fit() code together...
...and deprecate/remove the unbias parameter from SINDy objects? Rationale being (a) avoiding SINDy being a God Object antipattern, (b) encapsulation of optimization parameters w/in optimization objects. OTOH if we treat unbiasing as a step after optimization, the alternative is to remove it from SINDyOptimizer and make it a function in pysindy.py
Regardless, I'm motivated by the ZOP There should be one-- and preferably only one --obvious way to do it.
When using the example code in the documentation, the mean or median calculated from the
optimizer.coef_list
is different from the value byoptimizer.coef_
ormodel.coefficients()
. Is this a bug?Reproducing code example:
Error message:
PySINDy/Python version information:
The text was updated successfully, but these errors were encountered: