PHP code example of hi-folks / statistics
1. Go to this page and download the library: Download hi-folks/statistics library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hi-folks / statistics example snippets
use HiFolks\Statistics\Stat;
$mean = Stat::mean([1, 2, 3, 4, 4]);
// 2.8
$mean = Stat::mean([-1.0, 2.5, 3.25, 5.75]);
// 2.625
use HiFolks\Statistics\Stat;
// Unweighted mean (same as mean but always float)
$fmean = Stat::fmean([3.5, 4.0, 5.25]);
// 4.25
// Weighted mean
$fmean = Stat::fmean([3.5, 4.0, 5.25], [1, 2, 1]);
// 4.1875
// Custom precision
$fmean = Stat::fmean([3.5, 4.0, 5.25], null, 2);
// 4.25
$fmean = Stat::fmean([3.5, 4.0, 5.25], [1, 2, 1], 3);
// 4.188
use HiFolks\Statistics\Stat;
$mean = Stat::trimmedMean([1, 2, 3, 4, 5, 6, 7, 8, 9, 100], 0.1);
// 5.5 (outlier 100 and lowest value 1 removed)
$mean = Stat::trimmedMean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.2);
// 5.5 (removes 2 values from each side)
$mean = Stat::trimmedMean([1, 2, 3, 4, 5], 0.0);
// 3.0 (no trimming, same as regular mean)
use HiFolks\Statistics\Stat;
$mean = Stat::geometricMean([54, 24, 36], 1);
// 36.0
use HiFolks\Statistics\Stat;
$mean = Stat::harmonicMean([40, 60], null, 1);
// 48.0
use HiFolks\Statistics\Stat;
Stat::harmonicMean([40, 60], [5, 30], 1);
// 56.0
use HiFolks\Statistics\Stat;
$median = Stat::median([1, 3, 5]);
// 3
$median = Stat::median([1, 3, 5, 7]);
// 4
use HiFolks\Statistics\Stat;
$median = Stat::weightedMedian([1, 2, 3], [1, 1, 1]);
// 2.0 (equal weights, same as regular median)
$median = Stat::weightedMedian([1, 2, 3], [1, 1, 10]);
// 3.0 (heavy weight on 3 pulls the median)
$median = Stat::weightedMedian([1, 2, 3, 4], [1, 1, 1, 1]);
// 2.5 (equal weights, even count — averages the two middle values)
use HiFolks\Statistics\Stat;
$median = Stat::medianLow([1, 3, 5]);
// 3
$median = Stat::medianLow([1, 3, 5, 7]);
// 3
use HiFolks\Statistics\Stat;
$median = Stat::medianHigh([1, 3, 5]);
// 3
$median = Stat::medianHigh([1, 3, 5, 7]);
// 5
use HiFolks\Statistics\Stat;
$median = Stat::medianGrouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5]);
// 3.7
$median = Stat::medianGrouped([1, 3, 3, 5, 7]);
// 3.25
$median = Stat::medianGrouped([1, 3, 3, 5, 7], 2);
// 3.5
use HiFolks\Statistics\Stat;
// 172 people aged 20-30, 484 aged 30-40, 387 aged 40-50, etc.
$data = array_merge(
array_fill(0, 172, 25),
array_fill(0, 484, 35),
array_fill(0, 387, 45),
array_fill(0, 22, 55),
array_fill(0, 6, 65),
);
round(Stat::medianGrouped($data, 10), 1);
// 37.5
use HiFolks\Statistics\Stat;
$quantiles = Stat::quantiles([98, 90, 70,18,92,92,55,83,45,95,88]);
// [ 55.0, 88.0, 92.0 ]
$quantiles = Stat::quantiles([105, 129, 87, 86, 111, 111, 89, 81, 108, 92, 110,100, 75, 105, 103, 109, 76, 119, 99, 91, 103, 129,106, 101, 84, 111, 74, 87, 86, 103, 103, 106, 86,111, 75, 87, 102, 121, 111, 88, 89, 101, 106, 95,103, 107, 101, 81, 109, 104], 10);
// [81.0, 86.2, 89.0, 99.4, 102.5, 103.6, 106.0, 109.8, 111.0]
// Inclusive method
$quantiles = Stat::quantiles([1, 2, 3, 4, 5], method: 'inclusive');
// [2.0, 3.0, 4.0]
use HiFolks\Statistics\Stat;
$percentile = Stat::firstQuartile([98, 90, 70,18,92,92,55,83,45,95,88]);
// 55.0
use HiFolks\Statistics\Stat;
$percentile = Stat::thirdQuartile([98, 90, 70,18,92,92,55,83,45,95,88]);
// 92.0
use HiFolks\Statistics\Stat;
$value = Stat::percentile([10, 20, 30, 40, 50, 60, 70, 80, 90, 100], 50);
// 55.0 (median)
$value = Stat::percentile([10, 20, 30, 40, 50, 60, 70, 80, 90, 100], 90);
// 91.0
use HiFolks\Statistics\Stat;
$ranks = Stat::rank([10, 20, 20, 30]);
// [1, 2.5, 2.5, 4]
$ranks = Stat::rank([10, 20, 20, 30], Stat::RANK_DENSE);
// [1, 2, 2, 3]
use HiFolks\Statistics\Stat;
$rank = Stat::percentileRank([10, 20, 20, 30, 40], 20);
// 60.0
$rank = Stat::percentileRank([10, 20, 20, 30, 40], 20, Stat::PERCENTILE_RANK_STRICT);
// 20.0
use HiFolks\Statistics\Stat;
$stdev = Stat::pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75]);
// 0.986893273527251
$stdev = Stat::pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75], 4);
// 0.9869
use HiFolks\Statistics\Stat;
$stdev = Stat::stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75]);
// 1.0810874155219827
$stdev = Stat::stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75], 4);
// 1.0811
use HiFolks\Statistics\Stat;
$sem = Stat::sem([2, 4, 4, 4, 5, 5, 7, 9]);
// 0.7559...
$sem = Stat::sem([2, 4, 4, 4, 5, 5, 7, 9], 4);
// 0.7559
use HiFolks\Statistics\Stat;
$mad = Stat::meanAbsoluteDeviation([1, 2, 3, 4, 5]);
// 1.2
$mad = Stat::meanAbsoluteDeviation([1, 2, 3, 4, 5], 1);
// 1.2
use HiFolks\Statistics\Stat;
$mad = Stat::medianAbsoluteDeviation([1, 2, 3, 4, 5]);
// 1.0
// Robust to outliers — the outlier 1000 does not affect the result:
$mad = Stat::medianAbsoluteDeviation([1, 2, 3, 4, 1000]);
// 1.0
use HiFolks\Statistics\Stat;
$variance = Stat::variance([2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]);
// 1.3720238095238095
$data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5];
$mean = Stat::mean($data);
$variance = Stat::variance($data, xbar: $mean);
use HiFolks\Statistics\Stat;
$variance = Stat::pvariance([0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]);
// 1.25
// With pre-computed mean:
$data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25];
$mu = Stat::mean($data);
$variance = Stat::pvariance($data, mu: $mu);
use HiFolks\Statistics\Stat;
$skewness = Stat::skewness([1, 2, 3, 4, 5]);
// 0.0 (symmetric)
$skewness = Stat::skewness([1, 1, 1, 1, 1, 10]);
// positive (right-skewed)
use HiFolks\Statistics\Stat;
$pskewness = Stat::pskewness([1, 1, 1, 1, 1, 10]);
use HiFolks\Statistics\Stat;
$kurtosis = Stat::kurtosis([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// negative (platykurtic, lighter tails than normal)
$kurtosis = Stat::kurtosis([1, 2, 2, 2, 2, 2, 2, 2, 2, 50]);
// positive (leptokurtic, heavier tails due to outlier)
use HiFolks\Statistics\Stat;
$cv = Stat::coefficientOfVariation([10, 20, 30, 40, 50]);
// ~52.70 (sample)
$cv = Stat::coefficientOfVariation([10, 20, 30, 40, 50], round: 2);
// 52.7
$cv = Stat::coefficientOfVariation([10, 20, 30, 40, 50], population: true);
// ~47.14 (population)
use HiFolks\Statistics\Stat;
$zscores = Stat::zscores([2, 4, 4, 4, 5, 5, 7, 9]);
// array of z-scores, one per value
$zscores = Stat::zscores([2, 4, 4, 4, 5, 5, 7, 9], 2);
// z-scores rounded to 2 decimal places
use HiFolks\Statistics\Stat;
$outliers = Stat::outliers([1, 2, 3, 4, 5, 6, 7, 8, 9, 100]);
// [100]
$outliers = Stat::outliers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1.0);
// values more than 1 stdev from the mean
use HiFolks\Statistics\Stat;
$times = [110.2, 112.5, 108.9, 115.3, 111.7, 114.0, 109.8, 113.6, 200.0, 50.0];
$outliers = Stat::iqrOutliers($times);
// [200.0, 50.0] — the crash and the timing error are detected
$extremeOnly = Stat::iqrOutliers($times, 3.0);
// only the most extreme values
$covariance = Stat::covariance(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 1, 2, 3, 1, 2, 3]
);
// 0.75
$covariance = Stat::covariance(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[9, 8, 7, 6, 5, 4, 3, 2, 1]
);
// -7.5
$correlation = Stat::correlation(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9]
);
// 1.0
$correlation = Stat::correlation(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[9, 8, 7, 6, 5, 4, 3, 2, 1]
);
// -1.0
$correlation = Stat::correlation(
[1, 2, 3, 4, 5],
[1, 4, 9, 16, 25],
'ranked'
);
// 1.0
$correlation = Stat::correlation(
[12, 2, 1, 12, 2],
[1, 4, 7, 1, 0],
'kendall'
);
// -0.47140452079103173
$correlation = Stat::kendallTau(
[12, 2, 1, 12, 2],
[1, 4, 7, 1, 0],
4
);
// -0.4714
$years = [1971, 1975, 1979, 1982, 1983];
$films_total = [1, 2, 3, 4, 5]
list($slope, $intercept) = Stat::linearRegression(
$years,
$films_total
);
// 0.31
// -610.18
round($slope * 2022 + $intercept);
// 17.0
list($slope, $intercept) = Stat::linearRegression(
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
proportional: true,
);
// $slope = 2.0
// $intercept = 0.0
use HiFolks\Statistics\Stat;
// Simulated weekly running paces (seconds/km) — diminishing improvement
$weeks = [1, 2, 3, 4, 5, 6, 7, 8];
$paces = [350, 342, 337, 333, 330, 328, 326, 325];
[$a, $b] = Stat::logarithmicRegression($weeks, $paces);
// $a = -12.33 (pace drops by 12.33 sec per unit of ln(week))
// $b = 350.2
// Predict pace at week 12:
$predicted = $a * log(12) + $b;
// ~320 seconds = 5:20/km
// R² for logarithmic model (transform x first)
$logWeeks = array_map(fn($v) => log($v), $weeks);
$r2Log = Stat::rSquared($logWeeks, $paces);
// 0.9987
// R² for linear model
$r2Linear = Stat::rSquared($weeks, $paces);
// 0.9176
// Logarithmic wins — the data has diminishing returns
use HiFolks\Statistics\Stat;
// Data following y = 3 * x^2
$x = [1, 2, 3, 4, 5];
$y = [3, 12, 27, 48, 75];
[$a, $b] = Stat::powerRegression($x, $y);
// $a = 3.0
// $b = 2.0 (the exponent)
use HiFolks\Statistics\Stat;
// Data following y = 2 * e^(0.5*x)
$x = [1, 2, 3, 4, 5];
$y = [3.30, 5.44, 8.96, 14.78, 24.36];
[$a, $b] = Stat::exponentialRegression($x, $y);
// $a ≈ 2.0
// $b ≈ 0.5
use HiFolks\Statistics\Stat;
$r2 = Stat::rSquared([1, 2, 3, 4, 5], [2, 4, 6, 8, 10]);
// 1.0 (perfect linear relationship)
$r2 = Stat::rSquared(
[1971, 1975, 1979, 1982, 1983],
[1, 2, 3, 4, 5],
round: 2,
);
// 0.96
$r2 = Stat::rSquared(
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
proportional: true,
);
// 1.0
// R² for logarithmic regression
$logX = array_map(fn($v) => log($v), $x);
$r2 = Stat::rSquared($logX, $y);
// R² for power regression
$logX = array_map(fn($v) => log($v), $x);
$logY = array_map(fn($v) => log($v), $y);
$r2 = Stat::rSquared($logX, $logY);
// R² for exponential regression
$logY = array_map(fn($v) => log($v), $y);
$r2 = Stat::rSquared($x, $logY);
use HiFolks\Statistics\Stat;
[$lower, $upper] = Stat::confidenceInterval([2, 4, 4, 4, 5, 5, 7, 9]);
// 95% CI: [3.52, 6.48] (approximately)
[$lower, $upper] = Stat::confidenceInterval([2, 4, 4, 4, 5, 5, 7, 9], confidenceLevel: 0.99);
// 99% CI: wider interval
[$lower, $upper] = Stat::confidenceInterval([2, 4, 4, 4, 5, 5, 7, 9], round: 2);
// [3.52, 6.48]
use HiFolks\Statistics\Stat;
use HiFolks\Statistics\Enums\Alternative;
$result = Stat::zTest([2, 4, 4, 4, 5, 5, 7, 9], populationMean: 3.0);
// ['zScore' => 2.6457..., 'pValue' => 0.0081...]
$result = Stat::zTest([2, 4, 4, 4, 5, 5, 7, 9], populationMean: 3.0, alternative: Alternative::Greater);
// one-tailed test: is the sample mean greater than 3?
$result = Stat::zTest([2, 4, 4, 4, 5, 5, 7, 9], populationMean: 3.0, round: 4);
// ['zScore' => 2.6458, 'pValue' => 0.0081]
use HiFolks\Statistics\Stat;
use HiFolks\Statistics\Enums\Alternative;
$result = Stat::tTest([2, 4, 4, 4, 5, 5, 7, 9], populationMean: 3.0);
// ['tStatistic' => 2.6457..., 'pValue' => 0.0331..., 'degreesOfFreedom' => 7]
$result = Stat::tTest([2, 4, 4, 4, 5, 5, 7, 9], populationMean: 3.0, alternative: Alternative::Greater);
// one-tailed test: is the sample mean greater than 3?
$result = Stat::tTest([2, 4, 4, 4, 5, 5, 7, 9], populationMean: 3.0, round: 4);
// ['tStatistic' => 2.6458, 'pValue' => 0.0331, 'degreesOfFreedom' => 7]
use HiFolks\Statistics\Stat;
use HiFolks\Statistics\Enums\Alternative;
// Compare two groups
$group1 = [30.02, 29.99, 30.11, 29.97, 30.01, 29.99];
$group2 = [29.89, 29.93, 29.72, 29.98, 30.02, 29.98];
$result = Stat::tTestTwoSample($group1, $group2);
// ['tStatistic' => 1.6245..., 'pValue' => 0.1444..., 'degreesOfFreedom' => 6.84...]
// One-tailed test: is group1 mean greater than group2 mean?
$result = Stat::tTestTwoSample($group1, $group2, alternative: Alternative::Greater);
// Groups can have different sizes
$result = Stat::tTestTwoSample([1, 2, 3, 4, 5, 6, 7, 8], [3, 4, 5], round: 4);
use HiFolks\Statistics\Stat;
use HiFolks\Statistics\Enums\Alternative;
// Before and after treatment measurements
$before = [200, 190, 210, 220, 215, 205, 195, 225];
$after = [192, 186, 198, 212, 208, 198, 188, 215];
$result = Stat::tTestPaired($before, $after);
// ['tStatistic' => 5.715..., 'pValue' => 0.0007..., 'degreesOfFreedom' => 7]
// One-tailed: did the treatment decrease the values?
$result = Stat::tTestPaired($before, $after, alternative: Alternative::Greater);
$result = Stat::tTestPaired($before, $after, round: 4);
use HiFolks\Statistics\Stat;
// Are these category counts different from a uniform distribution?
$result = Stat::chiSquaredTest([8, 12, 10], round: 4);
// ['chiSquared' => 0.8, 'pValue' => 0.6703, 'degreesOfFreedom' => 2]
// Compare observed counts to expected counts
$result = Stat::chiSquaredTest([18, 32, 50], [20, 30, 50], round: 4);
// ['chiSquared' => 0.3333, 'pValue' => 0.8465, 'degreesOfFreedom' => 2]
use HiFolks\Statistics\Stat;
$table = [
[10, 20, 30],
[6, 9, 17],
];
$result = Stat::chiSquaredIndependence($table, round: 4);
// [
// 'chiSquared' => 0.2716,
// 'pValue' => 0.873,
// 'degreesOfFreedom' => 2,
// 'expected' => [[10.4348, 18.913, 30.6522], [5.5652, 10.087, 16.3478]],
// ]
use HiFolks\Statistics\Stat;
use HiFolks\Statistics\Enums\KdeKernel;
$data = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2];
$f = Stat::kde($data, h: 1.5);
$f(2.5);
// estimated density at x = 2.5
$f = Stat::kde($data, h: 1.5, kernel: KdeKernel::Triangular);
$f(2.5);
$F = Stat::kde($data, h: 1.5, cumulative: true);
$F(2.5);
// estimated CDF at x = 2.5 (probability that a value is <= 2.5)
use HiFolks\Statistics\Stat;
use HiFolks\Statistics\Enums\KdeKernel;
$data = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2];
$rand = Stat::kdeRandom($data, h: 1.5, seed: 8675309);
$samples = [];
for ($i = 0; $i < 10; $i++) {
$samples[] = round($rand(), 1);
}
// [2.5, 3.3, -1.8, 7.3, -2.1, 4.6, 4.4, 5.9, -3.2, -1.6]
$rand = Stat::kdeRandom($data, h: 1.5, kernel: KdeKernel::Triangular, seed: 42);
$rand();
use HiFolks\Statistics\Freq;
$fruits = ['🍈', '🍈', '🍈', '🍉','🍉','🍉','🍉','🍉','🍌'];
$freqTable = Freq::frequencies($fruits);
print_r($freqTable);
$freqTable = Freq::relativeFrequencies($fruits, 2);
print_r($freqTable);
$data = [1,1,1,4,4,5,5,5,6,7,8,8,8,9,9,9,9,9,9,10,10,11,12,12,
13,14,14,15,15,16,16,16,16,17,17,17,18,18, ];
$result = \HiFolks\Statistics\Freq::frequencyTableBySize($data, 4);
print_r($result);
/*
Array
(
[1] => 5
[5] => 8
[9] => 11
[13] => 9
[17] => 5
)
*/
$data = [1,1,1,4,4,5,5,5,6,7,8,8,8,9,9,9,9,9,9,10,10,11,12,12,
13,14,14,15,15,16,16,16,16,17,17,17,18,18, ];
$result = \HiFolks\Statistics\Freq::frequencyTable($data, 5);
print_r($result);
/*
Array
(
[1] => 5
[5] => 8
[9] => 11
[13] => 9
[17] => 5
)
*/
$stat = HiFolks\Statistics\Statistics::make(
[3,5,4,7,5,2]
);
echo $stat->valuesToString(5) . PHP_EOL;
// 2,3,4,5,5
echo "Mean : " . $stat->mean() . PHP_EOL;
// Mean : 4.3333333333333
echo "Count : " . $stat->count() . PHP_EOL;
// Count : 6
echo "Median : " . $stat->median() . PHP_EOL;
// Median : 4.5
echo "First Quartile : " . $stat->firstQuartile() . PHP_EOL;
// First Quartile : 2.5
echo "Third Quartile : " . $stat->thirdQuartile() . PHP_EOL;
// Third Quartile : 5
echo "Mode : " . $stat->mode() . PHP_EOL;
// Mode : 5
use HiFolks\Statistics\Statistics;
$s = Statistics::make(
[98, 90, 70,18,92,92,55,83,45,95,88,76]
);
$a = $s->frequencies();
print_r($a);
/*
Array
(
[18] => 1
[45] => 1
[55] => 1
[70] => 1
[76] => 1
[83] => 1
[88] => 1
[90] => 1
[92] => 2
[95] => 1
[98] => 1
)
*/
$a = $s->relativeFrequencies();
print_r($a);
/*
Array
(
[18] => 8.3333333333333
[45] => 8.3333333333333
[55] => 8.3333333333333
[70] => 8.3333333333333
[76] => 8.3333333333333
[83] => 8.3333333333333
[88] => 8.3333333333333
[90] => 8.3333333333333
[92] => 16.666666666667
[95] => 8.3333333333333
[98] => 8.3333333333333
)
*/
$normalDist = new NormalDist(float $mu = 0.0, float $sigma = 1.0);
$normalDist = new NormalDist(100, 15);
$normalDist->getMean(); // 100.0
$normalDist->getSigma(); // 15.0
$normalDist->getMedian(); // 100.0 (equals mean for normal dist)
$normalDist->getMode(); // 100.0 (equals mean for normal dist)
$normalDist->getVariance(); // 225.0 (sigma squared)
$normalDist->getVarianceRounded(2); // 225.0
$normalDist = NormalDist::fromSamples([2.5, 3.1, 2.1, 2.4, 2.7, 3.5]);
$normalDist->getVarianceRounded(5); // 0.25767
$samples = [2.5, 3.1, 2.1, 2.4, 2.7, 3.5];
$normalDist = NormalDist::fromSamples($samples);
$normalDist->getMeanRounded(5); // 2.71667
$normalDist->getSigmaRounded(5); // 0.50761
$normalDist = new NormalDist(100, 15);
// Generate 5 random samples
$samples = $normalDist->samples(5);
// e.g. [98.3, 112.7, 89.1, 105.4, 101.2]
// Reproducible results with a seed
$samples = $normalDist->samples(1000, seed: 42);
$normalDist = new NormalDist(100, 15);
echo $normalDist->zscore(130); // 2.0 (two std devs above mean)
echo $normalDist->zscore(85); // -1.0 (one std dev below mean)
echo $normalDist->zscoreRounded(114, 3); // 0.933
$normalDist->pdf(float $x): float
$normalDist = new NormalDist(10.0, 2.0);
echo $normalDist->pdf(12.0); // Output: 0.12098536225957168
$normalDist->cdf(float $x): float
$normalDist = new NormalDist(10.0, 2.0);
echo $normalDist->cdf(12.0); // Output: 0.8413447460685429
$normalDist = new NormalDist(10.0, 2.0);
// Calculate PDF at x = 12
$pdf = $normalDist->pdf(12.0);
echo "PDF at x = 12: $pdf\n"; // Output: 0.12098536225957168
// Calculate CDF at x = 12
$cdf = $normalDist->cdf(12.0);
echo "CDF at x = 12: $cdf\n"; // Output: 0.8413447460685429
$normalDist->invCdf(float $p): float
$normalDist = new NormalDist(0.0, 1.0);
// Find the value at the 95th percentile of a standard normal distribution
echo $normalDist->invCdfRounded(0.95, 5); // Output: 1.64485
// The median of a standard normal distribution
echo $normalDist->invCdf(0.5); // Output: 0.0
$normalDist = new NormalDist(100, 15);
// inv_cdf(0.5) equals the mean
echo $normalDist->invCdf(0.5); // Output: 100.0
// Round-trip: cdf(invCdf(p)) ≈ p
echo $normalDist->cdfRounded($normalDist->invCdf(0.25), 2); // Output: 0.25
$normalDist = new NormalDist(0.0, 1.0);
// Quartiles (default)
$normalDist->quantiles(); // [-0.6745, 0.0, 0.6745]
// Deciles
$normalDist->quantiles(10); // 9 cut points
// Percentiles
$normalDist->quantiles(100); // 99 cut points
$n1 = new NormalDist(2.4, 1.6);
$n2 = new NormalDist(3.2, 2.0);
echo $n1->overlapRounded($n2, 4); // 0.8035
// Identical distributions overlap completely
$n3 = new NormalDist(0, 1);
echo $n3->overlap($n3); // 1.0
$birth_weights = NormalDist::fromSamples([2.5, 3.1, 2.1, 2.4, 2.7, 3.5]);
$drug_effects = new NormalDist(0.4, 0.15);
$combined = $birth_weights->add($drug_effects);
$combined->getMeanRounded(1); // 3.1
$combined->getSigmaRounded(1); // 0.5
$birth_weights->getMeanRounded(5); // 2.71667
$birth_weights->getSigmaRounded(5); // 0.50761
$tempFebruaryCelsius = new NormalDist(5, 2.5); # Celsius
$tempFebFahrenheit = $tempFebruaryCelsius->multiply(9 / 5)->add(32); # Fahrenheit
$tempFebFahrenheit->getMeanRounded(1); // 41.0
$tempFebFahrenheit->getSigmaRounded(1); // 4.5
$nd = new NormalDist(100, 15);
$shifted = $nd->subtract(32);
$shifted->getMean(); // 68.0
$shifted->getSigma(); // 15.0 (unchanged)
// Convert Fahrenheit back to Celsius: (F - 32) / (9/5)
$tempFahrenheit = new NormalDist(41, 4.5);
$tempCelsius = $tempFahrenheit->subtract(32)->divide(9 / 5);
$tempCelsius->getMeanRounded(1); // 5.0
$tempCelsius->getSigmaRounded(1); // 2.5
use HiFolks\Statistics\StudentT;
$t = new StudentT(df: 10); // 10 degrees of freedom
$t = new StudentT(5);
$t->pdf(0); // ≈ 0.37961 (peak of the distribution)
$t->pdf(2.0); // density at t=2
$t->pdfRounded(0); // 0.38
$t = new StudentT(5);
$t->cdf(0); // 0.5 (symmetric around zero)
$t->cdf(2.0); // ≈ 0.94874
$t->cdfRounded(2.0); // 0.949
$t = new StudentT(10);
$t->invCdf(0.975); // ≈ 2.228 (critical value for 95% two-sided test)
$t->invCdf(0.5); // 0.0 (median)
$t->invCdfRounded(0.975, 3); // 2.228
use HiFolks\Statistics\StreamingStat;
$s = new StreamingStat();
$s->add(1)->add(2)->add(3)->add(4)->add(5);
$s->count(); // 5
$s->sum(); // 15.0
$s->min(); // 1.0
$s->max(); // 5.0
$s->mean(); // 3.0
$s->variance(); // 2.5
$s->stdev(); // 1.5811...
$s->skewness(); // 0.0
$s->kurtosis(); // -1.2
use HiFolks\Statistics\Utils\Arr;
$runners = [
['name' => 'Alice', 'age' => 30, 'score' => 95],
['name' => 'Bob', 'age' => 25, 'score' => 87],
];
[$ages, $scores] = Arr::extract($runners, ['age', 'score']);
// $ages = [30, 25], $scores = [95, 87]
[$men, $women] = Arr::partition($runners, 'gender', '==', 'M');
[$seniors, $others] = Arr::partition($runners, 'age', '>=', 40);
use HiFolks\Statistics\Utils\Format;
Format::secondsToTime(4845); // "1:20:45"
Format::timeToSeconds('1:20:45'); // 4845
Format::secondsToHms(4845); // ['hours' => 1, 'minutes' => 20, 'seconds' => 45]
php
Format::hmsToSeconds(1, 20, 45); // 4845