Skip to content
Français

Hierarchies that must add up

  • Lesson 18
  • 40 min
  • Module 6: Hard series and many series

Where we are

In Module 5, we forecast each product with regression, LightGBM and a foundation model. Kenji also needs forecasts per category and per region, and the numbers must add up. In this lesson, we see why separate forecasts do not agree, and we reconcile them with bottom-up, top-down and MinT.

Plays from YouTube. Nothing loads from YouTube until you press play. Privacy

What you will be able to do

Make forecasts at several levels tell one story. The total, each region, each category and each product get their own forecast, and these forecasts do not add up. Reconciliation changes them so that they do. You compare three ways to do it on Larkfield’s 634 series, and you see which level each one helps.

NotebookDo this lesson in a notebookColab · Kaggle · GitHub

The notebook has every code cell, task and check of this lesson. It builds Larkfield’s hierarchy, forecasts all 634 series with ETS, and reconciles them in three ways. Its setup cells install the packages and download the Larkfield data, so it needs no local setup. About 40 minutes, of which about 1 minute is waiting for the forecasts.

Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.

Before you startWhat to know and to have readyAbout 40 minutes

What you need to know

What you need on your computer

Nothing: the notebook runs in Colab or Kaggle. Its first cell also installs hierarchicalforecast 1.5.3.

Tested with Python 3.12, pandas 2.3.3, statsforecast 2.1.1 and hierarchicalforecast 1.5.3, in Jupyter on macOS.

A family plans a party. Each child guesses how many drinks their friends will want: 30 and 50. The parent guesses the drinks for the whole party: 90. Who is right?

Each guess is reasonable. Each person knows something. The children know their own friends. The parent remembers the last party, where everybody drank more than expected. But the shop sells one number of drinks. If the family buys 80, the parent’s plan fails. If it buys 90, the children’s lists are wrong.

The family needs numbers that add up: one number per child, and a total that equals their sum. Larkfield has the same problem, with 634 numbers instead of three.

Larkfield sells 200 products in 3 regions. Different people plan with different levels:

  • Kenji orders stock per product and region: 600 series.
  • The category buyers plan per category in each region: 30 series.
  • Each regional warehouse plans its staff and space per region: 3 series.
  • The finance team plans with the total: 1 series.

This structure is a hierarchy: each level is the sum of the level below it. Priya forecasts every one of the 634 series with ETS for the 14 days after the last cut-off, 15 March 2026. Then she compares the total’s own forecast with the sum of the 600 product forecasts:

total's own forecast 14346 | sum of the products 14764 | gap -2.8%

Two honest forecasts of the same 14 days disagree by 418 units. Finance plans with 14,346; Kenji’s orders add up to 14,764. Neither knows which number to trust.

  • Units sold, all products
  • Total's own forecast (14,346)
  • The 600 product forecasts added up (14,764)
The same 14 days, forecast twice: once for the total, once as the sum of 600 products. The two lines differ every day. Both are below the real sales (16,726 units): spring was starting.
Text description of the chart

The chart shows Larkfield’s total daily sales for four weeks before 15 March 2026, between about 900 and 1,100 units a day. For the next 14 days, it shows the real sales as dots and two forecast lines. The total’s own ETS forecast adds up to 14,346 units. The 600 product forecasts added up give 14,764 units. The two lines are close but different every day. The real sales, 16,726 units, are above both.

The gap is not a one-time accident. The course measured it at the last four cut-offs:

Cut-off Total’s own forecast Products added up Gap
21 Dec 2025 14,693 16,141 −9.0%
18 Jan 2026 13,709 13,993 −2.0%
15 Feb 2026 14,117 13,998 +0.9%
15 Mar 2026 14,346 14,764 −2.8%

At Christmas, the gap is 1,448 units.

Why do separate forecasts disagree? Each series has its own model, fitted on its own data. The total is smooth: random ups and downs of single products cancel out. A slow product is noisy, and ETS may even forecast below zero for it. At the last cut-off, 282 of the 8,400 product-day forecasts were negative. Nothing forces the 634 models to agree.

