Bayesian Calculator
This Object Pascal script calculates the Bayesian precision conversion for the TP = 6, FN = 4, FP = 8, TN = 3 scenario (Total Population = 21) directly in maXbox, logging both the derived intermediate parameters and veri
This Object Pascal script calculates the Bayesian precision conversion for the TP = 6, FN = 4, FP = 8, TN = 3 scenario (Total Population = 21) directly in maXbox, logging both the derived intermediate parameters and verifying that the Bayesian posterior calculation matches the empirical precision.
// maXbox5 / maXbox6 Script: Bayesian Precision Conversion Calculator
// Demonstrates how Bayes' Theorem calculates Precision from Recall, FPR, and Prevalence Tutor: 3
program Bayes_Precision_Conversion;
var
TP, FN, FP, TN, Total: Double;
Recall, FPR, Prevalence, OneMinusPrev: Double;
BayesNum, BayesDenom, BayesPrecision: Double;
EmpiricalPrecision: Double;
procedure CalculateBayesPrecision;
begin
// Given Confusion Matrix Counts: TP = 6, FN = 4, FP = 8, TN = 3
TP:= 6.0;
FN:= 4.0;
FP:= 8.0;
TN:= 3.0;
Total:= TP + FN + FP + TN; // 21
// 1. Derived Bayesian Parameters
Recall:= TP / (TP + FN); // 6 / 10 = 0.6000 (60.00%)
FPR:= FP / (FP + TN); // 8 / 11 = 0.7273 (72.73%)
Prevalence:= (TP + FN) / Total; // 10 / 21 = 0.4762 (47.62%)
OneMinusPrev:= 1.0 - Prevalence; // 11 / 21 = 0.5238 (52.38%)
// 2. Bayesian Precision Formula:
// Precision = (Recall * Prevalence) / [ (Recall * Prevalence) + (FPR * (1 - Prevalence)) ]
BayesNum:= Recall * Prevalence; // (6/10) * (10/21) = 6/21 = 0.2857
BayesDenom:= BayesNum + (FPR * OneMinusPrev); // (6/21) + (8/21) = 14/21 = 0.6667
BayesPrecision:= (BayesNum / BayesDenom) * 100.0; // (6/14) * 100 = 42.86%
// 3. Empirical Precision Verification: TP / (TP + FP)
EmpiricalPrecision:= (TP / (TP + FP)) * 100.0; // 6 / 14 = 42.86%
// 4. Output Results to Console
Writeln('================================================================');
Writeln(' BAYESIAN PRECISION CONVERSION CALCULATOR (maXbox)');
Writeln('================================================================');
Writeln('1. CONFUSION MATRIX INPUTS:');
Writeln(' True Positives (TP) : ' + FloatToStr(TP));
Writeln(' False Negatives (FN) : ' + FloatToStr(FN));
Writeln(' False Positives (FP) : ' + FloatToStr(FP));
Writeln(' True Negatives (TN) : ' + FloatToStr(TN));
Writeln(' Total Population : ' + FloatToStr(Total));
Writeln('');
Writeln('2. DERIVED BAYESIAN PARAMETERS:');
Writeln(' Recall (TPR) : ' + FormatFloat('0.00%', Recall * 100.0) + ' [6/10]');
Writeln(' False Positive Rate : ' + FormatFloat('0.00%', FPR * 100.0) + ' [8/11]');
Writeln(' Class Prevalence : ' + FormatFloat('0.00%', Prevalence * 100.0) + ' [10/21]');
Writeln(' 1 - Prevalence : ' + FormatFloat('0.00%', OneMinusPrev * 100.0) + ' [11/21]');
Writeln('');
Writeln('3. BAYESIAN FORMULA STEP-BY-STEP:');
Writeln(' Numerator (Rec * Prev) : ' + FormatFloat('0.0000', BayesNum) + ' [6/21]');
Writeln(' Denominator : ' + FormatFloat('0.0000', BayesDenom) + ' [14/21]');
Writeln('');
Writeln('4. PRECISION RESULT COMPARISON:');
Writeln(' Bayesian Precision : '+ FormatFloat('0.00%', BayesPrecision) +' [3/7 = 42.86%]');
Writeln(' Empirical Precision : '+ FormatFloat('0.00%', EmpiricalPrecision)+' [6/14 = 42.86%]');
Writeln('================================================================');
end;
begin //@main
setDebugOff;
maxform1.setconsole; //setConsole
memo2.font.name:= 'Courier';
memo2.font.size:= 10;
CalculateBayesPrecision;
end.
📰 Read the original article on Dev.to AI
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.
