diff --git a/Maths/Mean.php b/Maths/Mean.php deleted file mode 100644 index 2958a9da..00000000 --- a/Maths/Mean.php +++ /dev/null @@ -1,23 +0,0 @@ - $values[$i], 'freq' => $frequencies[$i]]; + } + usort($data, fn($a, $b) => $a['value'] <=> $b['value']); + + $cumulativeFrequency = 0; + $targetCF = $totalFrequency / 2; + $isEven = $totalFrequency % 2 === 0; + $prevValue = null; + + foreach ($data as $item) { + $cumulativeFrequency += $item['freq']; + + if ($isEven) { + if ($cumulativeFrequency > $targetCF) { + if ($cumulativeFrequency - $item['freq'] == $targetCF) { + return ($prevValue + $item['value']) / 2; + } + return $item['value']; + } + if ($cumulativeFrequency == $targetCF && $item['freq'] > 0) { + $prevValue = $item['value']; + } + } else { + if ($cumulativeFrequency >= $targetCF) { + return $item['value']; + } + } + } + + throw new \Exception('Could not determine median value'); +} + +/** + * Calculates the median of a continuous series. + * + * Formula: Median = L + (((N/2) - CF_prev) / f) * h + * Where: + * L = Lower bound of the median class + * N = Total frequency (Σf) + * CF_prev = Cumulative frequency of the class before the median class + * f = Frequency of the median class + * h = Class width (Upper bound - Lower bound) + * + * @param array $lower_bounds The lower bounds of the class intervals + * @param array $upper_bounds The upper bounds of the class intervals + * @param array $frequencies The frequencies of the class intervals + * @return int|float Median of the continuous series + * @throws \Exception + */ +function median_continuous(array $lower_bounds, array $upper_bounds, array $frequencies): int|float +{ + if (count($lower_bounds) !== count($upper_bounds) || count($lower_bounds) !== count($frequencies)) { + throw new \Exception('Lower bounds, upper bounds, and frequencies arrays must have the same length'); + } + + if (empty($lower_bounds)) { + throw new \Exception('Please pass values to find median value'); + } + + $lower_bounds = array_values($lower_bounds); + $upper_bounds = array_values($upper_bounds); + $frequencies = array_values($frequencies); + + foreach ($lower_bounds as $lb) { + if (!is_numeric($lb)) { + throw new \Exception('All lower bounds must be numeric'); + } + } + + foreach ($upper_bounds as $ub) { + if (!is_numeric($ub)) { + throw new \Exception('All upper bounds must be numeric'); + } + } + + foreach ($frequencies as $f) { + if (!is_numeric($f) || $f < 0) { + throw new \Exception('All frequencies must be non negative numeric values'); + } + } + + $totalFrequency = array_sum($frequencies); + + if ($totalFrequency == 0) { + throw new \Exception('Total frequency cannot be zero'); + } + + $data = []; + for ($i = 0; $i < count($lower_bounds); $i++) { + $data[] = [ + 'L' => $lower_bounds[$i], + 'U' => $upper_bounds[$i], + 'f' => $frequencies[$i] + ]; + } + usort($data, fn($a, $b) => $a['L'] <=> $b['L']); + + $targetCF = $totalFrequency / 2; + $cumulativeFrequency = 0; + $prevCF = 0; + + foreach ($data as $item) { + $cumulativeFrequency += $item['f']; + if ($cumulativeFrequency >= $targetCF) { + $L = $item['L']; + $f = $item['f']; + $h = $item['U'] - $item['L']; + + return $L + (($targetCF - $prevCF) / $f) * $h; + } + $prevCF = $cumulativeFrequency; + } + + throw new \Exception('Could not determine median value'); +} diff --git a/Maths/MeasuresOfCentralTendency/Mode.php b/Maths/MeasuresOfCentralTendency/Mode.php new file mode 100644 index 00000000..d5742f41 --- /dev/null +++ b/Maths/MeasuresOfCentralTendency/Mode.php @@ -0,0 +1,173 @@ + $lower_bounds[$i], + 'U' => $upper_bounds[$i], + 'f' => $frequencies[$i] + ]; + } + usort($data, fn($a, $b) => $a['L'] <=> $b['L']); + + $maxFreq = max(array_column($data, 'f')); + $modalIndex = -1; + + for ($i = 0; $i < count($data); $i++) { + if ($data[$i]['f'] == $maxFreq) { + $modalIndex = $i; + break; + } + } + + $L = $data[$modalIndex]['L']; + $f1 = $data[$modalIndex]['f']; + $f0 = $modalIndex > 0 ? $data[$modalIndex - 1]['f'] : 0; + $f2 = $modalIndex < count($data) - 1 ? $data[$modalIndex + 1]['f'] : 0; + $h = $data[$modalIndex]['U'] - $data[$modalIndex]['L']; + + $denominator = 2 * $f1 - $f0 - $f2; + + if ($denominator == 0) { + throw new \Exception('Cannot determine mode: denominator is zero (ambiguous modal class)'); + } + + return $L + (($f1 - $f0) / $denominator) * $h; +} diff --git a/Maths/MeasuresOfCentralTendency/images/CMean.jpeg b/Maths/MeasuresOfCentralTendency/images/CMean.jpeg new file mode 100644 index 00000000..9e0dea5e Binary files /dev/null and b/Maths/MeasuresOfCentralTendency/images/CMean.jpeg differ diff --git a/Maths/MeasuresOfCentralTendency/images/CMedian.jpeg b/Maths/MeasuresOfCentralTendency/images/CMedian.jpeg new file mode 100644 index 00000000..129d1fb1 Binary files /dev/null and b/Maths/MeasuresOfCentralTendency/images/CMedian.jpeg differ diff --git a/Maths/MeasuresOfCentralTendency/images/CMode.jpeg b/Maths/MeasuresOfCentralTendency/images/CMode.jpeg new file mode 100644 index 00000000..133b8338 Binary files /dev/null and b/Maths/MeasuresOfCentralTendency/images/CMode.jpeg differ diff --git a/Maths/MeasuresOfCentralTendency/images/DMean.jpeg b/Maths/MeasuresOfCentralTendency/images/DMean.jpeg new file mode 100644 index 00000000..dddf5cd0 Binary files /dev/null and b/Maths/MeasuresOfCentralTendency/images/DMean.jpeg differ diff --git a/Maths/MeasuresOfCentralTendency/images/DMedian.jpeg b/Maths/MeasuresOfCentralTendency/images/DMedian.jpeg new file mode 100644 index 00000000..b20c622d Binary files /dev/null and b/Maths/MeasuresOfCentralTendency/images/DMedian.jpeg differ diff --git a/Maths/MeasuresOfCentralTendency/images/IDMean.jpeg b/Maths/MeasuresOfCentralTendency/images/IDMean.jpeg new file mode 100644 index 00000000..ec1f66f3 Binary files /dev/null and b/Maths/MeasuresOfCentralTendency/images/IDMean.jpeg differ diff --git a/Maths/MeasuresOfCentralTendency/images/IDMode.jpeg b/Maths/MeasuresOfCentralTendency/images/IDMode.jpeg new file mode 100644 index 00000000..f4e2a89f Binary files /dev/null and b/Maths/MeasuresOfCentralTendency/images/IDMode.jpeg differ diff --git a/Maths/MeasuresOfCentralTendency/images/IMedian.jpeg b/Maths/MeasuresOfCentralTendency/images/IMedian.jpeg new file mode 100644 index 00000000..577a1266 Binary files /dev/null and b/Maths/MeasuresOfCentralTendency/images/IMedian.jpeg differ diff --git a/Maths/Median.php b/Maths/Median.php deleted file mode 100644 index 8ef86256..00000000 --- a/Maths/Median.php +++ /dev/null @@ -1,34 +0,0 @@ -assertEquals(-1, mean(-1)); + + // Notebook example: Tax driver daily earning for 5 days + // x = 100, 150, 200, 250, 300 => mean = 1000/5 = 200 + $this->assertEquals(200, mean(100, 150, 200, 250, 300)); + } + + public function testMeanDiscrete() + { + $this->assertEquals( + ((10 * 2) + (20 * 3) + (30 * 5)) / (2 + 3 + 5), + mean_discrete([10, 20, 30], [2, 3, 5]) + ); + + $this->assertEquals( + ((5 * 10) + (15 * 20)) / (10 + 20), + mean_discrete([5, 15], [10, 20]) + ); + + // Unsorted input + $this->assertEquals( + ((10 * 2) + (20 * 3) + (30 * 5)) / (2 + 3 + 5), + mean_discrete([30, 10, 20], [5, 2, 3]) + ); + } + + public function testMeanDiscreteExceptionMismatchedArrays() + { + $this->expectException(\Exception::class); + mean_discrete([1, 2], [1]); + } + + public function testMeanDiscreteExceptionEmpty() + { + $this->expectException(\Exception::class); + mean_discrete([], []); + } + + public function testMeanDiscreteExceptionZeroTotalFrequency() + { + $this->expectException(\Exception::class); + mean_discrete([1, 2], [0, 0]); + } + + public function testMeanContinuous() + { + $this->assertEquals( + ((5 * 2) + (15 * 3) + (25 * 5)) / (2 + 3 + 5), + mean_continuous([0, 10, 20], [10, 20, 30], [2, 3, 5]) + ); + + $this->assertEquals( + ((2.5 * 10) + (7.5 * 20)) / (10 + 20), + mean_continuous([0, 5], [5, 10], [10, 20]) + ); + + // Unsorted input + $this->assertEquals( + ((5 * 2) + (15 * 3) + (25 * 5)) / (2 + 3 + 5), + mean_continuous([20, 0, 10], [30, 10, 20], [5, 2, 3]) + ); + } + + public function testMeanContinuousExceptionMismatchedArrays() + { + $this->expectException(\Exception::class); + mean_continuous([0, 10], [10, 20], [1]); + } + + public function testMeanContinuousExceptionEmpty() + { + $this->expectException(\Exception::class); + mean_continuous([], [], []); + } + + public function testMeanContinuousExceptionZeroTotalFrequency() + { + $this->expectException(\Exception::class); + mean_continuous([0, 10], [10, 20], [0, 0]); } public function testMedian() @@ -166,6 +244,14 @@ public function testMedian() $this->assertEquals(3, median(1, 2, 3, 4, 5)); $this->assertEquals(4.5, median(1, 2, 3, 4, 5, 6, 7, 8)); $this->assertEquals(3, median(5, 3, 1, 2, 4)); + + // Notebook example: House prices (odd n=5) + // sorted: 7k,9k,12k,15k,20k => 3rd item = 12k + $this->assertEquals(12, median(12, 7, 20, 15, 9)); + + // Notebook example: Clinic waiting time (even n=6) + // sorted: 6,10,14,18,22,25 => (14+18)/2=16 + $this->assertEquals(16, median(14, 6, 22, 18, 10, 25)); } public function testMedianRejectsNonNumericValues() @@ -175,12 +261,171 @@ public function testMedianRejectsNonNumericValues() median('a', 'b', 'c'); } + public function testMedianDiscrete() + { + // Odd total frequency (N=5) + // Values: 10(2), 20(3). CF: 2, 5. N/2 = 2.5 + // Median is the value where CF >= 2.5, which is 20. + $this->assertEquals(20, median_discrete([10, 20], [2, 3])); + + // Even total frequency (N=4) + // Values: 10(2), 20(2). CF: 2, 4. Target = 2. + // First value CF = 2 (exactly matches target). So average of 10 and 20 = 15. + $this->assertEquals(15, median_discrete([10, 20], [2, 2])); + + // Even total frequency (N=6) + // Values: 10(1), 20(4), 30(1). CF: 1, 5, 6. Target = 3. + // First value > 3 is 20 (CF=5), previous CF=1 (not 3). Median = 20. + $this->assertEquals(20, median_discrete([10, 20, 30], [1, 4, 1])); + + // Zero-frequency + $this->assertEquals(15, median_discrete([10, 15, 20], [2, 0, 2])); + + // Unsorted input + $this->assertEquals(20, median_discrete([20, 10], [3, 2])); + } + + public function testMedianDiscreteExceptionMismatchedArrays() + { + $this->expectException(\Exception::class); + median_discrete([1, 2], [1]); + } + + public function testMedianDiscreteExceptionEmpty() + { + $this->expectException(\Exception::class); + median_discrete([], []); + } + + public function testMedianDiscreteExceptionZeroTotalFrequency() + { + $this->expectException(\Exception::class); + median_discrete([1, 2], [0, 0]); + } + + public function testMedianContinuous() + { + // N = 10, Target = 5 + // Intervals: 0-10 (f=2, CF=2) + // 10-20 (f=5, CF=7) -> Median Class + // 20-30 (f=3, CF=10) + // Formula: L = 10, f = 5, h = 10, prevCF = 2 + // Median = 10 + ((5 - 2) / 5) * 10 = 10 + (3/5)*10 = 10 + 6 = 16 + $this->assertEquals(16, median_continuous([0, 10, 20], [10, 20, 30], [2, 5, 3])); + + // Unsorted input + $this->assertEquals(16, median_continuous([20, 0, 10], [30, 10, 20], [3, 2, 5])); + } + + public function testMedianContinuousExceptionMismatchedArrays() + { + $this->expectException(\Exception::class); + median_continuous([0, 10], [10, 20], [1]); + } + + public function testMedianContinuousExceptionEmpty() + { + $this->expectException(\Exception::class); + median_continuous([], [], []); + } + + public function testMedianContinuousExceptionZeroTotalFrequency() + { + $this->expectException(\Exception::class); + median_continuous([0, 10], [10, 20], [0, 0]); + } + public function testMode() { $this->assertEquals([3], mode(1, 2, 3, 3, 4, 5)); $this->assertEquals([5, 6], mode(5, 5, 6, 6, 7)); $this->assertEquals([1, 2, 3, 4, 5], mode(1, 2, 3, 4, 5)); $this->assertEquals([2, 3, 4], mode(2, 2, 3, 3, 4, 4)); + + // Notebook example: Shoe store last 7 shoes sold: 8,2,8,3,5,8,2 + // Mode = 8 (appears 3 times) + $this->assertEquals([8], mode(8, 2, 8, 3, 5, 8, 2)); + } + + public function testModeExceptionEmpty() + { + $this->expectException(\Exception::class); + mode(); + } + + public function testModeDiscrete() + { + // Single mode + $this->assertEquals([20], mode_discrete([10, 20, 30], [2, 5, 3])); + + // Bimodal + $this->assertEquals([10, 30], mode_discrete([10, 20, 30], [5, 2, 5])); + + // Unsorted input + $this->assertEquals([30], mode_discrete([30, 10, 20], [5, 2, 3])); + + // Notebook example: Phone shop model name and units sold + // Samsung(f=15), iPhone(f=10), Nokia(f=10) => Mode = Samsung (highest freq) + // Note: values must be numeric for mode_discrete + // Samsung=1, iPhone=2, Nokia=3 (numeric mapping) + $this->assertEquals([1], mode_discrete([1, 2, 3], [15, 10, 10])); + } + + public function testModeDiscreteExceptionMismatchedArrays() + { + $this->expectException(\Exception::class); + mode_discrete([1, 2], [1]); + } + + public function testModeDiscreteExceptionEmpty() + { + $this->expectException(\Exception::class); + mode_discrete([], []); + } + + public function testModeDiscreteExceptionZeroTotalFrequency() + { + $this->expectException(\Exception::class); + mode_discrete([1, 2], [0, 0]); + } + + public function testModeContinuous() + { + // Intervals: 0-10 (f=2), 10-20 (f=5), 20-30 (f=3) + // Modal class: 10-20 (f1=5, f0=2, f2=3, h=10) + // Mode = 10 + ((5-2)/(2*5-2-3))*10 = 10 + (3/5)*10 = 16 + $this->assertEqualsWithDelta(16, mode_continuous([0, 10, 20], [10, 20, 30], [2, 5, 3]), 0.0001); + + // Unsorted input — result should be same + $this->assertEqualsWithDelta(16, mode_continuous([20, 0, 10], [30, 10, 20], [3, 2, 5]), 0.0001); + + // Notebook example: Car wash peak traffic time + // Intervals: 0-10(f=3), 10-20(f=8), 20-30(f=5), 30-40(f=2) + // Modal class: 10-20 (f1=8, f0=3, f2=5, h=10) + // Mode = 10 + ((8-3)/(2*8-3-5))*10 = 10 + (5/8)*10 = 10 + 6.25 = 16.25 + $this->assertEqualsWithDelta( + 16.25, + mode_continuous([0, 10, 20, 30], [10, 20, 30, 40], [3, 8, 5, 2]), + 0.001 + ); + } + + public function testModeContinuousExceptionMismatchedArrays() + { + $this->expectException(\Exception::class); + mode_continuous([0, 10], [10, 20], [1]); + } + + public function testModeContinuousExceptionEmpty() + { + $this->expectException(\Exception::class); + mode_continuous([], [], []); + } + + public function testModeContinuousExceptionZeroTotalFrequency() + { + $this->expectException(\Exception::class); + mode_continuous([0, 10], [10, 20], [0, 0]); } public function testGreatestCommonDivisor()