How to Calculate Expected Value in Python

By IncludeHelp Last updated : November 26, 2023

Python programming language has a vast variety of functions that can enable the programmer to perform all types of tasks. These also include all types of mathematical and statistical operations. Once such complex operations are the calculation of expected value.

Prerequisite

In this tutorial, you will learn about the Expected Value and its calculation in Python.

Expected Value (E(X))

The expected value is denoted as E(X) or μ. It is a fundamental concept in probability theory and statistics. It represents the long-term average or the "expected" value of a random variable. In simple terms, it tells you the expected value of the random experiment if you were to repeat it many times.

Calculating Expected Value

The expected value of a discrete random variable X is calculated as follows:

μ = Σx * P(x)

Here,

  • x are all the possible values that can be present in the variable X.
  • P(x) is the probability of x.

The expected value provides a measure of central tendency for a random variable.

The concept of expected value is fundamental in various areas of mathematics, statistics, economics, and decision theory, and it plays a crucial role in making informed decisions based on probabilities.

Python program to calculate Expected Value

Python allows programmers to perform the task, here is the Python program to calculate the expected value -

# Importing NumPy library
import numpy as nump
 
# Defining function to calculate Expected Value
def calculateExpectedValue(values, weights):
    values = nump.asarray(values)
    weights = nump.asarray(weights)
    return (values * weights).sum() / weights.sum()
 
# Defining scores and their chances of scoring
score = [0, 1, 2, 3, 4, 6]
chance = [0.23, 0.35, 0.10, 0.04, 0.16, 0.12]
 
expected_value = calculateExpectedValue(score, chance)
print("The expected value is :", expected_value)

Output

The output of the above program is:

The expected value is : 2.0300000000000002

Python NumPy Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.