6. A data-driven supply chain

Every model so far invented its randomness: an exponential here, a PERT there. Real studies start from data, such as a few years of daily sales or a log of delivery delays. The hard questions are about that data. Does a distribution fit it? Should we replay history or resample it? Does it matter that the sites’ demands move together?

Cimba Python treats these as first-class questions. This chapter builds a multi-echelon inventory network driven by recorded demand and lead times, checks the input model before trusting it, and then compares input models head to head in one experiment. The model code never changes along the way.

The full script is tutorial/multi_echelon_inventory.py; the data is in tutorial/data/multi_echelon_inventory/.

The network

Six facilities form a tree. An external source (node 0) has unlimited stock. A distribution centre (node 1) orders from it and supplies two downstream sites. One of them, node 3, is a regional warehouse that in turn supplies nodes 4 and 5:

0 source ──► 1 DC ──┬──► 2 store
                    └──► 3 warehouse ──┬──► 4 store
                                       └──► 5 store

Every stocking node (1–5) faces daily customer demand (node 3’s is zero: it only serves other nodes) and follows a base-stock policy. When its inventory position falls to the reorder point, it orders up to the base stock from its upstream node. Shipments take a base lead time plus a random number of extra days.

Two kinds of input

The model consumes two very different kinds of real-world variability:

  • Daily demand is indexed by time. There is one value per day per facility, and “today’s demand” means the value for today’s date, however often or rarely the model asks.

  • Extra lead-time days form a sequence. Each shipment takes the next value, whenever it happens.

Cimba has a field type for each:

Declaration

Read with

Meaning

cb.Input[T]

.next()

A sequence. Each call consumes the next value.

cb.Series[T] = cb.Series(step=..., origin=...)

.now(), .at(t)

Time-indexed buckets. The value at time t belongs to bucket floor((t - origin) / step).

A Series never “skips” a value because the model read it twice, or “loses” one because the model didn’t read it on a given day. The bucket depends only on the time.

Base classes and specializations

All facilities share fields, so they share a base class:

class Facility(cb.Model):
    network: cb.Ref["MultiEchelonInventory"]
    node: cb.Param[int]
    upstream: cb.Ref["Facility"] | None
    base_stock: cb.Param[float]
    reorder_point: cb.Param[float]
    initial_inventory: cb.Param[float]
    base_lead_time: cb.Param[float]
    on_hand: float = 0.0
    inventory_position: float = 0.0
    backorder: float = 0.0
    total_demand: float = 0.0
    total_shipped: float = 0.0
    total_late_sales: float = 0.0
    on_hand_total: float = 0.0
    on_hand_samples: int = 0
    avg_on_hand: cb.Output[float]
    service_level: cb.Output[float]
    orders: cb.Store[Order]

The source and the stocking facilities are subclasses. A subclass inherits every field and may add its own. Only stocking facilities have demand, so only they declare the Series:

class StockingFacility(Facility):
    """Inventory-holding node with a daily series and replenishment."""

    demand: cb.Series[float] = cb.Series(step=1.0, origin=1.0)

    @cb.on_start
    def initialize(self):
        self.on_hand = self.initial_inventory
        self.inventory_position = self.initial_inventory

cb.Series(step=1.0, origin=1.0) makes buckets one day wide, starting at time 1. The customer-service process holds for a day and then reads self.demand.now(): on day 1 bucket 0, on day 2 bucket 1, and so on.

@cb.process
def serve_customer(self):
    while True:
        self.on_hand_total += self.on_hand
        self.on_hand_samples += 1
        cb.hold(1.0)
        demand = self.demand.now()
        self.total_demand += demand
        if self.network.backorder >= 0.5:
            shipped = min(demand + self.backorder, self.on_hand)
            self.on_hand -= shipped
            self.inventory_position -= shipped
            remaining = demand - shipped
            self.backorder += remaining
            if remaining > 0.0:
                self.total_late_sales += remaining
        else:
            shipped = min(demand, self.on_hand)
            self.total_shipped += shipped
            self.on_hand -= shipped
            self.inventory_position -= shipped

