# How do I model inspecting a sample from a finite lot?

An inspector draws a small sample from a lot with a known number of defectives and does not put the parts back. The defectives found follow a Hypergeometric count. The script charges a minute of paperwork per defective found.

```python
from nagasim import build as nb

# A lot of 50 parts holds 5 defectives. The inspector draws 10 parts without putting any back, and
# each defective found costs one minute of paperwork. Hypergeometric(N, K, n) is the defective count
# of that sample; its mean is n * K / N = 1. Binomial would draw with replacement and overstate the spread.
m = nb.Model(time_limit=4000, warmup=0)
m.add_resource(name="Inspector")
lots = m.create(distribution=nb.Exponential(rate=1 / 4))
found = nb.Hypergeometric(N=50, K=5, n=10)
lots >> m.process(resource_name="Inspector", distribution=found) >> m.dispose()

results = m.run(replications=10, seed=1)
print(f"inspector busy {results.kpi('resource/Inspector/Usage % (snapshot)'):.0f}% (1 minute in 4 is 25%)")

```

Save it as `recipe-lot-sample.py` and run it with `python`. The script seeds itself, so the numbers below are the same on your machine.

```text
inspector busy 25% (1 minute in 4 is 25%)

```

## How it works

1. `Hypergeometric(N, K, n)` is the defectives in a sample of `n` from a lot of `N` that holds `K` of them.
2. Its mean is `n * K / N`, here 1 defective a lot, so the inspector is busy 25% of the time.
3. A Binomial would draw with replacement and overstate the spread when the sample is a large part of the lot.

## Elements used

- Distributions: [Hypergeometric](../../reference/distributions/hypergeometric.md), [Binomial](../../reference/distributions/binomial.md), [Exponential](../../reference/distributions/exponential.md).
- Blocks: [CREATE](../../reference/blocks/create.md), [PROCESS](../../reference/blocks/process.md), [DISPOSE](../../reference/blocks/dispose.md).
- Python: [`Model`](../../reference/api/Model.md).

Back to all [recipes](index.md).
