The prior probability of the event occurring in the population (P(A)).
Probability the test is positive given the event is present (P(B|A)).
Probability the test is negative given the event is NOT present (P(not B|not A)).
function calculateBayesianProbability() {
// Get input elements matching IDs exactly
var baseRateInput = document.getElementById("baseRate");
var sensitivityInput = document.getElementById("sensitivity");
var specificityInput = document.getElementById("specificity");
var resultDiv = document.getElementById("result");
// Parse values
var baseRateVal = parseFloat(baseRateInput.value);
var sensitivityVal = parseFloat(sensitivityInput.value);
var specificityVal = parseFloat(specificityInput.value);
// Validation
if (isNaN(baseRateVal) || isNaN(sensitivityVal) || isNaN(specificityVal)) {
resultDiv.style.display = "block";
resultDiv.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (baseRateVal 100 || sensitivityVal 100 || specificityVal 100) {
resultDiv.style.display = "block";
resultDiv.innerHTML = "Values must be between 0 and 100.";
return;
}
// Convert Percentages to Decimals
var p_A = baseRateVal / 100; // Prior (Prevalence)
var p_B_given_A = sensitivityVal / 100; // True Positive Rate
var p_notB_given_notA = specificityVal / 100; // True Negative Rate
// Calculate False Positive Rate: P(B|not A) = 1 – Specificity
var p_B_given_notA = 1 – p_notB_given_notA;
// Bayes Theorem Formula: P(A|B) = (P(B|A) * P(A)) / [ (P(B|A) * P(A)) + (P(B|not A) * P(not A)) ]
// Numerator: True Positives
var numerator = p_B_given_A * p_A;
// Denominator: Total Positives (True Positives + False Positives)
var denominator = numerator + (p_B_given_notA * (1 – p_A));
// Result: Posterior Probability
var posteriorProbability = 0;
if (denominator > 0) {
posteriorProbability = (numerator / denominator) * 100;
}
// Formatting result
var finalResult = posteriorProbability.toFixed(2);
// Natural Frequencies Logic (for 1000 people)
var population = 1000;
var withCondition = Math.round(population * p_A);
var withoutCondition = population – withCondition;
var truePositives = Math.round(withCondition * p_B_given_A);
var falsePositives = Math.round(withoutCondition * p_B_given_notA);
var totalPositives = truePositives + falsePositives;
// Display Logic
resultDiv.style.display = "block";
resultDiv.innerHTML =
'
' + finalResult + '%
' +
'
Posterior Probability (Positive Predictive Value):' +
'If a test result is positive, there is a ' + finalResult + '% chance the event is actually present.
' +
'
' +
'In a population of ' + population + ' people:' +
'• ' + withCondition + ' would have the condition.' +
'• ' + truePositives + ' would test positive correctly.' +
'• ' + falsePositives + ' would test positive falsely.' +
'Total positive tests: ' + totalPositives + '. (' + truePositives + ' / ' + totalPositives + ' = ' + finalResult + '%)' +
'
';
}
Understanding the Base Rate Calculation Formula
The Base Rate Calculation Formula (often associated with Bayes' Theorem) is a critical mathematical tool used to determine the actual probability of an event occurring given specific evidence, such as a positive test result. In statistics, this calculation corrects for the "Base Rate Fallacy"—the tendency to ignore the general prevalence of an event (the base rate) in favor of specific information (like a test accuracy).
This calculator is essential for professionals in data science, medicine, and quality assurance who need to convert sensitivity and specificity metrics into a real-world probability (Positive Predictive Value).
The Mathematical Formula
The logic behind the base rate calculation utilizes Bayesian inference. To calculate the posterior probability $P(A|B)$ (the probability that condition A is true given that test result B is positive), we use the following formula:
P(A|B) = (Sensitivity × Base Rate) / [ (Sensitivity × Base Rate) + (False Positive Rate × (1 – Base Rate)) ]
Input Definitions
Base Rate (Prevalence): The percentage of the total population that actually has the condition or attribute before any testing is done. This is the "prior" probability.
Sensitivity (True Positive Rate): The ability of the test to correctly identify those with the condition. If 100 people have the disease and the test catches 99 of them, sensitivity is 99%.
Specificity (True Negative Rate): The ability of the test to correctly identify those without the condition. If specificity is low, the False Positive Rate increases, which drastically lowers the reliability of the result.
Real-World Example: The Base Rate Fallacy
Why is this calculation important? Consider a scenario often used in medical diagnostics:
Base Rate: 1% (Only 1 in 100 people have the disease).
Sensitivity: 99% (The test is very good at finding the disease).
Specificity: 90% (The test has a 10% false positive rate).
Intuitively, if you test positive, you might think you are 99% likely to have the disease. However, using the base rate calculation formula, the actual probability is only about 9%.
This happens because in a population of 1,000, only 10 people have the disease (1%), but 99 healthy people (10% of 990) will test positive falsely. The false positives drown out the true positives because the base rate is so low.
Applications
While commonly used in medicine, this formula applies to various fields:
Spam Filtering: Calculating the probability an email is spam given it contains a certain keyword, factoring in how common spam is overall.
Quality Control: Determining the likelihood a product is defective given a failed automated test.
Algorithmic Fairness: Assessing the probability of correct classification in AI models across different population demographics.