Expectation or expected value of an array

Expectation or expected value of any group of numbers in probability is the long-run average value of repetitions of the experiment it represents. For example, the expected value in rolling a six-sided die is 3.5, because the average of all the numbers that come up in an extremely large number of rolls is close to 3.5. Less roughly, the law of large numbers states that the arithmetic mean of the values almost surely converges to the expected value as the number of repetitions approaches infinity. The expected value is also known as the expectation, mathematical expectation, EV, or first moment.
Given an array, the task is to calculate the expected value of the array.

Examples :

Input : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
Output : 3.5

Input :[1.0, 9.0, 6.0, 7.0, 8.0, 12.0]
Output :7.16

Below is the implementation :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to calculate expected 
// value of an array
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate expectation
float calc_Expectation(float a[], float n)
{
    /*variable prb is for probability 
    of each element which is same for
    each element  */
    float prb = (1 / n);
      
    // calculating expectation overall
    float sum = 0;
    for (int i = 0; i < n; i++) 
        sum += a[i] * prb;    
  
    // returning expectation as sum
    return sum;
}
  
// Driver program
int main()
{
    float expect, n = 6.0;
    float a[6] = { 1.0, 2.0, 3.0, 
                 4.0, 5.0, 6.0 };
      
    // Function for calculating expectation
    expect = calc_Expectation(a, n);
      
    // Display expectation of given array
    cout << "Expectation of array E(X) is : " 
         << expect << "\n";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to calculate expected
// value of an array
import java.io.*;
  
class GFG
{
    // Function to calculate expectation
    static float calc_Expectation(float a[], float n)
    {
        // Variable prb is for probability of each 
        // element which is same for each element 
        float prb = (1 / n);
      
        // calculating expectation overall
        float sum = 0;
        for (int i = 0; i < n; i++) 
            sum += a[i] * prb; 
  
        // returning expectation as sum
        return sum;
    }
  
    // Driver program
    public static void main(String args[])
    {
        float expect, n = 6f;
        float a[] = { 1f, 2f, 3f, 
                       4f, 5f, 6f };
          
        // Function for calculating expectation
        expect = calc_Expectation(a, n);
          
        // Display expectation of given array
        System.out.println("Expectation of array E(X) is : "
                           + expect);
    }
}
  
// This code is contributed by Anshika Goyal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# python code to calculate expected 
# value of an array
  
# Function to calculate expectation
def calc_Expectation(a, n):
      
    # variable prb is for probability 
    # of each element which is same for
    # each element 
    prb = 1 / n
      
    # calculating expectation overall
    sum = 0
    for i in range(0, n):
        sum += (a[i] * prb) 
          
    # returning expectation as sum
    return float(sum)
  
  
# Driver program
n = 6;
a = [ 1.0, 2.0, 3.0,4.0, 5.0, 6.0 ]
  
# Function for calculating expectation
expect = calc_Expectation(a, n)
  
# Display expectation of given array
print( "Expectation of array E(X) is : ",
                                 expect )
  
# This code is contributed by Sam007

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to calculate expected
// value of an array
using System;
  
class GFG {
      
    // Function to calculate expectation
    static float calc_Expectation(float []a,
                                    float n)
    {
          
        // Variable prb is for probability
        // of each element which is same
        // for each element 
        float prb = (1 / n);
      
        // calculating expectation overall
        float sum = 0;
          
        for (int i = 0; i < n; i++) 
            sum += a[i] * prb; 
  
        // returning expectation as sum
        return sum;
    }
  
    // Driver program
    public static void Main()
    {
        float expect, n = 6f;
        float []a = { 1f, 2f, 3f, 
                    4f, 5f, 6f };
          
        // Function for calculating
        // expectation
        expect = calc_Expectation(a, n);
          
        // Display expectation of given
        // array
        Console.WriteLine("Expectation"
               + " of array E(X) is : "
                             + expect);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to calculate expected 
// value of an array
  
// Function to calculate expectation
function calc_Expectation($a, $n)
{
    /*variable prb is for probability 
    of each element which is same for
    each element */
    $prb = (1 / $n);
      
    // calculating expectation overall
    $sum = 0;
    for ($i = 0; $i < $n; $i++) 
        $sum += $a[$i] * $prb
  
    // returning expectation as sum
    return $sum;
}
  
// Driver Code
$n = 6.0;
$a = array(1.0, 2.0, 3.0, 
           4.0, 5.0, 6.0);
  
// Function for calculating expectation
$expect = calc_Expectation($a, $n);
  
// Display expectation of given array
echo "Expectation of array E(X) is : "
                        $expect . "\n";
  
// This code is contributed by Sam007
?>

chevron_right



Output :

Expectation of array E(X) is : 3.5

Time complexity of the program is O(n).

As we can see that the expected value is actually average of numbers, we can also simply compute average of array.

This article is contributed by Himanshu Ranjan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m, Sam007



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.