# `opls` — Orthogonal PLS (OPLS)
_Group_: **Core PLS** · _Registry tolerance_: `0.001`
## Description
Orthogonal PLS (Trygg & Wold 2002)
From the `pls4all.sklearn.OPLSRegression` docstring:
> Orthogonal PLS regression (Trygg & Wold 2002).
> **Registry note** — Bioconductor `ropls::opls` is the external OPLS reference; convergence and orthogonal-component conventions may differ.
### Parameters
| Name | Type | Default | Notes |
|------|------|---------|-------|
| `n_components` | `int` | `2` | Number of latent components extracted (k). |
| `solver` | `str` | `'nipals'` | Inner algorithm: 'nipals', 'simpls', 'svd', 'kernel', 'orthogonal-scores', 'power', 'randomized-svd', 'wide-kernel'. |
| `center_x` | `bool` | `True` | Subtract the column mean of X before fitting. |
| `scale_x` | `bool` | `True` | Standardize X columns to unit variance before fitting. |
| `center_y` | `bool` | `True` | Subtract the column mean of y before fitting. |
| `scale_y` | `bool` | `False` | Standardize y columns to unit variance before fitting. |
| `tol` | `float` | `1e-06` | Convergence tolerance for iterative solvers (NIPALS / power-iteration). |
| `max_iter` | `int` | `500` | Maximum iterations for iterative solvers. |
| `store_scores` | `bool` | `False` | If True, keep the latent score matrix (`x_scores_`) after fit. |
## Explanations
### Bibliographic source
Trygg, J. & Wold, S. (2002). *Orthogonal projections to latent structures (O-PLS)*. Journal of Chemometrics 16(3), 119–128.
### Mathematical principle
OPLS rotates the standard PLS latent space so that a single direction captures all $\mathbf{Y}$-correlated variation while the remaining components capture $\mathbf{Y}$-orthogonal structural variation in $\mathbf{X}$. The resulting decomposition $\mathbf{X} = \mathbf{t}_p\mathbf{p}_p^{\top} + \mathbf{T}_o\mathbf{P}_o^{\top} + \mathbf{E}$ separates the **predictive component** $\mathbf{t}_p$ from the orthogonal block $\mathbf{T}_o$, which absorbs spectroscopic baselines, scatter and other nuisance factors that confound interpretation of the predictive loading.
Numerically OPLS proceeds by NIPALS-deflating $\mathbf{X}$ against directions orthogonal to $\mathbf{X}^{\top}\mathbf{y}$ before each new predictive component is extracted. Predictions are identical to those of a one-component PLS on the orthogonal-corrected $\mathbf{X}$; the value is in **the interpretation of the loadings**, not in better predictions per se.
OPLS shines in metabolomics and process spectroscopy where the spectra carry strong systematic but non-predictive variation; in those settings the single-vector predictive loading is far easier to relate to biology / chemistry than a multi-component PLS loading matrix.
### Implementation
`Algorithm.OPLS` + `Solver.NIPALS` + `Deflation.ORTHOGONAL`. Reference: Bioconductor `ropls::opls`. Note: orthogonal-component ordering and the criterion that stops orthogonal extraction differ between implementations — exact bit parity is not expected, but RMSE-rel parity within ~1e-3 is.
MATLAB header (`bindings/matlab/+pls4all/OplsRegression.m`):
```text
pls4all.OplsRegression — Orthogonal Partial Least Squares Regression model.
Example:
mdl = pls4all.OplsRegression(X, y, 5);
yhat = predict(mdl, Xnew);
```
### Usage
Every pls4all binding tab dispatches into the same C kernel; the external libraries listed at the bottom of the page are the parity references registered in `benchmarks.parity_timing.registry`. Switch tabs to read the same fit in your language. The R package now ships drop-in-compatible facades for the CRAN `pls` package (`plsr`, `pcr`, `mvr`) and for the `mdatools::pls(x, y, ...)` matrix idiom — those tabs appear only on the methods that have a meaningful equivalence.
**pls4all bindings**
::::{tab-set}
:class: pls4all-bindings
:::{tab-item} C ABI · libn4m
:sync: c
:class-label: lang-c
```c
/* C ABI — libn4m (Model.fit path) */
n4m_context_t* ctx = n4m_context_create();
n4m_config_t* cfg = n4m_config_create();
n4m_config_set_algorithm(cfg, N4M_ALGORITHM_PLS_REGRESSION);
n4m_config_set_solver (cfg, N4M_SOLVER_SIMPLS);
n4m_config_set_n_components(cfg, 4);
n4m_model_t* mdl = NULL;
n4m_model_fit(ctx, cfg, &x_view, &y_view, &mdl);
n4m_model_predict(ctx, mdl, &x_test_view, &y_hat_view);
n4m_model_destroy(mdl);
n4m_config_destroy(cfg);
n4m_context_destroy(ctx);
```
:::
:::{tab-item} Python · pls4all (raw)
:sync: python-raw
:class-label: lang-python
```python
import pls4all
from pls4all import Algorithm, Solver
with pls4all.Context() as ctx, pls4all.Config() as cfg:
cfg.algorithm = Algorithm.PLS_REGRESSION
cfg.solver = Solver.SIMPLS
cfg.n_components = 4
with pls4all.Model.fit(ctx, cfg, X, y) as mdl:
y_hat = mdl.predict(X_test)
```
:::
:::{tab-item} Python · pls4all.sklearn
:sync: python-sklearn
:class-label: lang-python
```python
from pls4all.sklearn import OPLSRegression
mdl = OPLSRegression(n_components=2, solver='nipals', center_x=True, scale_x=True, center_y=True, scale_y=False, tol=1e-06, max_iter=500, store_scores=False)
mdl.fit(X, y)
y_hat = mdl.predict(X_test)
```
:::
:::{tab-item} R · pls4all_method()
:sync: r-dispatcher
:class-label: lang-r
```r
library(pls4all)
# Unified low-level dispatcher (May 2026 R cleanup):
res <- pls4all_method("opls", X, y,
n_components = 4L)
# res is a named list with MethodResult arrays/scalars.
# selected_indices / top_k_intervals are 1-based.
```
:::
:::{tab-item} MATLAB · pls4all (MEX)
:sync: matlab-mex
:class-label: lang-matlab
```matlab
res = pls4all.opls(X, y, 4);
% see header of bindings/matlab/+pls4all/opls.m for full
% parameter surface:
% [coefs, x_mean, y_mean, predictions] = opls(X, Y, n_components)
yhat = predict(res, Xtest);
```
:::
:::{tab-item} MATLAB · pls4all (classdef)
:sync: matlab-classdef
:class-label: lang-matlab
```matlab
mdl = pls4all.fit("opls", X, y, "NumComponents", 4);
yhat = predict(mdl, Xtest);
```
:::
::::
**Registry parity references** 📐
:::{card}
:class-card: external-refs
- 📐 **`ref.r_ropls`** (R · r) — `ropls` Bioc · relaxed (rmse_rel ≤ 1e-03) — Bioconductor `ropls::opls` — OPLS reference. Permutations and plotting are disabled in benchmark timing; ropls still requires crossvalI >= 1 for a finite Q2 path.
:::
### Benchmarks
Adaptive wall-clock per cell measured against [`full_matrix.csv`](../benchmarks/overview.md). Only backends that implement this method are listed; libraries without the method are omitted.
**Verdict** · ✓ ref / ≈ ref / ~ shape mark a reference-gate pass at strict / relaxed / qualitative tolerance · ✓ bind = pls4all binding agrees with the C++ baseline · ✗ divergent · ⚠ error · — not run. The fastest backend per column is marked 🏆.
**Reference gate**: relaxed — known algorithmic drift between pls4all and the external reference (`rmse_rel_tol ≤ 1e-03`).
Rows tagged with **📐** are the canonical parity references for this method (declared in [`parity_timing.registry`](../benchmarks/methodology.md)). C++ and external rows show reference parity; pls4all language bindings show binding parity against the C++ backend. Hover the icon for role and tolerance band.
::::{tab-set}
:class: parity-tabs
:::{tab-item} 1 thread
:sync: threads-1
| Backend | Parity | 50×250 (ms) | 100×50 (ms) | 100×500 (ms) | 100×2500 (ms) | 200×50 (ms) | 250×50 (ms) | 500×50 (ms) | 500×500 (ms) | 500×2500 (ms) | 2500×50 (ms) | 2500×500 (ms) | 2500×2500 (ms) | 10000×50 (ms) | 10000×500 (ms) |
| C++ native · libn4m |
pls4all.cpp.blas | ≈ +5e-15 | 2.79 ms🏆 | 2.47 ms | 20.1 ms | 169.3 ms | 1.92 ms | 2.58 ms | 6.51 ms🏆 | 71.3 ms🏆 | 434.4 ms | 36.1 ms | 413.0 ms🏆 | 2.3 s | 148.5 ms | 1.8 s🏆 |
pls4all.cpp.blas+omp | ≈ +5e-15 | 3.29 ms | 2.51 ms | 15.7 ms🏆 | 158.4 ms🏆 | 2.01 ms | 2.73 ms | 7.33 ms | 75.0 ms | 433.1 ms🏆 | 34.7 ms | 427.0 ms | 2.2 s | 151.5 ms | 1.8 s |
pls4all.cpp.omp | ≈ +5e-15 | 3.16 ms | 2.31 ms | 18.1 ms | 166.0 ms | 1.90 ms | 3.06 ms | 6.86 ms | 72.7 ms | 446.7 ms | 33.7 ms🏆 | 430.8 ms | 2.3 s | 144.3 ms🏆 | 1.9 s |
pls4all.cpp.ref | ≈ +5e-15 | 3.01 ms | 2.26 ms🏆 | 18.6 ms | 162.0 ms | 1.88 ms🏆 | 2.72 ms | 7.46 ms | 75.1 ms | 456.7 ms | 36.4 ms | 414.1 ms | 2.2 s🏆 | 151.6 ms | 1.8 s |
| Python · pls4all |
pls4all.python | ✓ bind | 2.82 ms | — | — | — | 2.16 ms | 2.50 ms🏆 | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ bind | 3.21 ms | — | — | — | 3.97 ms | 2.78 ms | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✓ 1e-13 | 11.3 ms | — | — | — | 6.83 ms | 7.92 ms | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✓ 1e-13 | 19.8 ms | — | — | — | 9.63 ms | 8.97 ms | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✓ 1e-13 | 19.3 ms | — | — | — | 9.15 ms | 9.43 ms | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✓ 1e-13 | 18.0 ms | — | — | — | 9.77 ms | 8.58 ms | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +9e+00 | 5.58 ms | — | — | — | 3.26 ms | 5.85 ms | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +9e+00 | 5.68 ms | — | — | — | 3.69 ms | 4.53 ms | — | — | — | — | — | — | — | — |
| R · external |
📐ref.r_ropls | ≈ ref | — | — | — | — | 21.7 ms | — | — | — | — | — | — | — | — | — |
:::
:::{tab-item} 3 threads
:sync: threads-3
| Backend | Parity | 50×250 (ms) | 100×50 (ms) | 100×500 (ms) | 100×2500 (ms) | 200×50 (ms) | 250×50 (ms) | 500×50 (ms) | 500×500 (ms) | 500×2500 (ms) | 2500×50 (ms) | 2500×500 (ms) | 2500×2500 (ms) | 10000×50 (ms) | 10000×500 (ms) |
| C++ native · libn4m |
pls4all.cpp.blas | ≈ ref 5e-15 | — | — | — | — | 2.99 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.blas+omp | ≈ ref 5e-15 | — | — | — | — | 1.85 ms🏆 | — | — | — | — | — | — | — | — | — |
pls4all.cpp.omp | ≈ ref 5e-15 | — | — | — | — | 2.86 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.ref | ≈ ref 5e-15 | — | — | — | — | 3.39 ms | — | — | — | — | — | — | — | — | — |
| Python · pls4all |
pls4all.python | ✓ 2e-14 | — | — | — | — | 2.60 ms | — | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ 2e-14 | — | — | — | — | 2.37 ms | — | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✓ bind | — | — | — | — | 6.27 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✓ bind | — | — | — | — | 9.34 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✓ bind | — | — | — | — | 8.89 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✓ bind | — | — | — | — | 7.78 ms | — | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +9e+00 | — | — | — | — | 3.38 ms | — | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +9e+00 | — | — | — | — | 4.94 ms | — | — | — | — | — | — | — | — | — |
| R · external |
📐ref.r_ropls | source | — | — | — | — | 22.0 ms | — | — | — | — | — | — | — | — | — |
:::
:::{tab-item} 10 threads
:sync: threads-10
| Backend | Parity | 50×250 (ms) | 100×50 (ms) | 100×500 (ms) | 100×2500 (ms) | 200×50 (ms) | 250×50 (ms) | 500×50 (ms) | 500×500 (ms) | 500×2500 (ms) | 2500×50 (ms) | 2500×500 (ms) | 2500×2500 (ms) | 10000×50 (ms) | 10000×500 (ms) |
| C++ native · libn4m |
pls4all.cpp.blas | ≈ ref 5e-15 | — | — | — | — | 1.81 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.blas+omp | ≈ ref 5e-15 | — | — | — | — | 1.80 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.omp | ≈ ref 5e-15 | — | — | — | — | 1.80 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.ref | ≈ ref 5e-15 | — | — | — | — | 1.75 ms🏆 | — | — | — | — | — | — | — | — | — |
| Python · pls4all |
pls4all.python | ✓ 2e-14 | — | — | — | — | 1.83 ms | — | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ 2e-14 | — | — | — | — | 1.95 ms | — | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✓ bind | — | — | — | — | 4.96 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✓ bind | — | — | — | — | 6.77 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✓ bind | — | — | — | — | 6.29 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✓ bind | — | — | — | — | 6.65 ms | — | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +9e+00 | — | — | — | — | 3.05 ms | — | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +9e+00 | — | — | — | — | 3.27 ms | — | — | — | — | — | — | — | — | — |
| R · external |
📐ref.r_ropls | source | — | — | — | — | 17.3 ms | — | — | — | — | — | — | — | — | — |
:::
::::
---
_See also_: [benchmark overview](../benchmarks/overview.md) · [methods index](index.md) · [interactive dashboard](../landing/dashboard.md)