Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 8: Continuous Random Variables

Definition and Contrast with Discrete Variables

In the previous chapters, we focused on discrete random variables, which take on a finite or countably infinite number of distinct values. Think about the number of heads in 10 coin flips (0, 1, ..., 10), the number of emails received in an hour (0, 1, 2, ...), or the outcome of a die roll ({1, 2, 3, 4, 5, 6}).

Now, we turn our attention to continuous random variables. These variables can take on any value within a given range or interval. Examples include:

The key difference lies in the nature of the sample space. For discrete variables, we can list the possible outcomes. For continuous variables, there are uncountably infinite possible values between any two given values.

Example: Measuring the exact height of a person results in a continuous variable, as height can theoretically take any value within a range (e.g., 1.5m, 1.51m, 1.511m, ...). In contrast, counting the number of people taller than 1.8m in a sample of 100 yields a discrete variable (0, 1, 2, ..., 100).

This fundamental difference means we can no longer talk about the probability of the variable taking on a specific value, like P(Height=1.80000... meters)P(\text{Height} = 1.80000... \text{ meters}). Because there are infinitely many possible heights, the probability of hitting any single exact value is essentially zero. Instead, for continuous variables, we talk about the probability that the variable falls within a certain interval, such as P(1.7m<Height≤1.8m)P(1.7\text{m} < \text{Height} \le 1.8\text{m}).

To describe the probability distribution of continuous random variables, we introduce two key functions: the Probability Density Function (PDF) and the Cumulative Distribution Function (CDF).

Probability Density Function (PDF)

For a continuous random variable XX, the Probability Density Function (PDF), denoted as fX(x)f_X(x), describes the relative likelihood for the random variable to take on a given value. Unlike the PMF for discrete variables, the PDF value fX(x)f_X(x) is not a probability itself. Instead, the area under the PDF curve over an interval corresponds to the probability that the variable falls within that interval.

Key properties of a PDF fX(x)f_X(x):

  1. Non-negativity: fX(x)≥0f_X(x) \ge 0 for all xx.

  2. Total area equals 1: ∫−∞∞fX(x)dx=1\int_{-\infty}^{\infty} f_X(x) dx = 1. (The total probability over the entire range of possible values is 1).

  3. Probability as Area: The probability that XX falls within an interval [a,b][a, b] is given by the integral of the PDF over that interval:

    P(a≤X≤b)=∫abfX(x)dxP(a \le X \le b) = \int_a^b f_X(x) dx

Important Note: For any continuous random variable XX and any specific value cc, P(X=c)=∫ccfX(x)dx=0P(X=c) = \int_c^c f_X(x) dx = 0. This implies P(a≤X≤b)=P(a<X≤b)=P(a≤X<b)=P(a<X<b)P(a \le X \le b) = P(a < X \le b) = P(a \le X < b) = P(a < X < b). Including or excluding the endpoints does not change the probability for continuous variables.

Example: Imagine the distribution of IQ scores in a large population, often modeled by a Normal distribution (a “bell curve”). The PDF would be highest around the average IQ (e.g., 100) and taper off towards very high or very low scores. The value of the PDF at x=110x=110 tells us the density of the distribution around that point, but the probability P(IQ=110)P(\text{IQ}=110) is 0. However, we can calculate the probability P(100≤IQ≤110)P(100 \le \text{IQ} \le 110) by finding the area under the bell curve between 100 and 110.

In Python, libraries like scipy.stats provide objects representing various continuous distributions, allowing us to evaluate their PDFs using the .pdf() method.

Cumulative Distribution Function (CDF)

The Cumulative Distribution Function (CDF), denoted as FX(x)F_X(x), gives the probability that the random variable XX takes on a value less than or equal to a specific value xx.

FX(x)=P(X≤x)F_X(x) = P(X \le x)

For a continuous random variable with PDF fX(t)f_X(t), the CDF is obtained by integrating the PDF from negative infinity up to xx:

