# run_until_precision

Python reference for `nagasim.experiments.run_until_precision`.

`nagasim.experiments.run_until_precision`

```python
run_until_precision(layout, kpi: str, relative: float | None = 0.05, absolute: float | None = None, confidence: float = 0.95, n0: int = 10, max_replications: int = 200, seed: int = 0, run_length: float | None = None, warmup: float | None = None, workers: int = 1, on_progress = None, should_stop = None)
```

Keep adding replications until the confidence interval on a KPI is tight enough.

After the first *n0* replications the needed count is the textbook
``n >= (t * s / h)**2`` from the current sample. Each step runs at most as
many replications again as have run so far (never more than doubling), so a
wildly optimistic first estimate cannot blow the budget in one go.

Parameters
----------
layout : dict
    The model.
kpi : str
    A `flatten_kpis` key, e.g. ``"system/Avg Throughput Time"``.
relative : float, default 0.05
    Target half-width as a fraction of ``|mean|``; used when *absolute* is
    not given.
absolute : float, optional
    Target half-width in the KPI's own unit; wins over *relative*.
confidence : float, default 0.95
    Confidence level of the interval.
n0 : int, default 10
    Replications of the first pass.
max_replications : int, default 200
    Hard cap.
seed : int, default 0
    Seed of replication 0.
run_length, warmup : float, optional
    Fall back to the model's own settings.
workers : int, default 1
    Process-pool size.
on_progress, should_stop : callable, optional
    As in `nagasim.api.run_model`.

Returns
-------
tuple
    ``(results, report)``. The report carries ``replications``, the achieved
    ``half_width``, the ``target`` and ``met``.

See Also
--------
nagasim.api.run_model : A fixed number of replications.
nagasim.experiments.flatten_kpis : Where the KPI keys come from.

Examples
--------
>>> import json
>>> from pathlib import Path
>>> import nagasim
>>> from nagasim import experiments
>>> path = Path(nagasim.__file__).parent / "models" / "single_server.json"
>>> layout = json.loads(path.read_text(encoding="utf-8"))
>>> results, report = experiments.run_until_precision(
...     layout, "system/Avg Queue Length", relative=0.5, n0=3, run_length=200, warmup=20, seed=0)
>>> report["met"] or report["replications"] == 200
True
