Great Expectations
In the previous exercise, we introduced the concept of the expected value of a random variable. In this exercise, we will take that concept one step further, and explore the idea of the expected value of a function with respect to a random variable.
Consider a random variable X, with support elements x_0,x_1,\ldots,x_N. We will denote and define the expected value of a function f with respect to a random variable X as follows:
For example, consider a random variable A described by the following instance of dist.DDist:
a = dist.DDist({2: 0.5, -2: 0.3, 1: 0.1, 0: 0.1})
Then we could find E_A[A^2] as follows:
1) Expected Value
Define a Python function expectation which takes two arguments:
- a
DDistrepresenting a random variable - a function
fthat takes elements of that distribution's support as input, and returns a number
expectation should return a number that represents the expected value of f with respect to the random variable described by the distribution passed in as the first argument.
Enter your code below. Assume that all the classes and functions
from lib601.dist have been imported with:
import lib601.dist as dist
2) Mean
The mean of a random variable X is defined as:
Write a Python function mean that uses expectation to determine the mean of a random variable represented by a DDist that
is passed as an argument.
Enter your code below. Assume that expectation is defined for you, and that all the classes and functions
from lib601.dist have been imported with:
import lib601.dist as dist
3) Variance
The variance of a random variable X is a measure of how "spread out" a distribution is; it is defined as follows:
where \mu is the mean of X.
Write a Python function variance that uses expectation to determine the mean of a random variable represented by a DDist that
is passed as an argument.
Enter your code below. Assume that expectation and mean are defined for you, and that all the classes and functions from lib601.dist have been imported with: