> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tabsutra.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demand forecasting

> Item-by-store demand, with prices, promotions and holidays, for replenishment and planning.

A retailer or a consumer goods company needs to know how many units of each item each store will sell in the coming days or weeks. The forecasts drive orders to suppliers, transfers between stores and warehouses, and the stock set aside for promotions. This page shows how to set this up with tabSutra.

> [!TIP]
>
> For the business side of this problem, and how it compares with classic machine learning, read the case study on our website: [Demand forecasting for every SKU in every store](https://tabsutra.com/case-studies/retail-demand-forecasting/).

## The table

Use one row per item, store and day. Keep every day, including days with no sales, which are zeros, not missing rows.

```csv sales.csv theme={null}
store,sku,date,units,price,on_promotion,holiday,stock_out,store_type,category
S12,4471,2026-10-01,14,99.0,0,0,0,large,staples
S12,4471,2026-10-02,11,99.0,0,1,0,large,staples
S12,4471,2026-10-03,0,99.0,0,0,1,large,staples
S12,5520,2026-10-01,3,450.0,0,0,0,large,personal care
...
```

| Column | Kind | Note |
| - | - | - |
| `units` | target | Units sold that day. |
| `price`, `on_promotion`, `holiday` | known in advance | Send them again in `future_rows` for the days to forecast. |
| `stock_out` | known only in the past | A day with no stock has zero sales that say nothing about demand. Marking it lets the model treat that zero differently. |
| `store_type`, `category` | fixed for the series | The same on every row of a series. They let similar series share what they show. |

## Forecast the next 28 days

```python theme={null}
forecast = requests.post(f"{API}/v1/forecast", headers=HEADERS, json={
    "rows": records(sales),
    "future_rows": records(plan),            # store, sku, date, price, on_promotion, holiday
    "series_column": ["store", "sku"], "time_column": "date", "target": "units",
    "horizon": 28, "target_range": [0, None], "quantiles": [0.5, 0.8, 0.95],
}).json()
```

Send all the items of a store, or all the stores of a region, in one request. The series are read together, so an item launched three weeks ago gets a forecast informed by similar items, rather than by its three weeks alone.

## From a forecast to an order

A single number is not enough to set a stock level. The right level depends on the cost of running out against the cost of holding stock. With a full predicted distribution, you pick the quantile that matches that trade-off.

* For a fast-moving staple with a high cost of a lost sale, order up to a high quantile of the demand over the lead time, such as the 95% quantile.
* For a perishable item, where unsold stock is thrown away, order closer to the median.

The demand over a lead time of several days is a total of several steps. Its mean is the sum of the daily means. Its quantiles are not the sums of the daily quantiles. If you set orders on a weekly total, forecast the weekly total as a series of its own, at a weekly step.

## Slow-moving items

Many items sell on only a few days. Their median forecast is often zero, which does not mean that no stock is needed. Read the upper quantiles: the 90% quantile of a spare part tells you how many units cover nine weeks in ten.

## Check before you use it

```python theme={null}
result = requests.post(f"{API}/v1/evaluate-forecast", headers=HEADERS, json={
    "rows": records(sales), "series_column": ["store", "sku"], "time_column": "date",
    "target": "units", "horizon": 28, "windows": 3,
}).json()
print(result["ours"], result["baseline"])
```

Compare `mase` with 1, the score of a seasonal naive forecast that repeats last week. Look at `inside_80pct_interval` too. If it is far below 0.8, the stock levels set from the quantiles will run out more often than planned.

## Things that often go wrong

* **Missing days.** A day with no sales must be a row with zero units. If such days are left out, the model sees a series with gaps, not a series of zeros.
* **Stock-outs read as low demand.** A day without stock shows zero sales. Mark it, or the forecast learns that demand was zero.
* **Promotions known late.** A promotion added to `future_rows` only after the forecast was made cannot help that forecast. When you evaluate, use the promotion plan as it was known at the time.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.