FX(x)=∫−∞xfX(t)dtF_X(x) = \int_{-\infty}^{x} f_X(t) dt

Key properties of a CDF FX(x)F_X(x):

  1. FX(x)F_X(x) is a non-decreasing function of xx.

  2. lim⁡x→−∞FX(x)=0\lim_{x \to -\infty} F_X(x) = 0. (The probability of being less than or equal to a very small number is 0).

  3. lim⁡x→∞FX(x)=1\lim_{x \to \infty} F_X(x) = 1. (The probability of being less than or equal to a very large number is 1).

  4. The probability of XX falling in an interval (a,b](a, b] can be calculated using the CDF:

    P(a<X≤b)=FX(b)−FX(a)P(a < X \le b) = F_X(b) - F_X(a)
  5. The PDF can be obtained by differentiating the CDF (where the derivative exists): fX(x)=ddxFX(x)f_X(x) = \frac{d}{dx} F_X(x).

Example: For the IQ distribution, the CDF FX(110)F_X(110) would give the probability that a randomly selected person has an IQ score less than or equal to 110. This corresponds to the total area under the PDF curve to the left of x=110x=110.

scipy.stats objects also provide methods to evaluate the CDF using the .cdf() method.

Expected Value and Variance

Similar to discrete random variables, we can define measures of central tendency and spread for continuous variables using integration instead of summation.

Expected Value (Mean): The expected value or mean of a continuous random variable XX, denoted E[X]E[X] or μ\mu, represents the average value we would expect to observe over many repetitions of the experiment. It’s calculated by integrating the product of each possible value xx and its corresponding density fX(x)f_X(x) over the entire range:

E[X]=μ=∫−∞∞xfX(x)dxE[X] = \mu = \int_{-\infty}^{\infty} x f_X(x) dx

Think of it as the center of mass of the distribution.

Variance and Standard Deviation: The variance measures the spread or dispersion of the distribution around the mean. It’s the expected value of the squared deviation from the mean:

Var(X)=σ2=E[(X−μ)2]=∫−∞∞(x−μ)2fX(x)dxVar(X) = \sigma^2 = E[(X - \mu)^2] = \int_{-\infty}^{\infty} (x - \mu)^2 f_X(x) dx

An alternative computational formula is often more convenient:

Var(X)=E[X2]−(E[X])2Var(X) = E[X^2] - (E[X])^2

where E[X2]=∫−∞∞x2fX(x)dxE[X^2] = \int_{-\infty}^{\infty} x^2 f_X(x) dx is the second moment about the origin.

The Standard Deviation, σ=Var(X)\sigma = \sqrt{Var(X)}, provides a measure of spread in the original units of the random variable.

Example: For the IQ distribution (often modeled as Normal with mean 100 and standard deviation 15), E[IQ]=100E[\text{IQ}] = 100 and Var(IQ)=152=225Var(\text{IQ}) = 15^2 = 225. The standard deviation σ=15\sigma = 15 indicates the typical deviation of IQ scores from the average of 100.

scipy.stats distributions often have methods like .mean(), .var(), and .std() to compute these directly, or .expect() for more general expectations.

Percentiles and Quantiles

Percentiles and quantiles help us understand specific points in the distribution relative to the cumulative probability.

The pp-th percentile (where 0<p<10 < p < 1) is the value xpx_p such that the probability of the random variable being less than or equal to xpx_p is pp. In terms of the CDF:

FX(xp)=P(X≤xp)=pF_X(x_p) = P(X \le x_p) = p

The quantile function, often denoted Q(p)Q(p), is the inverse of the CDF: Q(p)=FX−1(p)=xpQ(p) = F_X^{-1}(p) = x_p. It tells you the value below which a certain proportion pp of the distribution lies.

Example: Finding the IQ score that marks the 95th percentile means finding the value x0.95x_{0.95} such that P(IQ≤x0.95)=0.95P(\text{IQ} \le x_{0.95}) = 0.95. For a Normal distribution with mean 100 and standard deviation 15, this value is approximately 124.7. This means 95% of the population has an IQ score of 124.7 or less.

