# Experiments

Everything about running more than one replication, comparing designs, and searching for a good
one: `nagasim.experiments` (scenarios, CRN, precision, warm-up) and `nagasim.optimize` (DOE,
ranking and selection, optimization), driven from the command line (`naga scenarios`, `naga doe`,
`naga optimize`) or from Python.

## Replications

A model runs several times ("replications"), one fresh `Environment` per seed
(`seed, seed+1, ..., seed+N-1`), because a single run is one sample from a stochastic process.
Every summary KPI comes with a 95% confidence interval across replications
(Student-t, `ddof=1`) -- read the interval, not just the mean; a narrow one earned by too few
replications is worse than a wide one that is honest about it.

## Run log levels

A run records at one of three levels, so a study does not pay for data nobody reads. The level
decides which frames the run builds:

- `kpi`: the summary frames only (counters, means, utilisation, throughput, every
  `..._summary` frame). The cheapest, and what studies use.
- `series`: plus the sampled `timeseries` and `dashboard` frames.
- `full`: plus the per-event `entity_log`, `resource_log` and `store_log`. The default.

Three profiles name a level for a purpose: `study` is `kpi`, `explore` and `replay` are `full`
(`replay` also records the first replication's trace for `naga run --player` and `--export`).
Give the level as `log_level=` to `run_model`, `run_scenarios`, `optimize`, `run_design` and
`calibrate`, as `--log-level` to `naga run`, or as `simulation.log_level` in the model file. On a queueing model
`kpi` makes 12-21% fewer Python calls than `full` and keeps every summary number bit-identical.

```python
from pathlib import Path

import nagasim
from nagasim import api

path = str(Path(nagasim.__file__).parent / "models" / "single_server.json")
quick = api.run_model(path, replications=3, run_length=200, seed=0, log_level="kpi")
print(quick.log_level)                          # kpi
print(quick.kpi("system/Avg Throughput Time"))  # the same number a full run gives
try:
    quick["entity_log"]
except KeyError as error:                       # NotRecordedError, a KeyError
    print(error)
```

A frame the level did not record is left out, never handed back empty, and asking for it says
which level to re-run at: `'entity_log' was not recorded: this run was at level kpi; re-run with
log_level='full'`. The same line comes from `results.kpi`, `results.interval` and
`results.plot` for a KPI or a series the level left out, such as the queue-length percentiles
(computed from the resource log) at `kpi`. An agent tool answers with the code `E_NOT_RECORDED`
and the same hint, and every result carries its level as `results.log_level`.

The first level named wins. A level you pass beats the model's own `simulation.log_level`, which
beats its `record_logs` and `timeseries_points`; a level never samples more often than the model
asks. With no level anywhere a run records `full`, exactly as before levels existed, and a model
that switches its own logs off (`record_logs: false`) keeps today's frames. The agent's
`run_model` tool explores by default, but a model that sets `log_level` or `record_logs` itself
keeps its own setting.

Studies run at `kpi` unless a KPI they name needs more. DOE, optimisation, selection, Sobol,
tornado and calibration pick the lowest level that records every response:
`timeseries/<series>/max` raises them to `series`, a log-derived column (`Queue Len P95`) or a
tail goal to `full`. A forecast, a reality check, loop dominance and sensitivity bands run at
`series`. One check is lost at `kpi`: the initial-bias check of `results.checks()` needs the entity
log, so a `kpi` study does not get its warm-up caveat; run one replication at `full` to see it.

## Warm-up and the MSER suggestion

Warm-up is a **scheduled statistics reset**, not a post-hoc filter: at `t = warmup`, every
collector resets so entities/statistics created before that point never enter a KPI. Pick a
warm-up long enough that the system has reached steady state (queueing systems typically start
empty, which biases early KPIs low).

**Run > Run... > Suggest warm-up** runs MSER-5 (White 1997): groups the across-replication
mean throughput-time series into batches of 5 observations and picks the truncation point that
minimizes the batch-mean variance of the tail. `reliable=False` means the minimum fell in the
second half of the batch means -- the standard MSER caveat: a still-drifting series can "look"
best when truncated near the end. Treat that as "the run is too short to tell," and lengthen the
run rather than trusting the suggestion.

## Run to precision

Instead of guessing a replication count, **run to precision**
(`nagasim.experiments.run_until_precision`) keeps adding replications until a
named KPI's confidence-interval half-width is within a relative or absolute target. After the
first `n0` replications it estimates the remaining count from `n >= (t * s / h)^2`, capped at
doubling per step so an optimistic first estimate cannot blow the budget in one call.

## Scenarios and CRN

A scenario is a named set of parameter overrides on the base model, stored in the model file
itself (so it travels with the model it belongs to). An optional `goal` (up to 500 characters)
states the question the scenario answers, for example "Can we meet the 2 h service target with one
picker fewer?"; Present mode, the Studies window and the HTML report (a Goal column in the scenario definitions) show it, and it does not enter a run. The browser viewer lists no stored scenarios, so it shows none. An override addresses a model parameter by
path -- `resources.Server.capacity`, `blocks.Cutting.params.dist_params.rate`,
`variables.rework_count.initial`, and so on; it can only change a value the model already has,
never invent a new one. A table's values are one value in this grammar, replaced whole. `conversions` (yields and release curves) is overridable too, since those change the
dynamics; entity types are not, since their only numeric field, `holding_cost`, feeds cost KPIs
only -- a holding-cost what-if is a post-hoc rescale (total holding cost = holding_cost x
time-averaged WIP), not a rerun.

A scenario may override `simulation.time_limit` or `simulation.warmup` and then runs with its own
window when the run length is left to the model (`naga scenarios`, `naga invest` and the agent tools
by default); a run length given explicitly (`--run-length`) applies
to every scenario. Compare count KPIs (entities completed, costs) across different windows with
care: the difference is partly the window, so prefer rates, or the investment case, which scales
each scenario by its own window.

**Common random numbers (CRN, the default)**: replication *r* of every scenario uses seed
`seed + r`, so a block a scenario did not touch draws identical numbers in every scenario. The
comparison then uses a **paired-t** interval on the per-replication differences -- normally much
narrower than an independent comparison, because the shared sampling noise cancels. `--no-crn`
uses disjoint seed blocks per scenario and **Welch's** interval instead. "Significant" means the
difference interval excludes 0.

## Parallel workers, and when they help

`--workers N` spreads work over a `ProcessPoolExecutor`. The unit of work is one replication of one
**design point** -- a scenario, a point of a design, a candidate the optimizer is trying -- so a
study of N points with R replications each has N x R tasks to fill the workers, whatever R is. That
matters most for the commonest shape of all, a screening factorial with ONE replication per point:
before, it used a single core. Measured on the bundled biogas plant, 27 full-year points, one
replication each: **148.7 s to 37.6 s on nine workers**, identical numbers.

Results are re-ordered by seed before concatenation, so the output is bit-identical to running
sequentially -- a replication depends on the seed it was given, never on which worker ran it.
`--workers auto` times one replication in-process and only pools when at least 3 tasks remain
**and** the total work left (that time x the tasks left) is worth the pool's start-up cost, about
3 seconds. Rule of thumb from measurement on Windows: one short run is faster sequential; a long
one, or many short ones, gives 2-4x with 4-9 workers.

## Design of experiments

```console
naga doe single_server --factor "resources.Server.capacity=int:1:2" --response "system/Avg Throughput Time" --design full:2 --replications 2 --run-length 200
```

In general: `naga doe MODEL --factor path=kind:low:high[:step] --response system/... --design
full:LEVELS|lhs:N|oat`. A factor addresses the model through the same override-path grammar as a
scenario, and may drive several paths at once with one value. Three design types:

| Design | What it does |
|---|---|
| `full:LEVELS` (full factorial) | Every combination of every factor's levels. Grows fast with factor count -- use for a handful of factors. |
| `lhs:N` (Latin hypercube) | N points, stratified one per 1/N of each continuous factor's range. Scales to many factors. |
| `oat` (one-at-a-time) | The baseline point, then one point per factor level with everything else held at baseline. Cheapest, weakest (misses interactions). |

`run_design`'s result reports main effects and two-factor interactions per response, all under
common random numbers by default (every design point runs on the same seeds, so point-to-point
differences are low-variance).

## Ranking and selection: the KN guarantee

`select_best` picks the best of a finite set of candidates by the fully sequential
indifference-zone procedure **KN** (Kim and Nelson, 2001). With indifference zone `d > 0` and
confidence `1 - alpha`:

> If the per-replication outputs are i.i.d. normal across replications (correlation *across*
> candidates is fine -- CRN is expected), then P{the selected candidate is the true best} >= 1 -
> alpha whenever the best candidate's true mean is at least `d` better than every other
> candidate's; when no such gap exists, the selected candidate's mean is within `d` of the best
> with at least that probability.

The guarantee needs the procedure to run to its own stopping rule; if `max_replications`, a
cancellation, or an exhausted budget ends it first, the best surviving sample mean is returned
with `guaranteed=False` instead of silently claiming the guarantee held.

## Optimization

```console
naga optimize single_server --factor "blocks.process_1.params.dist_params.rate=continuous:1.1:2.0" --response "system/Avg Queue Length" --goal min --budget 12 --replications 2 --run-length 200
```

`naga optimize MODEL --factor ... --response system/... --goal min|max --budget N`
searches continuous/integer/categorical factors for the best response within a replication
budget, an optional `optuna` backend aside (`--method optuna`, never a hard dependency). The
built-in method (`auto`/`pattern`):

1. Seed the search with a Latin hypercube (~10 x factor count points, bounded by the budget), all
   on the same CRN seeds.
2. Take the feasible point with the best mean as the incumbent.
3. Pattern search: try the 2n coordinate neighbours at each factor's **own** current step; a
   neighbour replaces the incumbent only if it *wins* (feasible where the incumbent is not, or a
   paired-t interval on the per-replication differences excludes 0 in its favour). An ambiguous
   challenger gets more replications before being retested.
4. The winning factor's step doubles only when the gain is worth it (see below); an axis whose own
   two neighbours beat nothing is halved on the spot, and a sweep with no move at all halves every
   axis. Converges when every factor's step is below its own grid unit, or the budget runs out.
5. Finishes by confirming the winner (and runners-up) with `select_best` on a reserved slice of
   the budget, so the final answer carries the KN guarantee above.

`--constraint path>=value` (repeatable) marks a design point infeasible without spending a full
evaluation comparing it as a winner. A search never changes the model file: it reports the best
point, and you keep it by adding it as a scenario.

### Significant is not the same as worth doing

With common random numbers a paired difference is measured very precisely -- which means a gain of
a few hundred on a response of half a million is *statistically significant*. A search that treats
every significant gain as a success spends its budget on the factor that improves consistently,
however little, and never gets a fine enough step on the factor that matters. That really happened
on the biogas sizing study; the tutorial records it.

`--min-improvement` is the guard: a relative threshold (default `0.01`, one per cent of the
objective) below which a move is still **taken** -- it is better, after all -- but does not widen
that factor's step and does not buy extra replications to settle itself. Raise it on a study where
only a few per cent matters; set it to `0` for the older behaviour.

Because the step is per factor and every poll values each axis separately, a factor the response
barely answers to converges quickly and then stops being polled at all, and the two evaluations an
iteration it was costing go to the factors that move the answer.
