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++
#include <bits/stdc++.h>
using namespace std;
float calc_Expectation( float a[], float n)
{
float prb = (1 / n);
float sum = 0;
for ( int i = 0; i < n; i++)
sum += a[i] * prb;
return sum;
}
int main()
{
float expect, n = 6.0;
float a[6] = { 1.0, 2.0, 3.0,
4.0, 5.0, 6.0 };
expect = calc_Expectation(a, n);
cout << "Expectation of array E(X) is : "
<< expect << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static float calc_Expectation( float a[], float n)
{
float prb = ( 1 / n);
float sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += a[i] * prb;
return sum;
}
public static void main(String args[])
{
float expect, n = 6f;
float a[] = { 1f, 2f, 3f,
4f, 5f, 6f };
expect = calc_Expectation(a, n);
System.out.println( "Expectation of array E(X) is : "
+ expect);
}
}
|
Python3
def calc_Expectation(a, n):
prb = 1 / n
sum = 0
for i in range ( 0 , n):
sum + = (a[i] * prb)
return float ( sum )
n = 6 ;
a = [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ]
expect = calc_Expectation(a, n)
print ( "Expectation of array E(X) is : " ,
expect )
|
C#
using System;
class GFG {
static float calc_Expectation( float []a,
float n)
{
float prb = (1 / n);
float sum = 0;
for ( int i = 0; i < n; i++)
sum += a[i] * prb;
return sum;
}
public static void Main()
{
float expect, n = 6f;
float []a = { 1f, 2f, 3f,
4f, 5f, 6f };
expect = calc_Expectation(a, n);
Console.WriteLine( "Expectation"
+ " of array E(X) is : "
+ expect);
}
}
|
PHP
<?php
function calc_Expectation( $a , $n )
{
$prb = (1 / $n );
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += $a [ $i ] * $prb ;
return $sum ;
}
$n = 6.0;
$a = array (1.0, 2.0, 3.0,
4.0, 5.0, 6.0);
$expect = calc_Expectation( $a , $n );
echo "Expectation of array E(X) is : " .
$expect . "\n" ;
?>
|
Javascript
<script>
function calc_Expectation(a, n)
{
let prb = (1 / n);
let sum = 0;
for (let i = 0; i < n; i++)
sum += a[i] * prb;
return sum;
}
let expect, n = 6;
let a = [ 1, 2, 3,
4, 5, 6 ];
expect = calc_Expectation(a, n);
document.write( "Expectation of array E(X) is : "
+ expect);
</script>
|
Output
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Sep, 2022
Like Article
Save Article