Skip to content

An example: a portfolio and its rules through every stretch of real prices since 1973 - #119

Merged
matthewjones372 merged 22 commits into
mainfrom
claude/eager-keller-5jqwz9
Nov 23, 2026
Merged

matthewjones372 merged 22 commits into
mainfrom
claude/eager-keller-5jqwz9

Conversation

@matthewjones372

Copy link
Copy Markdown
Collaborator

"What would a crash do to us" is usually answered with one or two remembered crashes and a guess. replay takes a portfolio and the rules it is run by and puts it through every 60 days of real share prices since 1973: 13,526 stretches, each of which happened.

python3 crates/watershed-gpu/examples/shared/prices.py prices                  # about 200 MB, a few minutes
cargo run --release -p watershed-gpu --example replay -- prices                # from 1973, 60-day stretches
cargo run --release -p watershed-gpu --example replay -- prices 1985           # more shares, fewer years
cargo run --release -p watershed-gpu --example replay -- made-up               # no data needed

Why this needs the engine

For a portfolio that is simply held, a replay is arithmetic. This one is run by rules, which make it depend on the path: a holding is sold once it falls a set distance below its highest value since the start, and the portfolio may be bought partly with borrowed money, on which interest is paid.

The graph is the portfolio for the 60 days: every holding's value, its high so far and whether it is still held, each day, with the cash, the debt and what the owner has left. A scenario is one stretch of history: every share's rise on each of its days (2,580 inputs for 43 shares). The stop and the borrowing are shared inputs, so each choice of rules is one more run of every stretch.

What it prints

  1. The history: how many shares, days and stretches.
  2. Held and left alone: how the 60-day outcome is spread, and the five worst stretches that do not overlap, with their dates.
  3. The rules: four stops (never, and after a fall of 25%, 15% and 10%) against three levels of borrowing, each through every stretch.
  4. When it mattered: in the stretches where the portfolio lost more than a tenth, and in all the rest, what selling after a fall did on average.
  5. If it happened again: six remembered times (1974, October 1987, 2002, autumn 2008, February 2020, spring 2022), left alone and with the rules.
  6. Checks.

On the 43 shares with prices from 1973, fetched on 19 November 2026:

  • Left alone, the portfolio is down after 60 days 27% of the time, 1 time in 20 by more than 9.2% and 1 in 100 by more than 21.7%. The worst stretch began on 24 December 2019: down 36.5%.
  • A stop at a 15% fall takes the worst stretch from 36.5% to 24.5% and costs 0.7 points of the median.
  • In the 590 stretches that lost more than a tenth, a 10% stop cut the average loss from 16.6% to 7.1%. In all the rest it cut the average gain from 4.3% to 2.7%.

The prices

shared/prices.py takes today's members of the S&P 500 from Wikipedia's list and each one's daily history from Yahoo Finance, and writes a CSV a share in the form backtest and experts already read. It needs only Python's standard library. The prices are not in the repository: Yahoo's data is for personal use.

What is there, and what is not:

  • Yahoo's histories start in 1970 at the earliest, and few go back that far. Of 503 shares, 26 had prices from 1970, 44 from 1973, 93 from 1980 and 201 from 1985. I had guessed 150 to 200 from 1970 before fetching them; that was wrong. The example defaults to 1973, which keeps the 1973 to 1974 fall.
  • These are survivors. Every company that failed or was bought on the way is missing, so every figure is kinder than the truth, and more so the earlier the year. The example's header and the README say so.
  • Prices are adjusted for splits and dividends. One share with a day's move too wild to be real is dropped (44 fetched, 43 used).
  • A holding is sold at the day's closing price with no cost and no gap. Real stops do worse.

Checks

  • The same sums written out by hand, for every stretch under one choice of rules: the furthest apart is 1×10⁻¹⁵.
  • The graph itself, a stretch at a time, for 20 stretches: the same to the last bit.
  • CI's Examples job runs replay -- made-up, which makes its own prices and runs both checks.
  • cargo fmt --check, clippy with and without --features cuda, and cargo doc pass.

Measured

On a 4-core cloud VM, one run each:

43 shares from 1973 200 shares from 1985
Nodes in the portfolio 11,352 51,815
Stretches 13,526 10,493
Every stretch, one choice of rules 0.7 s 2.8 s
Eleven more choices 8.3 s 29.6 s

Not run

Nothing here has run on a GPU. With --features cuda every run goes to the GPU.


Generated by Claude Code

Expressions, formulas and scenario plans have floor, on the CPU and in
the GPU kernel. It takes the last of a plan's 32 instruction codes.

watershed-xlsx builds the four rounding functions on it, for a number
that depends on a varied input and a fixed number of places. A number
within four parts in 10^16 of a half or a whole number is taken as it,
which is how 0.29 * 100 comes to 29 and 1.005 to 1.01 as they do in
Excel. The test workbook gains a Rounding sheet: sixteen formulas over
396 pairs of inputs, held to LibreOffice's values.

Nothing here has run on a GPU.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
credit_loss reads a workbook that works out the expected credit loss on
one mortgage, on three paths of the economy, and runs its formulas for
every loan of a made-up book at once. It checks itself against the values
saved in the workbook, against LibreOffice recalculating the workbook for
each of 300 loans, and against the graph bit for bit. Then it works the
book out again for twenty slumps, worse and likelier.

The workbook and the reference are written by shared/credit_loss.py.

Nothing here has run on a GPU.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
It read the reported edits once, as soon as the cell showed its old
value. With a WorkerTable the report comes after the cell repaints, so
the read could find one edit, not two. It now waits for the second, as
the file's other tests do.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
audit runs a workbook for points spread over the whole of its inputs'
ranges and at every corner of them, and says where an answer is an error
or negative. It moves each input through its range from many starting
points and says whether each answer rises, falls or turns round, and
where it jumps, closing in on each suspected jump to confirm it. Given a
later version of the workbook, it lists the cells changed and runs both
for the same points, to show which answers moved and for which inputs.

On its own it takes the credit_loss workbook and a later version that
shared/credit_loss.py writes with one change that is meant and one slip,
and checks that it finds both and where the slip reaches.

Workbook::cells lists every cell that holds a value or a formula.

Nothing here has run on a GPU.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
A loan in default was given the loss on a sale at today's prices, where
a loan's lifetime loss allowed for prices falling on each path. For some
loans the provision then fell as they passed 90 days past due, which the
audit example showed.

A property is now sold some quarters after a default (Model!B14, six),
at that quarter's prices on each path, in every quarter of the schedule
and for a loan already in default. The provision rises with days past
due on every line the audit tries.

Both workbooks and LibreOffice's reference are written again.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
sensitivities asks the graph for the derivative of a loan's provision,
in the credit_loss workbook, by 17 of the bank's choices and by every
quarter of three paths of the economy. It checks each against nudging
that input for the workbook's own loan, then adds them up over a made-up
book to say which choices and which quarters the provision rests on. It
ends with what a slope does not see: a threshold that moves loans from
one stage to another.

A derivative no longer comes to not-a-number where a division by zero
sits on a branch that is not taken. A long formula is differentiated in
steps, and the division's step stood outside the condition guarding it,
so nothing times infinity was passed down. Where nothing comes down to a
node, nothing is now passed on through a slope that can be infinite.

The made-up loan book moves to shared/loan_book.rs, for credit_loss and
this to share.

Nothing here has run on a GPU.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
quotes reads a car insurance rating workbook and works out the price for
every customer it can quote: 16 million combinations of age, area,
vehicle group, years without a claim, mileage, excess and cover. It
checks itself against the values saved in the workbook, against
LibreOffice recalculating the workbook for each of 300 customers, and
against the graph for 2,000 quotes. Then it puts next quarter's rates in,
works every quote out again, and says whose price moves.

The workbook and the reference are written by shared/rating.py.

