在概率论中,一组数字的期望值(或称为期望)代表了当我们重复进行该实验时,其结果的长期平均值。举个例子,当我们掷一个标准的六面骰子时,其期望值是 3.5。这是因为,当掷骰子的次数极其巨大时,出现的所有数字的平均值会无限接近于 3.5。更准确地说,根据大数定律,随着重复次数趋向于无穷大,这些数值的算术平均数几乎肯定会收敛于期望值。
期望值也常被称为期望、数学期望、EV(Expected Value)或一阶矩。如果给定一个数组,我们的任务就是计算该数组的期望值。
示例:
> 输入: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
> 输出: 3.5
>
> 输入: [1.0, 9.0, 6.0, 7.0, 8.0, 12.0]
> 输出: 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 << "
";
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.
`
JavaScript
`
// Javascript code to calculate expected
// value of an array
// Function to calculate expectation
function calc_Expectation(a, n)
{