# `Nous.Eval.Stats.Bootstrap`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/eval/stats/bootstrap.ex#L1)

Seeded paired cluster bootstrap (Koehn 2004, "Statistical Significance
Tests for Machine Translation Evaluation", §6; resampling unit per Miller
2024).

Each replicate draws as many clusters as there are, with replacement, and
evaluates a paired statistic on the drawn clusters. Because one draw
serves both arms, the candidate–baseline correlation is kept. The cluster
is the case, or a group of cases sharing a scenario/template when cases
are not independent. Per-case values should already be reduced over the
k samples (case means); metrics that are not case means (pass^k, F1,
average precision) are recomputed per replicate by a custom statistic.

## Determinism

Random draws come only from `:rand.uniform_s/2` on an explicit
`:rand.seed_s(:exsss, seed)` state (never the process dictionary, never
`normal_s`, whose output is platform-dependent). The algorithm is pinned
to `:exsss` because `:default` may change between OTP releases.

Replicates are split into `:streams` independent streams; stream `s`
starts from the seeded state advanced by `s` calls of `:rand.jump/1`
(2^64 draws each, so streams never overlap). The output depends only on
`{seed, resamples, streams}`: running the streams concurrently
(`:max_concurrency > 1`) gives results identical to running them
serially. Persist `:seed`, `:algorithm`, `:resamples` and `:streams` from
the result with any comparison so it can be reproduced.

Percentile intervals undercover at small n (Bowyer et al. 2025); flag
results with few clusters as such.

# `cluster`

```elixir
@type cluster() :: [term()]
```

One cluster of paired items. With the default `:mean_difference`
statistic each item is `{candidate, baseline}` (numbers, e.g. per-case
means); with a custom statistic items can be any term.

# `result`

```elixir
@type result() :: %{
  :estimate =&gt; float(),
  :ci =&gt; Nous.Eval.Stats.ci(),
  :se =&gt; float(),
  :p_le_0 =&gt; float(),
  :p_ge_0 =&gt; float(),
  :clusters =&gt; pos_integer(),
  :resamples =&gt; pos_integer(),
  :streams =&gt; pos_integer(),
  :seed =&gt; integer() | {integer(), integer(), integer()},
  :algorithm =&gt; :exsss,
  :confidence =&gt; float(),
  optional(:replicates) =&gt; [float()]
}
```

# `statistic`

```elixir
@type statistic() :: :mean_difference | ([term()] -&gt; number())
```

A function from the items of the drawn clusters to a number.

# `paired_cluster`

```elixir
@spec paired_cluster([cluster()], keyword()) ::
  {:ok, result()}
  | {:error,
     :seed_required
     | :insufficient_clusters
     | :empty_cluster
     | :invalid_values
     | :invalid_confidence
     | {:invalid_statistic, term()}
     | {:invalid_option, atom()}}
```

Paired cluster bootstrap of `statistic` over `clusters`.

`clusters` is a list of clusters; each cluster is a non-empty list of
paired items (see `t:cluster/0`). At least two clusters are required.

Options:

  * `:seed` - **required**; an integer or a 3-tuple of integers
  * `:resamples` - number of replicates B, default `10_000`
  * `:statistic` - `:mean_difference` (default: mean of candidate minus
    mean of baseline over all drawn items) or a function receiving the
    flat list of drawn items and returning a number
  * `:confidence` - percentile interval level, default `0.95`
  * `:streams` - number of jumped random streams, default `8`
  * `:max_concurrency` - streams evaluated at once, default `1` (serial,
    in the calling process); results do not depend on it. A custom
    statistic runs in task processes when this is above 1.
  * `:keep_replicates` - include `:replicates` (in stream order) in the
    result, default `false`

Result keys: `:estimate` (statistic on the original data), `:ci`
(type-7 percentiles of the replicates), `:se` (standard deviation of the
replicates), `:p_le_0` / `:p_ge_0` (share of replicates `<= 0` / `>= 0`,
the one-sided "no improvement" / "no regression" shares), plus the
reproducibility keys `:seed`, `:algorithm`, `:resamples`, `:streams`.

Errors: `{:error, :seed_required}`, `{:error, :insufficient_clusters}`,
`{:error, :empty_cluster}`, `{:error, :invalid_values}` (non-numeric
pairs for `:mean_difference`), `{:error, {:invalid_statistic, value}}`
when a custom statistic returns a non-number, and
`{:error, {:invalid_option, key}}`.

## Examples

    iex> clusters = [[{1.0, 0.0}], [{1.0, 1.0}], [{0.5, 0.0}], [{1.0, 0.5}]]
    iex> {:ok, r} = Nous.Eval.Stats.Bootstrap.paired_cluster(clusters, seed: 42, resamples: 200)
    iex> {r.estimate, r.resamples, r.algorithm}
    {0.5, 200, :exsss}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
