Random variables (discrete and continuous)
A random variable is a function that maps outcomes of a random experiment to real numbers. It's the bridge between the mesy real world (coin flips, pixel intensities, user clicks) and the clean mathematical world where we can calculate probabilities and expectations.
Why Random Variables Matter in ML
In machine learning, almost everything is a random variable:
- Features: pixel values, word counts, sensor readings
- Labels: class assignments, regression targets
- Model parameters: weights sampled during training
- Predictions: outputs with uncertainty
Understanding RVs lets you:
- Quantify uncertainty in data and predictions
- Design loss functions that match your data distribution
- Sample from complex distributions (GANs, VAEs)
- Reason about model behavior probabilistically
The Core Intuition
Example: Roll a die. The outcome space is {⚀, ⚁, ⚂, ⚃, ⚄, ⚅}. Define = "number of dots". Then maps⚂ →3, ⚅ → 6, etc. is the random variable, not the outcome "3" itself.
Discrete Random Variables
Probability Mass Function (PMF)
The probability mass function gives the probability that takes the exact value .
Requirements (these ensure it's a valid probability distribution):
- Non-negativity: for all
- Normalization: (all probabilities sum to 1)
Why these rules? Non-negativity: probabilities can't be negative. Normalization: something must happen, so total probability = 1.

From first principles:
- Imagine repeating the experiment times, getting values
- The sample average is
- Group identical values: if appears times, average =
- As , (frequency converges to probability)
- Therefore:
Why does each term get weighted by ? More probable values appear more often in the long run, so they contribute more to the average.
PMF:
Compact form: for
Why this form? When : . When : . It's a clever way to write both cases in one expression.
Expected value:
Why ? If you flip 100 times with , you expect about 30 heads. Average = 30/100 = 0.3 = .
ML Application: Binary classification labels (spam/not spam). Logistic regression outputs , which is literally for the Bernoulli RV "is this example class 1?".
Derivation of PMF:
- Probability of any specific sequence with heads: (heads occur times at prob , tails times at prob )
- Number of such sequences (orderings of heads in flips):
- Why multiplication? Each specific sequence has prob , and there are mutually exclusive sequences
Expected value:
Why? Each flip contributes expected value . With independent flips: .
ML Application: Dropout in neural networks. Each neuron independently "survives" with probability . Number of active neurons in a layer ~ Binomial.
Continuous Random Variables
Key difference from discrete: for any specific . Why? Infinitely many points, probability spread infinitely thin. We must ask about intervals: .
Probability Density Function (PDF)
The probability density function is NOT a probability—it's a density. Think of it as "probability per unit length".
Requirements:
- Non-negativity: for all
- Normalization:
Why integration? You're summing up infinitesimal probabilities over the interval. The "" converts density to probability: has units "probability per unit", has units "unit", product has units "probability".
Geometric intuition: Probability = area under the curve between and .
Why ? Same logic as discrete: weight each value by its "probability" (here, density × ).
PDF:
Why this form? Constant density in (equally likely). Must integrate to 1:
Expected value:
Simplify: , so:
Why is it the midpoint? Symmetry. Equally likely on both sides, average is the center.
ML Application: Weight initialization in neural networks often uses uniform distributions (e.g., Xavier/Glorot initialization samples from ).
PDF:
Parameters: (mean, location), (variance, spread)
Why this functional form? (Brief derivation from maximum entropy under mean/variance constraints—full proof is complex, but the intuition):
- We want the "most random" distribution given only knowledge of mean and variance
- Maximizing entropy subject to , yields this exponential-quadratic form
- The ensures
Why shape? It concentrates probability near the mean (), with tails dying off quickly. The quadratic in the exponent ensures smoothness and the "bell" shape.
Expected value: By symmetry around , .
ML Applications:
- Gaussian noise in data: , where
- Weight initialization (e.g., He initialization: )
- Gaussian processes, variational autoencoders (VAE latent space)
- Linear regression assumes
Cumulative Distribution Function (CDF)
Both discrete and continuous RVs have CDF .
For discrete RV: (sum up PMF values)
For continuous RV: (integrate PDF)
Why useful?
- CDFs always exist (PMFs/PDFs don't for mixed distributions)
- Easy to compute probabilities:
- Relationship to PDF: (PDF is the derivative of CDF)
ML Application: Percentile-based metrics, quantile regression, inverse transform sampling (generate samples from any distribution by applying to uniform random numbers).
Common Mistakes
Why it feels right: We're used to from discrete RVs.
The fix: is density, not probability. Units: is "probability per unit". means "locally, probability accumulates at 2 units per unit length". Actual probability: (which is small for small ).
Check: For , . But ✓
Why it feels right: Both use weighted averages.
The fix:
- Discrete:
E_X = sum(x * P(x) for x in values) - Continuous:
E_X = integrate(x * f(x), x, -inf, inf)orE_X = np.mean(samples)for samples
In NumPy, if you have samples from a continuous RV, use np.mean(samples), not sum(x * count(x)) (which assumes discrete).
Why it feels right: We measure "20°C" on thermometers.
The fix: Real measurements have finite precision. Your thermometer reads "20°C" = "between 19.5and 20.5°C" (an interval). For continuous RVs, always ask about intervals: .
Active Recall
Recall Explain Random Variables to a 12-Year-Old
Imagine you have a magic box that plays games. Sometimes it rolls dice, sometimes it spins a wheel. Every time you use the box, it gives you a number. That number is random (you don't know what it'll be before you use the box), but it follows rules.
A random variable is like a nickname for that number. We call it . If we roll a die, might be 1, 2, 3, 4, 5, or 6 (only these numbers, nothing in between). That's discrete—like counting candies.
But if we spin a wheel that can land anywhere from 0 to 100, could be 23.7145... or 99.001... or any number in between. That's continuous—like measuring how much water is in a cup.
For discrete, we can say "the probability of getting exactly 3 is..." (like for a fair die). For continuous, we can't say "exactly 23.7145" because there are infinite possibilities—it's like asking for one specific grain of sand on a beach. Instead, we ask "What's the probability is between 20 and 30?" and add up probabilities in that range (integration).
The cool part: in AI, almost everything is a random variable—what digit is in an image, what word comes next, whether an email is spam. Understanding RVs means understanding how AI handles uncertainty.
Memory Aids
- Discrete = Distinct, Discrete math (integers)
- PMF = Probability Mass (mass sits at points)
- PDF = Probability Density (spread like a fluid)
Alphabet trick:
- PMF uses Sum ()
- PDF uses Integral ()
- "S comes before I" → use sum for discrete, integral for continuous
Connections
- Probability Space – random variables are measurable functions on sample spaces
- Expectation and Variance – moments of random variables, higher-order statistics
- Joint and Conditional Distributions – multivariate RVs, dependence structure
- Central Limit Theorem – why sums converge to Gaussian
- Maximum Likelihood Estimation – fitting distributions to data (finding , , )
- Bernoulli and Categorical Distributions – classification labels as discrete RVs
- Gaussian Distribution – continuous RV for regression noise, VAE latents
- Sampling Methods – generating RV samples (inverse transform, rejection sampling)
- Information Theory – entropy of RV distributions
#flashcards/ai-ml
What is a random variable? :: A function that maps outcomes of a random experiment to real numbers. It's the bridge between real-world randomness and mathematical probability.
What is the difference between a discrete and continuous random variable?
What is a PMF and what are its two requirements?
What is a PDF and why is not a probability?
Why is for continuous random variables?
Expected value formula for discrete RV?
Expected value formula for continuous RV? :: . Weighted integral over the density.
Bernoulli distribution: PMF and expected value?
Binomial distribution: what does it model and what is ?
Uniform distribution on : PDF and expected value?
Gaussian distribution: PDF and parameters?
What is a CDF and how does it relate to PDF/PMF?
Why can a PDF have values greater than 1?
ML application of Bernoulli distribution?
ML application of Gaussian distribution?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Random variable samajhne ke liye ek simple idea hai: yeh ek function hai jo real-world randomness ko numbers mein convert karta hai. Jaise agarap ek coin flip karte ho, toh outcome "heads" ya "tails" hai. Lekin random variable X define karo: X=1 agar heads, X=0 agar tails. Ab ap mathematics use kar sakte ho — expectation, variance, probability — kyunki ab sab kuch numbers mein hai.
Do types hain: Discrete (jaise dice roll, class labels in AI — countable values) aur Continuous (jaise temperature, weight — any value in a range). Discrete mein aap directly bol sakte ho "P(X=3) = 1/6" (exact probability). Continuous mein aap exact point ka probability nahi mang sakte (always zero hota hai kyunki infinite points hain), ap sirf interval mang sakte ho: "P(20 ≤ X ≤ 25) kitna hai?" Iska answer PDF (probability density function) ko integrate karke milta hai — basically area under the curve.
Machine learning mein har chez random variable hai. Tumhara data (features, labels), model ke weights during training (randomly initialized), predictions (uncertainty ke sath). Gaussian distribution (bell curve) sabse common hai — noise modeling mein, weight initialization mein, VAEs mein. Bernoulli distribution binary classification ke liye (spam/not spam). Agar tum probability aur randomness handle karna seekh gaye, tum ML models ki behaviour ko deeply samajh paoge — kyunki end mein sab kuch uncertainty quantify karna hai