Blog Detail

13

Apr
Calculate Mathematical Statistics of Data with PHP Functions cover image

arrow_back Calculate Mathematical Statistics of Data with PHP Functions

Statistics is a PHP package that provides functions for calculating mathematical statistics of numeric data.

Installation

You can install this package via composer:

composer require hi-folks/statistics

Usage

Stat class

This class provides methods for calculating mathematical statistics of numeric data. Stat class has methods to calculate an average or typical value from a population or sample like:

  • mean(): arithmetic mean or “average” of data;
  • median(): median or “middle value” of data;
  • medianLow(): low median of data;
  • medianHigh(): high median of data;
  • mode(): single mode (most common value) of discrete or nominal data;
  • multimode(): list of modes (most common values) of discrete or nominal data;
  • quantiles(): cut points dividing the range of a probability distribution into continuous intervals with equal probabilities;
  • thirdQuartile(): 3rd quartile, is the value at which 75 percent of the data is below it;
  • firstQuartile(): first quartile, is the value at which 25 percent of the data is below it;
  • pstdev(): Population standard deviation;
  • stdev(): Sample standard deviation;
  • pvariance(): variance for a population;
  • variance(): variance for a sample;
  • geometricMean(): geometric mean;
  • harmonicMean(): harmonic mean;
  • correlation(): the Pearson’s correlation coefficient for two inputs;
  • covariance(): the sample covariance of two inputs;
  • linearRegression(): return the slope and intercept of simple linear regression parameters estimated using ordinary least squares.

Stat::mean( array $data )

Return the sample arithmetic mean of the array $data. The arithmetic mean is the sum of the data divided by the number of data points. It is commonly called “the average”, although it is only one of many mathematical averages. It is a measure of the central location of the data.

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

Stat::geometricMean( array $data )

The geometric mean indicates the central tendency or typical value of the data using the product of the values (as opposed to the arithmetic mean which uses their sum).

use HiFolks\Statistics\Stat;
$mean = Stat::geometricMean([54, 24, 36], 1);
// 36.0

Statistics class

$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    

Calculate Frequency Table

Statistics packages have some methods for generating Frequency Table:

  • frequencies(): a frequency is the number of times a value of the data occurs;
  • relativeFrequencies(): a relative frequency is the ratio (fraction or proportion) of the number of times a value of the data occurs in the set of all outcomes to the total number of outcomes;
  • cumulativeFrequencies(): is the accumulation of the previous relative frequencies;
  • cumulativeRelativeFrequencies(): is the accumulation of the previous relative ratio.

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
)
 */

This package has a lot more features and options. You can visit its complete details and documentation on Github.

Published at : 13-04-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project