Forecast next-window order volume with a causal dilated convolution, ordered quantiles and rolling-origin evaluation that respects feature availability.
Project: forecast warehouse queues with a causal temporal model
Build the observation ledger
For each warehouse and issue minute, retain event time, arrival time, queue size, worker capacity and future demand label. Split by time and warehouse identity according to the deployment question. Exclude late-arriving features from a forecast that would have run earlier. Compare against last-value and same-time-last-week baselines. Keep missing-interval and calendar rules fixed across train and test. The causal lesson defines the receptive field and timestamp gate.
Train one legal forward path
Use left padding before a dilated convolution so the final activation sees only the present and earlier measurements. The code accepts twelve observations, predicts one next-window outcome, and makes ordered lower, median and upper quantiles using positive distances. It runs a synthetic optimizer step to catch shape and gradient faults; it does not demonstrate forecast accuracy. Tune lookback, dilation and horizon on earlier validation periods only. The quantile lesson defines the loss.
Backtest at rolling origins
Replay many issue times in order, building each feature window from the as-available view. Advance the origin only after predictions are stored; do not normalize using future rows. Report pinball loss by quantile and horizon, median absolute error, interval coverage and width, plus error around promotions and capacity shocks. Group uncertainty by day or warehouse rather than pretending overlapping windows are independent. Preserve forecast timestamps to investigate any too-good result.
Evaluate the staffing decision
Feed frozen forecasts into a staffing rule with a declared overcapacity and undercapacity cost. Replay the decision under actual capacity limits and compare service-level breaches, idle hours and emergency shifts against the seasonal baseline. A model may reduce forecast loss but worsen a thresholded decision near a staffing boundary. Inspect intervals that crossed a schedule-change deadline too late to act. Keep human override and a low-information fallback when inputs are delayed.
Release and monitor
Predeclare acceptable interval coverage, width, service-level outcome and p95 scoring latency. Package feature arrival policy, normalization statistics, model weights, quantile order, horizon and calendar version. Re-run fixed windows in a clean process; alarm on missing telemetry, shifted arrival delays and rising interval misses. If the model fails the gate, route to the baseline rather than quietly widening intervals. The deliverable is a reproducible backtest and operational replay.
Implementation
import torch
from torch import nn
from torch.nn import functional as functional
torch.manual_seed(47)
queue_windows = torch.rand(4, 2, 12)
next_window_orders = torch.tensor([43.0, 51.0, 62.0, 47.0])
forecast_net = nn.Sequential(nn.ConstantPad1d((4, 0)),
nn.Conv1d(2, 8, kernel_size=3, dilation=2),
nn.ReLU(), nn.Conv1d(8, 3, kernel_size=1))
optimizer = torch.optim.AdamW(forecast_net.parameters(), lr=0.0007)
optimizer.zero_grad(set_to_none=True)
raw = forecast_net(queue_windows)[:, :, -1]
median = raw[:, 0]
lower = median - functional.softplus(raw[:, 1])
upper = median + functional.softplus(raw[:, 2])
ordered = torch.stack((lower, median, upper), dim=1)
assert ordered.shape == (4, 3)
assert torch.all(ordered[:, 0] <= ordered[:, 1])
assert torch.all(ordered[:, 1] <= ordered[:, 2])
quantiles = torch.tensor([0.2, 0.5, 0.8])
errors = next_window_orders[:, None] - ordered
loss = torch.maximum(quantiles * errors, (quantiles - 1) * errors).mean()
loss.backward()
optimizer.step()
assert torch.isfinite(loss)Performance and operating cost
For B windows of length T, a small width-k causal convolution costs roughly O(BTC_inC_outk), plus its activation memory and the final quantile head. The project also pays for as-available feature retrieval and rolling replay, which can dominate model inference in a warehouse system. Three quantiles require O(BQ) output values for Q quantiles; long horizons multiply that storage. The synthetic batch validates shapes and ordered outputs, not calibration or staffing value. Benchmark the full issue-time path.
Common Mistakes
- Do not let a centered or late-arriving feature into an issue-time window.
- Do not report interval coverage without its width and sample count.
- Do not claim an ordered synthetic output is a calibrated forecast.
