Ridge¶
- class kooplearn.linear_model.Ridge(n_components=None, *, lag_time=1, reduced_rank=True, alpha=1e-06, eigen_solver='auto', tol=0, max_iter=None, iterated_power='auto', n_oversamples=5, random_state=None, copy_X=True)[source]¶
Bases:
BaseEstimatorLinear model minimizing the \(L^{2}\) loss.
Implements a model approximating the Koopman (deterministic systems) or Transfer (stochastic systems) operator by lifting the state with a nonlinear feature map and then minimizing the \(L^{2}\) loss in the embedded space as described in Kostic et al. [1].
Tip
The dynamical modes obtained by calling
kooplearn.linear_model.Ridge.dynamical_modescorrespond to the Extended Dynamical Mode Decomposition by Williams et al. [2].- Parameters:
n_components (
intorNone, defaultNone) – Number of components to retain. IfNone, all components are used.lag_time (
int, default1) – Time delay between the pairs of snapshots \((X_t, X_{t + \text{lag_time}})\) used to train the estimator.reduced_rank (
bool, defaultTrue) – Whether to use reduced-rank regression introduced in Kostic et al. [1]. IfFalse, initializes the classical principal component estimator.alpha (
floatorNone, default1e-6) – Tikhonov (ridge) regularization coefficient.Noneis equivalent toalpha = 0, and internally calls specialized stable algorithms to deal with this specific case.eigen_solver (
{'auto', 'dense', 'arpack', 'randomized'}, default'auto') –Solver used to perform the internal SVD calculations. If
n_componentsis much less than the number of training samples,randomized(orarpackto a smaller extent) may be more efficient than thedensesolver.- auto :
the solver is selected automatically based on the number of samples and components: if the number of components to extract is less than 10 (strict) and the number of samples is more than 200 (strict), the
arpackmethod is enabled. Otherwise the exact full eigenvalue decomposition is computed and optionally truncated afterwards (densemethod).- dense :
run exact full eigenvalue decomposition calling the standard LAPACK solver via
scipy.linalg.eigh, and select the components by postprocessing.- arpack :
run SVD truncated to
n_componentscalling ARPACK solver usingscipy.sparse.linalg.eigsh. It requires strictly0 < n_components < n_samples.- randomized :
run randomized SVD as described in Turri et al. [3].
tol (
float, default0) – Convergence tolerance for arpack. If 0, optimal value is chosen automatically by arpack.max_iter (
intorNone, defaultNone) – Maximum number of iterations for arpack. IfNone, optimal value is chosen automatically by arpack.iterated_power (
{'auto'}orint, default'auto') – Number of iterations for the power method computed byeigen_solver == 'randomized'. When'auto', it is set to 7 whenn_components < 0.1 * min(X.shape), otherwise it is set to 4.n_oversamples (
int, default5) – Number of oversamples when using a randomized algorithm (eigen_solver == 'randomized').random_state (
int,RandomState instanceorNone, defaultNone) – Used wheneigen_solveris'arpack'or'randomized'. Pass an int for reproducible results across multiple function calls.copy_X (
bool, defaultTrue) – IfTrue, input X is copied and stored by the model in theX_fit_attribute. If no further changes will be done to X, settingcopy_X=Falsesaves memory by storing a reference.
- Variables:
X_fit (
ndarrayofshape (n_samples,n_features)) – The data used to fit the model. Ifcopy_X=False, thenX_fit_is a reference to the original data.y_fit (
ndarrayofshape (n_samples,)orNone) – The observable used to fit the model. If no observable is provided during fitting, this attribute isNone.cov_X (
ndarrayofshape (n_features,n_features)) – Covariance matrix evaluated at the initial states, that is \(x_t\).cov_Y (
ndarrayofshape (n_features,n_features)) – Covariance matrix evaluated at the evolved states, that is \(x_{t+1}\).cov_XY (
ndarrayofshape (n_features,n_features)) – Cross-covariance matrix between initial and evolved states.U (
ndarrayofshape (n_features,n_components)) – Projection matrix. The evolution operator is approximated as \(U U^T \mathrm{cov_{XY}}\).estimator (
ndarrayofshape (n_features,n_features)) – Least-squares estimator \(U U^T \mathrm{cov_{XY}}\).rank (
int) – Effective rank of the fitted estimator.
Examples
>>> from kooplearn.datasets import make_linear_system >>> from kooplearn.linear_model import Ridge >>> import numpy as np >>> >>> # Generate a linear system >>> A = np.array([[0.9, 0.1], [-0.1, 0.9]]) >>> X0 = np.array([1.0, 0.0]) >>> data = make_linear_system(X0, A, n_steps=100, noise=0.1, random_state=42).to_numpy() >>> >>> # Fit the model >>> model = Ridge(n_components=2, alpha=1e-3) >>> model = model.fit(data) >>> >>> # Predict the future state >>> pred = model.predict(data) >>> >>> # Get the eigenvalues of the Koopman operator >>> eigvals = model.eig()
Methods
- dynamical_modes(X, observable=False) DynamicalModes[source]¶
Compute the mode decomposition of arbitrary observables of the evolution operator at the states defined by
X. If \((\lambda_i, \xi_i, \psi_i)_{i = 1}^{r}\) are eigentriplets of the evolution operator, for any observable \(f\) the i-th mode of \(f\) at \(x\) is defined as: \(\lambda_i \langle \xi_i, f \rangle \psi_i(x)\). See Kostic et al. [1] for more details.- Parameters:
X (
ndarrayofshape (n_samples,n_features)) – States at which to evaluate the modes.observable (
bool, defaultFalse) – IfTrue, computes the modes of the observable rather than those of the system state.
- Returns:
Object containing the eigenvalues, modes, and their projections. See
kooplearn.structs.DynamicalModesfor details.- Return type:
DynamicalModes
- eig(eval_left_on=None, eval_right_on=None)[source]¶
Compute the eigendecomposition of the learned evolution operator.
This method returns the eigenvalues of the estimated evolution operator, and optionally evaluates the corresponding left and/or right eigenfunctions on user-provided data.
- Parameters:
eval_left_on (
ndarrayofshape (n_samples,n_features), optional) – Data points on which to evaluate the left eigenfunctions. IfNone, left eigenfunctions are not evaluated.eval_right_on (
ndarrayofshape (n_samples,n_features), optional) – Data points on which to evaluate the right eigenfunctions. IfNone, right eigenfunctions are not evaluated.
- Returns:
eigenvalues (
ndarrayofshape (n_components,)) – Eigenvalues of the estimated operator.left_eigenfunctions (
ndarrayofshape (n_samples,n_components), optional) – Values of the left eigenfunctions evaluated oneval_left_on. Returned only ifeval_left_onis provided.right_eigenfunctions (
ndarrayofshape (n_samples,n_components), optional) – Values of the right eigenfunctions evaluated oneval_right_on. Returned only ifeval_right_onis provided.
- fit(X, y=None)[source]¶
Fit the linear model using the selected algorithm.
Depending on the model parameters, this method fits the estimator using either a randomized or non-randomized algorithm, and either a full-rank or reduced-rank regression approach.
- Parameters:
X (
ndarrayofshape (n_samples,n_features)) – Training trajectory data.y (
ndarrayofshape (n_samples,n_features_out), defaultNone) – Optional observable used for training. IfNone, the observable is assumed to be the state itself.
- Returns:
self – Fitted model instance.
- Return type:
object
- predict(X, n_steps=1, observable=False) ndarray[source]¶
Predict the system state or its expected value after
n_steps.Computes the predicted state — or, in the case of a stochastic system, the expected value \(\mathbb{E}[X_t \mid X_0 = X]\) — after
t = n_stepstime steps given the initial conditionsX. Ifobservable=True, returns the corresponding predicted observable instead of the state.- Parameters:
X (
ndarrayofshape (n_samples,n_features)) – Initial conditions used for prediction.n_steps (
int, default1) – Number of future time steps to predict. Only the final predicted state (at timet = n_steps) is returned.observable (
bool, defaultFalse) – IfTrue, returns the predicted observable at time \(t\) instead of the system state.
- Returns:
Predicted (expected) state or observable at time \(t = n_steps\).
- Return type:
ndarrayofshape (n_samples,n_features)
- risk(X=None)[source]¶
Compute the estimator risk.
- Parameters:
X (
ndarrayorNone, defaultNone) – Trajectory used to evaluate the risk. If None, evaluates on training data.- Returns:
Risk of the estimator.
- Return type:
float
- score(X=None, y=None, n_steps=1, observable=False, metric=<function r2_score>, **metric_kws) float[source]¶
Score the model predictions for timestep
n_steps.Computes the
metric(default issklearn.metrics.r2_score) evaluated between the model predictions at timestepn_stepsand the true system state (or observable, ifobservable=True) at the same timestep.- Parameters:
X – ndarray of shape (n_samples, n_features) or None, default=None Trajectory used to compute the score. If None, evaluates on training data.
y – ndarray of shape (n_samples, n_features) or None, default=None Optional observable used to compute the score.
n_steps – int, default=1 Number of future time steps on which to compute the score. Only the predictions at the final timestep (
t = n_steps) are compared to the true system state (or observable).observable – bool, default=False If
True, returns the predicted observable at time \(t\) instead of the system state.metric – callable (default=r2_score) The metric function used to score the model predictions.
metric_kws – dict Optional parameters to pass to the metric function.
- Returns:
- float
Metric function value for the model predictions at the next timestep.
- Return type:
score
Vladimir Kostic, Pietro Novelli, Andreas Maurer, Carlo Ciliberto, Lorenzo Rosasco, and Massimiliano Pontil. Learning dynamical systems via koopman operator regression in reproducing kernel hilbert spaces. Advances in Neural Information Processing Systems, 35:4017–4031, 2022.
Matthew O. Williams, Ioannis G. Kevrekidis, and Clarence W. Rowley. A data–driven approximation of the koopman operator: extending dynamic mode decomposition. Journal of Nonlinear Science, 25(6):1307–1346, June 2015. URL: http://dx.doi.org/10.1007/s00332-015-9258-5, doi:10.1007/s00332-015-9258-5.
Giacomo Turri, Vladimir Kostic, Pietro Novelli, and Massimiliano Pontil. A randomized algorithm to solve reduced rank operator regression. SIAM Journal on Mathematics of Data Science, 2025.