Expectation or expected value of an array | Function to calculate expectation ; variable prb is for probability of each element which is same for each element ; calculating expectation overall ; returning expectation as sum ; Driver Code ; Function for calculating expectation ; Display expectation of given array
< ? 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 . " STRNEWLINE " ; ? >