Expected Value (Discrete Distribution)
Expected Value (Discrete Distribution)
Compute the expected value of a discrete random variable given its values and probabilities.
Mathematical Definition
Expected Value:
E[X]=i=0∑N−1xipiwhere xi are the values and pi are their corresponding probabilities with ∑pi=1.
Function Arguments
x: array-like, shape (N,)- possible valuesp: array-like, shape (N,)- corresponding probabilities
Examples
Input: x = [1, 2, 3], p = [0.2, 0.5, 0.3]
Output: E[X] = 2.1
Input: x = [1, 2, 3, 4], p = [0.25, 0.25, 0.25, 0.25]
Output: E[X] = 2.5
Hint 1
First validate that probabilities sum to 1 using np.allclose().
Hint 2
Compute element-wise product of x and p, then sum using np.sum().
Requirements
- Raise a
ValueErrorif probabilities don't sum to 1 (within tolerance 10−6) - any error message is accepted - Ensure shapes of x and p match
- Return a single float
Constraints
- 1 ≤ N ≤ 10,000
- NumPy only
- Time limit: 200ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array
Expected Value (Discrete Distribution)
Expected Value (Discrete Distribution)
Compute the expected value of a discrete random variable given its values and probabilities.
Mathematical Definition
Expected Value:
E[X]=i=0∑N−1xipiwhere xi are the values and pi are their corresponding probabilities with ∑pi=1.
Function Arguments
x: array-like, shape (N,)- possible valuesp: array-like, shape (N,)- corresponding probabilities
Examples
Input: x = [1, 2, 3], p = [0.2, 0.5, 0.3]
Output: E[X] = 2.1
Input: x = [1, 2, 3, 4], p = [0.25, 0.25, 0.25, 0.25]
Output: E[X] = 2.5
Hint 1
First validate that probabilities sum to 1 using np.allclose().
Hint 2
Compute element-wise product of x and p, then sum using np.sum().
Requirements
- Raise a
ValueErrorif probabilities don't sum to 1 (within tolerance 10−6) - any error message is accepted - Ensure shapes of x and p match
- Return a single float
Constraints
- 1 ≤ N ≤ 10,000
- NumPy only
- Time limit: 200ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array