# Checking predictions

> See which predictions are extrapolations, and turn intervals into the chance of crossing a limit.

A model is only as good as its training data is close to the row in front of it. Every tabular prediction says how close
that is, and regression intervals turn into probabilities.

```python
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from yhatlabs import Client

X, y = load_diabetes(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

yh = Client()
model = yh.fit(X_train.assign(progression=y_train), target="progression", task="regress")
new = X_test.head(3).copy()
new.iloc[2, new.columns.get_loc("bmi")] = 0.4  # far above any training patient
res = model.predict(new, quantiles=[0.05, 0.25, 0.5, 0.75, 0.95])
print(res.df[["prediction", "q5", "q95"]].assign(out_of_range=res.out_of_range, novelty=res.novelty.round(2)).round(1))
print("P(progression > 200):", res.prob(above=200).round(2).tolist())
model.delete()
```

Output:

```
 prediction     q5    q95 out_of_range  novelty
362       257.4  172.3  336.1           []      0.0
249       251.9  153.2  318.8           []      0.8
271       281.3  157.1  351.7        [bmi]      1.0
P(progression > 200): [0.86, 0.8, 0.84]
```

The third patient was given a BMI far above anyone in the training data.

## Out of range

`res.out_of_range` lists, per row, the columns whose value lies outside what training saw: a number beyond the training
minimum or maximum, or a category training never had. `[]` means none. The third patient is flagged on `bmi`.

## Novelty

`res.novelty` measures how unlike the training data a row is as a whole, even when every column is within range. It is the
distance to the nearest training rows, as a percentile of how far training rows are from each other:

| Novelty | Meaning |
|---|---|
| around 0.5 | as close to the training data as a typical training row |
| above 0.95 | farther than nearly every training row: the model is extrapolating; treat the prediction and its interval with care |

Numbers are standardised and each differing category counts as one unit of distance. Novelty is computed on first access.

## The chance of crossing a limit

`res.prob(above=x)` (or `below=`) reads each row's intervals as a distribution: the chance that this patient's progression
exceeds 200 is 0.86, 0.80 and 0.84 above. Predict with several quantiles for a finer answer; beyond the outermost level the
tail is extrapolated.

## What is kept

To compute these, a fit keeps a profile of its training table on your machine: each column's range and categories, and a
sample of at most 2,000 training rows. Nothing about it is sent to us. It is saved with `model.save()`, so a loaded fit flags
rows too. `yh.fit(..., profile=False)` drops it, and the flags are then `None`. The one-shot `yh.predict` builds the profile
from the `train` you pass.
