Back to trackLesson 11 of 25

Support Desk 1: Memory Between Rows

Uses Only Free-Tier SpiceGrinder (Recur is a Free-tier filter in 1.0)

Problem statement

Module 2 gave its model memory the hard way. In lesson 2.1 the balance after month 12 needed the sum of all twelve flows, so the Balance formula wrote that sum out for every month: 78 terms for a twelve-month year, and 300 for twenty-four months. In lesson 2.4 a calm or stressed spell was held in place by a Pool and two Switches, and the memory lived in the way the rows were laid out. Both worked. Neither would stretch to a long run, because the whole memory had to fit inside one row.

There is a simpler way to build a model that remembers. Let each row start from the row before it. A running balance, a backlog or a waiting line is a number that each step updates from the last step, and Recur is the filter that does exactly that. This lesson shows it twice: first on the runway of lesson 2.1, where the answer is already known, and then on a new problem.

A support desk opens tickets and closes tickets. Say each interval is one hour. In an average hour 5 tickets arrive and the team closes 4.5, so the backlog creeps up by half a ticket an hour. The questions:

  • How big is the backlog after 1,000 hours, and how much does that differ from one run to the next?
  • What does one run look like, and what does a row of output mean now?
  • How long a run can the model do?

Recur: the runway in one term

Recur reads one row from its input, computes a new state from the previous state and that row, outputs the new state and keeps it for the next row. Four parameters matter:

  • s[0] in the expression is the state: the previous output row. x[0] is the current input row. y[0] is the new output, and it becomes s[0] for the next row.
  • initial is the state before the first row. With initial="120" and y[0] = s[0] + x[0], the first output is 120 plus the first flow.
  • resetEvery sends the state back to initial every so many rows, so each block of rows is an independent path. With resetEvery="12" each block of twelve rows is one year.
  • One row is one step. Recur has no clock; the row count is the clock.

Here is the runway of lesson 2.2 with Recur doing the work of the 78-term Balance. The model is samples/simulation/recur-cash-runway.xml:

<dataset columns="balanceM1, balanceM2, balanceM3, balanceM4, balanceM5, balanceM6, balanceM7, balanceM8, balanceM9, balanceM10, balanceM11, balanceM12, lowPoint, ruin" seed="42">
  <root node="Ruin"/>
  <nodes>
    <Normal name="NetFlow" mean="-8" sigma="12"/>
    <Recur name="Balance" initial="120" expression="y[0] = s[0] + x[0]" resetEvery="12">
      <input name="NetFlow"/>
    </Recur>
    <Redimension name="Year" dimension="12">
      <input name="Balance"/>
    </Redimension>
    <Calculate name="LowPoint" dimension="14" expression="y[12] = min(min(min(min(x[0], x[1]), min(x[2], x[3])), min(min(x[4], x[5]), min(x[6], x[7]))), min(min(x[8], x[9]), min(x[10], x[11])))">
      <input name="Year"/>
    </Calculate>
    <Calculate name="Ruin" expression="y[13] = x[12] &lt; 0">
      <input name="LowPoint"/>
    </Calculate>
  </nodes>
</dataset>

Same founder, same flows, same low point and ruin columns (LowPoint receives the twelve balances straight from Year, and its dimension="14" makes the room for the two new columns, as in lesson 2.2). Two things moved. The 693-character Balance formula became y[0] = s[0] + x[0], 18 characters, and it stays that size at any horizon. And Balance now comes before Redimension: Recur produces one balance per row, so Redimension gathers twelve balances into a year afterward (in lesson 2.2 it gathered the flows first).

To see what Recur does, look at the node’s own output with grind --node Balance. These are the first 14 monthly flows and the 14 balances Recur made from them, with the model’s own seed, 42:

RowNetFlowBalance (the state)
1-16.53103.47
2-14.3889.09
3-16.0073.08
4-3.1469.94
………
12-7.8922.76
13-12.67107.33
146.51113.84

Row 1 is 120 - 16.53. Row 2 is row 1’s balance plus the next flow. Row 13 is the start of the second year: the state went back to 120, so the balance is 120 - 12.67, not 22.76 - 12.67. Year takes rows 1 to 12 as the first path, 13 to 24 as the second, and so on.

Does it give the same answers? It has the same seed (42) and the same flow node name (NetFlow) as the lesson 2.2 sample, so it draws the same random numbers. The two outputs, 100,000 rows of 14 columns each from each model’s own seed, hash to the same value:

ModelSHA-256 of the output (first 12 hex digits)
cash-runway.xml (78-term formula)cbe639d03f7d
recur-cash-runway.xml (Recur)cbe639d03f7d

With --seed 42 and a million rows each, both give 4a31218c6696. The outputs are identical, byte for byte: not close, identical. The model also has 5 nodes, and the Balance step does not grow when the year gets longer.

The model: a support desk’s backlog

Now the desk. Each row is one interval. Two Poisson nodes give the tickets opened (average 5) and closed (average 4.5), Append puts them side by side in one row, and Recur keeps the running backlog:

<dataset columns="backlog" seed="301">
  <root node="Backlog"/>
  <nodes>
    <Poisson name="Opened" lambda="5"/>
    <Poisson name="Closed" lambda="4.5"/>
    <Append name="Tickets">
      <input name="Opened"/>
      <input name="Closed"/>
    </Append>
    <Recur name="Backlog" initial="0" expression="y[0] = s[0] + x[0] - x[1]">
      <input name="Tickets"/>
    </Recur>
  </nodes>
</dataset>

Poisson is a count of events in an interval, a natural choice for tickets that arrive at random. The model is samples/simulation/ticket-backlog.xml, four nodes. Its expression reads: the new backlog is the old backlog (s[0]) plus the tickets opened (x[0]) minus the tickets closed (x[1]). initial="0" starts the desk empty. There is no resetEvery, so the state is never reset: one run is one trajectory, and --count is how long it is. Run it for 1,000 intervals:

grind --count 1000 ticket-backlog.xml

The first six rows with the model’s own seed, 301 (you get exactly these values if you leave the seed alone). The two count columns are not in the model’s output; they come from grind --node Opened and grind --node Closed with the same seed, and let you check the arithmetic by hand:

RowOpenedClosedBacklog
1844
2352
3310-5
441-2
5952
6552

Row 3 is a good hour for the team: three tickets in and ten closed. The backlog goes to -5. A backlog cannot really be negative, and we come back to that below. The running sum of opened minus closed matches the model’s output in all of the first ten rows.

This one run is 1,000 rows and ends at a backlog of 649. Is that typical? The average hour adds half a ticket, so after 1,000 hours the backlog should be around 500, and 649 is 1.5 standard deviations above that (the spread is worked out below). A second seed gives a second desk with a second history. To see that for yourself, leave out the seed:

grind --no-seed --count 1000 ticket-backlog.xml

One run is one path

Ten runs of 1,000 intervals each, seeds 1 to 10, are in the picture. Each gray line is the backlog over time for one desk.

Ten ticket backlogs over 1,000 intervals, each a wandering line that rises on average by half a ticket per interval; a red mean line and a shaded band of one standard deviation on each side widen to the right; the paths stay mostly inside the band, and one ends near 324, below the band

All ten drift upward, because the team closes half a ticket an hour less than arrives, and each wanders around its own path. The red line is the average path (0.5 times the interval). The band is one standard deviation either side: its half-width is the square root of 9.5 times the interval, so it grows like a square root, as the cash balance’s spread did in lesson 2.1. Seven of the ten finish inside the band and three outside it (one low at 324, two high at 610 and 627), about what a band of one standard deviation should give. Five of the ten were below zero at some interval early on.

Two properties of Recur show up here that the 78-term formula could never give:

  • A row can be a tick. In lesson 2.1 a row was a whole year, so the horizon was fixed by the number of columns. Here a row is one interval and the horizon is just --count. A million intervals is --count 1000000; it ran in half a second on a laptop, and ended at 500,548 (theory 500,000, with a spread of 3,082 around it). One hundred million ran in about half a minute and ended at 4.9982909E7, which is 49,982,909 (the output switches to this notation from ten million up).
  • Every row depends on the one before. The rows of one run are not independent samples, as the rows of every earlier model in this track were. That is the point of the model, and it changes how to read a run: a single run is one trajectory, so to learn about the distribution of the backlog you need many runs (many seeds), not many rows of one run.

Many runs: where the backlog ends up

To measure the spread, run the desk 200 times, once per seed (1 to 200), for 10,000 intervals each, and look at the backlog after 10, 100, 1,000 and 10,000 intervals. Shorter horizons are the first rows of the same runs. After 1,000 intervals:

Histogram of the backlog after 1,000 intervals over 200 runs with different seeds, with a bell curve of mean 500 and standard deviation 97.5 drawn over it; the bars follow the curve, centered near 500 and ranging from about 200 to 720

The 200 runs end between 205 and 723, with a mean of 501.3 and a standard deviation of 98.1. The curve is a Normal bell with the exact mean (500) and standard deviation (97.47) from the math below. It is a smooth stand-in for a count of tickets, and it describes the 200 runs well. For a feel of how the desk’s backlog behaves at other horizons, the companion page lets you pick 10, 100, 1,000 or 10,000 intervals and shows the 200 runs with the exact mean and spread beside them.

