Compute the expected value of a discrete random variable from its possible values and their probabilities:
E[X]=i=0∑N−1xipiHere, N is the number of possible outcomes, xi is outcome i, and pi 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.
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
np.asarray(values, dtype=float) prepares a list for vectorized arithmetic.
np.dot(x, p) computes the weighted sum directly.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Compute the expected value of a discrete random variable from its possible values and their probabilities:
E[X]=i=0∑N−1xipiHere, N is the number of possible outcomes, xi is outcome i, and pi 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.
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
np.asarray(values, dtype=float) prepares a list for vectorized arithmetic.
np.dot(x, p) computes the weighted sum directly.
Sign in to take notes on this problem
Accepts: array
Accepts: array