Why This Matters
Software does not live in a world of certainty. Will a user click this button? Is this email spam? Will the server respond in under 100ms? Every one of these questions is a probability question. Probability is the math of uncertainty, and it is the foundation of machine learning, A/B testing, security, and data-driven decision making.
Conditional probability asks: given that something has already happened, how does that change the odds? And Bayes' theorem lets you flip conditional probabilities around -- going from "what is the probability of a positive test given you are sick" to "what is the probability you are sick given a positive test." This reasoning pattern powers spam filters, medical diagnostics, and Bayesian machine learning.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Probability quantifies uncertainty. Bayes' theorem lets you update beliefs with new evidence.
Code Example
// Basic probability: favorable / total
function probability(favorable, total) {
return favorable / total;
}
// Rolling a 6-sided die
const pEven = probability(3, 6); // {2,4,6} out of {1..6}
console.log("P(even):", pEven); // 0.5
// Independent events: P(A and B) = P(A) * P(B)
const pTwoHeads = 0.5 * 0.5;
console.log("P(2 heads):", pTwoHeads); // 0.25
// Simulate probability with random trials
function simulate(trials, eventFn) {
let successes = 0;
for (let i = 0; i < trials; i++) {
if (eventFn()) successes++;
}
return successes / trials;
}
const simEven = simulate(10000, () => {
const die = Math.floor(Math.random() * 6) + 1;
return die % 2 === 0;
});
console.log("Simulated P(even):", simEven.toFixed(3)); // ~0.500
// Bayes Theorem
// P(sick | positive) = P(positive | sick) * P(sick) / P(positive)
function bayes(pBA, pA, pB) {
return (pBA * pA) / pB;
}
const pSick = 0.01; // 1% of people are sick
const pPosGivenSick = 0.99; // 99% true positive rate
const pPosGivenHealthy = 0.05; // 5% false positive rate
const pPos = pPosGivenSick * pSick + pPosGivenHealthy * (1 - pSick);
const pSickGivenPos = bayes(pPosGivenSick, pSick, pPos);
console.log("P(sick|positive):", pSickGivenPos.toFixed(3)); // ~0.167Interactive Experiment
Try these exercises:
- Roll a die 1000 times in code. How close is the simulated probability of rolling a 6 to the theoretical 1/6?
- Flip two coins 10000 times. How often are both heads? Does the simulation match 0.25?
- If P(rain) = 0.3 and P(umbrella | rain) = 0.9, what is P(rain AND umbrella)?
- A disease affects 1 in 1000 people. A test is 95% accurate. If you test positive, what is the actual probability you have the disease? (Hint: use Bayes.)
- Why is the answer to the previous question surprisingly low?
Quick Quiz
Coding Challenge
Write a function called `estimatePi` that uses a Monte Carlo simulation to estimate the value of Pi. Generate `n` random points with x and y in the range [0, 1). Count how many fall inside a quarter circle (x^2 + y^2 < 1). Return 4 * (inside / n). With n=100000 you should get a value close to 3.14.
Real-World Usage
Probability is the backbone of data-driven software:
- A/B testing: "Is variant B actually better, or was it luck?" Statistical significance tests use probability to answer this.
- Spam filters: Naive Bayes classifiers compute P(spam | words in email) using Bayes' theorem to classify messages.
- Machine learning: Training a model means finding parameters that maximize the probability of the observed data.
- Reliability engineering: "What is the probability all 5 servers fail at once?" drives redundancy decisions.
- Cryptography: Security depends on the probability of guessing a key being astronomically low.