Nothing here has run on a GPU.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
live reads a risk desk's workbook (sixty shares, six hundred positions,
each desk's totals against its limits, and the floor's figures) and feeds
its graph a stream of prices, one change at a time. Each change works
out again only the cells that price reaches. It reports the moments a
desk goes over its limit or a position is stopped out, and afterwards
says why one of them happened, from the history the graph kept.

It checks itself against LibreOffice following the same first 300
prices, and at the end against a second graph given the final prices at
once, cell for cell and to the last bit.

The workbook and the reference are written by examples/desk.py.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
illustrations reads a workbook that works out one member's yearly
pension statement (the pot followed to retirement at three rates of
growth, the income it might buy and whether that is enough) and runs it
for every member of a made-up scheme. It checks itself against the
values saved in the workbook, against LibreOffice recalculating the
workbook for each of 300 members, and against the graph bit for bit.

Then it seeks, for each member who is short, the least they could pay in
that would put them on track: Goal Seek for every member at once, each
try a run of the workbook, and each answer checked to be the least.

The workbook and the reference are written by shared/illustration.py.

Nothing here has run on a GPU.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
…unding reaches fifteen figures

households reads a workbook that works out what one household has to
live on after tax, social insurance and support, and runs it for a
made-up country. Then it raises each household's pay step by step, forty
times over, to find who keeps little of a rise and who would be worse
off for one, and does the same after a reform. It checks itself against
the values saved in the workbook, against LibreOffice recalculating the
workbook for each of 300 households, and against the graph bit for bit.

That check found a penny: ROUND(628 - 616.715, 2) is 11.29 in
LibreOffice and was 11.28 here. The subtraction loses figures, and what
is left is further from the half than the rounding allowed for. A number
that depends on an input is now taken for a half or a whole number
within 4.5 parts in 10^15 of it, which is as far as fifteen figures
reach, where it was 4 parts in 10^16. The test workbook's Rounding sheet
gains that case; the old allowance fails it.

The workbook and the reference are written by shared/household.py.

Nothing here has run on a GPU.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
… each risk

project_risk reads a building project's workbook (the estimate, the
programme of activities and what each waits for, and the risk register)
and draws every rate, duration and risk from the lows, highs and chances
in the workbook's own columns, a million trials at once. It reports what
the cost and the weeks will not exceed some share of the time, how often
the project is over budget or late, and what the cost moves with.

Then it runs the same trials again without each risk in turn, to rank
the risks by what being rid of each is worth, beside the register's own
ranking by chance times cost.

It checks itself against the values saved in the workbook, against
LibreOffice recalculating the workbook for each of 300 trials, and
against the graph bit for bit.

The workbook and the reference are written by shared/project.py.

Nothing here has run on a GPU.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
…rices

replay builds a portfolio for sixty days as a graph: every holding's
value, its high so far and whether it is still held, each day, with a
rule that sells a holding once it falls a set distance below its high,
and part of the portfolio bought with borrowed money. A scenario is one
stretch of history, one starting each day, and the stop and the
borrowing are shared inputs, so each choice of rules is one more run of
every stretch.

It says what holding the portfolio has done over every such stretch,
which were the worst, what each of twelve choices of rules did, whether
selling after a fall helped when it mattered, and what six remembered
times would do again. It checks itself against the same sums written out
by hand for every stretch, and against the graph bit for bit.

shared/prices.py fetches the prices: today's members of the S&P 500, as
far back as Yahoo Finance has them, which is 1970 at the earliest. They
are not kept in the repository. With made-up in place of the folder the
example runs on prices that wander at random, which is what CI runs.

Nothing here has run on a GPU.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
It turned the failures off and then clicked. A scroll near the end asks
for the page too, and one arriving between the two succeeded, took the
button away, and left the click waiting for a button that would not come
back. The page now fails until the click itself has been seen, so a
request from a scroll fails like the rest and the button is still there.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdzBXf3FUMtMeaNSmdF32v
@matthewjones372
matthewjones372 merged commit f4cc1df into main Nov 23, 2026
9 checks passed
@matthewjones372
matthewjones372 deleted the claude/eager-keller-5jqwz9 branch November 23, 2026 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants