Spencer - 1 year ago 72

PHP Question

I am trying to calculate Z-scores using PHP. Essentially, I am looking for the most efficient way to calculate the mean and standard deviation of a data set (PHP array). Any suggestions on how to do this in PHP?

I am trying to do this in the smallest number of steps.

Answer Source

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