⚡0 XP🔥0

Random Variables and Distributions

Lesson 2 of 5 ⏱ 5 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Python

Discrete and continuous random variables, expected value and variance, the binomial and Poisson distributions, the normal distribution, z-scores and the 68-95-99.7 rule, with Python.

Random variables#

A random variable gives a number to each outcome of a random experiment.

Type Takes Examples
discrete countable values number of heads, orders per day, defective items in a box
continuous any value in a range height, delivery time, temperature, petrol price

A discrete variable has a probability mass function (the probability of each value). A continuous variable has a probability density - we talk about the probability of a range, like P(20 < time < 30 minutes).

Expected value and variance#

The expected value E[X] (the mean) is the long-run average:

E[X] = Σ x · P(x)

The variance measures spread around the mean, and the standard deviation is its square root (same units as X):

Var(X) = Σ (x − μ)² · P(x), SD = √Var(X)

▶ Try it YourselfPython
Ctrl + Enter to run
Output
E[X] = 1.95 orders per day
SD   = 1.12
Is the lucky draw worth it?

A Rs 50 ticket wins Rs 10,000 with probability 1/500, otherwise nothing. E[winnings] = 10000 × 1/500 = Rs 20. Expected profit = 20 − 50 = −Rs 30 per ticket. On average you lose money - which is how every lottery and casino makes profit.

The binomial distribution#

When you repeat a yes/no trial n times, independently, each with success probability p, the number of successes X follows Binomial(n, p):

P(X = k) = C(n, k) · pᵏ · (1 − p)ⁿ⁻ᵏ, mean = np, SD = √(np(1 − p))

▶ Try it YourselfPython
Ctrl + Enter to run

The Poisson distribution#

Poisson(λ) models how many times an event happens in a fixed interval when events occur independently at an average rate λ - calls per minute, accidents per day, typos per page.

P(X = k) = e^(−λ) · λᵏ / k!, mean = variance = λ

▶ Try it YourselfPython
Ctrl + Enter to run

The normal distribution#

The normal (Gaussian) distribution is the famous bell curve, described by its mean μ and standard deviation σ. Heights, measurement errors, exam marks of large groups and many averages are approximately normal.

▶ Try it YourselfPython
Ctrl + Enter to run

The 68-95-99.7 rule#

Range Share of values
μ ± 1σ about 68%
μ ± 2σ about 95%
μ ± 3σ about 99.7%
हिंदी में समझें

Normal distribution एक घंटी (bell) के आकार का graph है - ज़्यादातर values बीच (mean) के पास होती हैं और किनारों पर बहुत कम। याद रखने का आसान नियम: mean से 1 SD की दूरी में लगभग 68%, 2 SD में 95% और 3 SD में 99.7% values आती हैं।

z-scores#

A z-score says how many standard deviations a value is from the mean:

z = (x − μ) ÷ σ

z-scores let you compare values on different scales - 78 marks in a hard paper may be better than 85 in an easy one.

▶ Try it YourselfPython
Ctrl + Enter to run
Output
Maths z = 2.00, beats 97.7% of students
English z = 1.25, beats 89.4% of students
Tip

A value with |z| > 3 is very unusual for normal data - a common rule for flagging outliers (e.g. a suspicious transaction amount).

Summary#

  • Random variables are discrete (counts) or continuous (measurements).
  • E[X] = Σ x·P(x) is the long-run average; variance and SD measure spread.
  • Binomial(n, p): successes in n trials. Poisson(λ): events per interval.
  • Normal(μ, σ): the bell curve; 68-95-99.7 rule; z = (x − μ)/σ compares values on different scales.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Fifties in a season

A batsman scores a fifty in any match with probability p = 0.3, independently. Over n = 10 matches, use the binomial formula (with math.comb) to compute:

  • exactly3 - the probability of exactly 3 fifties
  • at_least1 - the probability of at least one fifty

Print both with 4 decimals:

0.2668
0.9718
Expected output:
0.2668
0.9718
2

z-score and percentile

Exam marks are normally distributed with mean 65 and standard deviation 10. Asha scored 82. Compute her z-score z and the fraction of students who scored below her, below, using the normal CDF Φ(z) = 0.5 × (1 + erf(z / √2)) (math.erf, math.sqrt). Print:

z = 1.70
Below: 95.5%
Expected output:
z = 1.70
Below: 95.5%

Quick Quiz

Q1.

A game pays Rs 100 with probability 0.2 and Rs 0 otherwise. Its expected value is...

Q2.

Which distribution counts the number of successes in n independent yes/no trials?

Q3.

In a normal distribution, about what percentage of values lie within 2 standard deviations of the mean?

Q4.

Marks have mean 60 and SD 8. A student scores 76. Their z-score is...

Q5.

Which distribution is a good model for the number of calls a call-centre receives per minute?