Skip to content

An example: a workbook's answer and its derivative by 689 inputs; a derivative fix - #113

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

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

Conversation

@matthewjones372

Copy link
Copy Markdown
Collaborator

Whoever signs off a provision is asked what it rests on. A spreadsheet answers one input at a time: change the number, recalculate the book. sensitivities asks the graph for the derivative of the provision by hundreds of the workbook's inputs at once, and runs them for a whole loan book in the same plan as the provision.

cargo run --release -p watershed-gpu --example sensitivities            # 50,000 loans

What the example does

It reads credit_loss.xlsx (#111, #112) and calls Graph::gradient on the provision (Loan!B14), by 689 inputs: 17 of the bank's choices on the Model sheet, and all 112 quarters of the six typed columns of the economy.

  1. Checks every derivative for the workbook's own loan against nudging that input a millionth up and down. All 689 agree, the furthest by 4×10⁻⁹ of itself. The run asserts this.
  2. Adds them up over a made-up book and prints:
    • what a hundredth more of each of the bank's choices does to the provision;
    • what a year of the economy does, by the year it falls in: unemployment a point higher, or house prices a hundredth lower, on each path.
  3. Shows what a slope does not see. The threshold for stage 2 has a derivative of nought, yet raising it by a twentieth takes 0.8% off the provision of 10,000 loans. It moves loans between stages, which is a jump. That is what audit is for.

The loan's own cells are not among the 689: the workbook rounds interest and payments to the penny, and a rounded number's slope is nought. The example's header and the README say so.

Measured

On a 4-core cloud VM, one run, 50,000 loans:

The provision alone 1.4 s
With its 689 derivatives 7.6 s (5.5 times as long)
Nudging each input for the book: 1,378 runs 32 minutes (scaled from the first row)

The gradient added 15,823 nodes to the workbook's 3,125.

A fix to derivatives

The first run gave not-a-number for 44 of the derivatives. The workbook guards a division as spreadsheets do: IF(owed<=0, 0, ... / owed). A formula of more than a dozen operations is differentiated in steps, each step a node, and the division's step stands outside the condition that guards it. With nothing owed, the adjoint coming down is nought and the division's own slope is infinite; their product was passed on.

Now, where a slope can be infinite (it divides, or takes a logarithm, a root or a power), a node passes on nothing when nothing comes down to it. Other slopes are multiplied as before.

  • New test in watershed-core/tests/derivative.rs: a guarded division in a formula long enough to be taken in steps. It fails without the change (NaN) and passes with it, and checks the slope is the model's own once something is owed.
  • This treats nought times infinity as nought everywhere, which is the usual choice, not only on a branch not taken. The core README says so.

Also

  • The made-up loan book moves to examples/shared/loan_book.rs, shared with credit_loss, whose book is unchanged.
  • CI's Examples job runs sensitivities -- 5000.

Checks

cargo test --workspace, cargo fmt --check, clippy with and without --features cuda, and cargo doc pass, as do the three workbook examples' CI commands. 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
@matthewjones372
matthewjones372 merged commit cefe323 into main Nov 19, 2026
9 checks passed
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