# `weighted_pls` — Sample-weighted PLS
_Group_: **Robust / weighted** · _Registry tolerance_: `0.1`
## Description
Sample-weighted PLS (sqrt(w)-prescaled NIPALS)
From the `pls4all.sklearn.WeightedPLSRegression` docstring:
> Sample-weighted PLS (sqrt(w)-prescaled SIMPLS).
> **Registry note** — sklearn PLSRegression on the sqrt(w)-prescaled centered data is mathematically equivalent to weighted PLS. Both sides default to NIPALS, matching to ~1e-12. SIMPLS is still available as an opt-in via ``cfg.solver = pls4all.Solver.SIMPLS``.
### Parameters
| Name | Type | Default | Notes |
|------|------|---------|-------|
| `n_components` | `int` | `2` | Number of latent components extracted (k). |
## Explanations
### Bibliographic source
Martens, H. & Næs, T. (1989). *Multivariate Calibration*. Wiley. §4.5 'Weighted regression for non-i.i.d. errors'.
### Mathematical principle
When the residual variance is not constant across samples — typical when calibration spectra are aggregated across instruments, sites or operators — a weighted least squares fit can dramatically improve generalisation. Weighted PLS prescales centred rows by $\sqrt{w_i}$ before extracting the SIMPLS components: $\tilde{\mathbf{X}} = \operatorname{diag}(\sqrt{w})\,\mathbf{X}_c, \quad \tilde{\mathbf{Y}} = \operatorname{diag}(\sqrt{w})\,\mathbf{Y}_c$, then runs vanilla SIMPLS on $(\tilde{\mathbf{X}}, \tilde{\mathbf{Y}})$.
Weights $w_i > 0$ encode any known per-sample reliability: inverse residual variance from a previous fit, instrument noise estimates, sample replicate counts. The weighted fit is mathematically equivalent to running standard PLS on a duplicated dataset where each row appears $w_i$ times.
This is a building block for robust PLS (IRLS over a weighted fit) and for incorporating known measurement noise into the calibration.
### Implementation
`n4m_weighted_pls_fit` exports `coefficients`, `predictions`, `x_mean` and `y_mean`; the centering vectors are weight-dependent, so replaying predictions on new samples requires reconstructing the fit intercept as `y_mean - x_mean @ coefficients`. Python reference: sklearn `PLSRegression` on the prescaled matrices.
MATLAB header (`bindings/matlab/+pls4all/WeightedPlsRegression.m`):
```text
pls4all.WeightedPlsRegression — sqrt(w)-prescaled SIMPLS.
```
### Usage
Direct `n4m` Python helper:
```python
import numpy as np
import n4m
sample_w = np.ones(X.shape[0])
res = n4m.weighted_pls(X, y, sample_weights=sample_w, n_components=4)
y_hat = res["predictions"]
coef = res["coefficients"]
```
The `n4m.sklearn.NativeWeightedPLSRegressor` wrapper replays predictions from
the returned coefficients plus reconstructed intercept. Use `sample_weights`
as an estimator parameter when training-row weights are part of the fit
contract.
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_weighted_pls_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 weighted_pls_fit
with pls4all.Context() as ctx, pls4all.Config() as cfg:
res = weighted_pls_fit(ctx, cfg, X, y, n_components=4, sample_weights=sample_w)
# 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 WeightedPLSRegression
mdl = WeightedPLSRegression(n_components=2)
mdl.fit(X, y, sample_weight=sample_w)
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("weighted_pls", 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.weighted_pls(X, y, 4);
% see header of bindings/matlab/+pls4all/weighted_pls.m for full
% parameter surface:
% res = weighted_pls(X, Y, n_components, sample_weights)
yhat = predict(res, Xtest);
```
:::
:::{tab-item} MATLAB · pls4all (classdef)
:sync: matlab-classdef
:class-label: lang-matlab
```matlab
mdl = pls4all.fit("weighted_pls", X, y, "NumComponents", 4);
yhat = predict(mdl, Xtest);
```
:::
::::
**Registry parity references** 📐
:::{card}
:class-card: external-refs
- 📐 **`ref.python_scikit_learn`** (python · python) — `scikit-learn` 1.4.2 · qualitative (rmse_rel ≤ 1e-01) — Weighted PLS computed via sklearn PLSRegression on the sqrt(w)-prescaled centered (X, Y). sklearn is the external PLS engine; the row-scaling is a standard preconditioning step that is mathematically equivalent to weighted PLS.
:::
### 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**: qualitative — shape/smoke comparison only. The external library and pls4all do not produce numerically equivalent output for this method (see the MethodSpec notes); the `rmse_rel_tol ≤ 1e-01` budget is set wide on purpose. Treat ~ shape as *“we ran both, both finished”*, not as numerical agreement.
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 | ≈ +4e-15 | 2.66 ms | 1.05 ms🏆 | 9.36 ms | 50.5 ms | 1.97 ms | 2.63 ms | 4.62 ms | 55.5 ms | 299.1 ms | 25.3 ms | 325.3 ms | 1.7 s | 105.6 ms | 1.2 s🏆 |
pls4all.cpp.blas+omp | ≈ +4e-15 | 2.62 ms | 1.05 ms | 10.2 ms | 50.2 ms | 1.95 ms🏆 | 2.55 ms | 4.51 ms🏆 | 56.9 ms | 275.1 ms🏆 | 24.7 ms🏆 | 326.2 ms | 1.7 s | 99.5 ms🏆 | 1.3 s |
pls4all.cpp.omp | ≈ +4e-15 | 2.74 ms | 1.10 ms | 9.36 ms🏆 | 56.5 ms | 1.98 ms | 2.79 ms | 4.91 ms | 50.5 ms🏆 | 280.6 ms | 24.9 ms | 324.0 ms | 1.7 s🏆 | 99.6 ms | 1.3 s |
pls4all.cpp.ref | ≈ +4e-15 | 2.52 ms🏆 | 1.98 ms | 9.66 ms | 50.0 ms🏆 | 2.10 ms | 2.55 ms🏆 | 5.06 ms | 53.8 ms | 285.6 ms | 27.0 ms | 319.1 ms🏆 | 1.7 s | 116.3 ms | 1.3 s |
| Python · pls4all |
pls4all.python | ✓ bind | 2.82 ms | — | — | — | 1.99 ms | 2.58 ms | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ 3e-13 | 2.61 ms | — | — | — | 2.35 ms | 2.62 ms | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✗ +1e-02 | 14.2 ms | — | — | — | 7.27 ms | 10.4 ms | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✗ +1e-02 | 19.7 ms | — | — | — | 10.1 ms | 10.6 ms | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✗ +1e-02 | 18.5 ms | — | — | — | 8.10 ms | 8.05 ms | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✗ +1e-02 | 19.3 ms | — | — | — | 10.1 ms | 8.31 ms | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +9e+00 | 4.55 ms | — | — | — | 3.21 ms | 5.26 ms | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +9e+00 | 5.06 ms | — | — | — | 4.01 ms | 4.76 ms | — | — | — | — | — | — | — | — |
| Python · external |
📐ref.python_scikit_learn | source | 3.33 ms | — | — | — | 2.56 ms | 3.09 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 | ~ shape 4e-15 | — | — | — | — | 1.92 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.blas+omp | ~ shape 4e-15 | — | — | — | — | 1.93 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.omp | ~ shape 4e-15 | — | — | — | — | 1.91 ms🏆 | — | — | — | — | — | — | — | — | — |
pls4all.cpp.ref | ~ shape 4e-15 | — | — | — | — | 2.05 ms | — | — | — | — | — | — | — | — | — |
| Python · pls4all |
pls4all.python | ✓ bind | — | — | — | — | 1.94 ms | — | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ 3e-13 | — | — | — | — | 2.28 ms | — | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✗ +1e-02 | — | — | — | — | 6.75 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✗ +1e-02 | — | — | — | — | 8.43 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✗ +1e-02 | — | — | — | — | 7.81 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✗ +1e-02 | — | — | — | — | 8.28 ms | — | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +9e+00 | — | — | — | — | 3.23 ms | — | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +9e+00 | — | — | — | — | 3.85 ms | — | — | — | — | — | — | — | — | — |
| Python · external |
📐ref.python_scikit_learn | source | — | — | — | — | 2.30 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 | ~ shape 4e-15 | — | — | — | — | 1.77 ms🏆 | — | — | — | — | — | — | — | — | — |
pls4all.cpp.blas+omp | ~ shape 4e-15 | — | — | — | — | 1.81 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.omp | ~ shape 4e-15 | — | — | — | — | 1.84 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.ref | ~ shape 4e-15 | — | — | — | — | 1.81 ms | — | — | — | — | — | — | — | — | — |
| Python · pls4all |
pls4all.python | ✓ bind | — | — | — | — | 2.84 ms | — | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ 3e-13 | — | — | — | — | 1.98 ms | — | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✗ +1e-02 | — | — | — | — | 5.09 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✗ +1e-02 | — | — | — | — | 6.46 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✗ +1e-02 | — | — | — | — | 6.44 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✗ +1e-02 | — | — | — | — | 6.27 ms | — | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +9e+00 | — | — | — | — | 2.98 ms | — | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +9e+00 | — | — | — | — | 3.25 ms | — | — | — | — | — | — | — | — | — |
| Python · external |
📐ref.python_scikit_learn | source | — | — | — | — | 2.21 ms | — | — | — | — | — | — | — | — | — |
:::
::::
---
_See also_: [benchmark overview](../benchmarks/overview.md) · [methods index](index.md) · [interactive dashboard](../landing/dashboard.md)