See Release Notes
Long Term Support Release
1 <?php 2 3 declare(strict_types=1); 4 5 namespace Phpml\Math\Statistic; 6 7 use Phpml\Exception\InvalidArgumentException; 8 9 class Mean 10 { 11 /** 12 * @throws InvalidArgumentException 13 */ 14 public static function arithmetic(array $numbers): float 15 { 16 self::checkArrayLength($numbers); 17 18 return array_sum($numbers) / count($numbers); 19 } 20 21 /** 22 * @return float|mixed 23 * 24 * @throws InvalidArgumentException 25 */ 26 public static function median(array $numbers) 27 { 28 self::checkArrayLength($numbers); 29 30 $count = count($numbers); 31 $middleIndex = (int) floor($count / 2); 32 sort($numbers, SORT_NUMERIC); 33 $median = $numbers[$middleIndex]; 34 35 if ($count % 2 === 0) { 36 $median = ($median + $numbers[$middleIndex - 1]) / 2; 37 } 38 39 return $median; 40 } 41 42 /** 43 * @return mixed 44 * 45 * @throws InvalidArgumentException 46 */ 47 public static function mode(array $numbers) 48 { 49 self::checkArrayLength($numbers); 50 51 $values = array_count_values($numbers); 52 53 return array_search(max($values), $values, true); 54 } 55 56 /** 57 * @throws InvalidArgumentException 58 */ 59 private static function checkArrayLength(array $array): void 60 { 61 if (count($array) === 0) { 62 throw new InvalidArgumentException('The array has zero elements'); 63 } 64 } 65 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body