> ## 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.

# Trust, calibration, and rejection

> Calibrate the Personalizer's gate three ways — a bound on wrong commands, a conformal certificate, or a pinned accept rate — and what each promises.

Every `BrainState` leaves the Personalizer through a **gate**: the fixed checks of `DecisionConfig`, or a **certificate** calibrated on held-out trials. A fixed check is a number you chose. A certificate is a promise with conditions written on it. This page is about the second kind: how to get one, what it guarantees, and where it stops.

> [!NOTE]
>
> The page is about `nimbus-bci` 0.10.0. The conformal gate arrived in 0.9.0; the risk gate, the certificate as the default on four heads, and the gates' own verdicts on every trial arrived in 0.10.0.

## Three ways to calibrate a gate

| Calibration | Call | What it promises | What it costs |
| - | - | - | - |
| **Risk gate** | `calibrate_risk_gate(X_val, y_val, alpha=0.1)` | At most `alpha` of **all** trials end in a wrong command being sent | Labelled trials the head has not seen |
| **Conformal gate** | `calibrate_conformal_gate(X_val, y_val, alpha=0.1)` | The true label is in the predicted set at least `1 − alpha` of the time | Labelled trials; it refuses about `alpha` of trials whatever their confidence |
| **Pinned rate** | `calibrate_decision_gate(X_val, y_val, target_accept=0.7)` | The accept rate — and nothing else | Labelled trials |

All three are fitted on a held-out set the head never trained on. An in-sample set inflates every one of them.

## A certificate decides on four heads

Since 0.10.0, on `NimbusProbit`, `NimbusLDA`, `NimbusQDA` and `NimbusSoftmax`, a fresh certificate **replaces** the confidence floor and the entropy ceiling: the shipped path accepts exactly what the certificate accepts. `fit_calibrated` fits the risk gate on these heads unless you name another method.

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

p = Personalizer.for_deployment(enc, ["left_hand", "right_hand"])

# The head, then the risk gate at alpha 0.1 (the default on the four heads above):
p.fit_calibrated(X_cal, y_cal, X_val, y_val)

# The conformal gate instead, at alpha 0.1:
p.fit_calibrated(X_cal, y_cal, X_val, y_val, method="conformal")

# The pinned rate — what fit_calibrated fitted before 0.10.0:
p.fit_calibrated(X_cal, y_cal, X_val, y_val, target_accept=0.7, method="accept_rate")

states = p.predict(X_test)
assert all(s.extras["gate_calibrated"] for s in states)
```

* **Without a certificate, nothing changes.** The fixed gate stays, every `BrainState` carries `extras["gate_calibrated"] = False`, and a warning says so once per process.
* **`NimbusFusion` is unchanged.** `fit_calibrated` still pins an accept rate on it, and a certificate is stacked on the fixed checks.
* **A certificate is void after any update of the head.** `Personalizer.risk_stale` and `Personalizer.conformal_stale` flip to `True`, the fixed checks stand again, and the certificate must be calibrated afresh.
* **It costs labelled trials.** At least nine for `alpha = 0.1`, in practice 8–12 per class. At 4 trials per class on two classes nothing is certified.

> [!NOTE]
>
> Why the default changed: the fixed gate is restrictive and not safe. Behind `NimbusProbit` it rejected 21–48 % of the trials and still sent 8–18 % wrong commands; behind QDA it sent 83–99 % of the trials with 20–37 % wrong. The risk gate kept its promise behind every head. The numbers are below, under [What is measured](#what-is-measured).

## The conformal gate: a certified set

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

p = Personalizer.for_deployment(enc, ["left_hand", "right_hand"])
p.fit(X_cal, y_cal)
summary = p.calibrate_conformal_gate(X_val, y_val, alpha=0.1)   # 90 % coverage
states = p.predict(X_test)
# Every state now carries state.prediction_set (posterior-ranked class names).
# A set that is not a single class is rejected with reason "ambiguous_prediction_set".
```

**What is guaranteed.** Adaptive prediction sets (Romano, Patterson & Candès 2020) over held-out labelled posteriors give, distribution-free whenever calibration and test trials are exchangeable:

> P(y\_true ∈ S(x)) ≥ 1 − alpha, marginally

With the default `max_set_size=1`, an accepted trial's set is exactly the top class, so **P(accepted AND wrong) ≤ alpha**: the probability that an emitted command should have been rejected is bounded.

