# How sure am I, given my input data? Input uncertainty

> **In short**
>
> - **You build:** a script that reports a mean wait with two intervals, one from run-to-run noise and one that adds the [input uncertainty](../glossary.md#input-uncertainty) of a [distribution](../glossary.md#distribution) fitted to 30 observations.
> - **You need:** Python 3.11 or later, `pip install nagasim`, and the [fitting tutorial](fit-input-distributions.md).
> - **Time:** about ten minutes.

A confidence interval from many [replications](../glossary.md#replication) only covers the random draws of the model. It does not cover the error in the data the inputs were fitted to, and more replications never shrink that error. By the end you measure it with a [bootstrap](../glossary.md#bootstrap), read the wider interval, and see what share of the spread comes from the inputs.

This page has three parts: [Step by step](#step-by-step), [Check yourself](#check-yourself) and [If stuck](#if-stuck). The last section lists the [reference pages](#elements-this-page-uses) for the elements used.

## Step by step

1. Save this script as `goal-uncertainty.py`. The 30 handling times are drawn from a seeded lognormal, as a stand-in for your own data. The model's service time is replaced by a distribution fitted to them.

```python
import numpy as np
from nagasim import build as nb, uncertainty

# Only 30 observed handling times, so the fit itself is uncertain. Here they are drawn from a seeded
# lognormal; in a study they are your own data.
times = np.random.default_rng(7).lognormal(mean=-0.4, sigma=0.5, size=30)

m = nb.Model(time_limit=480, warmup=0)
m.add_resource(name="Server")
arrive = m.create(distribution=nb.Exponential(rate=1.0))
serve = m.process(resource_name="Server", distribution=nb.Exponential(rate=1.25))   # replaced by the fit
arrive >> serve >> m.dispose()

answer = uncertainty.input_uncertainty(
    m.to_dict(), [{"path": f"blocks.{serve.name}.params.distribution", "values": list(times)}],
    kpis=["resource/Server/Time in Queue Avg"], resamples=20, replications=5, seed=1)

row = answer["kpis"][0]
print(f"{answer['runs']} runs: {answer['resamples_used']} resamples of the data x {answer['replications']} replications, plus the fit itself")
print(f"mean wait {row['mean']:.2f} minutes")
print(f"interval from run-to-run noise only: {row['run_ci'][0]:.2f} to {row['run_ci'][1]:.2f}")
print(f"interval with the inputs' uncertainty: {row['input_interval'][0]:.2f} to {row['input_interval'][1]:.2f}")
print(f"share of the spread that comes from the inputs: {row['input_share']:.0%}")

```

2. Run it.

```console
python goal-uncertainty.py
```

You should see this. The script seeds itself, so the numbers are the same on your machine.

```text
105 runs: 20 resamples of the data x 5 replications, plus the fit itself
mean wait 0.47 minutes
interval from run-to-run noise only: 0.36 to 0.58
interval with the inputs' uncertainty: 0.22 to 0.72
share of the spread that comes from the inputs: 88%

```

3. Read the first line. The engine fits the 30 observations once. Then it redraws the data 20 times with replacement, refits each draw in the same family, and runs the model five times on each fit. The 105 runs are 21 fits times five [replications](../glossary.md#replication). All of them use the same seeds, so the fits differ only in their inputs.

4. Read the interval from run-to-run noise. It is the usual 95% [confidence interval](../glossary.md#confidence-interval) over five replications of the point fit. It is what you would report if the fit were exact.

5. Read the interval with the inputs' uncertainty. It is wider, because it adds the spread of the 20 refits in quadrature. It always contains the first interval.

6. Read the share. It is the part of the spread of the mean that comes from the inputs. More replications shrink the rest of it. Only more data shrinks this part. A large share tells you to collect data before you run more replications.

## Check yourself

- Predict, then run: if the sample has 120 observations instead of 30 (`size=120`), will the input share rise or fall? Change it and check.
- Explain it back: why does the second interval contain the first?

## Done when

- [ ] The script prints a run count, a mean, two intervals and a share.
- [ ] You can say which of the two intervals you would report, and why.
- [ ] You can say what to do when the share is large.

## If stuck

| What happens | Likely cause | Fix |
|---|---|---|
| `ValueError: at least 2 resamples` | `resamples` is below 2 | Pass `resamples=20` or more. A few dozen make the interval steadier. |
| `ModelValidationError: override path 'blocks.nope.params.distribution': no blocks entry named 'nope'` | The input's path names a block the model does not have | Build the path from the block object, as the script does with `serve.name`. |
| `ValueError` naming the number of runs and `at most 2000` | `(resamples + 1) x replications` is over the limit | Lower `resamples` or `replications`. The check runs before any run starts. |
| A note says the interval is rough | Fewer than 30 observations, or fewer than 20 usable resamples | Collect more data, or raise `resamples`. |

## Elements this page uses

- Python: `input_uncertainty` from `nagasim.uncertainty`, [`Model`](../reference/api/Model.md) and [`best_fit`](../reference/api/best_fit.md) for the fit it repeats.
- Blocks: [CREATE](../reference/blocks/create.md), [PROCESS](../reference/blocks/process.md) and [DISPOSE](../reference/blocks/dispose.md).
- Distribution: [Exponential](../reference/distributions/exponential.md).
- KPI: `Time in Queue Avg` in the [resource summary](../reference/kpis.md#resource_summary).
- Command line: `naga uncertainty` runs the same analysis on a model file and a data file.

Next: [Calibrate a model to data](calibrate-to-data.md).