Check: do the numbers match the math?

The desk’s backlog after n intervals is the total opened minus the total closed. A sum of independent Poisson counts is itself Poisson, so the total opened is Poisson(5n) and the total closed is Poisson(4.5n). The mean of the difference is 5n - 4.5n = 0.5n. The variance of a Poisson is its mean, and the variances of independent counts add, even when one is subtracted, so the variance of the difference is 5n + 4.5n = 9.5n. After 1,000 intervals the backlog has mean 500 and a standard deviation of the square root of 9,500, which is 97.47. This is exact; nothing here is an approximation, apart from the bell curve in the picture.

Measured from the 200 runs (seeds 1 to 200), against the exact values. “z” is the gap divided by the standard error, so a z of 1 is one standard error away. A standard error of the mean is the standard deviation over the square root of 200; one for a standard deviation is the standard deviation over the square root of 398.

IntervalsMean (theory)zStandard deviation (theory)zBelow zero (exact)
105.02 (5.0)0.039.87 (9.75)0.2529.5% (28.6%)
10049.77 (50.0)-0.1031.47 (30.82)0.417.0% (5.1%)
1,000501.25 (500.0)0.1898.10 (97.47)0.130 of 200 (about 0)
10,0005,013.97 (5,000.0)0.66301.17 (308.22)-0.470 of 200 (about 0)

The standard errors at 1,000 intervals are 6.94 for the mean and 4.92 for the standard deviation, so the gaps to theory (1.25 and 0.63) are small. Every z is under 1.3 (the largest is 1.25). The “below zero” column is the share of runs whose backlog is negative at that moment, against the exact probability for the difference of the two Poisson counts; at 10 and 100 intervals it is within 1.3 standard errors.

The long single run (seed 301, 1,000,000 intervals) checks the per-interval numbers directly. The change in the backlog from one interval to the next has a mean of 0.5005 (standard error 0.0031, theory 0.5) and a variance of 9.490 (theory 9.5). The final backlog, 500,548, is 0.18 standard deviations from the exact 500,000.

The hand check is the one at the top: the running sum of the opened and closed columns (from grind --node) equals the model’s backlog in the first 10 rows. Replaying a seed gives the same run, so the check can be repeated at any row.

Both models are valid and fit Free: ModelValidatorApp reports VALID for ticket-backlog.xml (4 nodes) and recur-cash-runway.xml (5 nodes), against Free’s limit of 20.

What the model leaves out

The backlog here is unfloored: it is the net build-up, opened minus closed, and it can go below zero. In the 200 runs, 139 were below zero at some interval within their first 1,000. That is not a defect, because it is what makes the mean 0.5 and the variance 9.5 exact, which is why the check above could be so clean. But a real desk’s backlog cannot be negative: closing ten tickets when there are only three to close leaves zero, not minus seven. A floor, max(0, ...), changes the answer: the backlog is no longer a sum of independent steps, and the exact formulas above stop holding. Calculate has min and max but no comparison or if, and the floor is the first thing the next lesson builds. In a long run of this particular desk the floor hardly matters (one run of a million intervals was below zero in only three rows, all at the start), because the drift is upward. It matters when the team closes about as many tickets as arrive.

The desk also has no memory of individual tickets. Recur carries the count, so the model cannot say how long any one ticket waited. It treats every interval alike, with no busy hours and no weekends. And it counts a ticket the interval it is opened, so the team can close, in the same interval, a ticket that arrived in it.

What this gives you: a model with memory from one row to the next, built with Recur: the state s, the input row x, the new state y, the starting state initial, and resetEvery to cut a run into independent blocks. The runway of lesson 2.2 in one term instead of 78, with output identical to the byte, and a ticket backlog (Poisson opened and closed, Append, Recur) whose spread after 1,000 intervals is 98.1 against an exact 97.47. A row can now be a tick, so the horizon is just --count; and the rows of a run are not independent, so a run is one trajectory and the spread comes from many seeds. The companion page shows the 200 runs at four horizons.

The backlog counts tickets, and every ticket counts the same. The next problem asks how long each one waits. In a queue, a customer who arrives at a busy desk waits for the ones already there, and the length of that wait depends on the wait of the customer before. Waiting time itself is the state, handed from one customer to the next, and unlike the backlog it can never go below zero. Lesson 3.2 builds that queue with Recur, adds the floor, and checks the average wait against a formula from queueing theory.