scipy.stats provides the .ppf() (percent point function) method, which is the inverse of the CDF (quantile function).

Functions of a Continuous Random Variable

Often, we are interested in a new random variable YY that is a function of an existing continuous random variable XX. That is, Y=g(X)Y = g(X). How can we find the PDF or CDF of YY?

Method 1: Using the CDF

  1. Find the CDF of YY: FY(y)=P(Y≤y)=P(g(X)≤y)F_Y(y) = P(Y \le y) = P(g(X) \le y).

  2. Express the event {g(X)≤y}\{g(X) \le y\} in terms of XX. This might involve solving the inequality for XX. Be careful if g(x)g(x) is not monotonically increasing (i.e., if it goes up and down).

  3. Calculate P(event in terms of X)P(\text{event in terms of } X) using the CDF of XX, FX(x)F_X(x).

  4. Differentiate the resulting FY(y)F_Y(y) with respect to yy to find the PDF of YY: fY(y)=ddyFY(y)f_Y(y) = \frac{d}{dy} F_Y(y).

Method 2: Change of Variables Formula (for monotonic functions) If y=g(x)y = g(x) is a strictly monotonic (always increasing or always decreasing) differentiable function over the range of XX, then it has an inverse function x=g−1(y)x = g^{-1}(y). The PDF of YY is given by:

fY(y)=fX(g−1(y))∣dxdy∣f_Y(y) = f_X(g^{-1}(y)) \left| \frac{dx}{dy} \right|

where dxdy\frac{dx}{dy} is the derivative of the inverse function x=g−1(y)x = g^{-1}(y) with respect to yy. The absolute value ensures the PDF remains non-negative.

Example: Suppose the temperature TT in Celsius is a random variable, perhaps uniformly distributed between 15°C and 25°C. We want to find the distribution of the temperature in Fahrenheit, F=1.8T+32F = 1.8T + 32. Here, g(T)=1.8T+32g(T) = 1.8T + 32. This is strictly increasing. The inverse function is T=g−1(F)=(F−32)/1.8T = g^{-1}(F) = (F - 32) / 1.8. The derivative is dTdF=1/1.8\frac{dT}{dF} = 1 / 1.8. If the PDF of T is fT(t)=125−15=0.1f_T(t) = \frac{1}{25-15} = 0.1 for 15≤t≤2515 \le t \le 25 (and 0 otherwise), then the PDF of FF is:

fF(f)=fT(f−321.8)∣11.8∣f_F(f) = f_T\left(\frac{f - 32}{1.8}\right) \left| \frac{1}{1.8} \right|

We need to find the range for F. When T=15T=15, F=1.8(15)+32=59F = 1.8(15) + 32 = 59. When T=25T=25, F=1.8(25)+32=77F = 1.8(25) + 32 = 77. So, for 59≤f≤7759 \le f \le 77:

fF(f)=(0.1)×∣11.8∣=0.11.8=118f_F(f) = (0.1) \times \left| \frac{1}{1.8} \right| = \frac{0.1}{1.8} = \frac{1}{18}

Thus, F is uniformly distributed between 59°F and 77°F.

Calculating the expected value of a function Y=g(X)Y=g(X) can often be done more directly without finding the full distribution of Y, using the Law of the Unconscious Statistician (LOTUS):

E[Y]=E[g(X)]=∫−∞∞g(x)fX(x)dxE[Y] = E[g(X)] = \int_{-\infty}^{\infty} g(x) f_X(x) dx

Hands-on: Working with Continuous Variables in Python

Let’s put these concepts into practice using Python libraries. We’ll simulate heights assuming a Normal distribution and compare the histogram to the theoretical PDF, as well as perform calculations using scipy.stats and scipy.integrate.

1. Defining PDFs and CDFs with scipy.stats

