Bayesian Precision Calculator: Verifying the Confusion Matrix with Object Pascal

An Object Pascal script connects the confusion matrix and the Bayes formula, verifying in both directions the posterior-probability nature of precision.
The article introduces a Bayesian precision calculator implemented in Object Pascal in the maXbox environment. Starting from a specific confusion matrix (TP=6, FN=4, FP=8, TN=3), the script computes recall, false positive rate, and class prevalence, substitutes them into the Bayes formula to obtain precision, and compares it with the result from the traditional definition TP/(TP+FP)—the two are completely identical. The article points out that this equivalence is no coincidence but an algebraic necessity once the parameters are derived from the same confusion matrix. The script presents the four-step calculation process as structured logs, making it suitable for teaching, formula troubleshooting, and lightweight prototype verification, but it hard-codes a single set of values and does not support multi-class scenarios, so it cannot replace production-grade evaluation libraries.
Why Do We Need a Bayesian Precision Calculator?
In machine learning classification tasks, precision and recall are core metrics for evaluating model performance. But many people don't realize that precision itself implies a Bayesian inference process: it is actually computing the posterior probability that a sample is truly positive, given that it was predicted positive.
Recently an interesting practice appeared in the developer community: implementing a Bayesian precision conversion calculator using an Object Pascal script in the maXbox environment. The value of this tool isn't in how complex it is, but in how it connects the mathematical equivalence among the confusion matrix, the Bayes formula, and the definition of precision using runnable code.

