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++
// CPP code to calculate expected// value of an array#include <bits/stdc++.h>using namespace std;// Function to calculate expectationfloat 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 programint 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;} |
Java
// Java code to calculate expected// value of an arrayimport 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. |
Python3
# python code to calculate expected# value of an array# Function to calculate expectationdef 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 programn = 6;a = [ 1.0, 2.0, 3.0,4.0, 5.0, 6.0 ]# Function for calculating expectationexpect = calc_Expectation(a, n)# Display expectation of given arrayprint( "Expectation of array E(X) is : ", expect )# This code is contributed by Sam007 |
C#
// C# code to calculate expected// value of an arrayusing 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. |
PHP
<?php// PHP code to calculate expected// value of an array// Function to calculate expectationfunction 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 arrayecho "Expectation of array E(X) is : ". $expect . "\n";// This code is contributed by Sam007?> |
Javascript
<script>// Javascript 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 let prb = (1 / n); // calculating expectation overall let sum = 0; for (let i = 0; i < n; i++) sum += a[i] * prb; // returning expectation as sum return sum; } // driver function let expect, n = 6; let a = [ 1, 2, 3, 4, 5, 6 ]; // Function for calculating expectation expect = calc_Expectation(a, n); // Display expectation of given array document.write("Expectation of array E(X) is : " + expect);</script> |
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 write.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.



