Variance from a Distribution
~10 mincode completion
Implement distribution_variance(values, probs) returning as a float.
Compute the mean first, then the weighted mean of the squared deviations.
Examples
Two equally likely outcomes five either side of the mean
- Input
- distribution_variance([0, 10], [0.5, 0.5])
- Output
- 25
A lopsided distribution has a mean of 2.5 and variance 18.75
- Input
- distribution_variance([0, 10], [0.75, 0.25])
- Output
- 18.75
No spread at all means zero variance
- Input
- distribution_variance([4, 4, 4], [0.2, 0.3, 0.5])
- Output
- 0
Hints
Hint 1
Sum with , and check which axis you are summing over.
Hint 2
Watch for this: uses the unweighted mean.
Requirements
: array of outcomes, shape (n,)
probs: array of probabilities, shape (n,), summing to 1Return float: sum of p_i * (x_i - mu)^2
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~10 min
••••••••••••••••
8 employers weight this skill
4 quant funds, 2 health and bio companies, 2 big tech firms. Top match scores 93.
Python
import numpy as np
def distribution_variance(values, probs):
"""
Variance of a discrete distribution.
Args:
values: array of outcomes, shape (n,)
probs: array of probabilities, shape (n,), summing to 1
Returns:
float: sum of p_i * (x_i - mu)^2
"""
# YOUR CODE HERE
pass