Forecasts that add up at every level are coherent. Reconciliation takes the separate forecasts (the base forecasts) and changes them until they are coherent. There are three common ways.

  • Bottom-up: keep the product forecasts, and add them up for every level above. The total becomes 80 in the party example.
  • Top-down: keep the total, and split it down to the products in proportion to their own forecasts. The children get 90 × 30/80 = 33.75 and 90 × 50/80 = 56.25.
  • MinT (minimum trace): change every forecast a little, at every level, so that they add up. It moves most the forecasts that had the largest errors in the past. “Trace” is the sum of the error variances of all the series; MinT chooses the coherent forecasts that make it as small as possible.

The party, with every value:

Method Child 1 Child 2 Total What changed
Base forecasts 30 50 90 nothing: 30 + 50 ≠ 90
Bottom-up 30 50 80 the total
Top-down 33.75 56.25 90 the children
Middle way, equal trust 33.33 53.33 86.67 all three, by 3.33 each

The last row is MinT’s idea when every forecast is trusted the same. The gap is 10. Each of the three numbers moves by 3.33: the children up, the total down. Then 33.33 + 53.33 = 86.67. MinT does the same, but with weights. It learns them from the past errors of each series, so a noisy series moves more and a reliable series moves less.

The notebook’s first cell checks the first two rows:

bottom-up: products [30.0, 50.0] total 80.0
top-down: products [33.75, 56.25] total 90.0

The bottom series is one product in one region, for example the garden hose in the north. hierarchicalforecast’s aggregate() builds every level from it. It also builds a summing matrix S, a table of 0s and 1s that says which bottom series add up to which upper series. A product that starts later gets zero sales before its launch, so that every series has every day.

spec = [["total"], ["total", "region"], ["total", "region", "category"], ["total", "region", "category", "product"]]
Y, S, tags = aggregate(bottom[["total", "region", "category", "product", "ds", "y"]], spec)
print({k: len(v) for k, v in tags.items()}, "| rows:", len(Y))
{'total': 1, 'total/region': 3, 'total/region/category': 30, 'total/region/category/product': 600} | rows: 461552

That is 634 series × 728 days. The tree below shows the upper levels with their forecasts for the last cut-off, before and after MinT.

A tree with the total at the top, three regions below it, then categories and products. Each of the total and the region boxes shows the units sold, the level's own forecast, the sum of its products' forecasts with the gap, and a green row with the forecast after MinT.
Before reconciliation, each level's own forecast differs from the sum of its products (north: −6.0%). After MinT, the regions add up to the total: 6,265 + 5,141 + 3,283 = 14,689.
Text description of the diagram

The diagram is a tree. At the top is the total: 16,726 units sold in the 14 days; its own forecast 14,346; its products add up to 14,764, a gap of −2.8%; after MinT, 14,689. Below it are three regions. North: sold 6,684; own forecast 5,983; products 6,364, a gap of −6.0%; after MinT 6,265. South: sold 6,058; own 5,097; products 5,128, a gap of −0.6%; after MinT 5,141. Coast: sold 3,984; own 3,310; products 3,271, a gap of +1.2%; after MinT 3,283. Below the north are its 10 categories, such as garden-tools and kitchen, and below a category its products, such as the garden hose P077. In all, there are 634 series: 1 total, 3 regions, 30 categories in a region and 600 products in a region.

HierarchicalReconciliation takes the base forecasts, the fitted values of the training days (MinT uses their errors to learn its weights) and the summing matrix. It returns one coherent column per method:

hrec = HierarchicalReconciliation(reconcilers=[BottomUp(), TopDown(method="forecast_proportions"),
MinTrace(method="mint_shrink", nonnegative=True)])
rec = hrec.reconcile(Y_hat_df=base, Y_df=fitted, S_df=S, tags=tags)

nonnegative=True keeps every product forecast at zero or above. The notebook scores every level at the last cut-off:

