PHP Salt


A little dash of PHP... my collection of php functions

Version: 2.20.2
Last Build: October 16, 2017 12:40pm (PST)

Main Menu

Home

Get PHP Salt


The Functions

array
file
graph
html
mail
math
misc
mobile
script
string
system
time
psStats()
Statistics


Description:

Returns common statisitcs for an array of numbers


Usage:

$stats = psStats($aryNumbers,[$s]);
$aryNumers = array of numbers
$s = 1 if this is a subset of a dataset


Example:

	$aryNumbers = array(5,6,7,8,10,200);
$stats = psStats($aryNumbers);
print_r($stats);


Output:
Array
(
[n] => 6
[sum] => 236
[avg] => 39.333333333333
[dev] => 71.869480464396
[ddis] => 83.333333333333
[dlo] => -32.536147131063
[dhi] => 111.20281379773
[median] => 7.5
[mode] => 5
[max] => 200
[min] => 5
[range] => 195
[xout] => Array
(
[n] => 5
[sum] => 36
[avg] => 7.2
[dev] => 1.7204650534085
[ddis] => 60
[dlo] => 5.4795349465915
[dhi] => 8.9204650534085
[median] => 7
[mode] => 5
[max] => 10
[min] => 5
[range] => 5
)

[sample] => 0
)




[n] => number of data items
[sum] => sum of data items
[avg] => average (mean)
[dev] => standard deviation
[ddis] => percent of data within std deviation
[dlo] => standard deviation low from mean (avg - dev)
[dhi] => standard deviation high limit from mean (avg + dev)
[median] => median, middle value data
[mode] => mode, the most apearing value in the data
[max] => largest number
[min] => smallest number
[range] => value range (max-min)
[xout] => stats run on data within the std dev range (remove outliers)







Code:


// $s = 1 when using a sample of a data set
// $o = internal use for re-entry, so we dont loop
function psStats($ary,$s=0,$o=1)
{
$sSum = 0;
$sVSum = 0;
$sXSum = 0;

$sN = count($ary);

// get the average and a few other items
foreach ($ary as $key=>$val)
{
$sSum += $val; // used for average
@$asMode[$val]++; // finding the mode
@$asMed[] = $val; // build a keyless set for finding median
}


$sAvg = $sSum / $sN; // calc average


asort($asMode); // sort the mode
//print_r($asMode);
end($asMode);
$sMode = key($asMode);


asort($asMed); // sort the median data
$sMid = floor( $sN / 2 ); // get the median index
if ( $sMid == ($sN/2) )
{
$sML = $asMed[$sMid-1];
$sMH = $asMed[$sMid];
$sMed = ($sML + $sMH) / 2;
//print " ($sML:$sMH) ";
}
else
{
$sMed = $asMed[$sMid];
}



// get the variance
foreach ($ary as $key=>$val)
{
$dif = $val - $sAvg;
$sVSum += ($dif * $dif);
}

// calc std deviation
$sDev = sqrt($sVSum / ($sN - $s) );
$sDevLow = $sAvg - $sDev;
$sDevHigh = $sAvg + $sDev;


// gather a data list exluding outliers past the std deviation
foreach ($ary as $key=>$val)
{
if ( ($val >= $sDevLow) && ($val <= $sDevHigh) )
{
@$saGData[] = $val;
}
}

$sND = count($saGData); // number of data points within std deviation

// get stats for this group exluding outliers
if ( $o == 1 ) // check if we re-enter for deeper stats
{
$asGrp = psStats($saGData,0,0);
unset($asGrp['xout']);
unset($asGrp['sample']);
}


$res['n'] = $sN;
$res['sum'] = $sSum;
$res['avg'] = $sAvg;
$res['dev'] = $sDev;
$res['ddis'] = ($sND/$sN) * 100;
$res['dlo'] = $sDevLow;
$res['dhi'] = $sDevHigh;

$res['median'] = $sMed;
$res['mode'] = $sMode;

$res['max'] = max($ary);
$res['min'] = min($ary);
$res['range'] = max($ary) - min($ary);

if ($o == 1) { $res['xout'] = $asGrp; }


$res['sample'] = $s;

return $res;

}



Changlelog:



See Also

And a shot out to:

LinuxSimple - www.linuxsimple.com
PHP - php.net
Fedora Server - getfedora.com
Shameless ads to pay for site