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.5Input: [1.0, 9.0, 6.0, 7.0, 8.0, 12.0]
Output: 7.16
Below is the implementation :
// 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;
} |
// 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. |
# 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 |
// 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. |
<?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 ?> |
<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> |
Expectation of array E(X) is : 3.5
Time complexity: O(n)
Auxiliary Space: O(1)
As we can see that the expected value is actually average of numbers, we can also simply compute average of array.