TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Expected Value (Discrete Distribution)

Probability and Statistics
Easy

Compute the expected value of a discrete random variable from its possible values and their probabilities:

E[X]=∑i=0N−1xipi\mathbb{E}[X] = \sum_{i=0}^{N-1} x_i p_iE[X]=i=0∑N−1​xi​pi​

Here, NNN is the number of possible outcomes, xix_ixi​ is outcome iii, and pip_ipi​ is its probability. The two input lists have the same length, and the probabilities sum to 1.

Return the expected value as a Python float.

Loading visualization...

Examples

Input: x = [1, 2, 3], p = [0.2, 0.5, 0.3]

Output: 2.1

Explanation: The weighted sum is 1(0.2) + 2(0.5) + 3(0.3) = 2.1.

Input: x = [1, 2, 3, 4], p = [0.25, 0.25, 0.25, 0.25]

Output: 2.5

Hint 1

np.asarray(values, dtype=float) prepares a list for vectorized arithmetic.

Hint 2

np.dot(x, p) computes the weighted sum directly.

Requirements

  • Convert x and p to NumPy arrays for the computation
  • Compute the probability-weighted sum of the outcomes
  • Return a Python float

Constraints

  • x and p are one-dimensional lists with the same length
  • 1≤N≤10,0001 \leq N \leq 10{,}0001≤N≤10,000
  • Every probability is non-negative and the probabilities sum to 1
  • Use NumPy only
Try Similar Problems
Mean Median ModeSample Var StdBernoulli PmfBinomial Pmf CdfPercentiles

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Expected Value (Discrete Distribution)

Probability and Statistics
Easy

Compute the expected value of a discrete random variable from its possible values and their probabilities:

E[X]=∑i=0N−1xipi\mathbb{E}[X] = \sum_{i=0}^{N-1} x_i p_iE[X]=i=0∑N−1​xi​pi​

Here, NNN is the number of possible outcomes, xix_ixi​ is outcome iii, and pip_ipi​ is its probability. The two input lists have the same length, and the probabilities sum to 1.

Return the expected value as a Python float.

Loading visualization...

Examples

Input: x = [1, 2, 3], p = [0.2, 0.5, 0.3]

Output: 2.1

Explanation: The weighted sum is 1(0.2) + 2(0.5) + 3(0.3) = 2.1.

Input: x = [1, 2, 3, 4], p = [0.25, 0.25, 0.25, 0.25]

Output: 2.5

Hint 1

np.asarray(values, dtype=float) prepares a list for vectorized arithmetic.

Hint 2

np.dot(x, p) computes the weighted sum directly.

Requirements

  • Convert x and p to NumPy arrays for the computation
  • Compute the probability-weighted sum of the outcomes
  • Return a Python float

Constraints

  • x and p are one-dimensional lists with the same length
  • 1≤N≤10,0001 \leq N \leq 10{,}0001≤N≤10,000
  • Every probability is non-negative and the probabilities sum to 1
  • Use NumPy only
Try Similar Problems
Mean Median ModeSample Var StdBernoulli PmfBinomial Pmf CdfPercentiles

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.