数组的期望值或期望值

期望值 概率中的任何一组数字,都是它所代表的实验重复次数的长期平均值。例如,滚动六面模具的预期值为3.5,因为在极大量的滚动中出现的所有数字的平均值接近3.5。不那么粗略地说,大数定律表明,当重复次数接近无穷大时,这些值的算术平均值几乎肯定会收敛到预期值。预期值也称为 预料 , 数学期望 ,EV,或第一刻。 给定一个数组,任务是计算 期望值 阵列的一部分。 例如:

null
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

以下是实施情况:

C++

// 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 << "" ;
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

<?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 . "" ;
// 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>


输出:

Expectation of array E(X) is : 3.5

程序的时间复杂度是 O(n) . 正如我们所看到的,期望值实际上是数的平均值,我们也可以简单地 计算数组的平均值 . 本文由 希曼舒兰扬 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧,技术咨询可以联系QQ407933975
点赞6 分享