WAPE % by level, last cut-off:
total region category product
base (not coherent) 14.2 14.0 28.1 62.9
bottom-up 12.2 12.6 30.7 62.9
top-down 14.2 14.3 28.3 63.3
MinT 12.3 12.5 28.2 61.3

category means one category in one region. At this cut-off, MinT looks very good. It is the only method that improves the products (61.3% against 62.9%). It is also close to the best at the total and the regions. After MinT, the gap between the total and its products is 0.0%.

One cut-off is 14 days. The course ran the same comparison at the last four cut-offs, from 21 December 2025 to 15 March 2026. The base forecasts took 224 seconds; the reconciliation took 14.

A table with four rows (total, region, category in a region, product in a region) and four columns (base, bottom-up, top-down, MinT). Each cell is a WAPE percentage; the lowest of each row is green.
Over four cut-offs, top-down is best at the three upper levels, and MinT is best only at the product level, by 1.1 points. The differences are small.
Text description of the diagram

The table shows the WAPE in percent over the last four cut-offs. Total: base 12.5, bottom-up 13.6, top-down 12.5, MinT 13.2; base and top-down are best. Region: base 13.8, bottom-up 14.8, top-down 13.6 (best), MinT 14.3. Category in a region: base 26.5, bottom-up 26.3, top-down 24.8 (best), MinT 25.2. Product in a region: base 64.7, bottom-up 64.7, top-down 66.6, MinT 63.6 (best). Bottom-up keeps the product forecasts, so its product row equals the base row.

The honest result:

  • MinT is best only at the product level: 63.6% against 64.7%.
  • Top-down is best at the three upper levels. It keeps the total’s own forecast, and the total is the easiest series to forecast. But it is the worst at the products (66.6%).
  • Bottom-up is the worst at the total and the regions, because it adds up 600 noisy product forecasts.
  • All the differences are small: at most about 2 points at any level.

Reconciliation did not make Larkfield’s forecasts much more accurate. Its main gift is that every level now tells the same story.

The course’s forecasting job (Module 8) uses bottom-up. That is not the most accurate choice in the table, so here are the reasons.

  • Kenji orders products. Bottom-up changes nothing at the level where the money is spent. MinT would gain 1.1 points there.
  • It is always coherent and never negative. The job sets negative product forecasts to zero. A sum of numbers that are zero or above is never negative.
  • It is simple to explain. “The region’s forecast is the sum of its products” needs no matrix and no weights.
  • It needs nothing more. MinT needs the fitted values of all 634 series and a matrix solver at every run.

If finance cared most about the total, top-down would serve them better. A team can also use two numbers: bottom-up for the orders, and the total’s own forecast as a check. When they drift apart by more than a few percent, someone looks.

Reconcile when different people plan with different levels of the same numbers. Then a disagreement becomes a meeting, and coherent numbers end it.

You do not need it when only one level is used. For example, Grace plans one help-desk roster from one ticket series.

It costs:

  • Forecasts at every level. 634 base forecasts instead of 600 took 224 seconds with ETS. Bottom-up needs only the 600.
  • Time and memory for MinT. With thousands of series, its matrix gets large. Here it took 14 seconds.
  • Explanation. A planner asks: “Why is my number not the model’s number?” With top-down and MinT, the answer involves the other series.

Common mistake

Choosing from one cut-off

Symptom: Tomás runs the notebook, sees the last cut-off’s table and writes: “MinT is the best or close to the best at every level. Let us use it everywhere.” The finance team then gets a total forecast that is worse than the one they had.

Cause: one cut-off is 14 days. At the last cut-off, MinT’s total WAPE was 12.3%. Over four cut-offs, it was 13.2%, worse than the base and top-down (12.5%). The order of the methods changes from one cut-off to the next.

Fix: run the reconciliation in the full backtest, score every level, and choose for the level where the decision is made. For Kenji, that is the product in a region.

Consequence in AI work: a method chosen from one good window looks worse in the next season. The team then loses trust in the forecasts and in the people who chose them.

Guided practice: reconcile in the notebook

Section titled “Guided practice: reconcile in the notebook”
  1. Open the notebook from the box Do this lesson in a notebook at the top of this page.
  2. Run the setup cells. The data cell ends with the Ready: line.
  3. In section 1, predict which method changes the total. Then run the cell. You see total 80.0 and total 90.0.
  4. In section 2, run the cell. You see the four levels and rows: 461552.
  5. In section 3, predict if the total’s forecast is higher or lower than the sum of its products. Run the cell. You see gap -2.8%.
  6. In section 4, run the reconciliation. You see the table of four methods and four levels.
  7. In section 5, write your answer in the Your turn cell, then run the check cell.

Before you run it, predict: after MinT, how far apart are the total’s forecast and the sum of the 600 products? In the notebook, section 5, compute the gap in % for the MinT column, as in section 3, and put it in mint_gap.

Show the answer
col = columns["MinT"]
t = rec[rec["unique_id"] == "total"][col].sum()
p = rec[rec["unique_id"].isin(tags["total/region/category/product"])][col].sum()
mint_gap = round(float(100 * (t - p) / p), 1)
print(mint_gap)
0.0

The gap is 0: after reconciliation, the total is exactly the sum of its products. The same is true for bottom-up and top-down. Only the base forecasts disagree.

Check your understanding

Explain with Larkfield's numbers why separate forecasts at each level are a problem, and what reconciliation does about it.

Show the answer

Each of the 634 series has its own ETS model, so nothing makes them agree. At the last cut-off, the total’s own forecast was 14,346 units. The 600 product forecasts added up to 14,764, a gap of −2.8%. At Christmas, the gap was −9.0%. Finance and Kenji would plan with different numbers. Reconciliation changes the forecasts so that every level is the sum of the level below. Bottom-up adds up the products, top-down splits the total, and MinT moves every forecast a little, most where past errors were largest.

Check your understanding

Over four cut-offs, which method wins at which level? Why does the forecasting job still use bottom-up?

Show the answer

Top-down wins at the total (12.5%, tied with the base), the region (13.6%) and the category in a region (24.8%). MinT wins only at the product level (63.6% against 64.7%). Bottom-up is the worst at the total and the regions. The job uses bottom-up because Kenji orders products, and bottom-up keeps the product forecasts. It is always coherent, never negative (the job sets negative product forecasts to zero), easy to explain, and needs no fitted values or matrix. MinT would gain 1.1 points at the product level.

Check your understanding

Predict: Larkfield opens a fourth region with 40 products. Finance wants the best total, and Kenji wants the best product orders. Which reconciliation would you suggest, and what would you check first?

Show the answer

There is no single best method for both. Top-down served the total best and bottom-up keeps the product forecasts that Kenji orders from. One option: use bottom-up for the orders, and show finance the total’s own forecast next to it. Add an alarm for when they differ by more than a few percent. Before deciding, run the full backtest with the new region and score every level. A new region has a short history, so check its product forecasts first. With top-down, its share would come from very noisy forecasts.

Recap

  • A hierarchy is a set of series where each level is the sum of the level below. Larkfield has 1 total, 3 regions, 30 categories in a region and 600 products in a region.
  • Separate forecasts are not coherent: the total’s own forecast and the sum of the products differed by −9.0% to +0.9%.
  • Reconciliation makes them add up: bottom-up keeps the products, top-down keeps the total, MinT moves every forecast, most where past errors were largest.
  • Over four cut-offs, MinT is best only at the product level (63.6% against 64.7%); top-down is best above it. The differences are small.
  • The forecasting job uses bottom-up: Kenji orders products, and it is simple, coherent and never negative.

Our forecasts add up now. At the bottom of the hierarchy, many products sell on only a few days. Next, you forecast these slow sellers, and you see why the usual error measures choose the wrong forecast for them.

Next: Intermittent demand