Why This Matters
Every prediction, every risk assessment, and every machine learning model rests on probability axioms. When a weather app says there is a 30% chance of rain, when a spam filter flags an email, or when a self-driving car decides whether to brake — probability is the engine underneath. Without a rigorous foundation, you cannot reason about uncertainty, and uncertainty is everywhere in computing.
A sample space is the set of all possible outcomes of an experiment. An event is a subset of that sample space — the outcomes you care about. The probability of an event is a number between 0 and 1 that measures how likely it is. These three ideas — sample space, event, and the axioms governing probability values — are the bedrock of everything from A/B testing to neural network training.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Probability starts with a sample space, defines events as subsets, and assigns probabilities following three axioms.
Code Example
// Sample space for a fair die
const sampleSpace = [1, 2, 3, 4, 5, 6];
// Probability of an event
function probability(event, space) {
return event.length / space.length;
}
// Event A: rolling an even number
const eventA = sampleSpace.filter(x => x % 2 === 0);
console.log("P(even):", probability(eventA, sampleSpace)); // 0.5
// Event B: rolling greater than 4
const eventB = sampleSpace.filter(x => x > 4);
console.log("P(>4):", probability(eventB, sampleSpace)); // 0.333...
// Intersection: A AND B
const intersection = eventA.filter(x => eventB.includes(x));
console.log("A and B:", intersection); // [6]
console.log("P(A and B):", probability(intersection, sampleSpace)); // 0.167
// Union: A OR B (inclusion-exclusion)
const pUnion = probability(eventA, sampleSpace)
+ probability(eventB, sampleSpace)
- probability(intersection, sampleSpace);
console.log("P(A or B):", pUnion.toFixed(4)); // 0.6667
// Complement: P(not A) = 1 - P(A)
console.log("P(not even):", 1 - probability(eventA, sampleSpace)); // 0.5
// Simulate: flip coin 10000 times
let heads = 0;
for (let i = 0; i < 10000; i++) {
if (Math.random() < 0.5) heads++;
}
console.log("Simulated P(heads):", (heads / 10000).toFixed(3));Interactive Experiment
Try these exercises:
- Write out the sample space for flipping two coins. How many outcomes are there? What is P(at least one head)?
- For a standard deck of 52 cards, compute P(drawing an Ace) and P(drawing a Heart).
- Verify inclusion-exclusion: P(Ace OR Heart) = P(Ace) + P(Heart) - P(Ace of Hearts). What do you get?
- Simulate rolling two dice 10,000 times. What fraction sum to 7? Compare to the theoretical 6/36 = 1/6.
- What is P(empty set)? What is P(sample space)? Why must these always be 0 and 1?
Quick Quiz
Coding Challenge
Write a function `eventProbability(eventSize, spaceSize)` that returns the probability of an event given the number of favorable outcomes and total outcomes. Also write a function `unionProbability(pA, pB, pIntersection)` that returns P(A or B) using the inclusion-exclusion principle. Both should return numbers (not strings).
Real-World Usage
Probability foundations appear throughout technology and science:
- A/B testing: Tech companies compute the probability of observed conversion differences to decide whether a new feature is genuinely better or just random noise.
- Spam filtering: Email classifiers estimate P(spam | words in email) using probability theory to decide which messages to block.
- Game design: Loot drop rates, critical hit chances, and matchmaking systems all rely on carefully tuned probability distributions.
- Reliability engineering: P(server failure) determines how many redundant servers you need. If P(failure) = 0.01, two independent servers give P(both fail) = 0.0001.
- Insurance and finance: Risk models, option pricing, and actuarial tables are built on probability axioms.