Random Variables and Distributions
A random variable assigns a number to an outcome. The number of alternate reads at a site is one example. Its distribution describes possible values and their probabilities before the reads are observed.
Read probability first. A distribution is a model for variation, so its assumptions belong beside its formula.
Expectation and variance
Section titled “Expectation and variance”For a discrete random variable X, its expectation is the sum of each possible value multiplied by its probability. Write this as E[X] = Σ x*P(X=x). The expectation need not be an integer even when every possible observation is an integer.
Variance measures expected squared distance from the mean. Its definition is Var(X) = E[(X-E[X])**2]. Standard deviation is the square root of variance and has the same units as the measurement.
import numpy as np
values = np.array([0.0, 1.0, 2.0])probabilities = np.array([0.2, 0.5, 0.3])mean = np.sum(values * probabilities)variance = np.sum((values - mean) ** 2 * probabilities)print(f"Expectation: {mean:.3f}")print(f"Variance: {variance:.3f}")print(f"Standard deviation: {np.sqrt(variance):.3f}")assert np.isclose(probabilities.sum(), 1.0)Expectation: 1.100Variance: 0.490Standard deviation: 0.700The distribution above is specified exactly. Estimating variance from a sample is a different calculation. NumPy uses a denominator of n by default; ddof=1 gives the usual sample variance with denominator n-1.
Choose a distribution from the experiment
Section titled “Choose a distribution from the experiment”| Distribution | Mathematical setting | Example and assumption |
|---|---|---|
| Bernoulli | One binary outcome | Whether one independent read supports an alternate allele |
| Binomial | A fixed number of independent Bernoulli trials with the same probability | Alternate read count at fixed depth |
| Poisson | A count with a specified mean and equal variance | Events under a constant-rate Poisson process |
| Negative binomial | A count with variance larger than its mean | A model for RNA sequencing counts across biological samples |
| Normal | A continuous, symmetric distribution | Measurement errors when the normal approximation is justified |
A normal distribution is continuous. Probability comes from area under its density across an interval, not the density height at one point.
Compare two count models
Section titled “Compare two count models”A probability mass function assigns probability to each possible discrete value. Those probabilities sum to one over the full support. A probability density instead describes how probability accumulates along a continuous axis. Its integral over the full support equals one.
For a continuous variable, the probability of one exact value is zero even when the density at that value is positive. A measured value usually represents a finite measurement interval because instruments have finite precision. That interval can have positive probability.
A cumulative distribution function gives P(X <= x). For a count variable, “at least k” is the complement of “at most k-1.” This explains the use of sf(7) for the event “at least 8” in the example below.
A Poisson distribution has variance equal to its mean. One negative binomial parameterization has mean mu and variance mu + mu**2 / size. Smaller positive size produces more dispersion at a fixed mean.
from scipy.stats import binom, nbinom, poisson
depth = 20allele_probability = 0.25print(f"Expected alternate reads: {binom.mean(depth, allele_probability):.3f}")print(f"P(at least 8): {binom.sf(7, depth, allele_probability):.4f}")mu = 10.0size = 5.0success_probability = size / (size + mu)print(f"Poisson variance: {poisson.var(mu):.3f}")count_variance = nbinom.var(size, success_probability)print(f"Negative binomial variance: {count_variance:.3f}")Expected alternate reads: 5.000P(at least 8): 0.1018Poisson variance: 10.000Negative binomial variance: 30.000SciPy’s negative binomial uses size and a success probability. Those are not the mean and dispersion parameters used by every biological package. Translate the parameterization before comparing results.
from pathlib import Path
import matplotlib.pyplot as plt
counts = np.arange(41)fig, ax = plt.subplots(figsize=(7, 3.8))ax.plot(counts, poisson.pmf(counts, mu), "o-", label="Poisson")ax.plot(counts, nbinom.pmf(counts, size, success_probability), "o-", label="Negative binomial")ax.set(xlabel="Count", ylabel="Probability")ax.legend()fig.tight_layout()Path("outputs").mkdir(exist_ok=True)fig.savefig("outputs/count_distributions.png", dpi=160)plt.close(fig)
Covariance connects variables
Section titled “Covariance connects variables”Covariance measures whether two variables tend to depart from their means together. Positive covariance indicates aligned departures. Its magnitude depends on measurement units. Correlation divides covariance by both standard deviations.
measurements = np.array([[1.0, 2.0], [2.0, 4.0], [3.0, 5.0], [4.0, 7.0]])print("Sample covariance:")print(np.round(np.cov(measurements, rowvar=False, ddof=1), 3))Sample covariance:[[1.667 2.667] [2.667 4.333]]Here rows are samples and columns are variables. A covariance matrix places variances on its diagonal and pairwise covariances elsewhere. PCA will use this structure. Zero covariance does not generally imply independence.
Variation and uncertainty in a mean
Section titled “Variation and uncertainty in a mean”Standard deviation describes the spread of individual observations. Standard error describes the spread of an estimator across repeated samples. These answer different questions even though both can appear as error bars.
For independent observations with common variance sigma**2, the sample mean has variance sigma**2/n. Its standard error is therefore sigma/sqrt(n). Correlated observations do not generally follow that formula. Repeated measurements from one patient cannot automatically replace independent patients in the denominator.
Check your understanding
Section titled “Check your understanding”If every measurement is multiplied by a positive constant c, how do its mean and variance change?
The mean is multiplied by c. Each deviation from the mean is also multiplied by c, so its square is multiplied by c**2. The variance therefore changes by c**2, while the standard deviation changes by c. This is why variance carries squared measurement units.
Further reading
Section titled “Further reading”-
The reference defines probability masses, cumulative probabilities, and distribution moments. Compare the trial count and success probability with the observed-count example.
-
MIT Introduction to Probability and Statistics
The course develops random variables, expectation, and probability distributions. Its exercises connect the discrete calculations here with broader probability models.
See SciPy’s binomial reference for its probability functions. Continue with calculus.