Numerical Stability: When Small Errors Grow
Numerical stability asks one thing: does the route taken through the arithmetic grow a small error or shrink it? Two formulas that are the same on paper can part company on a machine. Add 10,000 to every month of the fifty month record, which cannot change its variance at all, and one route still reads 24.250000 while the other, on an eight digit calculator, reads 20.000000.
Every figure below comes out of the arithmetic printed beside it, so a figure is checked by recomputing it and in no other way. An eight digit calculator reproduces every collapse below by hand. A machine nobody can see inside would hide the very step that does the damage.
Two of those numbers are the interesting pair. The 24.250000 is right and the 20.000000 is wrong, they were produced from identical data by two textbook formulas for the same quantity, and neither calculation broke a single rule of arithmetic on the way. Nothing crashed. Nothing warned. One of them just quietly stopped being able to give the answer.
Everything below leans on three things settled elsewhere and used here without being re-explained. First, what a varianceA settled measure of how spread out a set of values is: take each value's distance from the average, square it, and average the squares. The measure and its uses are covered separately. is, and the fact that adding the same amount to every value leaves it exactly where it was. Second, the fifty month record of the Nakshatra unit, an invented traded unit built for teaching, whose fifty monthly changes have a variance of 24.25. Third, the Nakshatra ladder, a stated arithmetic rule which opens at Rs 100/-, grows the balance by a monthly rate, removes Rs 2/-, and repeats that twelve times. All three arrive finished. And one plain fact about machines, used everywhere below and explained nowhere: any calculator, any spreadsheet, any program carries a fixed number of digits and no more.
What is numerical stability, and what is it a property of?
Consider giving somebody directions to a familiar place. There are two ways to get there. One goes along the main road, and a walker who drifts twenty metres off it can see where they are and correct. The other cuts through five turns in a warren of lanes. Twenty metres of drift at the first turn puts the walker on the wrong lane. The wrong lane at the first turn puts them on the wrong lane at the second, and by the fifth they are somewhere else entirely. The destination did not change. The drift did not change. The route decided whether the drift mattered.
A calculation is stable when a small error in what goes in stays small in what comes out, and unstable when that small error grows as the arithmetic runs, and this is a property of the route chosen rather than of the question asked. The route, not the question, is the part that surprises people. Saying that some quantities are simply hard to compute is tempting, and occasionally the claim is true. Far more often the quantity is perfectly well behaved and one particular way of getting to it is not.
Which means instability is usually something picked up rather than something handed over. Nobody chooses it on purpose. An analyst chooses the formula that reads the data once instead of twice because it is faster, or the version they were taught, or the one that fits on a line, and the stability came attached to the choice without ever being mentioned.
A quantity has been computed and the answer is badly wrong. No rule of arithmetic was broken anywhere in the working. Where does the instability live?
How can the same rule grow an error one way and forget it the other?
The cleanest demonstration is the Nakshatra ladder. The ladder opens at Rs 100/-, grows the balance by the monthly rate, then removes the standing chargeA flat deduction that lands at the same size each period no matter how large or small the balance underneath it has grown to be. Here it is the Rs 2/- step of the Nakshatra ladder. of Rs 2/-. At a monthly rate of exactly 2.00 per cent the two effects cancel each month and the ladder reads Rs 100.00/- in every one of the twelve months, forever, flat.
Now one tiny mistake at the start. Suppose the opening figure is written as Rs 100.01/- rather than Rs 100.00/-. One paisa. Run forward, the ladder reads Rs 100.010200/- after the first month, so the paisa has become 1.0200 paise. After twelve months it reads Rs 100.012682/-, so the paisa is now 1.2682 paise. Nothing dramatic has happened, and the honest description of that growth is modest. Direction matters here rather than size: the deviationHow far one value sits from the reference it is being measured against, written with its direction attached. Settled elsewhere and used here as it stands. from the flat line is multiplied by 1.02 every single month, and multiplication by anything above one never stops. Over fifty months the factor is 2.691588. The mistake does not fade. It compounds, quietly, alongside everything else.
Now the same rule run the other way. Suppose this month's reading is known and last month's is wanted. The two steps are undone in reverse: add the Rs 2/- back, then divide by 1.02. The rule is the same, the rate is the same, the ladder is the same. Dividing by 1.02 repeatedly drives anything towards nothing. Started one paisa high, the error has fallen to 0.7885 of a paisa after twelve steps, and after another twelve it would be smaller still.
Same arithmetic. Same starting mistake. Opposite behaviour. One direction of travel remembers every error it has ever been given and multiplies it forever, and the other direction forgets. The pair is the single clearest picture of what stability means and where it lives, and it settles whether instability belongs to the problem. The problem here is one rule, and the rule is stable one way round and unstable the other.
The Nakshatra ladder at 2.00 per cent, started one paisa high, reads Rs 100.012682/- after twelve months. What single number turned one paisa into 1.2682 paise, and what would running the rule the other way have done to that same paisa?
How can two formulas for one variance disagree?
The ladder was the gentle case. The variance is not gentle, and every other failure below grew out of it.
There are two standard ways to work out how spread out a set of numbers is. The first reads the data twice: one pass gives the average, and a second pass takes each value away from that average, squares the difference, and averages the fifty squares. The two pass route is so named because it reads the data two times. The one pass route reads it once: each value is squared as it arrives, those squares are averaged, and the square of the average is subtracted. The two are the same thing written differently: expanded algebraically, the first becomes the second, exactly, with nothing approximated and nothing dropped. On paper they are not two methods. The two formulas are one method in two costumes.
On the fifty month record both give 24.25. Now do something that cannot possibly matter. Add the same amount to every one of the fifty months. Adding a constant slides all the values along together without pulling them any further apart, so the spread is untouched and the variance must come out at 24.25 again. Every honest answer below has to read 24.25.
Watch what happens on a calculator that carries eight significant figuresThe digits of a written number that actually carry its size, starting from the leftmost one that is not a zero. How many of them a machine keeps, and why, is covered separately under programming..
| Added to every month | The two pass route | The one pass route, eight digits | What it should say |
|---|---|---|---|
| 0 | 24.250000 | 24.250000 | 24.25 |
| 100 | 24.250000 | 24.250000 | 24.25 |
| 1,000 | 24.250000 | 24.300000 | 24.25 |
| 10,000 | 24.250000 | 20.000000 | 24.25 |
| 1,00,000 | 24.250000 | 0.000000 | 24.25 |
| 10,00,000 | 24.250000 | 0.000000 | 24.25 |
The two pass route reads 24.250000 at every single setting, and the one pass route falls off a cliff. Read the third row again, though. The third row is the frightening one. At an addition of 1,000 the one pass route gives 24.30. Not zero. Not an error message. Not a wild number that anybody would query. The route gives 24.30 against a true 24.25. A person reads a disagreement that small as a rounding difference and moves past. The failure has already started at that row, and there is nothing anywhere on the screen that says so.
The magnified panel on the right of that chart is there because of a real drawing problem, and the problem is itself the lesson. On a scale that runs from zero to twenty six, the gap between 24.25 and 24.30 is about half a pixel wide. The gap cannot be drawn. It cannot be seen. Any chart wide enough to also show the collapse to zero hides it, and a report hides it for exactly the same reason. The failure is well underway before it is large enough for anybody to notice. The third of the three signs set out below is therefore a test that has to be run rather than a thing to look for.
At an addition of 1,000 the one pass route reads 24.30 against a true 24.25. What warning does the calculation give at that point?
Both halves are worth deciding now, while the panel underneath still gives nothing away. The next step adds 1,00,000 to every month of the fifty month record. What should happen to its variance, and what will the one pass route report?
Add the same amount to every month and watch one column hold and the other empty out
One dial, and it moves one thing: how much is added to every one of the fifty months. Nothing else changes. The bars are drawn against the average of whatever setting is in force, so the picture of the record is identical at all six settings. Adding a constant genuinely does nothing to the spread, and an identical picture is the honest one. Underneath, the two answers redraw. The panel opens at an addition of 10,000. The two pass answer there is 24.250000 and the one pass answer is 20.000000, the same two figures printed in the table above. Two extra readouts show the actual pair of eight digit numbers being subtracted, so they can be watched creeping towards each other digit by digit before either answer moves.
What happens when two numbers that are nearly the same size are subtracted?
The why is one sentence with a number attached, and the number is worth working through slowly.
At an addition of 1,00,000 the one pass route needs two quantities. The average of the fifty squares comes to 10,00,01,00,024.50. The square of the average comes to 10,00,01,00,000.25. Subtracted exactly, they give 24.25. The right answer came entirely out of the ninth, tenth and eleventh digits. The first eight digits of the two numbers are identical, so they contribute exactly nothing to the answer. Every scrap of the information actually wanted is sitting in the tail.
Now hand those two numbers to a calculator that keeps eight digits. It keeps 10,00,01,00,000 and 10,00,01,00,000. Both of them. The same. Eight digits is all there was room for, so the tail was never kept. The tail was the answer.
The error in each of those two big numbers never changed size at all; what changed is that the answer got small enough for the error to swallow it whole. An eight digit machine writing a number of that size is entitled to be out by up to five hundred in either direction. Five hundred against ten thousand crore is nothing, a relative errorA gap measured as a share of the quantity it sits in, rather than in the units the quantity is written in. Five rupees is a large gap on a bus fare and a rounding on a house. of about five parts in a hundred million, and no reasonable person would call it a problem. Five hundred against an answer of 24.25 is a catastrophe.
The ratio has a size, and the size can be computed rather than gestured at. The two big numbers are 41,23,75,258.7423 times as large as the answer they are supposed to produce. The factor is the magnification, and the magnification converts the harmless relative error on each big number into the relative error on the answer. Five parts in a hundred million, multiplied by 41,23,75,258, comes to about twenty. The answer can be out by twenty times its own size. Reporting 0.000000 amounts to being wrong by a hundred per cent, comfortably inside that.
Run the same sum at the other settings and it predicts what the table already showed. At an addition of 1,000 the magnification is 41,279.3608 and the arithmetic says up to about 0.21 per cent of the answer is at risk. The one pass route gave 24.30 against 24.25, out by 0.2062 per cent. At an addition of 10,000 the magnification is 41,24,124.7216, the arithmetic says up to about 20.62 per cent is at risk, and the route gave 20.00 against 24.25, out by 17.5258 per cent. The magnification is not a story about a mysterious machine. The magnification is one multiplication, and it flags the answer as being in trouble before the answer is computed.
Two numbers that agree in their first eight digits are subtracted. The error in each one is exactly the same size it was before. Why is the error in the answer so much worse, and what is that magnification ratio actually measuring?
Why does rounding at every step cost so much more than rounding once?
The second case is about a different route to the same trouble, and it needs no large numbers at all.
Worth settling before reading on. Rs 100/- compounded through all fifty months of the fifty month record reaches exactly Rs 120.808714/-. Suppose instead the price were rounded to the nearest rupee at the end of every month. How far off would the ending price be?
Rs 100/- compounded through the fifty month record gives the following. The order of the fifty months is the stated one, produced by the arithmetic rule named here from the starting number 4321 rather than chosen by hand, and it rearranges the fifty values without altering any of them. Done exactly, the price finishes at Rs 120.808714/-.
Now do the identical run, but round the price to the nearest rupee at the close of every month, the way a system storing whole rupees would have to. The rounded run finishes at Rs 115.00/-. The gap is Rs 5.808714/-, or 4.8082 per cent of the exact answer, produced by nothing but rounding.
Do it a third time, rounding to the nearest paisa instead. The paisa run finishes at Rs 120.82/-, out by Rs 0.011286/-. No report would carry a gap that small.
The rounding is not the error; compounding the rounding is. No single month's rounding is worth more than fifty paise. But each month's rounded price becomes the base the next month's change is applied to, and then that month's answer is rounded and becomes the base after it, and so on for fifty months. A rounding made in month three is not worth fifty paise by month fifty. The month three rounding has been carried through forty seven further multiplications, and it comes out the other end worth whatever those multiplications made of it. The ladder's forwards direction is back again, wearing different clothes: a quantity carried forward and multiplied by more than one, repeatedly.
Notice the lower chart in that figure. The lower chart says something the ending figures alone do not. The gap does not climb steadily. It swings above and below zero for most of the run, reaching Rs 6.7568/- at its widest before settling back to Rs 5.808714/- at the end. Each month's rounding can go up or down, so the errors partly cancel each other. Every one of them is then compounded by everything that follows, so no error can stay the size it started at.
And this is why the same rule at a different precision costs almost five per cent one way and nothing the other. The rupee run rounds coarsely fifty times, and each of those coarse roundings gets compounded. The paisa run rounds finely fifty times, and each of those fine roundings gets compounded too, but a hundredth of nearly nothing, compounded, is still nearly nothing.
Rounding to the nearest paisa each month costs Rs 0.011286/- over the fifty months, and rounding to the nearest rupee costs Rs 5.808714/-. The rounding got exactly one hundred times coarser. Why did the cost rise by far more than one hundred times?
What are the three signs, and can they be seen before anything fails?
All of this is much less mysterious than it looks from the outside, and it comes down to three things to look for. Two of them are visible in the arithmetic itself, before anything has been run.
The first is a subtraction of two numbers that are nearly the same size. The one pass variance route is that subtraction in a single line, and so is every calculation that measures a small difference between two large measured quantities. Wherever it appears, the agreeing digits are about to be thrown away and whatever the machine had rounded is about to become the answer.
The second is a quantity carried forward and multiplied by something bigger than one, again and again. The Nakshatra ladder run forwards is exactly that, and so are the fifty months of compounding above. Anything already sitting in that quantity, including any mistake, gets multiplied along with everything else, every single time.
The third is different in kind. It is an answer that changes when the data is shifted, scaled or reordered and should not have. Checking for that third sign is the cheapest test in this whole area of work: add a constant to every value, run the calculation again, and see whether the answer moves. The test costs a single line. It needs no understanding of the code being tested. And unlike the first two it does not require anything to have been spotted in advance. The first two are only visible to someone already looking for them.
A constant is added to every input of a routine that should not care, and the routine gives a different answer. What has that established, and what has it not?
What is to be done when the unstable step cannot be avoided?
Sometimes the subtraction is spotted and there is no way around needing it. Four things are available, and they are not equal.
Reorder the arithmetic so that the subtraction happens early. Doing it while the numbers are still small beats doing it at the end once they have grown. Reordering is free. Same data, same operations, different order.
A rough centre can be taken off the data at the outset. Any convenient number near the middle of the values is subtracted from all of them, the work is done, and the answer for a variance comes out unchanged because a shift cannot change a spread. Taking a rough centre off costs one extra read of the fifty values, and it is exactly what the two pass route already does when it takes the average off first. The two pass route never budged from 24.250000 in the table above for precisely that reason.
Carry more digits. Extra digits are the weakest of the four remedies and the one people reach for first. Buying digits feels like buying safety. More digits do not remove the collapse. They move it. The eight digit calculator empties out at an addition of 1,00,000; a full machine carrying about sixteen digits empties out at an addition of 1,00,00,00,000. Same failure, same shape, further along the road, and now much harder to trip over in testing because the setting that triggers it is somewhere nobody thought to look.
And where the route runs in a direction that grows errors, the other direction is available if the question allows. Reversing the direction is different from the other three: it does not change the route, it changes the question being asked. Working out next month from this month and recovering last month from this month are not the same question, and no amount of rearranging turns one into the other. Where they happen to be interchangeable, the direction that shrinks is the one to take.
Of the four remedies above, one changes the question rather than the route. Which is it, and which of the four is the weakest?
What is the one habit worth keeping?
Everything above collapses into a single working habit, and it belongs to whoever actually has to trust a figure: the analyst signing off a variation number, the lender reading a volatility measure off a spreadsheet somebody else built, the researcher whose model rests on a covariance produced three layers down in a library, the household treasurer reconciling a passbook against a system that stores whole rupees.
Before any figure produced by a chain of arithmetic is trusted, a large constant is added to the data, the chain is run again, and anything that moved without any business moving shows itself. Shift it. Scale it. Reorder it. Any of the three, and the shift is the easiest to write.
The reason this is worth a habit rather than a note is that it catches the one class of fault no amount of reading the code will ever catch. When a test fails, the usual course is to read the code and find the mistake. There is normally a mistake to find. Here there is not. The formula is right. The formula is in the textbook. Its implementation matches it line for line. Every test anybody wrote was checking whether the code does what the formula says, and it does, so every test passes. The formula and the machine simply disagree about the answer. The only thing that surfaces the disagreement is running the same code on data that should give the same answer and seeing that it does not.
The habit also fails safe. If the answer does not move, nothing has been learned and one line has been spent. If it does move, something has been found that no amount of care in the writing could have prevented, and found before somebody quoted the number.
The failure: a routine that reported no variation at all, and passed every test on the way
A reporting routine works out the variation in a set of month end readings. Reading the data once is faster than reading it twice, so the routine uses the one pass route. On readings in the tens it has run correctly for years. Nobody has any reason to look at it. It is not new, it is not clever, and it has never been wrong.
Then the readings are restated on a base of 1,00,000 rather than 100. The restatement is a presentation change and nothing else. Not one reading moved relative to any other; the same months are still the same distance apart; the underlying record is untouched. Somebody decided the numbers read better that way, and a decision of that sort gets made without a meeting.
The routine now reports zero variation in data that visibly varies, and it reports it without an error, a warning or a single failed test. The chart beside the figure still shows the readings going up and down. The number under it says they do not move at all. The code is correct and always was, so nothing in the system is aware of the contradiction. Every check anybody runs on it will pass, including the ones written specifically to catch this sort of thing.
The cost is a report stating that a quantity is perfectly steady when it is not, produced by a system with a spotless record, read by people with no reason to doubt it. Nobody involved did anything careless. The person who wrote the routine picked a standard formula out of a standard book. The person who restated the base changed nothing about the data. The person reading the report read it correctly.
And the fix is one line rather than a rewrite: a large constant added to the readings, the routine run again, and the two compared. Anything that should be unchanged and is not has just shown where the instability lives. On this routine that test takes seconds and it fails loudly. The entire rest of the system managed nothing of the kind.
Is there anything here resting on somebody else's word?
There is not. Arithmetic has no issuing body, no jurisdiction and no revision date, so no source exists for anybody to confirm a figure against. Instead there is a recipe for every figure, and recomputing is the only kind of checking that applies. The table below gives each one and what reproduces it. Two of them need a calculator that shows eight digits and nothing else; the rest need patience. The loss of digits when nearly equal numbers are subtracted has a standard name in the literature on computation, and it belongs to no single author.
| The figure | What reproduces it | What is needed to hand |
|---|---|---|
| The variance of 24.25 on the fifty month record | The fifty monthly changes, average them, take each away from the average, square, average the fifty squares | Patience, or a spreadsheet |
| The one pass readings of 24.30, 20.00 and 0.00 | Add the stated amount to all fifty, average the squares, square the average, round each to eight digits, subtract | A calculator showing eight digits |
| The magnifications of 41,279.3608, 41,24,124.7216 and 41,23,75,258.7423 | The average of the fifty squares at that addition, divided by 24.25 | One division, three times |
| The ladder readings of Rs 100.010200/- and Rs 100.012682/- | Start at Rs 100.01/-, multiply by 1.02, subtract Rs 2/-, twelve times over | Twelve multiplications |
| The reversed ladder error of 0.7885 of a paisa | Start at Rs 100.01/-, add Rs 2/-, divide by 1.02, twelve times over | Twelve divisions |
| The ending prices of Rs 120.808714/-, Rs 115.00/- and Rs 120.82/- | Rs 100/- compounded through the fifty months in their stated order, once exactly, once rounding to the rupee each month, once to the paisa | The record in its stated order, and a spreadsheet |
| The stated order of the fifty months | Multiply by 16,807 and take the remainder on 2,14,74,83,647, from the starting number 4321, walking from the last month back to the second and swapping each position with an earlier one the rule picks | A machine that will do whole number arithmetic that large |
The Nakshatra unit, the Nakshatra ladder, the fifty month record and the reporting routine in the failure above are invented.
Educational material. Not advice on any investment, tax, budget or market position.
