dynadojo.baselines.sindy.SINDy#
- class dynadojo.baselines.sindy.SINDy#
Bases:
AbstractAlgorithm
Sparse Identification of Nonlinear Dynamical systems (SINDy). Wrapper for ``pysindy``[1]_.
References
Note
For an example of how to use
SINDy
with a challenge, seeLorenzSystem
.Example
from dynadojo.systems.lorenz import LorenzSystem from dynadojo.wrappers import SystemChecker, AlgorithmChecker from dynadojo.utils.lds import plot from dynadojo.baselines.sindy import SINDy latent_dim = 3 embed_dim = latent_dim n = 50 test_size = 10 timesteps = 50 system = SystemChecker(LorenzSystem(latent_dim, embed_dim, noise_scale=0, seed=1912)) x0 = system.make_init_conds(n) y0 = system.make_init_conds(30, in_dist=False) x = system.make_data(x0, timesteps=timesteps) y = system.make_data(y0, timesteps=timesteps, noisy=True) plot([x, y], target_dim=min(latent_dim, 3), labels=["IND", "OOD"], max_lines=test_size)
sindy = AlgorithmChecker(SINDy(embed_dim, timesteps, max_control_cost=0, seed=100)) sindy.fit(x) y_pred = sindy.predict(y[:, 0], timesteps) y_err = system.calc_error(y, y_pred) print(f"{y_err=}") plot([y_pred, y], target_dim=min(3, latent_dim), labels=["pred", "true"], max_lines=15)
The error should be around
5.38
and the prediction looks like this:Methods
__init__
(embed_dim, timesteps[, ...])Initialize the class.
act
(x, **kwargs)Determines the control for each action horizon.
fit
(x, **kwargs)Fits the algorithm on a tensor of trajectories.
predict
(x0, timesteps, **kwargs)Predict how initial conditions matrix evolves over a given number of timesteps.
Attributes
The embedded dimension of the dynamics.
The maximum control cost.
The random seed for the algorithm.
The timesteps per training trajectory.
- __init__(embed_dim, timesteps, max_control_cost=0, differentiation_method=None, **kwargs)#
Initialize the class.
- Parameters:
embed_dim (int) – The embedded dimension of the system. Recommended to keep embed dimension small (e.g., <10).
timesteps (int) – The timesteps of the training trajectories. Must be greater than 2.
differentiation_method (str, optional) – The differentiation used in SINDy. See PySINDy documentation for more details.
max_control_cost (float, optional) – Ignores control, so defaults to 0.
- act(x, **kwargs)#
Determines the control for each action horizon. control.
- Parameters:
x (numpy.ndarray) – (n, timesteps, embed_dim) Trajectories tensor.
**kwargs – Additional keyword arguments.
- Returns:
(n, timesteps, embed_dim) controls tensor.
- Return type:
numpy.ndarray
- property embed_dim#
The embedded dimension of the dynamics.
- fit(x, **kwargs)#
Fits the algorithm on a tensor of trajectories.
- Parameters:
x (np.ndarray) – (n, timesteps, embed_dim) Trajectories tensor.
**kwargs – Additional keyword arguments.
- Return type:
None
- property max_control_cost#
The maximum control cost.
- predict(x0, timesteps, **kwargs)#
Predict how initial conditions matrix evolves over a given number of timesteps.
Note
The timesteps argument can differ from the ._timesteps attribute. This allows algorithms to train on a dataset of a given size and then predict trajectories of arbitrary lengths.
Note
The first coordinate of each trajectory should match the initial condition x0.
- Parameters:
x0 (np.ndarray) – (n, embed_dim) initial conditions matrix
timesteps (int) – timesteps per predicted trajectory
**kwargs – Additional keyword arguments.
- Returns:
(n, timesteps, embed_dim) trajectories tensor
- Return type:
np.ndarray
- property seed#
The random seed for the algorithm.
- property timesteps#
The timesteps per training trajectory.