Lesson 7 of 15
Discrete Random Variables
From Events to Numbers
A random variable is a function that assigns a number to each outcome in . A discrete random variable takes countably many values.
Probability Mass Function (PMF)
— the probability of each value. Must satisfy:
Expectation
The long-run average value of over many repetitions.
Variance
Variance measures spread around the mean.
# Fair die: values 1-6, each with prob 1/6
values = [1, 2, 3, 4, 5, 6]
probs = [1/6] * 6
mean = sum(x * p for x, p in zip(values, probs))
var = sum(x**2 * p for x, p in zip(values, probs)) - mean**2
print(round(mean, 4)) # 3.5
print(round(var, 4)) # 2.9167
Your Task
Implement discrete_stats(values, probs) that prints and , each rounded to 4 decimal places.
Pyodide loading...
Loading...
Click "Run" to execute your code.