PHP code example of richjenks / stats

1. Go to this page and download the library: Download richjenks/stats 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/ */

    

richjenks / stats example snippets



ichJenks\Stats\Stats;
echo Stats::mean([1, 2, 3]);
// 2

Stats::mean([1, 2, 3]);
// 2

Stats::mean([15, 1000, 68.5, 9]);
// 273.125

Stats::median([1, 2, 3, 4]);
// 2.5

Stats::median([3.141, 1.618, 1.234]);
// 1.618

Stats::mode([1, 2, 2, 3]);
// [2]

`Stats::mode([1, 2, 2, 3, 3]);
// [2, 3]

Stats::frequencies([1, 2, 3]);
// [
//   1 => 1,
//   2 => 1,
//   3 => 1,
// ]

Stats::frequencies([10, 20, 20]);
// [
//   20 => 2,
//   10 => 1,
// ]

Stats::range([1, 9]);
// 8

Stats::range([-41, 1.61803]);
// 42.61803

$data = [1, 2, 3, 4, 5];

Stats::variance($data);
// 2.5

Stats::sd($data);
// 1.5811388301

Stats::deviations([1, 2, 3, 4, 5]);
// [
//   1 => 4,
//   2 => 1,
//   3 => 0,
//   4 => 1,
//   5 => 4,
// ]

Stats::deviations([42, 75, 101, 22.5, 18]);
// [
//   42   => 94.09,
//   75   => 542.89,
//   101  => 2430.49,
//   22.5 => 852.64,
//   18   => 1135.69,
// ]

$data = [1, 2, 3, 4, 5];

Stats::variance($data, Stats::POPULATION);
// 2

Stats::sd($data, Stats::POPULATION);
// 1.4142135624

Stats::sem([1, 2, 3, 4, 5]);
// 0.70710678118655

Stats::quartiles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
// [
//   0 => 1,
//   1 => 3.5,
//   2 => 6.5,
//   3 => 9.5,
//   4 => 12,
// ]

Stats::quartiles([839, 560, 607, 828, 875, 805, 646, 450, 930, 443])
// [
//   0 => 443,
//   1 => 560,
//   2 => 725.5,
//   3 => 839,
//   4 => 930,
// ]

Stats::iqr([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
// 6

Stats::iqr([839, 560, 607, 828, 875, 805, 646, 450, 930, 443])
// 279

Stats::outliers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// []

Stats::outliers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 999])
// [999]

Stats::inliers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 999])
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Stats::whiskers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 999])
// ['lower' => -6, 'upper' => 18]

// Closest Rank
Stats::percentiles([15, 20, 35, 40, 50]);
// [
//   15 => 0,
//   20 => 14,
//   35 => 57,
//   40 => 71,
//   50 => 100,
// ]

Stats::percentile([15, 20, 35, 40, 50], 75);
// [
//   'value'      => 40,
//   'percentile' => 71,
// ]

Stats::intrapercentile([15, 20, 35, 40, 50], 60);
// [
//   15 => 0,
//   20 => 14,
//   35 => 57,
// ]
bash
phpunit --bootstrap Stats.php tests/StatsTest