任何一组数字的期望值或期望值是它表示的实验重复的长期平均值。例如,轧制六面模具的期望值为3.5,因为在大量轧辊中出现的所有数字的平均值接近3.5。粗略地讲,大数定律指出,随着重复次数接近无穷大,这些值的算术平均值几乎可以肯定地收敛到期望值。期望值也称为期望值,数学期望值,EV或第一时刻。
给定一个数组,任务是计算数组的期望值。
例子 :
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
下面是实现:
C++
// CPP code to calculate expected
// value of an array
#include
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
// 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.
Python3
# 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#
// 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
输出 :
Expectation of array E(X) is : 3.5
程序的时间复杂度为O(n) 。
正如我们看到的,期望值实际上是数的平均值,我们也可以简单地计算数组的平均值。