Skip to content
Log inGet startedHugging FaceGitHub

Time-series forecasting

Tuning on your data

ChakraTS forecasts series it has never seen. Tuning adapts it to yours: it trains on your history for a few minutes, then your forecasts use what it learned (how your paydays or promotions move demand, your own seasonality). This tunes on two stores’ daily orders, which jump on paydays and promotion days, and scores the forecast on 28 days held back:

import numpy as np
import pandas as pd
from yhatlabs import Client
# a synthetic example: daily orders at two stores, which jump on paydays (the 1st and the 15th) and on promotion days
days = pd.date_range("2023-01-01", periods=3 * 365, freq="D")
rng = np.random.default_rng(0)
stores = []
for store, base in (("north", 200), ("south", 120)):
payday = days.day.isin([1, 15]).astype(float)
promo = (rng.random(len(days)) < 0.08).astype(float)
orders = base * (1 + 0.6 * payday + 0.4 * promo + 0.1 * np.sin(2 * np.pi * days.dayofweek / 7)) + rng.normal(0, 8, len(days))
stores.append(pd.DataFrame({"unique_id": store, "ds": days, "y": orders.round(1), "payday": payday, "promo": promo}))
data = pd.concat(stores)
history, actual = data[data.ds < days[-28]], data[data.ds >= days[-28]] # hold the last 28 days back to score on
future = actual.drop(columns="y")
yh = Client()
tuned = yh.tune(history, h=28, known=["payday", "promo"], future=future)
print(tuned)
zero_shot = yh.forecast(history, h=28, known=["payday", "promo"], future=future)
with_tune = tuned.forecast(history, known=["payday", "promo"], future=future)
wape = lambda fc: (fc.df["median"].to_numpy() - actual["y"].to_numpy()).__abs__().sum() / actual["y"].abs().sum()
print(f"error on the 28 days held back (WAPE): zero-shot {wape(zero_shot):.3f}, tuned {wape(with_tune):.3f}")
tuned.delete()
Output
Tuned(id='tune_4c80b41cdceb795eb858c9b1', model='chakra-ts', used=True, series=2, horizon=28, holdout WAPE 0.0492 -> 0.0408)
error on the 28 days held back (WAPE): zero-shot 0.042, tuned 0.039

Parameters: Client.tune, tuned.forecast

  1. It holds back the last h steps of every series and trains on the rest.
  2. It forecasts the held-back steps with and without the tuning, and keeps the tuning only where it was better.
  3. It trains again on the whole history, so the latest data is in it.

tuned.used says whether the tune beat the model as it is on your held-back steps, and tuned.holdout has both scores (wape: the error as a share of the actuals; loss: the scaled quantile loss, lower is better). A tune that did not win forecasts exactly like the model as it is, so using one never makes a forecast worse than not tuning on that test.

fc = tuned.forecast(df, known=["payday", "promo"], future=future) # h defaults to the tune's
fc = yh.forecast(df, h=28, tune=tuned) # the same; tune= also takes the id ("tune_...")

A tune is kept 30 days after its last use (ttl_days=, up to 365) or until tuned.delete(). yh.tunes() lists your project’s tunes and yh.get_tune(id) reattaches to one; the handle can be pickled. The first forecast with a tune after a quiet spell takes 20 to 30 seconds while it loads; the next ones take about as long as any forecast.

  • Drivers the model can’t read from the history alone: paydays, promotions, price changes, holidays specific to you. Pass them as known covariates (known= with their future values in future=); they are part of the tuning.
  • A few long series, or many alike: the tune learns from all the series of the request together.
  • Not for short history: every series needs at least 2 x h + 64 values. Shorter ones are refused with a message.
Argument Meaning
h the horizon you will forecast, and the steps the tune is scored on
steps=300 training steps, 50 to 1,000; the best count is picked on the held-back steps
known=, future= known covariates and their next h values, as in forecast
name= a label, shown in yh.tunes()
ttl_days=30 how long the tune is kept after its last use
background=True returns the Job at once; job.wait() gives the tune

A tune takes the series of one request (at most 256 series and 2,000,000 points with covariates): for more, tune on a representative subset, then forecast every series with it. It runs a few minutes as a background job, and its price is in GET /v1/limits (pricing.tokens.tune). From the command line:

Terminal window
yhatlabs tune orders.csv --h 28 --known payday,promo --future future.csv # prints the tune and its id
yhatlabs forecast orders.csv --h 28 --known payday,promo --future future.csv --tune tune_...