Here is the whole model as cimba.diagrams.process_graph() sees it, before any data is bound (hence demand · no source). Each stocking facility’s place_order spawns an Order and puts it into its upstream’s orders store. fulfill_orders gets orders and spawns Shipment models, which read the lead-time input and hand themselves back to the network:

        ---
config:
  layout: elk
---
flowchart TD
  subgraph g0["multiecheloninventory: MultiEchelonInventory"]
    n0[/"lead_time_delay · trace"/]
    n1["completed_shipments · Store[Shipment]"]
    n2(["reclaim_shipments"])
  end
  subgraph g1["multiecheloninventory.facilities[0]: SourceFacility"]
    n3["orders · Store[Order]"]
    n4(["fulfill_orders"])
  end
  subgraph g2["multiecheloninventory.facilities[1]: StockingFacility"]
    n5["orders · Store[Order]"]
    n6[/"demand · no source"/]
    n7(["place_order"])
    n8(["fulfill_orders"])
    n9(["serve_customer"])
  end
  subgraph g3["multiecheloninventory.facilities[2]: StockingFacility"]
    n10["orders · Store[Order]"]
    n11[/"demand · no source"/]
    n12(["place_order"])
    n13(["fulfill_orders"])
    n14(["serve_customer"])
  end
  subgraph g4["multiecheloninventory.facilities[3]: StockingFacility"]
    n15["orders · Store[Order]"]
    n16[/"demand · no source"/]
    n17(["place_order"])
    n18(["fulfill_orders"])
    n19(["serve_customer"])
  end
  subgraph g5["multiecheloninventory.facilities[4]: StockingFacility"]
    n20["orders · Store[Order]"]
    n21[/"demand · no source"/]
    n22(["place_order"])
    n23(["fulfill_orders"])
    n24(["serve_customer"])
  end
  subgraph g6["multiecheloninventory.facilities[5]: StockingFacility"]
    n25["orders · Store[Order]"]
    n26[/"demand · no source"/]
    n27(["place_order"])
    n28(["fulfill_orders"])
    n29(["serve_customer"])
  end
  subgraph g7["Shipment (spawned)"]
    n30[["new Shipment"]]
    n31(["deliver"])
  end
  subgraph g8["Order (spawned)"]
    n32[["new Order"]]
  end
  n1 -->|get| n2
  n3 -->|get| n4
  n4 -->|spawn| n30
  n7 -->|spawn| n32
  n7 -->|put| n3
  n5 -->|get| n8
  n8 -->|spawn| n30
  n6 -->|now| n9
  n12 -->|spawn| n32
  n12 -->|put| n5
  n10 -->|get| n13
  n13 -->|spawn| n30
  n11 -->|now| n14
  n17 -->|spawn| n32
  n17 -->|put| n5
  n15 -->|get| n18
  n18 -->|spawn| n30
  n16 -->|now| n19
  n22 -->|spawn| n32
  n22 -->|put| n15
  n20 -->|get| n23
  n23 -->|spawn| n30
  n21 -->|now| n24
  n27 -->|spawn| n32
  n27 -->|put| n15
  n25 -->|get| n28
  n28 -->|spawn| n30
  n26 -->|now| n29
  n0 -->|next| n31
  n31 -->|put| n1
    

Look at what this code does not contain: no array, no day counter, no check for the end of the data. It asks for today’s demand and gets it.

The network keeps its facilities in a heterogeneous list, facilities: list[Facility], and the global lead-time input lives on the root model because every shipment uses it:

class MultiEchelonInventory(cb.Model):
    backorder: cb.Param[float] = 0.0
    lead_time_delay: cb.Input[float] = inputs.trace([0.0], on_exhausted="wrap")
    completed_shipments: cb.Store[Shipment]
    facilities: list[Facility]

The class default, inputs.trace([0.0], on_exhausted="wrap"), means “no extra days” until we bind real data. It gives the model a sensible behavior out of the box.

Step 1: look at the data

