This step-wise procedure is copied from a Google Help Page, although I have done this myriad time. I forget easily. To calculate the standard deviation of an array of numbers:
- Work out the Mean (the simple average of the numbers)
- Then for each number: subtract the Mean and square the result.
- Then work out the mean of those squared differences.
- Take the square root of that and we are done!
Also note: The standard deviation is simply the square root of the variance. ...
(So Step 3 is "The Variance" and Step 4 is "The Standard Deviation")
Here is a link that shows exactly how to do it in PHP.
[https://www.geeksforgeeks.org/php-program-find-standard-deviation-array/][1]
Here is that code copied to this post:
To calculate the standard deviation, we have to first calculate the variance. The variance can be calculated as the sum of squares of differences between all numbers and means. Finally to get the standard deviation we will use the formula, √(variance/no_of_elements).
Below is the implementation in PHP to calculate the standard deviation:
<?php
// function to calculate the standard deviation
// of array elements
function Stand_Deviation($arr)
{
$num_of_elements = count($arr);
$variance = 0.0;
// calculating mean using array_sum() method
$average = array_sum($arr)/$num_of_elements;
foreach($arr as $i)
{
// sum of squares of differences between
// all numbers and means.
$variance += pow(($i - $average), 2);
}
return (float)sqrt($variance/$num_of_elements);
}
// Input array
$arr = array(2, 3, 5, 6, 7);
print_r(Stand_Deviation($arr));
?>