Inventory 1: Stock as State
Uses Only Free-Tier SpiceGrinder
Problem statement
Module 5 is about systems: models whose parts read and change the same thing. The thing here is stock. A shop has units on a shelf, customers take them, and the shop orders more when the shelf runs low. Lesson 3.1 gave a model memory with Recur: a running backlog, each row starting from the row before. Stock is the same kind of quantity, with one new difficulty. The decision to order depends on the stock, and the order changes the stock a few days later.
This lesson builds the store that the whole module uses, in the simplest way SpiceGrinder allows: one Recur node that holds everything. Everything here is Free tier (NegativeBinomial and Recur); the later lessons use Pro components.
The store, with every choice made once so that the next four lessons can reuse it:
- One row is one day. Demand for the day comes from
NegativeBinomialwithr = 8andp = 0.5: a mean of 8 units a day and a variance of 16, twice what aPoissonwith the same mean would have. Real demand clumps (a promotion, a rainy day), and a Poisson count is too regular for it. Days with no sales and days with 20 or more both happen: the chance of demand 0 is 0.39% against 0.034% for a Poisson, and of 20 or more it is 0.96% against 0.025%. - The reorder rule is a reorder point. At the end of each day the store looks at its inventory position: the stock on the shelf plus everything it has already ordered and not yet received. If the position is 16 or less (the reorder point R), it orders 40 units (the order quantity Q). Looking at the position, not just the shelf, stops the store from ordering again every day while a delivery is on its way.
- A delivery takes 2 days. An order placed at the end of Monday is on the shelf on Wednesday morning. Orders arrive whole.
- Unmet demand is lost. A customer who finds the shelf empty goes elsewhere (lost sales, not backorders). That keeps the stock a true count that never goes below zero, and it makes the unmet demand something we can count.
- The store starts with 40 units and nothing on order.
The questions:
- How many days a year does the store run out (a stockout day: a day on which some demand is not met)?
- What share of demand does it serve (the fill rate: 1 minus the lost units over the units demanded)?
- How much stock does it hold on average, and how many orders does it place a year?
- Can any of this be checked against an exact answer, and what does the model’s single node leave out?
The model
The state of the store is four numbers: the stock at the end of the day, the order that arrives tomorrow morning, the order placed tonight (which arrives the morning after tomorrow), and the units lost today. Recur keeps all four as its state, one column each, and the demand is its input. The model is samples/simulation/inventory-recur-store.xml:
<dataset columns="stock, orderTomorrow, orderPlaced, lost, demand" seed="501">
<root node="Store"/>
<nodes>
<NegativeBinomial name="Demand" r="8" p="0.5"/>
<Recur name="Store" initial="40, 0, 0, 0"
expression="y[0] = max(0, s[0] + s[1] - x[0]); y[1] = s[2]; y[2] = if(max(0, s[0] + s[1] - x[0]) + s[2] <= 16, 40, 0); y[3] = max(0, x[0] - s[0] - s[1])"
appendInput="true">
<input name="Demand"/>
</Recur>
</nodes>
</dataset>
initial has four values, so the state has four columns, s[0] to s[3] for the previous day and y[0] to y[3] for the new one; x[0] is the day’s demand. Each assignment in the expression is one column of the state:
| Column | Meaning | Rule |
|---|---|---|
y[0] | stock at the end of the day | last night’s stock s[0] plus the delivery that arrives this morning s[1], minus the demand x[0], never below zero |
y[1] | order arriving tomorrow morning | the order placed last night, s[2], moves up one place |
y[2] | order placed tonight (0 for none) | 40 if the new stock plus the order still on its way (s[2]) is at most 16, else 0 |
y[3] | units of demand lost today | the demand that the stock plus this morning’s delivery could not cover |
There are two things to note here. 1) The order placed last night is counted in the inventory position through s[2]: the one in s[1] arrived this morning and is already in the stock. 2) Every assignment reads the previous state, so the stock after demand has to be written out twice, once in y[0] and once inside the order rule. A day’s y[2] is the order placed that night; the next day it shows as y[1], and the morning after that it is part of the stock. That is the 2-day lead time.
appendInput="true" adds the demand as a fifth column, so each row reads: stock, arriving tomorrow, ordered tonight, lost, demand. In the model’s if(...), the <= is just <= written for XML.
Run one year of 365 days:
grind --count 365 inventory-recur-store.xml
The first nine rows with the model’s own seed, 501 (you get exactly these values if you leave the seed alone). The Demand column is the model’s fifth output column, and grind --node Demand gives the same values:
| Day | Demand | Stock at end | Arriving tomorrow | Ordered tonight | Lost |
|---|---|---|---|---|---|
| 1 | 14 | 26 | 0 | 0 | 0 |
| 2 | 2 | 24 | 0 | 0 | 0 |
| 3 | 4 | 20 | 0 | 0 | 0 |
| 4 | 5 | 15 | 0 | 40 | 0 |
| 5 | 10 | 5 | 40 | 0 | 0 |
| 6 | 19 | 26 | 0 | 0 | 0 |
| 7 | 10 | 16 | 0 | 40 | 0 |
| 8 | 4 | 12 | 40 | 0 | 0 |
| 9 | 4 | 48 | 0 | 0 | 0 |
Day 4 is the first order: 20 on the shelf, 5 sold, 15 left, and 15 is at most 16, so the store orders 40. On day 5 the shelf drops to 5, but the position is 5 plus the 40 on order, so no second order. On day 6 the 40 arrive in the morning: 5 + 40 - 19 = 26. On day 7 the stock is 16, exactly the reorder point, and “16 or less” means it orders again. On day 9 the order placed on day 7 arrives: 12 + 40 - 4 = 48.
One year, one run
The picture is days 1 to 120 of the same run: a sawtooth. Stock falls with demand, the store orders when the position crosses 16, and two days later the stock jumps by 40. Twenty-three orders are placed in these 120 days. Stock reaches zero with demand still unmet on two days: day 39 (7 units lost) and day 103 (3 units lost). The stock can fall well below the reorder point before a delivery arrives, because it keeps falling for the 2 days the order is on its way.
For the whole year of 365 days of this run:
- 17 stockout days, with 99 of the 3,047 units demanded lost, so a fill rate of 96.75%;
- 74 orders placed;
- an average end-of-day stock of 20.18 units.
Is that typical? A second seed is a second year. To see that for yourself, leave out the seed:
grind --no-seed --count 365 inventory-recur-store.xml
One year is one draw
The 400 years in the picture are 400 runs of 365 days each, seeds 1 to 400. Each starts, like the shipped run, with 40 units and nothing on order.
The stockout days in a year range from 4 to 28, with a mean of 15.98 and a standard deviation of 3.92. The fill rate of a single year ranges from 95.3% to 99.5%. In 57 of the 400 years the store ran out on more than 20 days, in 74 the fill rate was below 97%, in 5 it reached 99% or more, and in none was it below 95% or free of stockouts. Quoting “this store’s fill rate” from one year’s run would be quoting one draw: the year of seed 501 (96.75%, 17 days) is an ordinary one.
The long run
A year is short, and the store starts from a made-up state. To measure what the policy does in steady state, run it for 250,000 days, 80 times (seeds 1000 to 1079), and drop the first 1,000 days of each run. That is 20 million days. The 80 runs are independent, so the spread between them gives the standard error.
The end-of-day stock is spread over every level from 0 to 56 (R + Q). The tallest single bar is zero: 5.46% of days end with an empty shelf (exact; measured 5.459%), against 4.41% of days with a stockout, because on some days the demand exactly uses up the stock and nothing is lost. One unit is the end-of-day stock on 1.22% of days. From about 6 to 36 units the shares are nearly flat, between 2.04% and 2.47% per level, and above 36 they fall away to 0.06% at 50 units. That flat middle is what a steady fall of stock topped up by a fixed block of 40 produces. The measured bars sit on the exact dots, and the dashed line shows that the Poisson store, with the same mean, empties its shelf less often (3.9% of days, exact).
Check: do the numbers match the math?
There is an exact answer for this store, and it comes from a Markov chain (lesson 3.5). At the end of a day the store is in a state (stock, order due tomorrow, order due the day after). The next state depends only on that state and on the day’s demand, whose probabilities are the negative binomial’s. Starting from the initial state (40, 0, 0), 74 states can ever be reached. Write down, for each state, the probability of every next state, and the long-run share of days spent in each state follows from repeating the step until it stops changing (203 steps here). The long-run fill rate, stockout share, order rate and average stock are sums over those states. The computation is a short Python class in learn/figures/sim101-21-make.py; it uses no SpiceGrinder code, only the negative binomial’s formula.
The long run, 80 runs of 250,000 days. “z” is the gap divided by the standard error, so a z of 1 is one standard error away:
| Measure | Measured (standard error) | Exact | z |
|---|---|---|---|
| Stockout days a year | 16.085 (0.015) | 16.113 | -1.83 |
| Fill rate | 97.619% (0.0028) | 97.614% | 1.65 |
| Orders a year | 71.246 (0.0074) | 71.258 | -1.66 |
| Average stock | 21.007 (0.0026) | 21.003 | 1.60 |
All four agree to within 2 standard errors. The row for stockout days is the stockout share of 4.407% against the exact 4.414%, times 365. The measured demand itself has mean 7.9982 and variance 15.985 over the 20 million days (exact 8 and 16). The measured stock distribution is within a total variation distance of 0.00057 of the exact one (the largest possible gap is 1). The four gaps above all lean the same way (fewer stockouts and orders than exact, a little more stock), by less than a tenth of a percent, so a second set of independent runs is worth having: 160 runs of 250,000 days with other seeds (9000 to 9159) give z values of -0.76, 0.48, 0.23 and 0.16 for the same four measures, and the same for Poisson demand (seeds 9500 to 9659) -0.26, 1.23, 1.02 and 0.03 for stockout days, fill rate, orders and stock. The lean did not repeat.
The first year is not quite the long run, because it starts from 40 units and no orders. The exact chain, run for 365 days from that start, gives 16.045 stockout days, a fill rate of 97.624%, 71.181 orders and an average stock of 21.019. The 400 one-year runs give 15.98 days (standard error 0.20), 97.625%, 71.21 and 21.018, with z values of -0.33, 0.02, 0.36 and -0.04. The start matters by less than a tenth of a day of stockouts a year.
There is also a check you can do by hand. Every unit the store sells arrived on an order, so over a long time the orders times the order quantity equal the units sold. In the exact answer, 71.258 orders a year times 40 is 2,850.33 units; the units sold in a year are 365 days times 8 units times the fill rate of 0.976141, which is 2,850.33 as well. In the 80 long runs, the orders times 40 come to 156,155,840 units and the units sold to 156,155,879.
And an exact bookkeeping check on the one run of seed 501. The stock on day 365 must equal the starting stock, plus 40 for every order that has arrived (placed on or before day 363), minus the units sold (the demand minus the lost units): 40 + 40 x 73 - (3,047 - 99) = 12, and the model’s stock on day 365 is 12.
Why not Poisson?
Changing the demand to Poisson with the same mean of 8 and keeping the policy shows what the overdispersion costs. The exact values, with the measurement from 80 runs of 250,000 days (seeds 2000 to 2079) beside them:
| Measure | NegativeBinomial (exact) | Poisson, exact | Poisson, measured | z |
|---|---|---|---|---|
| Stockout days a year | 16.11 | 10.41 | 10.38 | -2.01 |
| Fill rate | 97.61% | 98.93% | 98.931% | 1.35 |
| Orders a year | 71.26 | 72.22 | 72.218 | 0.03 |
| Average stock | 21.00 | 20.73 | 20.737 | 1.73 |
The same mean, the same policy: 16 stockout days against 10, and a fill rate of 97.6% against 98.9%. The overdispersed store even holds a little more stock on average and still serves less. A planner who sized the reorder point from the mean and a Poisson-like spread would be wrong in the dangerous direction. To reach a fill rate of 98% with Q = 40, the Poisson store needs R = 16 and the overdispersed store needs R = 18 (98.56%). The companion page lets you pick the demand model, R and Q, and see all four measures, measured and exact, along with the stock distribution.
What one node cannot do
The model works, and it is checked. But look at what it is made of.
- The policy is buried in a string. The reorder point 16 and the order quantity 40 are literals inside a 143-character expression. The state is addressed by position (
s[2]is “the order placed last night”), and the stock after demand appears twice. Change the lead time to 3 days and every part of the expression changes. An attribute of a library can be a parameter only when it is the whole attribute value, so a library that wanted R and Q as parameters would have to pass them in as input columns. - Nothing outside can read the state. The stock is the
Recurnode’s private state; its output is one stream that other nodes can read, but they cannot change it, and the node cannot read them back. A second store that draws on the same stock, a warehouse that two stores order from, a price that goes up when the stock is low: each needs a node that reads the stock and changes it. The model is a graph in which every node reads others one way, and two nodes that read each other are refused. A two-node loop,AreadingBandBreadingA, givesERROR: Circular dependency detected among model node inputs: [A, B]. - One expression for everything. The only way to put a second store and a warehouse into this model is to put them into the same
Recur: more state columns and one longer expression. It would run. It would also be unreadable, and it could not be reused: the second store would be a copy and paste of the first, with every index shifted.
What the model leaves out
The delivery time is fixed at 2 days and orders arrive whole; there is no review cost, no cost for holding stock, and no price to go up and down. The store reviews every day and orders at most once a day. Demand is the same every day, with no weekly pattern, and demand is independent from one day to the next. Customers who find the shelf empty are lost for good. The costs are not in this lesson; lesson 5.5 adds them.
What this gives you: a store as the state of one Recur node: four state columns (stock, two orders on their way, lost units), a reorder-point rule written with if and max, and demand from NegativeBinomial. The measures that matter for a store (stockout days, fill rate, average stock, orders a year) are counted from the rows, and the long-run values match an exact Markov-chain computation to within 2 standard errors: 16.085 stockout days a year against 16.113, a fill rate of 97.619% against 97.614%. A year is one draw (stockout days from 4 to 28 over 400 years), and the same mean demand with Poisson variance would have given 10.4 stockout days instead of 16.1. The companion page lets you try other reorder points and order quantities.
The store works as long as it is the only thing that reads and changes its stock. Lesson 5.2 gives the stock a home of its own. A shared cell holds the stock and another the quantity on order, and separate nodes read them and write them: one reads the stock, one decides, one applies the day’s demand and the deliveries. Each read of a shared cell sees the value at the end of the previous row, so a loop closed through a cell always has a one-row delay, and the lesson shows exactly what that does to the policy compared with the Recur version. Once the stock is a cell, the second store and the warehouse are just more nodes that read and write it.