**What is not guaranteed.** The conditional accuracy of accepted trials. It is measured (`evaluate_personalizer(...).conformal["accuracy_on_accepted"]`), never claimed. The accept rate at a given `alpha` is whatever falls out for your trunk and is reported, not pinned.

* `max_set_size > 1` accepts narrow sets for two-stage interfaces, such as 2 of 8 speller classes.
* `randomized=True` (default) is seeded: a batch is deterministic for fixed inputs, and a live loop advances the stream one draw per trial. `randomized=False` is the conservative variant — no randomness, weakly larger sets, the same bound.
* The fitted gate round-trips through `save` / `load` and the profile (`export_profile` → `from_profile`). `p.conformal_gate_.set_alpha(0.05)` re-quantiles a loaded profile without the calibration data.

## The risk gate: a bound on wrong commands

The conformal gate keeps its promise by refusing about `alpha` of the trials, confident or not. The risk gate promises what a command interface asks for instead.

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

p = Personalizer.for_deployment(enc, ["left_hand", "right_hand"])
p.fit(X_cal, y_cal)
summary = p.calibrate_risk_gate(X_val, y_val, alpha=0.1)   # at most 10 % wrong commands
states = p.predict(X_test)
# A trial below the bar, or with a tied top class, gets prediction_set == () and is rejected.
```

It places a bar on the head's own top posterior by conformal risk control (Angelopoulos, Bates, Fannjiang, Jordan & Zrnic 2022), so that at most `floor(alpha * (n + 1) - 1)` wrong calibration trials would pass it. The gate is deterministic: `predict_one` and a batch agree.

**What is guaranteed.** The share of new trials that end in a wrong command is at most `alpha`, in expectation over the calibration draw, for independent trials from one distribution. It is not a coverage statement, and not a bound on the error rate among accepted trials as such. The gate's verdict on each trial is `BrainState.extras["risk_passed"]`.

> [!WARNING]
>
> **On a weak decoder it is the wrong tool.** The promise is a share of all trials and says nothing about how right the sent commands are. On one weak head the gate kept its promise by sending 17 % of the trials, of which 40 % were right. Read the accuracy before trusting a command: the summary `calibrate_risk_gate` returns is in-sample for the bar and optimistic; `evaluate_personalizer` returns `risk` on held-out trials, with `accuracy_on_accepted` in it.

Both gates can be calibrated together; a trial must then pass both. With fewer than `ceil(1 / alpha) - 1` held-out trials nothing is certified: the bar is infinite and a `UserWarning` says so.

## The pinned rate and its entropy ceiling

`calibrate_decision_gate` — or `fit_calibrated(..., method="accept_rate")` — tunes the confidence floor so that the target share of the held-out trials is accepted. It promises the rate and nothing else.

On three or more classes the gate is a confidence floor **under** an entropy ceiling of `0.75 * log2(K)` bits, and a diffuse posterior fails the ceiling first. When fewer than the target share of the held-out trials pass the ceiling, no floor can reach the target. The pin then **keeps the ceiling** (`entropy="keep"`, the default): it installs the floor, warns `cannot reach target_accept`, and reports the maximum it can reach as `achieved_accept`.

```python theme={null}
p = Personalizer.for_features(["left", "right", "feet"]).fit(X_cal, y_cal)

# Keep the ceiling (the default) and hear why the target is out of reach:
summary = p.calibrate_decision_gate(X_val, y_val, target_accept=0.7, entropy="keep")

