function calculate_variance($data_set = array()) { $mean = calculate_mean($data_set); $squared_sum = 0.0; foreach ($data_set as $data_point) { $deviation_from_mean = $data_point - $mean; $squared_sum += pow($deviation_from_mean, 2); } return $squared_sum / count($data_set); }
/** * The mean of the beta deviates B(a,b) should be about equal to a/(a+b). * * E[B(a,b)] = a / (a + b) */ public function testBetaDistributionMean() { $a = rand($this->a_minimum, $this->a_maximum); $b = rand($this->b_minimum, $this->b_maximum); $data_points = array(); for ($i = 0; $i < $this->number_of_samples; ++$i) { $data_points[] = generate_random_beta_variate($a, $b); } $expected_mean = $a / ($a + $b); $beta_mean = calculate_mean($data_points); $this->assertEquals(round($expected_mean, $this->precision), round($beta_mean, $this->precision), "The mean of the beta deviates does not equal a/(a+b)"); }