dynamicslab / pysindy

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

Can you specify terms in feature libraries for system of equations? #333

Closed calinacopos closed 1 year ago

calinacopos commented 1 year ago

I'm running pysindy in the context of reaction-diffusion equations for biological chemicals. I am using pysindy on data to infer Lotka-Volterra ODEs with spatial diffusion. However, I am running into issues with using multiple libraries. Is there a way to restrict and specify the feature libraries for each PDE in the system?

The equations to be learned are of the form: u' = f(u,v) + u{xx} v' = g(u,v) + v{xx} Can one specify the feature libraries for each ODE in the system? For example, u' should only include library terms with u, u{x}, uu{x}, u^2, etc BUT NOT v{x} v{xx} and v' should only include library terms with v, v{x}, vv{x}, v^2 but not u{x} u{xx} etc? Ideally we would input this as a 2d library, e.g., [u{x},0] rather than letting u{x} appear in both equations. I haven't yet found an example like this but perhaps I am missing something. Thanks!

Jacob-Stevens-Haas commented 1 year ago

No, I don't think so. You can either make two SINDy models, e.g. one for $u$ where $v$ is a control input and vice versa, or you can let the models fit with the full library terms. By design, the optimizers promoting sparsity will hopefully ignore the insignificant terms.

calinacopos commented 1 year ago

In your opinion is this something implementable in the existing pysindy framework or is it impossible due to the philosophy of the approach? And could you elaborate on what you mean about using v as is a control input.

znicolaou commented 1 year ago

@calinacopos , you can probably use the ConstrainedSR3Optimizer class to accomplish this, but it will take a little bit of effort to figure out the input syntax. See the example in the first example notebook (http://localhost:8888/notebooks/examples/1_feature_overview.ipynb) and set up a PDELibrary with all the functions you'd like to use in all equations. Then make constraints to zero out the ones you don't want in each equation.

Jacob-Stevens-Haas commented 1 year ago

is it impossible due to the philosophy of the approach?

It's not impossible, it's just a lower priority. SINDy was created with the goal of learning unknown differential equations, rather than finding the coefficients of known terms. As a result, we rely on the sparse optimizer to reject the incorrect terms.

It is possible, though, mainly for testing other aspects of the library against known ODEs:

could you elaborate on what you mean about using v as is a control input.

Control inputs are variables that are used in the functions, but for which a regression is not performed. Shown below, but be aware that argument names are a bit counter to your case: we call the independent variable t, the dependent variable x, and control inputs u.

f_lib = CustomLibrary(<pass in your own functions for f here>)
pde_lib = PDELibrary(spatial_grid=x, derivative_order=2)
function_library = GeneralizedLibrary([f_lib, pde_lib], inputs_per_library=np.array([[0, 1], [0, 0]])
model.fit(x=u, t=x, u=v)

Then you'd make a different model swapping u and v