Probability for MLIntro
Expected Value
~8 mincode completion
Implement expected_value(values, probs) returning as a float.
Do it with array arithmetic, not a loop.
Examples
A 10% chance of 10 and a 90% chance of nothing is worth 1
- Input
- expected_value([0, 10], [0.9, 0.1])
- Output
- 1
A fair die averages 3.5
- Input
- expected_value([1, 2, 3, 4, 5, 6], [
- Output
- 3.5
Negative outcomes pull the expectation below zero
- Input
- expected_value([-2, 5], [0.75, 0.25])
- Output
- -0.25
Hints
Hint 1
Sum with , and check which axis you are summing over.
Hint 2
Watch for this: averages the values and ignores the probabilities.
Requirements
: array of outcomes, shape (n,)
probs: array of probabilities, shape (n,), summing to 1Return float: the expected value
Use a fully vectorised implementation without Python loops
Constraints
Vectorised implementation only, no Python loops
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~8 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 expected_value(values, probs):
"""
Weighted average of values under the distribution probs.
Args:
values: array of outcomes, shape (n,)
probs: array of probabilities, shape (n,), summing to 1
Returns:
float: the expected value
"""
# YOUR CODE HERE
pass