Z-Scores(Standard Deviation and Mean) in PHP

z-Scores(standard deviation and mean) in PHP

to calculate the mean you can do:

$mean = array_sum($array)/count($array)

standard deviation is like so:

// Function to calculate square of value - mean
function sd_square($x, $mean) { return pow($x - $mean,2); }

// Function to calculate standard deviation (uses sd_square)
function sd($array) {
// square root of sum of squares devided by N-1
return sqrt(array_sum(array_map("sd_square", $array, array_fill(0,count($array), (array_sum($array) / count($array)) ) ) ) / (count($array)-1) );
}

right off this page

php statistics z-score normal distributions

The following code will give a good approximation of the CDF (Abramowitz & Stegun (1964))

function normal_pdf($x) {
return exp(-$x * $x / 4) / sqrt(2 * M_PI);
}

function normal_cdf($x) {
$b = array(0.2316419, 0.319381530, -0.356563782, 1.781477937, -1.821255978, 1.330274429);
$t = 1 / (1 + $b[0] * $x);
$result = 0;
for ($i = 1; $i < 6; $i++) {
$result += $b[$i] * pow($t, $i);
}
return 1 - normal_pdf($x) * $result;
}

This assumes a standard normal distribution. Recall that to standardize, use z = (x - mean) / (standard deviation)

Z-score to percentile in PHP

Simplest way is aboulang2002 at yahoo dot com's contribute on PHP manual:

<?
function erf($x)
{
$pi = 3.1415927;
$a = (8*($pi - 3))/(3*$pi*(4 - $pi));
$x2 = $x * $x;

$ax2 = $a * $x2;
$num = (4/$pi) + $ax2;
$denom = 1 + $ax2;

$inner = (-$x2)*$num/$denom;
$erf2 = 1 - exp($inner);

return sqrt($erf2);
}

function cdf($n)
{
if($n < 0)
{
return (1 - erf($n / sqrt(2)))/2;
}
else
{
return (1 + erf($n / sqrt(2)))/2;
}
}

$zscore = MYZSCORE;
print 'Percentile: ' . cdf($zscore) * 100 . "\n";
?>

How to calculate if a value is outside one standard deviation in PHP

Not sure if this is what you want

$json = '[["1258765200","12350"],["1259370000","13000"],["1259974800","11840"],["1260579600","16359"],["1261184400","14230"],["1261789200","07406"],["1262394000","12846"],["1262998800","11204"],["1263603600","10234"]]';
$json = json_decode ( $json, true );
$values = array ();
foreach ( $json as $value ) {
$values [] = $value [1]; // Get Values
}

$median = median ( $values );
$sd = stddev ( $values );
$percentage = ($sd / $median) * 100;
$benchmark = 95 / 100;

if($percentage > $benchmark)
{

echo "outside 1 standard deviation (95%)";
}

Output

outside 1 standard deviation (95%)

Functions

function stddev($array) {
$n = 0;
$mean = 0;
$M2 = 0;
foreach ( $array as $x ) {
$n ++;
$delta = $x - $mean;
$mean = $mean + $delta / $n;
$M2 = $M2 + $delta * ($x - $mean);
}
$variance = $M2 / ($n - 1);
return sqrt ( $variance );
}

function median($arr) {
sort($arr);
$count = count($arr); //total numbers in array
$middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
if($count % 2) { // odd number, middle is the median
$median = $arr[$middleval];
} else { // even number, calculate avg of 2 medians
$low = $arr[$middleval];
$high = $arr[$middleval+1];
$median = (($low+$high)/2);
}
return $median;
}

PHP Standard Deviation. Array_map function not found inside class function

This kind of sidesteps the issue (and you should address it), but sq_square is small enough you may want to consider simply inlining

Change:

return sqrt(array_sum(array_map("sd_square", $array, array_fill(0,count($array), (array_sum($array) / count($array)) ) ) ) / (count($array)) );

To:

return sqrt(array_sum(array_map(function ($x, $mean) { return pow($x - $mean,2); }, $array, array_fill(0,count($array), (array_sum($array) / count($array)) ) ) ) / (count($array)) );

Or:

$sd_square = function ($x, $mean) { return pow($x - $mean,2); };
return sqrt(array_sum(array_map($sd_square, $array, array_fill(0,count($array), (array_sum($array) / count($array)) ) ) ) / (count($array)) );

Can we calculate range of a data if we only know mean, SD and N?

Generally speaking, in order to estimate the range of your data, you need to know the distribution of the data, the mean and the standard deviation.

There are some particular/special cases whereby knowing only the mean, SD and N you can find the range but I do not think that this applies to your case unless I miss something. Think that you have the following equations:

  1. (X1+X2+...+X43)/43 = 42.2
  2. ((X1-mean)^2+...+(X43-mean)^2)/(43-1) = 9.61^2

and you need to find the min and max Xi.

Quicker Mean and Standard Deviation Calculation for Large Array

Use sliding_window_view:

window_view = np.lib.stride_tricks.sliding_window_view(pixel_array, (5, 5))

window_view_mean = window_view.mean(axis=(2, 3))
window_view_std = window_view.std(axis=(2, 3))


Related Topics



Leave a reply



Submit