# Covariates

> Give the model what you know about the future, what you see only in the past, and what is fixed per series.

Forecasts improve when the model knows what drives the series: tomorrow's temperature for electricity load, a planned
promotion for sales. These extra columns are **covariates**. There are three kinds:

| Kind | Example | Argument |
|---|---|---|
| Known ahead | prices, planned promotions, holidays, weather forecasts | `known=[...]`, with the future values in `future=` |
| Seen only in the past | footfall, a competitor's price you learn afterwards | `past=[...]` |
| Fixed per series | region, store size, product family | `static=[...]` |

## Values you know ahead

The model needs each known covariate for every step it forecasts. `make_future` builds the table of future timestamps for you
to fill in. This example uses a small synthetic electricity load that rises with temperature:

```python
import numpy as np
import pandas as pd
from yhatlabs import Client
from yhatlabs.features import make_future

# a small synthetic example: hourly electricity load that rises with temperature
hours = pd.date_range("2026-06-01", periods=24 * 21, freq="h")
temperature = 20 + 6 * np.sin((hours.hour - 9) * np.pi / 12)
load = 500 + 12 * temperature + np.random.default_rng(0).normal(0, 5, len(hours))
df = pd.DataFrame({"ds": hours, "y": load, "temperature": temperature})

future = make_future(df, h=24)  # the next 24 timestamps
future["temperature"] = 20 + 6 * np.sin((future["ds"].dt.hour - 9) * np.pi / 12)  # your weather forecast

yh = Client()
fc = yh.forecast(df, h=24, known=["temperature"], future=future)
print(fc.df.head(3).round(1))
```

Output:

```
  unique_id                  ds    q10  median    q90
0  series_0 2026-06-22 00:00:00  684.4   690.1  695.8
1  series_0 2026-06-22 01:00:00  672.6   679.3  686.6
2  series_0 2026-06-22 02:00:00  665.4   671.1  678.1
```

- `make_future(df, h=24)` returns the id and timestamp of the next 24 steps of every series.
- `future` must have a value for every known covariate at every future step. A missing value is an error, not a silent guess.
- Known covariates may be numbers or text categories (`"promo"`, `"none"`).
- Instead of a separate `future` table, the same table can carry the future: rows after a series' last value, with the target
  blank and the known columns filled, are used as `future=`.

## Calendar effects

Day of week, month and holidays are known for any date, so the client can add them for you, for the history and the horizon,
with no future table needed. This adds day of week and month to the temperature forecast:

```python
import pandas as pd
from yhatlabs import Client

url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/"
temps = pd.read_csv(url + "daily-min-temperatures.csv")

yh = Client()
fc = yh.forecast(temps, h=28, time_col="Date", target_col="Temp", calendar=["dayofweek", "month"])
print(fc.provenance["settings"]["known"])
print(fc.df.head(2).round(1))
```

Output:

```
['dayofweek', 'month']
  unique_id       Date   q10  median   q90
0  series_0 1991-01-01  11.5    13.8  15.9
1  series_0 1991-01-02  11.8    14.4  17.2
```

- `calendar=True` adds hour, day of week, month and weekend.
- A list (`calendar=["dayofweek", "month"]`) chooses which.
- `calendar={"holidays": "US"}` also adds a holiday flag for that country (needs `pip install holidays`; `"subdiv": "TX"` for a state).
- `yhatlabs.features.add_calendar(df)` adds the same columns to any table.

## How much each covariate matters

`explain_forecast` shows how much each covariate moves the forecast. It forecasts once with every covariate, then once per
covariate with that one set to a neutral value (numbers: the series' mean over the history and the future; categories and
flags: their most common value), and compares the medians step by step. Here load follows temperature, a hot day is coming,
and a promotion flag does nothing:

```python
import numpy as np
import pandas as pd
from yhatlabs import Client
from yhatlabs.features import make_future

# hourly electricity load that rises with temperature (which varies day to day), and a promotion flag that does nothing
rng = np.random.default_rng(0)
hours = pd.date_range("2026-06-01", periods=24 * 28, freq="h")
day = rng.normal(0, 3, 28).repeat(24)  # warmer and cooler days
temperature = 20 + day + 6 * np.sin((hours.hour - 9) * np.pi / 12)
load = 500 + 12 * temperature + rng.normal(0, 5, len(hours))
df = pd.DataFrame({"ds": hours, "y": load, "temperature": temperature, "promo": rng.integers(0, 2, len(hours))})

future = make_future(df, h=24)
future["temperature"] = 26 + 6 * np.sin((future["ds"].dt.hour - 9) * np.pi / 12)  # a hot day ahead
future["promo"] = 0

yh = Client()
ex = yh.explain_forecast(df, h=24, known=["temperature", "promo"], future=future)  # 3 forecasts: all, and one per covariate
print(ex.attrs["summary"].round(1))
print(ex[ex["covariate"] == "temperature"].iloc[12:15].round(1).to_string(index=False))
```

Output:

```
 covariate  mean_abs_effect
0  temperature             93.0
1        promo              2.3
unique_id                  ds   covariate  median  median_without  effect
 series_0 2026-06-29 12:00:00 temperature   857.9           769.0    88.9
 series_0 2026-06-29 13:00:00 temperature   871.4           780.0    91.3
 series_0 2026-06-29 14:00:00 temperature   877.1           788.2    88.9
```

- `.attrs["summary"]` is the average absolute effect per covariate, largest first, in the target's units: temperature moves
  the forecast by about 90 here; the promotion flag hardly at all.
- The table has one row per covariate and step: `median` (every covariate), `median_without` (this one neutral) and `effect`.
- It costs one forecast plus one more per covariate in `known=` and `past=`. Calendar and `static=` columns are used in every
  forecast but not explained.
