> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nimbusbci.com/llms.txt
> Use this file to discover all available pages before exploring further.

# NimbusLDA

> NimbusLDA for Python: the Bayesian linear discriminant head with a pooled covariance and Student-t predictive — the default Personalizer head for motor imagery.

**Python**: `NimbusLDA` | **Julia**: `NimbusLDA`, a different implementation on RxInfer.jl — see the [Julia API reference](https://docs.nimbusbci.com/julia-sdk/api-reference.md#nimbuslda)\

**Mathematical model**: Normal-Inverse-Wishart posterior over class means and one shared precision; Student-*t* predictive

`NimbusLDA` is the default head. It assumes the classes share one covariance, which makes it the fastest of the heads and the right first choice for motor imagery, where two or four well-separated classes are the common case. Its probabilities are read from the posterior, so they carry how much evidence sits behind them.

> [!NOTE]
>
> **Python Personalizer head:** the default — `Personalizer(head="lda")`, or `Personalizer.for_deployment(enc, classes)`. On this head `fit_calibrated` fits a certificate. See [Trust & calibration](https://docs.nimbusbci.com/personalizer/trust-and-calibration.md).

## Quick start

```python theme={null}
from nimbus_bci import NimbusLDA

clf = NimbusLDA()                    # mu_scale=3.0
clf.fit(X_train, y_train)            # (n_trials, n_features)
labels = clf.predict(X_test)
probabilities = clf.predict_proba(X_test)   # the predictive, not a plug-in softmax
clf.partial_fit(X_new, y_new)        # conjugate update; no refit
```

sklearn-compatible: it drops into `make_pipeline(StandardScaler(), NimbusLDA())`, `cross_val_score`, and `GridSearchCV` like any classifier.

## When to use NimbusLDA

* Motor imagery with two to four classes whose covariance structure is similar across classes.
* You want the cheapest online update: `partial_fit` is a conjugate update, about 87 µs against \~835 µs for a sklearn LDA refit — see [Why Bayesian BCI Inference](https://docs.nimbusbci.com/core-concepts/why-bayesian-bci.md).
* A calibration set of a few dozen trials per class; the prior carries the estimate when the data is thin.

## When not to use it

* **The classes have clearly different covariances** — P300 target versus non-target often does. Use [`NimbusQDA`](https://docs.nimbusbci.com/models/nimbusqda).
* **The boundary is not Gaussian** — a multiclass problem with non-linear structure. Use [`NimbusSoftmax`](https://docs.nimbusbci.com/models/nimbussoftmax).
* **Features outnumber trials many times over** — a frozen trunk's embeddings with a few dozen labelled trials. The head no longer collapses there (see below), but [`NimbusProbit`](https://docs.nimbusbci.com/models/nimbusprobit) is built for that regime and is better calibrated.
* **The session drifts in a structured way.** Use [`NimbusSTS`](https://docs.nimbusbci.com/models/nimbussts).

## Model

One shared precision matrix `W` across the classes; a class conditional `p(x | y = k) = N(mu_k, W⁻¹)`; conjugate Normal-Inverse-Wishart priors, so the posterior is closed-form and `partial_fit` is exact. Predictions integrate over the posterior, which gives a multivariate Student-*t* predictive rather than a plug-in Gaussian — the difference between "the mode says 0.9" and "the evidence says 0.9".

When a class has `n_k` trials, its posterior mean sits `n_k / (n_k + kappa0)` of the way from the prior mean to the sample mean: the fewer the trials, the more the prior holds it back.

## Hyperparameters

| Parameter | Default | Meaning |
| - | - | - |
| `mu_loc` | `0.0` | Prior mean location for the class means; broadcast to every feature |
| `mu_scale` | `3.0` | Prior scale for the class means — the width of "we don't know yet" |
| `wishart_df` | `None` | Degrees of freedom of the precision prior; `None` means `n_features + 2`, the minimum for a valid prior |
| `class_prior_alpha` | `1.0` | Dirichlet smoothing on the class priors; higher is more uniform |
| `prior_fraction` | `None` | A data-scaled prior worth `prior_fraction * n` observations, with the pooled within-class covariance as the prior scatter. Scale-equivariant; `None` keeps the identity-scale prior |
| `kappa0` | `None` | The prior weight on the class means, in observations, as a number or `"cv"` to choose it by cross-validation. Overrides `mu_scale`; cannot be combined with `prior_fraction` |
| `forgetting` | `1.0` | Discount applied to the accumulated statistics on each `partial_fit`; `1.0` never forgets |
| `training_sfreq` | `None` | Sampling rate of the training data; persisted for the cross-session check |

When the calibration set has fewer within-class degrees of freedom than features, the sample covariance is singular. The head's prior scatter is then the OAS-shrunk estimate, so it stays positive definite and the head stops returning a uniform distribution for every trial — the defect fixed in 0.10.0.

`kappa0` (0.10.0) is the one worth tuning when features outnumber trials: it is better calibrated than the default there. A plain `LogisticRegression` is still more accurate.

## The gate behind it

The fixed gate — a confidence floor of 0.6 and an entropy ceiling — promised nothing and delivered little: within a session on BNCI2015-001 it sent 25–93 % of the test trials with 4.7–26.2 % of all trials wrong commands, and the pinned accept rate sent 63–70 % with 12.1–20.0 % wrong (cell S22, read once). So this head lets a **certificate** decide.

```python theme={null}
p = Personalizer.for_deployment(enc, ["left", "right"])
p.fit_calibrated(X_cal, y_cal, X_val, y_val)   # the risk gate at alpha 0.1
```

The risk gate kept wrong commands at 0.048–0.098 of all trials from 8 held-out trials per class on, sending 0.32–0.57 of the trials; at 4 per class on two classes nothing is certified at alpha 0.1. Without a certificate the fixed gate stays and `BrainState.extras["gate_calibrated"]` is `False`. Details: [Trust & calibration](https://docs.nimbusbci.com/personalizer/trust-and-calibration.md).

## Model inspection

```python theme={null}
params = clf.model_.params

for k, label in enumerate(clf.classes_):
    print(label, params["mu"][k])           # posterior class means

print(params["nu"], params["psi"].shape)    # shared Wishart posterior: dof, scatter
```

## Model selection context

| Head | Use it for |
| - | - |
| `NimbusLDA` | The default; motor imagery; the cheapest online update |
| [`NimbusQDA`](https://docs.nimbusbci.com/models/nimbusqda) | Classes with their own covariance; P300 |
| [`NimbusSoftmax`](https://docs.nimbusbci.com/models/nimbussoftmax) | Non-Gaussian multiclass boundaries (optional JAX) |
| [`NimbusProbit`](https://docs.nimbusbci.com/models/nimbusprobit) | Honest confidence on a few trials over many features |
| [`NimbusSTS`](https://docs.nimbusbci.com/models/nimbussts) | Non-stationary sessions and latent-state adaptation |

What was measured head against head — LDA, QDA and Softmax on the same corpora, and where plain logistic regression wins: [Evidence](https://docs.nimbusbci.com/personalizer/evidence.md#which-head-lda-vs-qda-vs-softmax).

## Next read

> [card] [Personalizer](https://docs.nimbusbci.com/personalizer/overview.md)
>
> This head behind `wrap` → `Personalizer` → `BrainState`.

> [card] [Trust & calibration](https://docs.nimbusbci.com/personalizer/trust-and-calibration.md)
>
> The certificate that decides in front of this head.

> [card] [sklearn Integration](https://docs.nimbusbci.com/python-sdk/sklearn-integration.md)
>
> Pipelines, cross-validation and tuning with the heads.

> [card] [Evidence](https://docs.nimbusbci.com/personalizer/evidence.md)
>
> LDA against QDA, Softmax and logistic regression.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.