Search HoldMyTwist

Browse all posts

Episode 40

The Last Four Raindrops

Duck's rain gauge (four seats) has a line of readings waiting to be updated. When you update the mean you need to know not just the new arrivals, but also who's leaving (even if the new reading is zero).

Episodes

Computing Basics

    Duck is inside monitor next to four sample seats. The oldest to newest reads two, four, six, eight. Robot is outside watching.
    Robot points at sum of readings, 2+4+6+8=20. The order of readings is the same.
    Duck points at dial which is set to five. The math is taking the total (twenty) and dividing by the number of readings (four).
    Robot sees new raindrop entering. New reading is ten. Old readings stay on rail. New one stands next to them.
    Duck points at reading of 2 (red bow). There are 4 readings on the rail. Ten is standing by them.
    Two is out there on a shelf by the window. Four, six and eight are on the rail. Ten is standing by them.
    Duck adds the latest 10. So now you have 4, 6, 8, 10. The 2 that went is outside the window.
    Duck subtracts 2 and adds 10 to the old sum. The new sum is 28.
    Dial goes from five to seven. Window still shows four, six, eight and ten.
    Window still shows four, six, eight and ten. Duck thinks about skipping zero. The four numbers in the window didn't change.
    Current window shows six, eight, ten and zero. 2 and 4 are on another shelf. Duck is sweeping next to the rail.
    Duck computes 28 minus 4 plus 0, which gives sum 24 and mean 6. The latest sample is still 0.
    Current window shows six, eight, ten and zero. There are two and four (earlier readings) on a separate shelf in an archive outside of window.
    This has readings of three, five, seven and nine. The new reading is 1. This one is next to the rail.
    Duck opens wings over 4 values on cart. New value is standing next to them. New window and mean are still hidden.
    The departed 3 is in a box outside the monitor. Duck puts his wings up.
    Total is 22. Average is 5.5. The numbers on the rail are 5, 7, 9 and 1. In the basket is 3.
    Duck is holding the umbrella above the latest reading of one. There are five, seven, nine and one in the window and three in the basket next to the window.

    Episode transcript

    Four little seats

    Duck is inside monitor next to four sample seats. The oldest to newest reads two, four, six, eight. Robot is outside watching.

    Diagram values: OLDEST→NEWEST; 2,4,6,8; WINDOW SIZE:4

    Duck: I'd like this rain gauge to remember recent readings. Let's take the last 4 and average them.

    Robot points at sum of readings, 2+4+6+8=20. The order of readings is the same.

    Diagram values: 2 + 4 + 6 + 8 = 20

    Robot: Use the last four readings weighted the same.

    Duck points at dial which is set to five. The math is taking the total (twenty) and dividing by the number of readings (four).

    Diagram values: 20 ÷ 4 = 5

    Duck: 20 / 4 is 5.

    Robot sees new raindrop entering. New reading is ten. Old readings stay on rail. New one stands next to them.

    Diagram values: NEW:10

    Duck: New read for 10! I'll add it to the party.

    The next guest

    Duck points at reading of 2 (red bow). There are 4 readings on the rail. Ten is standing by them.

    Diagram values: LAST FOUR READINGS

    Robot: 4 places. 5 reads. Which read has to go when 10 comes in?

    Two is out there on a shelf by the window. Four, six and eight are on the rail. Ten is standing by them.

    Diagram values: LEAVES:2; RETAIN:4,6,8

    Duck: On each new sample drop the oldest one.

    Duck adds the latest 10. So now you have 4, 6, 8, 10. The 2 that went is outside the window.

    Diagram values: 4,6,8,10

    Robot: Maintain the order of the readings.

    Duck subtracts 2 and adds 10 to the old sum. The new sum is 28.

    Diagram values: 20 − 2 + 10 = 28

    Duck: We subtract two from twenty, and add ten.

    Dial goes from five to seven. Window still shows four, six, eight and ten.

    Diagram values: 28 ÷ 4 = 7

    Robot: 28 / 4 is 7.

    Zero gets to stay

    Window still shows four, six, eight and ten. Duck thinks about skipping zero. The four numbers in the window didn't change.

    Diagram values: NEW:0

    Duck: 0 is the smallest reading I get. Can I just ignore it and use the old 4?

    Current window shows six, eight, ten and zero. 2 and 4 are on another shelf. Duck is sweeping next to the rail.

    Diagram values: LEAVES:4; JOINS:0; 6,8,10,0

    Robot: A small new value will still be included.

    Duck computes 28 minus 4 plus 0, which gives sum 24 and mean 6. The latest sample is still 0.

    Diagram values: 28 − 4 + 0 = 24; 24 ÷ 4 = 6

    Duck: This includes the new value of zero.

    Current window shows six, eight, ten and zero. There are two and four (earlier readings) on a separate shelf in an archive outside of window.

    Diagram values: WINDOW:6,8,10,0; OUTSIDE:2,4

    Robot: This mean represents four recent samples. Not all samples taken.

    A fresh rain gauge

    This has readings of three, five, seven and nine. The new reading is 1. This one is next to the rail.

    Diagram values: 3,5,7,9; SUM:24; NEW:1

    Duck: Try another meter. This has readings of three, five, seven and nine. And here comes one.

    Duck opens wings over 4 values on cart. New value is standing next to them. New window and mean are still hidden.

    Diagram values: WINDOW SIZE:4

    Robot: What reading drops out? What is the arithmetic mean?

    The departed 3 is in a box outside the monitor. Duck puts his wings up.

    Diagram values: LEAVES:3; 5,7,9,1

    Duck: Three leaves.

    Total is 22. Average is 5.5. The numbers on the rail are 5, 7, 9 and 1. In the basket is 3.

    Diagram values: 24 − 3 + 1 = 22; 22 ÷ 4 = 5.5

    Robot: New total = 22 Divide by 4 = 5.5

    Duck is holding the umbrella above the latest reading of one. There are five, seven, nine and one in the window and three in the basket next to the window.

    Diagram values: OLDEST, NOT SMALLEST

    Duck: Even if the most recent reading is smaller, the oldest reading leaves.

    The lesson

    The gauge is simply an average of the last 4 readings. Each reading is weighted equally. Maintain the order of the readings. As new readings are added, drop the oldest and add the new one to the end. A small new value will still be included.

    We already know what the sum for the old window is. We don't have to go through all the readings again. Just subtract the reading of the leaving one, and add the reading of the incoming one. In this case, it's two leaving, and ten arriving. We subtract two from twenty, and add ten. Now you have twenty-eight.

    We don't touch the remaining three readings. They're already in the sum before. We don't change their contribution. We divide twenty-eight by four. The average is seven.

    When zero is added 4 is the oldest reading and it's gone. Now we have 6,8,10,0 in the window. Take 4 from your previous total (28) and add in 0. The total is now 24. Divide this by four to get an average of 6. The fact that 6 is on the dial with the newest reading being 0 is possible because it takes the average of all four readings. This example assumes a full window. It doesn't state what happens in a partially filled window.

    Try it

    A full window (4) contains 4 samples: [4, 0, -4, 8] (oldest to newest). There's a new sample of -8. What value goes away? What's the new window (in order of time)? What is the sum? What is the arithmetic mean?

    Answer

    The oldest one is gone now, 4. The new window is [0, -4, 8, -8]. The sum is -4, the average is -1. 8 remains, it was not the oldest sample. The new -8 remains, it's new.

    Sources

    Python collections: deque recipes (opens in a new tab)

    Python statistics: Arithmetic mean (opens in a new tab)