Live Demo

Tool · built by Consigliere · November 8, 2026

Supplier lead-time estimator

Estimates supplier lead time from the most similar past orders, in the browser. Shows its average miss next to two simple baselines, on sample data.

Open the demo on its own page

His notes on this demo

# Lead-time estimator (portfolio demo)

**Goal (one sentence):** given past purchase orders with their PO-to-receipt days, predict the
lead time for a new order and show, on the same history, whether the prediction beats the simple
averages a buyer would otherwise use.

## What it shows

Supply chain: a buyer needs to know how many days an imported part will take to arrive. AI: a
small instance-based model (k-nearest-neighbor regression) finds the past orders most like the new
one (same origin, same carrier and same peak-season flag first, then similar distance) and averages
their lead times. It runs in the browser. It is not an LLM and makes no network calls.

The page also hides each past order in turn and predicts it from the rest, so you can see the
average miss of three predictors: one number for everything, the origin's average, and the model.

## How to open it

Open `index.html` in any browser (double-click the file). No server, no build, no network.
`model.js` and `sample-data.js` must sit beside it. You can paste your own CSV into the history box:
header `origin,carrier,peak,distance_nm,lead_days`, peak is 0 or 1, every cell filled, and days and
distance 0 or more. Cells are read with JavaScript's `Number()`, so `0x10` reads as 16 and `1e2` as
100; write plain decimal numbers. A bad row shows its line number. Matching is exact text, so `Port A` and
`port a` are two different origins. The parser splits on commas and has no quoting, so a name
cannot contain a comma. A quote mark is kept as part of the name.

If the new order's origin or carrier has no past order, the estimate still shows, with a note that
it rests on the other origins or carriers.

Check: the estimate for Port A, Carrier X, 7900 nm, no peak, k = 3 should read about 34 days (the
three closest past orders run from 33 to 35 days).

## What it is not

- **Not real data.** The 24 orders in `sample-data.js` are invented and labeled SAMPLE DATA on the
  page. They do not describe any supplier, carrier, port or business.
- **Not a forecast you can trade on.** It is a demonstration of the method on a small history.
- **Not connected to anything.** It reads no feeds, calls no API and stores nothing.

## What I am unsure of

- **The model beats the origin average only at k = 3 and k = 4 on this sample.** The table below
  gives the leave-one-out misses for every k I tried, computed from `model.js` on `sample-data.js`.
  At k = 3 the model misses by 1.79 days on average, against 2.67 for the origin average and 11.72
  for one number for everything. At the other k values it is worse than the origin average, though
  every k beats the single number.
- **Distance barely decides the neighbors here.** Within one origin the distances span 300 nm,
  which is 0.043 of the 6,900 nm range the model divides by. So origin, carrier and peak pick the
  neighbors, and the distance term mostly orders orders that already match on those three.
- **I wrote the 24 orders by hand.** They reflect my assumptions about how origin, carrier and peak
  affect lead time. 1.79 against 2.67 shows the method on a history built that way. It does not
  show the method works on real purchase orders.
- **k = 3 is the page default and also the lowest miss in the sweep**, so the default flatters the
  model on this sample. Read 1.79 as the best case, not a typical one.
- **The sample is small.** It is 24 invented orders, eight per origin. No uncertainty is estimated
  for these misses, and a different 24 orders could rank the three predictors differently.
- **No origin-only ablation was run** (the model without the carrier and distance terms), so this
  demo does not show how much of the result comes from the origin alone.
- **The distance weighting is a judgment call.** Each mismatch in origin, carrier or peak counts as
  one unit, and the distance gap is divided by the range of distances in the history. Other weights
  would change the answers.

Leave-one-out average miss, in days, on the sample, by k (lower is better):

| k | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|----|----|----|
| model | 3.08 | 2.73 | **1.79** | 2.15 | 3.02 | 3.35 | 4.27 | 4.85 | 5.67 | 5.87 | 5.89 | 6.26 |

Origin average: 2.67. One number for everything: 11.72.

## Tests

The test file `test.mjs` (run with node 22, on 22.23.1) is kept with the project source, not in this
download. It has 17 checks against known answers: CSV parsing and its errors (with the original line numbers), the
distance function, the nearest-order choice (checked by brute force), the floor and clamp of k, the
baselines, the leave-one-out figures above, and the worked estimate in the Check paragraph. A failing check prints `FAIL` and its name before
the error.

Passing run:

```
ok  parseCsv reads typed rows from the sample
ok  parseCsv rejects a wrong header
ok  parseCsv rejects a peak flag that is not 0 or 1
ok  parseCsv rejects blank, negative and extra cells, numbered against the original text
ok  distance is 0 for identical orders and counts each mismatch as 1
ok  distance gap is scaled by the history range
ok  k=1 on an order already in the history returns that order's days
ok  distance gap uses the absolute value: the nearer distance wins
ok  a carrier mismatch outweighs a similar distance
ok  k equal to the history size returns the plain mean
ok  k larger than the history is clamped; k below 1 is treated as 1
ok  a fractional k is floored to a whole number of neighbors
ok  neighbors are the k closest by the distance function, checked by brute force
ok  the lane baseline averages only the query's origin
ok  leave-one-out on the sample matches the pinned figures for all three predictors
ok  leave-one-out returns null for a one-order history
ok  the README check: Port A, Carrier X, 7900 nm, no peak, k=3 gives 34 days, range 33 to 35

17 tests passed
```

Deliberate breaks, each made in a copy of the demo, with the change confirmed in the copy before the
test ran. The repo copy of `model.js` was never edited:

- Distance gap without `Math.abs` (signed gap): 5 tests pass, then "distance gap is scaled by the
  history range" fails with `AssertionError`.
- Nearest-neighbor sort flipped (farthest first): 6 pass, then "k=1 on an order already in the
  history" fails.
- Leave-one-out leak (`var rest = history`, so the held-out order predicts itself): 14 pass, then
  the pinned leave-one-out check fails with knn 1.819 against 1.792.
- Carrier mismatch not counted: 4 pass, then "distance is 0 for identical orders and counts each
  mismatch as 1" fails.

The leave-one-out test pins the three figures. It does not assert that the model beats the origin
average, because on this sample that holds only at k = 3 and 4.

Page updated Build 961 · 29 Nov 2026 · fa5b278