Problems
Loading...
1 / 1

Expected Value (Discrete Distribution)

Probability and Statistics
Easy

Compute the expected value of a discrete random variable given its values and probabilities.

Mathematical Definition

Expected Value:

E[X]=i=0N1xipi\mathbb{E}[X] = \sum_{i=0}^{N-1} x_i \, p_i

where xi are the values and pi are their corresponding probabilities with ∑pi=1.

Function Arguments

  • x: array-like, shape (N,) - possible values
  • p: array-like, shape (N,) - corresponding probabilities
Loading visualization...

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 ValueError if 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