What this is
What is a metric?
In machine learning, a metric is the number you use to decide whether a trained AI model is good enough. It is a different number from the loss the AI model was trained to push down, and the two can disagree. Each metric answers one question and says nothing about the others, so accuracy can look excellent while the rare cases you care about go unflagged. Classifiers, search results and generated text each have their own metrics. An AI model used to grade answers needs checking against people too.
Why it matters: You need this whenever a number decides something, such as whether an AI model ships or which search system you keep. A fraud detector with 94% accuracy can still miss 4 frauds in 10.
At a glance
Key takeaways
- Precision is how much of what was flagged was right. Recall is how much of what mattered was found. F1 combines the two.
- Accuracy misleads when one class is rare: flagging nothing when 1% of transactions are fraud scores 99%.
- ROC-AUC is the probability that a random positive scores above a random negative.
- Choose the threshold by what each kind of error costs.
- When a search step feeds a language model, measure recall@k first: a passage that is not retrieved cannot be used.
- BLEU and ROUGE score shared wording, so they punish correct paraphrases. For open-ended answers, use an LLM judge, checked against people with Cohen's kappa.
How it works, from scratch
Which metric to use depends on what the model produces. A classifier makes a yes-or-no decision about each item, a search system returns a ranked list, and a language model writes text. Each needs its own metrics. The classifier comes first, because search borrows its precision and recall.
Chapter 1
Classification: precision, recall and the confusion matrix
Take a fraud detector checking 100 transactions, 10 of which are fraud. It flags 8, and 6 of those are fraud. Every transaction lands in one of four boxes: 6 hits (true positives, TP), 2 false alarms (false positives, FP), 4 misses (false negatives, FN) and 88 correct passes (true negatives, TN). Precision asks how much of what was flagged is fraud. Recall asks how much of the fraud was flagged. F1 combines the two into one number, and accuracy counts every right decision.
| Metric | Calculation | Result |
|---|---|---|
| Precision | 6 correct ÷ 8 flagged | 75% |
| Recall | 6 found ÷ 10 actual fraud | 60% |
| F1 | 2 × 0.75 × 0.60 ÷ (0.75 + 0.60) | 67% |
| Accuracy | (6 + 88 correct) ÷ 100 | 94% |
Accuracy reads 94% while the model misses 4 frauds in 10.
The detector never outputs "fraud" directly. It outputs a score, and a threshold you choose turns the score into a flag. Comparing each flag with the truth puts the transaction in one of the four boxes, so moving the threshold changes every count and every metric. An accuracy figure is incomplete without the threshold it was measured at.
Figure 1 · Diagram
flowchart LR
X[Item] --> M[Model]
M --> S[Score<br/>e.g. 0.73]
S --> T{Score >= threshold?}
T -->|yes| P[Flagged positive]
T -->|no| N[Not flagged]
P --> CM[Confusion matrix<br/>TP / FP / FN / TN]
N --> CM
L[True label] --> CM
CM --> MET[Precision, recall,<br/>F1, accuracy]
Laid out as a grid, the four counts are the confusion matrix:
| predicted positive | predicted negative | |
|---|---|---|
| actual positive | TP = 6 | FN = 4 (missed) |
| actual negative | FP = 2 (false alarm) | TN = 88 |
The formula and its symbols
Symbols
| Symbol | Meaning here | Shape / range |
|---|---|---|
| TP, FP, FN, TN | counts of hits, false alarms, misses and correct passes | whole numbers |
| , | precision and recall | 0 … 1 |
| the harmonic mean of P and R: an average that is dragged towards the smaller of the two | 0 … 1 | |
| total number of items, TP + FP + FN + TN | whole number | |
| fraction bar | "divided by" |
In words: precision is hits over everything flagged; recall is hits over everything that should have been flagged; F1 is twice their product over their sum; accuracy is everything right over everything.
On the worked example: P = 6/(6 + 2) = 0.75; R = 6/(6 + 4) = 0.60; F1 = 2 × 0.75 × 0.60 / 1.35 = 0.667; accuracy = (6 + 88)/100 = 0.94. The harmonic mean punishes imbalance: P = 1.0 with R = 0.1 gives F1 = 0.18, not the ordinary average of 0.55.
In Python
TP, FP, FN, TN = 6, 2, 4, 88
N = TP + FP + FN + TN
# precision, recall
P, R = TP / (TP + FP), TP / (TP + FN)
P, R # → (0.75, 0.6)
# F1, accuracy
round(2 * P * R / (P + R), 3), (TP + TN) / N # → (0.667, 0.94)
# F1 when P = 1.0 and R = 0.1
round(2 * 1.0 * 0.1 / (1.0 + 0.1), 2) # → 0.18
New to Σ, ∂ or ‖x‖? The symbols reference decodes each one as a few lines of Python.
In code: confusion counts the four boxes, Confusion computes the
four metrics, and flag_nothing_trap is the model below. View source
Accuracy is the metric that misleads. If 1% of transactions are fraud, a model that flags nothing is 99% accurate and catches no fraud at all (recall 0). When one class is rare, report precision and recall, not accuracy.
Chapter 2
ROC-AUC: how well does the model rank?
Every count in the confusion matrix depends on the threshold. To compare two models before anyone has chosen one, ask how well each ranks: does it give frauds higher scores than legitimate transactions?
Take two frauds scored 0.9 and 0.4, and two legitimate transactions scored 0.6 and 0.1. Compare every (fraud, legitimate) pair: 0.9 > 0.6 ✓, 0.9 > 0.1 ✓, 0.4 > 0.6 ✗, 0.4 > 0.1 ✓. Three of the four pairs are in the right order: a share of 0.75. That share is the model's ROC-AUC, or AUC for short.
The name comes from a curve. Call the frauds the positives and the legitimate transactions the negatives, and lower the threshold from above every score, one score at a time. Each positive that becomes flagged raises the true-positive rate (TPR, the same thing as recall), and each negative that becomes flagged raises the false-positive rate (FPR). Plotting TPR against FPR traces the ROC curve, and the AUC is the area under it.
Figure 2 · Diagram
flowchart TD
A[Sort items by score, highest first] --> B[Start with threshold above every score<br/>nothing flagged: point 0,0]
B --> C[Lower the threshold past the next distinct score]
C --> D{Which items became flagged?}
D -->|a positive| E[Step up: TPR rises]
D -->|a negative| F[Step right: FPR rises]
D -->|a tie of both| G[Diagonal step: half credit]
E --> H{Everything flagged?}
F --> H
G --> H
H -->|no| C
H -->|yes| I[End at point 1,1<br/>AUC = area under the path]
The formula and its symbols
Symbols
| Symbol | Meaning here | Shape / range |
|---|---|---|
| TPR | true-positive rate: recall, the share of positives flagged | 0 … 1 |
| FPR | false-positive rate: the share of negatives wrongly flagged | 0 … 1 |
| , (in the AUC sum) | the set of positive items and the set of negative items | sets |
| , | how many items each set holds | whole numbers |
| , | the model's score for positive p and negative n | real |
| 1 if the statement inside is true, else 0 | 0 or 1 | |
| add over every (positive, negative) pair |
In words: AUC is the share of (positive, negative) pairs in which the positive gets the higher score, counting ties as half.
On the worked example: 2 positives × 2 negatives = 4 pairs; 3 are ordered correctly; AUC = 3/4 = 0.75. One point on the curve: at threshold 0.5 the model flags the 0.9 fraud and the 0.6 legitimate transaction, so TPR = 1/(1 + 1) = 0.5 and FPR = 1/(1 + 1) = 0.5.
In Python
# scores of the positives (frauds)
s_P = [0.9, 0.4]
# scores of the negatives (legitimate)
s_N = [0.6, 0.1]
t = 0.5
TP, FN = sum(s >= t for s in s_P), sum(s < t for s in s_P)
FP, TN = sum(s >= t for s in s_N), sum(s < t for s in s_N)
# TPR, FPR
TP / (TP + FN), FP / (FP + TN) # → (0.5, 0.5)
pairs = sum((s_p > s_n) + 0.5 * (s_p == s_n) for s_p in s_P for s_n in s_N)
# AUC: the share of pairs ranked correctly
pairs / (len(s_P) * len(s_N)) # → 0.75
Figure 3 · Chart
ROC curve bowing well above the coin-flip diagonal: the classifier ranks a random positive above a random negative 89% of the time (AUC 0.89)
AUC needs no threshold, which is what makes it good for comparing models. When positives are rare it can flatter, though. With nine legitimate transactions for every fraud, a pile of false alarms is still a small false-positive rate. The precision-recall curve plots the same scores in a way that shows the false alarms:
Figure 4 · Chart
Precision-recall curve falling as recall rises: at 80% recall only about a third of the flags are real, against a 10% base rate
In code: roc_auc measures the area under the walk and roc_auc_rank
counts the pairs, and the two give the same number. View source
Chapter 3
Picking the threshold by what errors cost
A detector in use must flag each transaction or not, so someone has to choose the threshold. The right choice depends on what each kind of mistake costs. A disease screen flags generously, because a missed disease is far worse than a second test. A spam filter flags cautiously, because losing an important email is worse than reading some spam. Put a price on a miss and on a false alarm, and pick the threshold where the total is lowest.
Take the four scores from the ROC example: frauds 0.9 and 0.4, legitimate 0.6 and 0.1. If a miss costs 10 and a false alarm costs 1, the cheapest rule is "flag at 0.4 or above". It catches both frauds and wrongly flags one legitimate transaction, for a total cost of 1. Swap the prices, and the cheapest rule is "flag only 0.9". It misses one fraud and raises no false alarms, again for a total of 1.
The formula and its symbols
Symbols
| Symbol | Meaning here | Shape / range |
|---|---|---|
| the threshold: flag items scoring at least t | real | |
| , | misses and false alarms at that threshold | whole numbers |
| , | the price of one miss and of one false alarm | ≥ 0 |
| "the value of t that makes this smallest" | ||
| the best threshold | real |
In words: the total cost at a threshold is misses times their price plus false alarms times theirs; pick the threshold where that total is lowest.
On the worked example: c_FN = 10, c_FP = 1: at t = 0.4, FN = 0 and FP = 1, cost 1, the minimum. The other thresholds cost 10 (t = 0.9, one miss), 11 (t = 0.6, one miss and one false alarm) and 2 (t = 0.1, two false alarms).
In Python
frauds, legit = [0.9, 0.4], [0.6, 0.1]
c_FN, c_FP = 10, 1
def cost(t):
# frauds below t are missed
FN = sum(s < t for s in frauds)
# legitimate ones at or above t are false alarms
FP = sum(s >= t for s in legit)
return c_FN * FN + c_FP * FP
[cost(t) for t in (0.9, 0.6, 0.4, 0.1)] # → [10, 11, 1, 2]
# t* = argmin_t cost(t)
min((0.9, 0.6, 0.4, 0.1), key=cost) # → 0.4
Figure 5 · Chart
Total cost against threshold for three error prices: the cheapest threshold moves left when misses cost more and right when false alarms cost more
In code: best_threshold_by_cost tries every score as the threshold
and keeps the cheapest. View source
Chapter 4
Retrieval metrics: judging a search result list
A search system makes a different kind of output: not a yes or no for each item, but a ranked list of results for a query. Precision and recall carry over, measured on the top k results. Two more metrics reward putting the best results near the top.
Take one query. The system returns five documents in the order d7, d3, d9, d1, d4. Three documents are relevant, each graded by how relevant it is: d3 (grade 3, very relevant), d4 (grade 2) and d8 (grade 1, never returned).
- recall@5, the share of the relevant documents found in the top 5: 2 of 3 = 0.667
- precision@5, the share of the top 5 that is relevant: 2 of 5 = 0.4
- reciprocal rank, one over the rank of the first relevant result: it is at rank 2, so 1/2 = 0.5. Averaged over many queries, this is the mean reciprocal rank (MRR).
- nDCG@5 (normalized discounted cumulative gain), which adds up the grades, counting each less the lower it sits, and divides by the best total any order could get: 0.560, worked below.
Figure 6 · Diagram
flowchart LR G[Golden set<br/>query + relevant doc ids] --> R[Retriever] R --> K[Ranked top-k list] K --> RK["recall@k<br/>did we find them?"] K --> PK["precision@k<br/>how much noise?"] K --> RR[MRR<br/>how high is the first hit?] K --> ND["nDCG@k<br/>are the best ones on top?"] G --> RK & PK & RR & ND
The formula and its symbols
Symbols
| Symbol | Meaning here | Shape / range |
|---|---|---|
| how many top results you look at | e.g. 5 | |
| "in both": documents that are relevant and in the top k | set | |
| how many items a set holds | ||
| , | the set of test queries, and one query | |
| position (1 = top) of the first relevant result for query q | 1, 2, … | |
| the relevance grade of the result at position i (0 if irrelevant) | e.g. 0 … 3 | |
| logarithm base 2: how many times you halve i + 1 to reach 1. It grows slowly, so it is a gentle position discount | 1 at rank 1, 1.58 at rank 2 | |
| IDCG | "ideal DCG": the DCG of the same grades sorted best-first, so nDCG tops out at 1 |
In words: recall@k is the share of relevant documents found in the top k; MRR averages one-over-the-rank of the first hit; DCG adds each result's grade, discounted by the log of its position; nDCG divides by the best possible DCG.
On the worked example: recall@5 = 2/3 = 0.667; with one query whose first hit is at rank 2, MRR = 1/2 = 0.5. DCG = 3/log₂(3) + 2/log₂(6) = 1.8928 + 0.7737 = 2.6665 (d3 at rank 2, d4 at rank 5). Ideal order d3, d4, d8: IDCG = 3/1 + 2/1.585 + 1/2 = 4.7619. nDCG = 2.6665/4.7619 = 0.560.
In Python
import math
ranked = ["d7", "d3", "d9", "d1", "d4"]
# relevance grades; missing means 0
rel = {"d3": 3, "d4": 2, "d8": 1}
k = 5
# recall@k
round(len(set(rel) & set(ranked[:k])) / len(rel), 3) # → 0.667
rank_q = next(i for i, doc in enumerate(ranked, start=1) if doc in rel)
# MRR over a single query
1 / rank_q # → 0.5
DCG = sum(rel.get(doc, 0) / math.log2(i + 1) for i, doc in enumerate(ranked[:k], start=1))
# the same grades, best first
best = sorted(rel.values(), reverse=True)[:k]
IDCG = sum(g / math.log2(i + 1) for i, g in enumerate(best, start=1))
print(f"{DCG:.4f} {IDCG:.4f} {DCG / IDCG:.3f}") # → 2.6665 4.7619 0.560
Figure 7 · Chart
DCG weight by rank: rank 1 counts fully, rank 2 counts 63% and rank 10 still counts 29%
In code: recall_at_k, precision_at_k, mean_reciprocal_rank and
ndcg_at_k are the four metrics. View source
In retrieval-augmented generation (RAG), a search step finds passages and a language model answers from them. There, measure recall@k first, with k set to the number of passages the language model is given. It cannot use a passage that was never retrieved, and no prompt fixes that.
Chapter 5
Generation metrics: why word overlap misleads
A language model's answer is neither a decision nor a ranked list, so none of the metrics so far apply. The oldest automatic check compares the generated text, the candidate, with a reference answer written by a person, and counts the words they share. BLEU, from machine translation, multiplies together how many of the candidate's 1-, 2-, 3- and 4-word sequences appear in the reference, with a penalty for being too short. ROUGE-L, from summarisation, measures the longest sequence of words the two share in the same order, gaps allowed.
Neither checks whether the candidate is true. Here are two candidates, scored against the reference "The meeting was moved to Friday because the manager is sick."
| candidate | BLEU | ROUGE-L |
|---|---|---|
| "Since the boss is ill, the team rescheduled the meeting for Friday." (correct) | 0.16 | 0.26 |
| "The meeting was moved to Monday because the manager is sick." (wrong) | 0.73 | 0.91 |
The wrong answer scores far higher on both, because it copies the reference's wording.
The formula and its symbols
Symbols
| Symbol | Meaning here | Shape / range |
|---|---|---|
| share of the candidate's n-word sequences also found in the reference, each counted at most as often as the reference has it | 0 … 1 | |
| , | natural log and its inverse; exp of the average log is the geometric mean of the four | |
| BP | brevity penalty: 1 if the candidate is longer than the reference, else e^(1 − ref length / candidate length) | 0 … 1 |
| LCS | longest common subsequence: the longest run of words appearing in both texts in the same order | |
| , | LCS length over candidate length, and over reference length | 0 … 1 |
In words: BLEU is the geometric mean of the n-gram match rates, scaled down for short answers; ROUGE-L is the F1 of the longest shared in-order word sequence.
On a hand example: candidate "a b c d", reference "a c d e": the LCS is "a c d", 3 words, so P = 3/4, R = 3/4, ROUGE-L = 0.75. And "the the the the" against "the cat is here" scores unigram precision 1/4: "the" is credited only as often as the reference contains it.
With the numbers: the Monday answer and the reference are both 11 words (full stop and capitals dropped), so BP = 1. It matches 10 of its 11 words, 8 of its 10 word pairs, 6 of its 9 triples and 4 of its 8 four-word runs. This BLEU adds 1 to the top and bottom for n ≥ 2 (smoothing, so one missing four-word run can't zero the score), giving p = 10/11, 9/11, 7/10, 5/9, and BLEU = exp(¼(ln 0.909 + ln 0.818 + ln 0.7 + ln 0.556)) = 0.73. Its longest shared in-order run is 10 words, so ROUGE-L = 2 · (10/11) · (10/11) / (20/11) = 0.91.
In Python:
import math
ref = "the meeting was moved to friday because the manager is sick".split()
cand = "the meeting was moved to monday because the manager is sick".split()
# every run of n words, in order
def grams(words, n):
return [tuple(words[i:i + n]) for i in range(len(words) - n + 1)]
def p(n):
c, r = grams(cand, n), grams(ref, n)
# clipped: at most as often as ref has it
hits = sum(min(c.count(g), r.count(g)) for g in set(c))
# add-one smoothing for n ≥ 2
s = 1 if n > 1 else 0
return (hits + s) / (len(c) + s)
[round(p(n), 3) for n in range(1, 5)] # → [0.909, 0.818, 0.7, 0.556]
BP = 1.0 if len(cand) > len(ref) else math.exp(1 - len(ref) / len(cand))
# BLEU
round(BP * math.exp(sum(math.log(p(n)) for n in range(1, 5)) / 4), 2) # → 0.73
# every word but "monday", in order
P_LCS, R_LCS = 10 / len(cand), 10 / len(ref)
# ROUGE-L
round(2 * P_LCS * R_LCS / (P_LCS + R_LCS), 2) # → 0.91
# the hand example: LCS "a c d"
P_LCS, R_LCS = 3 / 4, 3 / 4
2 * P_LCS * R_LCS / (P_LCS + R_LCS) # → 0.75
Figure 8 · Chart
BLEU and ROUGE-L bars: the correct paraphrase scores lowest while the wrong answer that copies the wording scores almost as high as an exact copy
In code: bleu and rouge_l score one candidate against one
reference. View source
Overlap metrics still suit tasks where a right answer is nearly word for word, such as translation. For open-ended answers, teams use a large language model as a judge (an LLM judge): it grades each answer against a written rubric, a list of what a good answer must do. Scores that compare meaning through embeddings, such as BERTScore, do somewhat better than overlap. A judge, though, has to be checked before it is trusted.
Chapter 6
Calibrating a judge: Cohen's kappa
An LLM judge is checked the way a new human grader would be: it grades a sample that people have already graded, and you compare. How often the two agree is the wrong measure, for the same reason accuracy was for rare fraud.
Say people graded 20 essays and passed 18 of them. A lazy judge that stamps "pass" on everything without reading agrees with them on 18 of 20, or 90%. It gets that by luck, because people passed nearly everything. Cohen's kappa takes away the agreement expected by chance. Chance agreement is the chance both say pass plus the chance both say fail, each at its own rate: 0.9 × 1.0 + 0.1 × 0 = 90%. That equals the agreement observed, so kappa = 0. A second example: labels (y, y, n, n) and (y, n, n, n) agree on 3 of 4 items; chance predicts 0.5 × 0.25 + 0.5 × 0.75 = 0.5, so kappa = 0.5.
The formula and its symbols
Symbols
| Symbol | Meaning here | Shape / range |
|---|---|---|
| kappa: agreement beyond chance | ≤ 1; 0 = chance, 1 = perfect | |
| observed agreement: share of items both raters labelled the same | 0 … 1 | |
| agreement expected by chance | 0 … 1 | |
| one possible label (pass, fail…) | ||
| , | how often rater A (and rater B) uses label ℓ | 0 … 1 |
In words: kappa is how far agreement beats chance, as a share of the most it could beat chance by.
On the worked example: p_o = 0.75, p_e = 0.5, κ = (0.75 − 0.5)/(1 − 0.5) = 0.5.
In Python
# rater A's labels
A = ["y", "y", "n", "n"]
# rater B's labels
B = ["y", "n", "n", "n"]
p_o = sum(a == b for a, b in zip(A, B)) / len(A)
# Σ_ℓ p_A(ℓ) p_B(ℓ)
p_e = sum((A.count(ell) / len(A)) * (B.count(ell) / len(B)) for ell in ("y", "n"))
p_o, p_e # → (0.75, 0.5)
# κ
(p_o - p_e) / (1 - p_e) # → 0.5
Figure 9 · Diagram
flowchart LR S[Sample of real outputs] --> H[Humans label<br/>pass / fail] S --> J[LLM judge + rubric<br/>labels pass / fail] H --> K[Cohen's kappa] J --> K K -->|kappa high enough| U[Use the judge at scale] K -->|too low| F[Fix rubric, examples<br/>or judge model] F --> J
In code: cohens_kappa takes two lists of labels and returns κ. View source
Kappa above about 0.6 is commonly read as substantial agreement. A new judge model or an edited rubric makes a new judge, so measure kappa again whenever either changes.
Test yourself
6 questions
Answer each one out loud or on paper before you open it.
Question 1Q: A fraud model has 94% accuracy. Is it good?Think it through, then reveal
A: You can't tell from accuracy. If fraud is 10% of traffic, check recall and precision. In the worked example recall is only 60%, so 4 in 10 frauds slip through.
Question 2Q: When do you optimize for precision vs. recall?Think it through, then reveal
A: By the cost of each error. If a miss is expensive (fraud, cancer screening, a relevant legal document), favor recall. If a false alarm is expensive (blocking a good customer, paging an engineer at 3 a.m.), favor precision. Set the threshold to minimize expected cost.
Question 3Q: What does an AUC of 0.8 mean?Think it through, then reveal
A: A randomly chosen positive gets a higher score than a randomly chosen negative 80% of the time. It measures ranking quality across all thresholds; 0.5 is random.
Question 4Q: Which retrieval metric matters most for RAG, and why?Think it through, then reveal
A: Recall@k, where k is the number of chunks you pass to the model. If the right passage isn't in the top k, no prompt engineering can recover the answer. Add MRR or nDCG when position within the context matters.
Question 5Q: Why not grade LLM answers with BLEU or ROUGE?Think it through, then reveal
A: They measure surface overlap with one reference. A correct paraphrase scores low and a wrong answer that reuses the reference's words scores high. Use code-based checks where possible and a rubric-driven LLM judge otherwise, validated against human labels.
Question 6Q: Your LLM judge agrees with humans 90% of the time. Good enough?Think it through, then reveal
A: Not necessarily. If 90% of answers are "pass", a judge that always says "pass" also agrees 90%. Compute Cohen's kappa to correct for chance agreement.
Primary sources
The papers behind this lesson
Zheng et al., Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena (2023): Measured how well strong language models agree with human preferences when used as judges, and catalogued their biases (position, verbosity, self-preference).
Read on rumblr →The paperPapineni et al., BLEU: a Method for Automatic Evaluation of Machine Translation (2002): Introduced clipped n-gram precision with a brevity penalty.
The paperLin, ROUGE: A Package for Automatic Evaluation of Summaries (2004): Introduced recall-oriented overlap measures, including the LCS-based ROUGE-L.
The paperZhang et al., BERTScore: Evaluating Text Generation with BERT (2019): Compared candidate and reference by embedding similarity rather than exact word overlap.
The paperResearcher's shelf
Further reading
- scikit-learn, Metrics and scoring: https://scikit-learn.org/stable/modules/model_evaluation.html
- Receiver operating characteristic (Wikipedia): https://en.wikipedia.org/wiki/Receiver_operating_characteristic
- Discounted cumulative gain (Wikipedia): https://en.wikipedia.org/wiki/Discounted_cumulative_gain
- Cohen's kappa (Wikipedia): https://en.wikipedia.org/wiki/Cohen%27s_kappa
About this lesson. This is the illustrated edition of a lesson from the open-source AI Primer ↗. Its text, figures and numbers are generated from the Primer's source at commit f8b43d1, so the two always agree: the explanation, the code that builds it and the tests that prove it.