scipy.stats provides convenient objects for many standard continuous distributions (we’ll explore these in Chapter 9). Let’s use the Normal distribution as an example again. Suppose adult male heights (in cm) are Normally distributed with a mean (μ\mu) of 177 cm and a standard deviation (σ\sigma) of 7 cm.

<Figure size 1000x600 with 1 Axes>
<Figure size 1000x600 with 1 Axes>

2. Calculating Probabilities using PDF and CDF

We want to calculate the probability of a height falling within a specific range, e.g., P(170 cm < Height ≤ 185 cm).

Method 1: Using the CDF P(170<X≤185)=FX(185)−FX(170)P(170 < X \le 185) = F_X(185) - F_X(170)

P(170 < Height <= 185) using CDF: 0.7148
<Figure size 1000x600 with 1 Axes>

Method 2: Using Numerical Integration of the PDF We need to calculate ∫170185fX(x)dx\int_{170}^{185} f_X(x) dx. We can use scipy.integrate.quad (which performs quadrature, a numerical integration method).

P(170 < Height <= 185) using PDF integration: 0.7148
Estimated integration error: 7.94e-15

As expected, both methods give essentially the same result (within numerical precision). Using the CDF is generally more direct and computationally efficient when available. Integration is necessary if you only have the PDF function defined (e.g., a custom, non-standard distribution).

3. Calculating Expected Value, Variance, and Percentiles

scipy.stats objects have built-in methods for these common statistics.

Expected Height E[X]: 177.00 cm
Variance Var(X): 49.00 cm^2
Standard Deviation std(X): 7.00 cm
25th Percentile (Q1) of Height: 172.28 cm
Median Height (Q2): 177.00 cm
75th Percentile (Q3) of Height: 181.72 cm
95th Percentile of Height: 188.51 cm

Calculating Moments using Numerical Integration

We can also calculate moments like E[X]E[X] and E[X2]E[X^2] using integrate.quad and the definitions: E[X]=∫−∞∞xfX(x)dxE[X] = \int_{-\infty}^{\infty} x f_X(x) dx E[X2]=∫−∞∞x2fX(x)dxE[X^2] = \int_{-\infty}^{\infty} x^2 f_X(x) dx

We integrate over a wide practical range instead of (−∞,∞)(-\infty, \infty), e.g., μ±10σ\mu \pm 10\sigma.

E[X] via integration: 177.00
E[X^2] via integration: 31378.00
Var(X) via integration (E[X^2] - E[X]^2): 49.00

The results from numerical integration closely match the built-in methods, confirming our understanding of the definitions.

4. Simulating Data and Comparing Empirical vs. Theoretical

We can simulate drawing random samples from the distribution using the .rvs() method (Random VariateS) and compare the results (like the histogram and sample statistics) to the theoretical distribution. This is a fundamental technique in Monte Carlo methods (Chapter 18).

Theoretical Mean: 177.00, Empirical Mean (from 10000 samples): 176.99
Theoretical Std Dev: 7.00, Empirical Std Dev (from 10000 samples): 7.02
<Figure size 1000x600 with 1 Axes>

As the plot shows, the histogram of the randomly generated samples closely follows the shape of the theoretical Probability Density Function. Furthermore, the calculated sample mean and standard deviation are very close to the theoretical parameters (μ=177\mu=177, σ=7\sigma=7) of the distribution we sampled from.

According to the Law of Large Numbers (Chapter 14), as the number of samples (num_samples) increases, the empirical histogram will better approximate the true PDF, and the sample statistics (like mean and standard deviation) will converge to the true parameters of the distribution.

This hands-on section demonstrated how to work with the core concepts of continuous random variables – PDFs, CDFs, expectations, percentiles – using scipy.stats. We also saw how numerical integration can be used for calculations and how simulation allows us to generate data that follows a specific distribution, enabling comparison between empirical results and theoretical models.

In the next chapter, we will delve into specific, commonly encountered continuous distributions like the Uniform, Exponential, and the Normal distribution in more detail, exploring their properties and applications.