# Or let the ceiling give way as far as the target needs, and no further:
summary = p.calibrate_decision_gate(X_val, y_val, target_accept=0.7, entropy="yield")
print(summary["entropy_threshold"], summary["achieved_accept"], summary["acc_accepted"])
```

`entropy="yield"` sends more and promises only the rate; `acc_accepted` is the accuracy it carries. Whether that trade is worth it is the application's call — in typing, a wrong character is cheap to fix; where an error is costly, it is not. The ceiling is yours to set as well: `DecisionConfig(entropy_threshold=...)` in bits, `None` for the class-count rule. On the four heads above, `fit_calibrated` no longer pins a rate at all: pass `method="accept_rate"`.

## Each gate's own verdict on every trial

A loop that runs both gates can audit each promise on its own:

| `BrainState.extras` key | Meaning | Appears |
| - | - | - |
| `gate_calibrated` | A certificate, not the fixed gate, decided this trial | On the four heads above |
| `conformal_set` | What the conformal gate certified by itself, before the risk gate acted | Once the conformal gate is calibrated |
| `risk_passed` | Whether the risk gate let the trial through | Once the risk gate is calibrated |
| `conformal_stale` / `risk_stale` | The head changed since the certificate was calibrated | Once that gate is calibrated |

`prediction_set` stays the set **after** both gates. Decisions are unchanged by these keys.

> [!NOTE]
>
> **A live loop draws per trial.** `predict_one` — and any `predict` call that holds exactly one trial — takes the next draw of the conformal gate's stream, exactly as each row of a batch takes its own. Scoring the same trial twice can return different sets. The stream position is saved with the profile (`live_draws`).

## Deciding inside a set

The gate certifies a set; a command is one symbol. For a speller, `choose_within_sets(posteriors, sets, log_prior)` picks the member a language model finds most plausible after what was already typed, and `spell_with_prior` threads the chosen prefix through a sequence. The choice is confined to the set and abstains on an empty one, or on one whose every member the prior forbids.

It raises the information rate, not the guarantee: a pick is right when the true label is in the set — bounded — **and** ranks first inside it — not bounded.

## What is measured

Preregistered cells, read once each, offline on public corpora. "Wrong" is wrong commands as a share of **all** trials.

**Within a session** (BNCI2015-001, twelve subjects, the head on 4–20 trials per class, every calibrated gate fitted on a second set of as many held-out trials):

| Head | Default gate: sent / wrong | Pinned rate (target 0.7): sent / wrong | Risk gate, alpha 0.1: sent / wrong |
| - | - | - | - |
| `NimbusProbit` | 52–79 % / 8–18 % | 65–72 % / 12–20 % | 36–72 % / **5–10 %** |
| LDA | 25–93 % / 4.7–26.2 % | 63–70 % / 12.1–20.0 % | 32–57 % / **4.8–9.8 %** |
| QDA | 83–99 % / **19.6–36.5 %** | 52–70 % / 14.4–26.1 % | 23–54 % / **3.5–10.0 %** |
| Softmax | 39–76 % / 4.8–18.6 % | 64–72 % / 11.4–20.2 % | 34–72 % / **5.1–10.5 %** |

The risk gate kept its promise in all 36 cells.

**In the next session** — the same certificate, the head and the gate's trials drawn from one session, every trial of the next session read; three public corpora, thirty subjects:

| Corpus | Risk gate: wrong commands in the next session | Within alpha + 0.02 | Against the pinned rate |
| - | - | - | - |
| BNCI2014-001 (9 subjects, two sessions, 4 classes) | 0.045–0.118 | 12 of 12 cells | Better behind QDA only |
| BNCI2014-004 (9 subjects, five sessions) | 0.070–0.137 | 11 of 12 cells | Better in all 12 |
| BNCI2015-001 (12 subjects) | 0.055–0.125 | 11 of 12 cells | Better in 9 of 12 |

The promise held in 34 of 36 cells; the risk gate sent 16–71 % of the next session's trials. It is a corpus's mean, not a subject's: one subject's gate sent 0.26 wrong.

## What none of this promises

* **Nothing across a session change.** Exchangeability is the load-bearing assumption. Under drift — a session change, a cap change, fatigue — a guarantee is void. Recalibrate on the session you type in. The next-session numbers above are a reading, not a promise.
* **Nothing per user or per session.** The guarantee is marginal, an average over trials and draws. A 40-trial session's coverage moves with a standard deviation of 0.04–0.07 over the conformal gate's draws.
* **Nothing after the head changes.** Any update of the head voids the certificate; the staleness flags say so on every trial.
* **Nothing about accuracy.** A head with a calibrated gate is not a more accurate head. On covariance features a plain logistic regression is as accurate as every head measured.
* **One level and one target were measured:** `alpha = 0.1` and `target_accept = 0.7`.

For the gates on SSVEP decoders, and the classic decision as a head, see [SSVEP](https://docs.nimbusbci.com/personalizer/ssvep.md).

## Next read

> [card] [SSVEP](https://docs.nimbusbci.com/personalizer/ssvep.md)
>
> Filter-bank CCA, TRCA and TDCA, and the gates on them.

> [card] [BrainState](https://docs.nimbusbci.com/personalizer/brain-state.md)
>
> The fields every trial carries, including each gate's verdict.

> [card] [Evidence](https://docs.nimbusbci.com/personalizer/evidence.md)
>
> How the numbers on this page were produced, and their boundaries.

> [card] [Changelog](https://docs.nimbusbci.com/python-sdk/changelog.md)
>
> Release and upgrade notes, newest first.


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