# Backtesting

> Score the model on your own history against a seasonal baseline.

Before you rely on a forecast, see how it would have done. `evaluate` cuts each series at a few points in its own past,
forecasts from each cut, and scores the forecasts against what really happened, next to simple baselines.

```python
res = model.evaluate(df, h=7, windows=3)
res.summary
```

The summary has one row per model (ChakraTS-OS-10M and each baseline) and one column per metric: `mase`, `sql`, `wql`,
`mae`, `rmse` and `coverage@0.8`, then `skill_<metric>` for each. A skill says how much better than the seasonal
baseline a model is: 0.25 means 25% lower error, and anything above 0 beats it.

![A backtest of four past days and its calibration plot](https://raw.githubusercontent.com/yhatlabs/yhatmodels/main/docs/assets/backtest_light.png)

*Four past days of Gulf Coast demand, each forecast from the history before it. The third was a hot day the history gave no sign of, and the forecast misses it; across all four regions, the quantiles stay close to calibrated.*

## Options

- **windows:** how many cuts per series (default 3), `step` apart (default `h`), at the end of each series.
- **baselines:** `seasonal_naive` (repeats the last season: a week for daily data, a day for hourly) and `naive` (the
  last value). The first one is the reference for the skill columns.
- **metrics:** `mase`, `sql`, `wql`, `mae`, `rmse` and `coverage@0.8` (the share of actual values inside the 10% to 90%
  range). All are computed by default.
- **season:** the seasonal period in steps, if you want to fix it instead of inferring it from the frequency.
- **past, known, preset, cross_series** and the column names work as in `forecast`. Known covariates are read from the
  data over the history; nothing after a cut reaches that window's forecast.

## The result

```python
res.summary            # per model: mean metrics and skill vs the baseline
res.scores             # per series and window
res.forecasts          # each window's quantiles next to the actual values
res.calibration        # share of actual values below each quantile and inside each interval
res.plot()             # each forecast against what happened (pip install "yhatmodels[plot]")
res.plot_calibration() # share below each quantile against its level
```

## Compare with your current forecast

Score the forecasts you use today on the same windows:

```python
res = model.evaluate(df, h=7, compare={"current": mine})
```

`mine` is a DataFrame with the id and time columns and either quantile columns (`median`, `q10`, ...) or a point
column named `forecast`.
