Basics
| Rule | Formula | Condition |
|---|---|---|
| Complement | always | |
| Independent events | independent | |
| Mutually exclusive | disjoint | |
| General addition | always | |
| Conditional |
Bayes’ Theorem
Mathematical Proof
By the definition of conditional probability, and also , since intersection is symmetric. Setting these two expressions equal and dividing through by isolates , giving Bayes’ Theorem.
Used to “flip” a conditional probability when the reverse direction is easier to measure or reason about — e.g. given the probability of a symptom given a disease, find the probability of a disease given a symptom.
Law of Total Probability
If partition the sample space (mutually exclusive, collectively exhaustive),
This is typically paired with Bayes’ Theorem: expand the denominator in Bayes’ formula by conditioning on every way could have occurred.
Expected Value
For a random variable taking value with probability :
Linearity of Expectation
Holds for any — independent or not. This is the single most useful tool in probabilistic counting: split a complicated sum into simple, independently-computable pieces.
Indicator-Variable Trick
To compute the expectation of a complicated count, write it as a sum of indicator variables, one per event being counted:
This avoids reasoning about dependence between events.
Variance
For independent :
Unlike expectation, variance is not linear in general — the independence condition is required, since in the general case .
Common Distributions
| Distribution | Notes | ||
|---|---|---|---|
| Bernoulli() | single trial, success probability | ||
| Binomial() | sum of independent Bernoulli() trials | ||
| Geometric() | number of trials until the first success |
Probability / Expectation DP
State captures “where we are” in a process; the DP value is either a probability of reaching that state, or an expected value computed backward from terminal states.
Probability form (forward):
Process states in an order that finalizes before it updates any (e.g. decreasing problem size).
Expectation form (backward from terminal states):
Solved by processing states in dependency order, or — when transitions form cycles — by setting up and solving a linear system (e.g. via Gaussian elimination).
Random Walks & Markov Chains
A Markov chain models a sequence of states where the probability of moving to the next state depends only on the current state, not the history:
Collecting the into a transition matrix , the distribution after steps from an initial distribution is
which can be computed in with Matrix Exponentiation when is large ( = number of states). A classic special case is the 1D random walk: from position , move to with probability and to with probability ; expected hitting times and absorption probabilities are typically found by setting up the expectation-DP recurrence above and solving the resulting linear system.
Worked Example: Coupon Collector
Expected number of dice rolls needed to see all distinct faces at least once:
double couponCollectorExpectation(int n) {
double harmonic = 0;
for (int i = 1; i <= n; i++)
harmonic += 1.0 / i;
return n * harmonic;
}Algorithm:
- Split the process into phases, where phase begins right after collecting the -th distinct face.
- In phase , each roll succeeds (reveals a new face) with probability , so by the Geometric distribution above the expected length of phase is .
- By linearity of expectation, sum the phase lengths to get .
Time Complexity:
Space Complexity: