# objectives.ece

``` python
objectives.ece(data, /, predictive=None, n_bins=15, *, generator=None)
```

Expected calibration error with equal-width bins, positions pooled.

A `Categorical` predictive compares the top-label confidence with accuracy; a `Bernoulli` one compares $p(y = 1)$ with the frequency of $y = 1$ over all entries. With $M$ entries and $S_b$ the entries in bin $b$,

$$\mathrm{ECE} = \sum_{b} \frac{|S_b|}{M} \left| \operatorname{acc}(S_b)
    - \operatorname{conf}(S_b) \right|.$$

Binning follows `torchmetrics`: a confidence of exactly 1.0 forms its own bin. Every position counts: a likelihood’s mask is not applied. The value is detached, so tune it with `lk.GridSearch`, not `lk.Minimize`.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| data | Data \| Iterable\[Any\] | Data with targets, in any form `lk.laplace` takes, checked as in `brier`. It is read once, into a list. | *required* |
| predictive | Predictive\[Posterior\] \| None | The predictive method. `None` means `Linearized()`, resolved against the posterior. | `None` |
| n_bins | int | The number of equal-width bins on $[0, 1]$, at least 1. | `15` |
| generator | torch.Generator \| None | Seeds the draws of a sampled predictive (`lk.Linearized(n_samples=64)`, say). One seed is drawn when the objective is built, and every call reuses it, so the objective is a function of the posterior alone, as `lk.Minimize` needs. `None` draws the seed from torch’s global RNG. | `None` |

## Returns

| Name | Type | Description |
|----|----|----|
|  | Objective\[Posterior\] | The objective, a function of a posterior to a detached 0-dim tensor. When called, it raises as `brier` does, and `ValueError` when there are no batches. |

## Raises

| Name | Type       | Description   |
|------|------------|---------------|
|      | ValueError | `n_bins < 1`. |