The confusion matrix is a foundational tool for binary classification evaluation. It cross-partitions prediction results and true labels into four quadrants: true positives (TP, predicted positive and actually positive), false positives (FP, predicted positive but actually negative, i.e., false alarms), false negatives (FN, predicted negative but actually positive, i.e., missed detections), and true negatives (TN, predicted negative and actually negative). All common metrics such as precision, recall, F1 score, and accuracy can be derived from these four numbers. The value of the confusion matrix is that it preserves the full distribution of error types rather than compressing performance into a single scalar, which is also why it serves as the starting point for extracting Bayes formula parameters.
From Confusion Matrix to Bayesian Parameters: A Complete Derivation
The script sets up a specific confusion matrix scenario: true positives (TP) = 6, false negatives (FN) = 4, false positives (FP) = 8, true negatives (TN) = 3, for a total of 21 samples. Though these numbers are small, they are enough to make every step of the formula clear and verifiable.
Geometric Intuition of the Derived Parameters
The script first computes three key derived quantities:
- Recall = TP / (TP + FN) = 6/10 = 60.00%
- False Positive Rate (FPR) = FP / (FP + TN) = 8/11 ≈ 72.73%
- Class Prevalence = (TP + FN) / Total = 10/21 ≈ 47.62%
These three parameters respectively characterize the model's ability to capture positives, its tendency to falsely report negatives, and the class distribution of the data itself. They form the inputs to the Bayes formula.
Prevalence is borrowed from epidemiology; here it refers to the proportion of true positives in the dataset, i.e., (TP+FN)/Total. It characterizes the degree of class imbalance and is the prior probability P(positive) in Bayesian inference. When prevalence is very low, even if the model's false positive rate is not high, precision can be dragged down by a large number of false alarms—this is the root of the "precision paradox." Understanding prevalence helps explain why, in scenarios with extreme class imbalance (such as fraud detection or rare disease screening), high recall does not automatically bring high precision.
Step-by-Step Expansion of the Bayes Formula
The Bayes precision formula used in the script is:
Precision = (Recall × Prevalence) / [(Recall × Prevalence) + (FPR × (1 - Prevalence))]
Substituting the values, the numerator is (6/10) × (10/21) = 6/21 ≈ 0.2857, and the second term in the denominator is (8/11) × (11/21) = 8/21 ≈ 0.3810. Adding them gives 14/21 ≈ 0.6667. The final Bayesian precision is (6/21) / (14/21) = 6/14 ≈ 42.86%.
The Bayes formula has at its core the idea of updating posterior belief about an event using a prior probability and a likelihood function. In the context of precision, we care about P(true positive | predicted positive), which by Bayes' theorem can be expanded as: P(predicted positive | true positive) × P(true positive) / [P(predicted positive | true positive) × P(true positive) + P(predicted positive | true negative) × P(true negative)]. Here, P(predicted positive | true positive) is exactly recall, P(predicted positive | true negative) is exactly the false positive rate, and P(true positive) is prevalence. This mapping is the mathematical basis of the script's derivation and explains why precision is essentially a posterior probability.
Verification: Consistency Between the Bayesian Result and Empirical Precision
The most valuable design of this script is its "two-way verification": it not only computes precision using the Bayes formula but also directly computes empirical precision using the traditional definition TP / (TP + FP) = 6/14 ≈ 42.86%, then compares the two.
The results are completely identical. This is no coincidence—algebraically, when both Prevalence and FPR are derived from the same confusion matrix, the Bayes formula naturally simplifies to TP/(TP+FP). By outputting the numerator and denominator at each step (such as 6/21, 8/21, 14/21), the script makes this equivalence transparent and verifiable rather than a conclusion to be memorized.
Engineering Implementation: Object Pascal Script in maXbox
The script runs in the maXbox5/maXbox6 environment and uses Free Pascal-style syntax. The core function CalculateBayesPrecision performs all the calculations, while the main program section configures console output and fonts, then calls that function.
The output is organized into four clear sections: confusion matrix input, derived Bayesian parameters, step-by-step formula expansion, and precision result comparison. This structured logging is friendly for both teaching and debugging. It's worth noting that the script uses FormatFloat('0.00%', ...) to format percentages and appends the raw fraction form (such as [6/10]) after each line, making it easy for readers to check by hand.
maXbox is a lightweight Windows-oriented script development environment with a built-in Object Pascal (Free Pascal-compatible) interpreter, often used for rapid prototyping, teaching demonstrations, and small automation tools. It can run scripts without a full IDE and supports console output, string formatting, and file operations. Compared with Python, maXbox's advantages are fast startup and few dependencies, making it suitable for validating pure algorithmic logic; however, its ecosystem and library support are far inferior to Python, so it's reasonable to position the script in this article as teaching verification rather than a production tool.
Who Is This Tool For?
Judging from its positioning, this calculator leans more toward teaching and verification than toward being a performance evaluation tool in production environments. Its value is reflected at three levels:
- Conceptual alignment: Helping developers understand the often-overlooked perspective that "precision is a Bayesian posterior probability";
- Formula troubleshooting: When the Bayesian result and empirical result are inconsistent, it often means a parameter calculation is wrong, and the script's output can directly locate the problem;
- Lightweight prototyping: Quickly verifying confusion matrix numbers in a lightweight script environment like maXbox is more direct than opening a full Python environment.
Of course, the script's limitations are also obvious: it hard-codes a single set of values, has no input interface, and does not handle multi-class scenarios. For actual projects, the more common approach is to use scikit-learn's precision_score or confusion_matrix. But this Pascal script provides a rare "transparent box"—every line of calculation is visible, which is more enlightening for understanding the probabilistic logic behind the metrics than calling ready-made libraries.
Summary: Big Lessons in a Small Script
The Bayesian precision calculator itself is just a few dozen lines of Pascal code, but it demonstrates a good engineering habit: using minimally runnable code to align and verify the metric definition, the probability formula, and the empirical calculation. In an era when the machine learning toolchain is becoming increasingly black-box, this practice of "calculating it by hand once" remains one of the most reliable ways to understand model evaluation.
One-line Summary
An Object Pascal script connects the confusion matrix and the Bayes formula, verifying in both directions the posterior-probability nature of precision.
Paragraph Summary
The article introduces a Bayesian precision calculator implemented in Object Pascal in the maXbox environment. Starting from a specific confusion matrix (TP=6, FN=4, FP=8, TN=3), the script computes recall, false positive rate, and class prevalence, substitutes them into the Bayes formula to obtain precision, and compares it with the result from the traditional definition TP/(TP+FP)—the two are completely identical. The article points out that this equivalence is no coincidence but an algebraic necessity once the parameters are derived from the same confusion matrix. The script presents the four-step calculation process as structured logs, making it suitable for teaching, formula troubleshooting, and lightweight prototype verification, but it hard-codes a single set of values and does not support multi-class scenarios, so it cannot replace production-grade evaluation libraries.
Related articles

WorkBuddy Full Guide: How an AI Coworker Takes Over Workplace Automation
What is WorkBuddy? This article analyzes the core capabilities of this full-scenario AI agent: autonomous execution with deliverable results, local file processing, multi-agent parallelism, and workplace automation workflows, helping you judge its essential difference from ordinary AI chat tools.

The Truth About AI Agents: A Hundred Lines of Code + an LLM, Deconstructing the Agent's Underlying Logic
AI agents aren't a new AI species—just a language model plus tools, a loop, and a goal. Here's the underlying logic: how 100 lines of Python hit a 75% fix rate, why the harness beats the model, and Gartner's "agent washing."

AI Agent Deployment: Three Things Developers Should Do
AI Agent deployment doesn't require rushing into multi-agent systems. This article outlines an actionable deployment path and engineering acceptance thresholds for developers, covering human baselines, bounded single-agent scope, and a controlled code-modification loop.