THE MATHS / LEARNING NOTES
From a probability to a decision
A trained network produces a number. Using that number requires more choices: which mistakes matter, which threshold turns a score into a class, and whether the score behaves like a reliable probability. This chapter makes those choices explicit, then uses the chain rule to explain local input sensitivity.
Read Beyond XOR first. Open a newly generated moons or
circles report beside this guide, and use the worked exercises
to check your understanding. The calculations live in
diagnostics.py; they use NumPy directly.
1. A score and a class answer different questions
For each row
The equality matters: a score exactly equal to the threshold is class 1.
Changing
| Actual label | Predicted 0 | Predicted 1 |
|---|---|---|
| 0 | True negative, TN | False positive, FP |
| 1 | False negative, FN | True positive, TP |
Our confusion matrix is [[TN, FP], [FN, TP]]. Precision is
The original metric cards and boundary replay retain threshold 0.5. The decision lab uses the validation-selected model and lets you explore a separate threshold on validation rows. Its held-out summary stays fixed at the threshold chosen by Python before test evaluation.
2. Make the cost of mistakes explicit
Let
The numerator has units of cost; dividing by
In this lab both costs default to 1. To make a missed class-1 example three times as expensive as a false alarm, run:
python -m neural_math_lab experiment --dataset moons --fp-cost 1 --fn-cost 3 --output runs/moons-cost-3
The sequence is:
- Fit the scaler and model on training rows.
- Restore the epoch with the smallest validation cross-entropy.
- Select a threshold minimizing validation cost using that restored model.
- Apply the frozen model, scaler, and threshold to the test rows.
The baseline gets its own validation-selected epoch and threshold, using the same costs and data partitions. Neither model uses test labels to select a threshold. Since validation is used for both epoch and threshold selection, its best cost can be optimistic; the separate test partition still matters. Threshold fitting is itself model selection, as discussed in the scikit-learn threshold guide.
Why finitely many thresholds are enough
Sort the distinct validation scores. Between two consecutive scores, every predicted label is constant. It is therefore sufficient to evaluate the distinct scores, plus a threshold that predicts every row as negative. The smallest score already represents an all-positive rule. Equal scores must enter together; their row order cannot decide which class they receive.
The implementation also includes 0.5 as a candidate. It minimizes cost up to floating-point roundoff, then breaks cost ties by choosing the candidate closest to 0.5, then the higher threshold if both distance and cost tie. This is a reproducible convention, not a claim that 0.5 has special optimality. The search is exhaustive over the validation decision patterns; it cannot promise the best threshold on new data.
Computers represent these numbers in binary floating point: 3 * 0.1 can be
slightly greater than 0.3, and 0.5 - 0.2 can differ from 0.8 - 0.5.
For selection, costs within 8 × float64 epsilon (about 1.78e-15, relative)
of the smallest computed cost are treated as tied. The same rule applies to
distance from 0.5 within that group. Comparisons always use the group's fixed
minimum, so candidate order cannot accumulate tolerance. There is no absolute
tolerance: zero cost beats every positive cost, however small, and an exact
threshold of 0.5 beats an adjacent float when costs tie. Differences beyond
this roundoff tolerance still determine the winner. Recorded costs retain their
computed values; the tie tolerance affects selection, not cost calculation.
For the all-negative rule, the stored threshold is np.nextafter(1.0, np.inf):
the smallest representable float64 number above 1. This finite sentinel is
approximately 1.0000000000000002. It includes scores that round to exactly 1,
unlike a threshold of 1, and stays valid JSON. Interpret it as predict none
positive, not as a probability greater than 1.
A complete four-row example
Use these scores from a hypothetical already-fitted model:
| Row | True label |
Score |
|---|---|---|
| A | 0 | 0.1 |
| B | 1 | 0.4 |
| C | 0 | 0.4 |
| D | 1 | 0.9 |
| Threshold | TN | FP | FN | TP | Mean cost, |
Mean cost, |
|---|---|---|---|---|---|---|
| Above 1 | 2 | 0 | 2 | 0 | ||
| 0.9 | 2 | 0 | 1 | 1 | ||
| 0.5 | 2 | 0 | 1 | 1 | ||
| 0.4 | 1 | 1 | 0 | 2 | ||
| 0.1 | 0 | 2 | 0 | 2 |
With equal costs, 0.4, 0.5, and 0.9 tie; the stated convention selects 0.5. With missed positives costing three times as much, the selected threshold is 0.4. Rows B and C have identical scores, so a score threshold cannot classify both correctly. More threshold searching cannot solve that ambiguity.
If
3. ROC: how well are positives ranked above negatives?
At each distinct score threshold, compute
The ROC curve plots
The trapezoidal area is
An independent interpretation checks the arithmetic. Compare every positive
score with every negative score, awarding 1 for a larger score, 1/2 for a tie,
and 0 for a smaller score. The positives
AUC describes ranking across thresholds; it is neither accuracy at the chosen
threshold nor a probability-calibration measurement. If the evaluated labels
contain only one class, there are no positive–negative pairs. This lab reports
ROC AUC as null and any rate with a missing denominator as null, rather than
inventing a curve value. For the standard metric definition, see the
official ROC metric documentation.
4. Precision–recall: what happens among predicted positives?
For the same descending thresholds, the example's
Average precision uses the recall increments:
This is a step-weighted sum, not trapezoidal area under a precision–recall curve. Linear interpolation would give a different quantity. The official average-precision definition specifies the same non-interpolated sum.
Here AP is null when there are no positive labels, because recall has no
denominator. With every label positive, AP is 1; that result does not establish
an ability to reject negative examples. Class prevalence affects precision,
so compare datasets and splits with their positive counts in view.
5. Reliability: does a score of 0.8 behave like 80%?
The report groups scores into ten equal-width intervals. Bin
Plot null averages; drawing it at
Our class-1 probability ECE is the weighted absolute bin gap:
It bins
For a hand calculation, use two bins on the four-row example:
| Bin | Scores | Count | Mean score | Positive fraction | Weighted absolute gap |
|---|---|---|---|---|---|
| 0.1, 0.4, 0.4 | 3 | ||||
| 0.9 | 1 | 1 |
The ECE is
The binary Brier score uses every row directly:
This score uses the binary
6. Input sensitivity: apply the chain rule to raw coordinates
The earlier backpropagation chapter differentiates loss with respect to
weights. Now hold the learned parameters fixed and differentiate one output
For one row, let
Adding the paths through all hidden units gives
For a batch,
dp_d_standard = ((1 - H**2) * W2.T) @ W1.T # (N, 2)
dp_d_raw = (P * (1 - P)) * dp_d_standard / scale
P has shape (N, 1) and scale has shape (1, 2), so broadcasting scales
each row and coordinate appropriately. There is no division by batch size:
each row contains the gradient of its own probability, not the gradient of a
mean loss. Forgetting
A positive component means that an infinitesimal increase in that coordinate, holding the other one fixed, increases class-1 probability locally. Its units are probability change per raw coordinate unit. Components measured in different units should not be compared as universal feature importance.
A numerical example with every step visible
Use one hidden unit,
The raw gradient is
For displacement
The actual perturbed calculation uses
An independent numerical check perturbs the raw coordinate itself:
Here tanh or sigmoid outputs can yield
very small gradients. That describes the local function, not reliability or
robustness over a large region. A derivative of a learned predictor also does
not establish a causal effect in the world.
7. Turn exploration into an explanation
Before moving a control, predict what will change and what will stay fixed. Lowering the threshold changes confusion counts, but not probabilities, Brier, AUC, or AP. Moving an input changes that point's probability and gradient, but does not retrain the weights. Selecting a replay checkpoint changes the point inspector's model; the decision lab evaluates the fixed selected model.
Record costs, threshold, sample counts, and which split supplied each metric. Do not choose a cost ratio or model by repeatedly optimizing displayed test results. Those test rows would then become part of your development process; reserve fresh evaluation data for a later final claim. Small synthetic experiments teach these distinctions, without establishing deployment quality.