demand, lead_time_delay = load_data()
demand.shape            # (10000, 5): 10,000 days × stocking nodes 1..5
lead_time_delay[:10]    # [1. 0. 1. 2. 1. 2. 1. 1. 2. 0.]

Two observations shape our input model:

  • The extra lead-time days are small integers (0 to 6). A continuous distribution would be a poor description. The simplest faithful model is to resample the observed values: inputs.bootstrap.iid(lead_time_delay). (For continuous data, inputs.fit(data) ranks exponential, gamma, lognormal, normal and Weibull fits by AIC. See Choose and check an input model.)

  • Demand is a panel: five series over the same days. If a heat wave lifts sales everywhere, resampling each site independently would break that link. A joint resample keeps it: it picks the same calendar blocks for every site.

Step 2: build and check the demand model

nodes = range(1, NUM_NODES)
panel = {node: demand[:, node - 1] for node in nodes}
mean_block = round(demand.shape[0] ** (1 / 3))       # 22 days

joint = inputs.bootstrap.joint(panel, mean_block=mean_block,
                               tag="facility-demand")

bootstrap.joint is a stationary block bootstrap over the whole panel. It builds new histories from random runs of consecutive days, with an average run of 22 days, and uses the same runs for every member. Indexing it, joint[2], gives the source for node 2.

Before trusting it, check it. cimba.analysis.check_input() generates a sample from a source and compares it with the reference data:

report = cb.analysis.check_input(joint[1], reference=panel[1])
report.reference_mean, report.generated_mean           # 49.54, 48.67
report.reference_variance, report.generated_variance   # 2426.4, 2312.9
report.ks_statistic                                    # 0.011
report.reference_acf[:4], report.generated_acf[:4]     # both ≈ (1, 0, 0, 0)

Means, variances and quantiles agree. The Kolmogorov–Smirnov distance between the marginal distributions is about 1%, and the autocorrelation structure (none to speak of here) is preserved. For the panel as a whole, pass the joint source and the dictionary. The report adds the reference and generated cross-correlation matrices:

panel_report = cb.analysis.check_input(joint, reference=panel)
panel_report.reference_correlation
panel_report.generated_correlation

(Node 3’s row is nan because its demand is constantly zero.)

Step 3: bind the sources and run

Binding is plain assignment to the model objects:

def main() -> int:
    demand, lead_time_delay = load_data()
    network = MultiEchelonInventory()
    mean_block = round(demand.shape[0] ** (1.0 / 3.0))
    joint = inputs.bootstrap.joint(
        {node: demand[:, node - 1] for node in range(1, NUM_NODES)},
        mean_block=mean_block, tag="facility-demand",
    )
    for node in range(1, NUM_NODES):
        network.facilities[node].demand = joint[node]
    network.lead_time_delay = inputs.bootstrap.iid(lead_time_delay)
    result = cb.Experiment(
        network, replications=20,
        window=cb.Window(warmup=WARMUP, duration=DURATION), seed=123,
    ).run()
    if result.failed.any():
        raise RuntimeError(f"{result.failed.sum()} inventory trials failed")
    print("Average on-hand by node:", np.round([
        result[facility].avg_on_hand.values.mean()
        for facility in network.facilities], 3))
    print("Service level by node:", np.round([
        result[facility].service_level.values.mean()
        for facility in network.facilities], 4))
    return 0
$ uv run python tutorial/multi_echelon_inventory.py
Average on-hand by node: [   0.    1755.096  321.631  363.721   56.403   84.804]
Service level by node: [1.     1.     0.9995 0.     0.3082 0.2617]

(Node 3’s “service level” is 0 only because it has no customers of its own.)

Step 4: ask the inputs what they did

Every input reports its provenance and consumption:

store = network.facilities[2]
record = result[store].demand

record.source[0]
# {'method': 'bootstrap.joint', 'parameters': {'mean_block': 22, 'member': '2',
#   'panel_sha256': '094a…'}, 'sha256': '756b…', 'tag': 'facility-demand',
#  'prefix_stable': True, 'on_exhausted': 'extend', ...}

record.consumed[0, :5]     # [359 359 359 359 359]  days read per trial
record.extended.max()      # 0: no trial ran out
record.rows(0, 0)[:5]      # the exact demand trial (0, 0) saw

result[network].lead_time_delay.consumed[0, :5]   # [40 49 41 40 41] shipments

source records what produced the values: method, parameters and SHA-256 fingerprints of the reference data, so a result can always be traced back to the exact data it used. rows(point, replication) regenerates the exact values any trial saw. Rows are regenerated on demand rather than stored, so this costs nothing until you ask.

How much data is enough?

We never said how many days of demand to generate. For a Series, Cimba computes the number of buckets the window needs, ceil((warmup + duration + cooldown − origin) / step), and generates at least that many rows per replication. For sequence inputs like the lead time, it starts with a default length (1,024 values) and, if a trial reads past the end, extends it. The row is regenerated at double the length and the trial rerun. Resampling sources are prefix-stable: the longer row starts with exactly the same values. So an extended trial gives exactly the result it would have given with enough data from the start. results.meta.extensions counts how often this happened.

Step 5: compare input models head to head

Does keeping the sites’ days aligned actually matter for the answer? Put the two input models side by side in one experiment. Each facility’s demand becomes a two-valued sweep: joint member or independent stationary bootstrap. These five sweeps must move together. We want “all joint” versus “all independent”, not 2⁵ mixtures. That’s what cb.sweeps (plural) is for:

independent = {n: inputs.bootstrap.stationary(panel[n], mean_block=mean_block)
               for n in nodes}
axes = cb.sweeps(*[[joint[n], independent[n]] for n in nodes])   # linked
for n, axis in zip(nodes, axes):
    network.facilities[n].demand = axis

network.lead_time_delay = inputs.bootstrap.iid(lead_time_delay)
result = cb.Experiment(network, replications=50,
                       window=cb.Window(duration=360.0), seed=123).run()

for n in (1, 2, 4, 5):
    store = network.facilities[n]
    print(n, cb.analysis.compare(result[store].service_level, a=0, b=1))
1 Comparison(a=0, b=1, n=50, difference= 0.0005, lower=-0.0001, upper= 0.0011)
2 Comparison(a=0, b=1, n=50, difference=-0.0007, lower=-0.0018, upper= 0.0004)
4 Comparison(a=0, b=1, n=50, difference=-0.0467, lower=-0.0891, upper=-0.0044)
5 Comparison(a=0, b=1, n=50, difference=-0.0376, lower=-0.0738, upper=-0.0015)

For the DC and node 2 the choice makes no detectable difference. For nodes 4 and 5, which share the regional warehouse, resampling each site independently lowers the estimated service level by about 4 percentage points, and the paired intervals exclude zero. The input model is a modeling decision with consequences, and now you can measure them.

Two details make this comparison sharp:

  • Both design points run with the same replication seeds, and the lead-time input has its own stream, identified by its place in the model. Every shipment delay is therefore identical across the two points. Only the demand model differs.

  • compare pairs replication r of point 0 with replication r of point 1, which removes most of the run-to-run noise.

The same pattern compares any input models: a fitted AR model (inputs.fitted.sieve(history)) against a block bootstrap, a trace against a resample, or a fitted distribution against the data it was fitted to.

What you learned

  • Input[T] is a sequence (next()); Series[T] is time-indexed (now(), at(t)). Model code never manages cursors, lengths or the end of the data.

  • The input workflow is look → choose → check → bind → run → inspect → compare.

  • inputs.bootstrap.joint resamples a panel while keeping its members aligned; check_input compares a source with reference data before a run.

  • Results carry provenance (source), consumption (consumed, extended) and replay (rows) for every input.

  • Linked sweeps (cb.sweeps) compare input models head to head under common random numbers.

Where next

The next chapter, 7. Tuning a policy, searches for better policy parameters and estimates the performance of the chosen settings. For other directions:

  • Concepts explains each idea more precisely, including edge cases the tutorial glossed over.

  • How-to guides has focused recipes for recurring jobs.

  • Architecture shows how the pieces fit together inside.