# `o2pls` — O2-PLS (two-way orthogonal)
_Group_: **Multi-block / cross-modal** · _Registry tolerance_: `1e-10`
## Description
O2PLS — bi-directional OPLS (Trygg & Wold 2003)
From the `pls4all.sklearn.O2PLSRegression` docstring:
> O2-PLS (bi-directional OPLS, Trygg & Wold 2003).
> **Registry note** — R `OmicsPLS::o2m` 2.1.0 (Bouhaddani 2018) joint-SVD O2PLS. pls4all defaults to the OmicsPLS::o2m algorithm and matches R bit-for-bit (~1e-13 max_abs). The pre-0.97 peel-then-PLS path (Trygg & Wold 2003 §3.2 power-iteration recipe) is reachable via the `legacy=True` adapter kwarg / `cfg.solver = NIPALS`.
### Parameters
| Name | Type | Default | Notes |
|------|------|---------|-------|
| `n_predictive` | `int` | `2` | Number of joint (predictive) components shared by X and Y in O2-PLS. |
| `n_x_orthogonal` | `int` | `1` | Number of X-orthogonal components (Y-irrelevant structure in X). |
| `n_y_orthogonal` | `int` | `1` | Number of Y-orthogonal components (X-irrelevant structure in Y). |
| `n_targets` | `int` | `4` | registry benchmark cell value |
## Explanations
### Bibliographic source
Trygg, J. & Wold, S. (2003). *O2-PLS, a two-block (X–Y) latent variable regression method with an integral OSC filter*. Journal of Chemometrics 17(1), 53–64.
### Mathematical principle
O2-PLS extends OPLS symmetrically to both $\mathbf{X}$ and $\mathbf{Y}$: it decomposes each block into a joint predictive component plus block-orthogonal components. Unlike OPLS, which is asymmetric ($\mathbf{Y}$ drives the decomposition of $\mathbf{X}$), O2-PLS treats both matrices as observation blocks of equal status.
Required hyperparameters: $n_{\mathrm{pred}}$ (joint components), $n_{\mathrm{X,ortho}}$ (X-unique orthogonal), $n_{\mathrm{Y,ortho}}$ (Y-unique orthogonal). Choosing all three by cross-validation is a 3-D grid which can be expensive; a common compromise fixes the orthogonal counts at 1 and tunes only $n_{\mathrm{pred}}$.
O2-PLS is dominant in metabolomics ↔ transcriptomics integration where the analyst wants to disentangle platform-specific orthogonal variation from biology that is consistent across platforms.
### Implementation
`n4m_o2pls_fit`. Reference: CRAN `OmicsPLS 2.1.0`.
MATLAB header (`bindings/matlab/+pls4all/O2plsRegression.m`):
```text
pls4all.O2plsRegression O2-PLS (bi-directional OPLS).
```
### 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 */
n4m_context_t* ctx = n4m_context_create();
n4m_config_t* cfg = n4m_config_create();
n4m_method_result_t* res = NULL;
n4m_o2pls_fit(ctx, cfg, &x_view, &y_view, /* hyperparams */, &res);
/* … read coefficients / mask / scores via */
/* n4m_method_result_get_double_matrix / vector / scalar … */
n4m_method_result_destroy(res);
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._methods import o2pls_fit
with pls4all.Context() as ctx, pls4all.Config() as cfg:
res = o2pls_fit(ctx, cfg, X, y)
# then: res.matrix("predictions"), res.matrix("coefficients"),
# res.vector("mask"), res.scalar("intercept"), …
```
:::
:::{tab-item} Python · pls4all.sklearn
:sync: python-sklearn
:class-label: lang-python
```python
from pls4all.sklearn import O2PLSRegression
mdl = O2PLSRegression(n_predictive=2, n_x_orthogonal=1, n_y_orthogonal=1)
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("o2pls", X, y,
n_components = 2L, params = list(n_targets = 4L, n_predictive = 2L, n_x_orthogonal = 1L, n_y_orthogonal = 1L))
# 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.o2pls(X, y, 2);
% see header of bindings/matlab/+pls4all/o2pls.m for full
% parameter surface:
% res = o2pls(X, Y, n_predictive, n_x_orthogonal, n_y_orthogonal)
yhat = predict(res, Xtest);
```
:::
:::{tab-item} MATLAB · pls4all (classdef)
:sync: matlab-classdef
:class-label: lang-matlab
```matlab
mdl = pls4all.fit("o2pls", X, y, "NumComponents", 2);
yhat = predict(mdl, Xtest);
```
:::
::::
**Registry parity references** 📐
:::{card}
:class-card: external-refs
- 📐 **`ref.r_omicspls`** (R · r) — `OmicsPLS` 2.1.0 · strict (rmse_rel ≤ 1e-10) — R `OmicsPLS::o2m` (Bouhaddani 2018), joint-SVD O2PLS. pls4all's default O2PLS path now matches this algorithm bit-for-bit (max_abs ~1e-13 on the parity sizes); the legacy peel-then-PLS implementation is opt-in.
:::
### 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**: strict — numeric equivalence (`rmse_rel_tol ≤ 1e-10`).
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×30 (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 | ≈ +4e-10 | 3.13 ms | 1.39 ms | 12.8 ms | 70.1 ms | 1.32 ms | 2.73 ms | 6.52 ms🏆 | 72.7 ms | 344.6 ms | 35.1 ms | 350.3 ms | 1.7 s | 138.6 ms | 1.3 s |
pls4all.cpp.blas+omp | ≈ +4e-10 | 3.12 ms | 1.64 ms | 13.8 ms | 70.9 ms | 1.32 ms | 3.10 ms | 6.65 ms | 70.7 ms | 341.8 ms | 35.1 ms | 349.9 ms | 1.6 s🏆 | 138.0 ms | 1.2 s |
pls4all.cpp.omp | ≈ +4e-10 | 2.96 ms | 1.78 ms | 12.8 ms | 71.6 ms | 1.36 ms | 2.78 ms | 7.18 ms | 72.3 ms | 349.2 ms | 34.8 ms🏆 | 359.4 ms | 1.6 s | 131.6 ms🏆 | 1.2 s🏆 |
pls4all.cpp.ref | ≈ +4e-10 | 2.80 ms | 1.22 ms🏆 | 11.8 ms🏆 | 69.6 ms🏆 | 1.29 ms🏆 | 3.00 ms | 7.63 ms | 69.2 ms🏆 | 341.1 ms🏆 | 38.6 ms | 349.4 ms🏆 | 1.7 s | 144.4 ms | 1.3 s |
| Python · pls4all |
pls4all.python | ✓ bind | 2.76 ms🏆 | — | — | — | 1.44 ms | 2.59 ms🏆 | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ 2e-15 | 2.82 ms | — | — | — | 1.77 ms | 3.15 ms | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✗ +1e+00 | 11.8 ms | — | — | — | 4.55 ms | 10.7 ms | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✗ +1e+00 | 19.0 ms | — | — | — | 5.90 ms | 8.46 ms | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✗ +1e+00 | 17.6 ms | — | — | — | 5.34 ms | 10.6 ms | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✗ +1e+00 | 20.8 ms | — | — | — | 5.33 ms | 9.57 ms | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +9e+00 | 4.82 ms | — | — | — | 2.23 ms | 4.38 ms | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +9e+00 | 4.95 ms | — | — | — | 2.85 ms | 7.91 ms | — | — | — | — | — | — | — | — |
| R · external |
📐ref.r_omicspls | source | 24.4 ms | — | — | — | 11.1 ms | 13.4 ms | — | — | — | — | — | — | — | — |
:::
:::{tab-item} 3 threads
:sync: threads-3
| Backend | Parity | 50×250 (ms) | 100×50 (ms) | 100×500 (ms) | 100×2500 (ms) | 200×30 (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 4e-10 | — | — | — | — | 1.47 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.blas+omp | ✓ ref 4e-10 | — | — | — | — | 1.30 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.omp | ✓ ref 4e-10 | — | — | — | — | 1.29 ms🏆 | — | — | — | — | — | — | — | — | — |
pls4all.cpp.ref | ✓ ref 4e-10 | — | — | — | — | 1.40 ms | — | — | — | — | — | — | — | — | — |
| Python · pls4all |
pls4all.python | ✓ bind | — | — | — | — | 1.33 ms | — | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ 9e-16 | — | — | — | — | 1.82 ms | — | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✗ +1e+00 | — | — | — | — | 4.15 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✗ +1e+00 | — | — | — | — | 5.40 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✗ +1e+00 | — | — | — | — | 5.42 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✗ +1e+00 | — | — | — | — | 5.30 ms | — | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +9e+00 | — | — | — | — | 2.31 ms | — | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +9e+00 | — | — | — | — | 3.04 ms | — | — | — | — | — | — | — | — | — |
| R · external |
📐ref.r_omicspls | source | — | — | — | — | 12.0 ms | — | — | — | — | — | — | — | — | — |
:::
:::{tab-item} 10 threads
:sync: threads-10
| Backend | Parity | 50×250 (ms) | 100×50 (ms) | 100×500 (ms) | 100×2500 (ms) | 200×30 (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 4e-10 | — | — | — | — | 1.22 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.blas+omp | ✓ ref 4e-10 | — | — | — | — | 1.23 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.omp | ✓ ref 4e-10 | — | — | — | — | 1.21 ms🏆 | — | — | — | — | — | — | — | — | — |
pls4all.cpp.ref | ✓ ref 4e-10 | — | — | — | — | 1.23 ms | — | — | — | — | — | — | — | — | — |
| Python · pls4all |
pls4all.python | ✓ bind | — | — | — | — | 1.46 ms | — | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ 9e-16 | — | — | — | — | 1.41 ms | — | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✗ +1e+00 | — | — | — | — | 3.04 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✗ +1e+00 | — | — | — | — | 3.74 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✗ +1e+00 | — | — | — | — | 3.67 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✗ +1e+00 | — | — | — | — | 3.67 ms | — | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +9e+00 | — | — | — | — | 2.01 ms | — | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +9e+00 | — | — | — | — | 2.53 ms | — | — | — | — | — | — | — | — | — |
| R · external |
📐ref.r_omicspls | source | — | — | — | — | 7.33 ms | — | — | — | — | — | — | — | — | — |
:::
::::
---
_See also_: [benchmark overview](../benchmarks/overview.md) · [methods index](index.md) · [interactive dashboard](../landing/dashboard.md)