PHP Program to calculate standard deviation

What is meant by standard deviation?

standard deviation is a measure, that is used to quantify the amount of variation in a set of data values. Standard deviations are classified into two types. They are :

1. Low Standard Deviation

2. High Standard Deviation

Data with a low standard deviation are more likely to be close to the expected value of the set. A high standard deviation, however, indicates more variation between the data points. Standard deviation is defined mathematically as the square root of variance. Standard deviation differs from variance in that it is expressed in the same units as the data. Another measure of deviation including Mean, Absolute Deviation, provides different mathematical properties from standard deviation. As a measure of confidence in statistical conclusions, the standard deviation is commonly used.  
To calculate standard deviation we first have to find the mean (To find the mean we have to add all the data and divide it by the number of data), Then we have to calculate the variance(To calculate variance we have to subtract the mean from the value of the data point. Each of those resulting values is then squared, and after that, we have to add those results together. The result is then divided by the number of data points less one. The square root of the variance calculated before is then used to find the standard deviation.

How to calculate the standard deviation using PHP?

To calculate the standard deviation of the numbers first we have to initialize an array a[] with values in it. After that, we have to assign the elements of the array a[] into the variable num and assign the return value of the user-defined function find_sd() with the array a[] as the argument into the variable sd. In the function find_sd() we first have to assign the size of the array a[] to the variable count, the value 0 into the variable v, and  the computed value of 'array_sum(a) / count' into the variable avg. After that we have to perform the foreach loop for every element in the array a[] in the block of loop we have to assigh the computed value of 'v + pow((i - avg),2)' to the variable  and after the completion of the loop we can return the computed value of 'sqrt(v / count)'. And at last we can print the value of the variable sd as the standard deviation of the entered numkbers.

ALGORITHM

Step 1: Initialize an array a[] with values in it

Step 2: Assign the elements of the array a[] into the variable num

Step 3: Assign the return value of the user-defined function find_sd() with the array a[] as the argument into the variable sd

Step 4: Print the value of the variable sd as the standard deviation of the entered numbers

ALGORITHM function : find_sd(a)

Step 1: Assign the size of the array a[] to the variable count

Step 2: Assign the value 0 into the variable v

Step 3: Assign the computed value of 'array_sum(a) / count' into the variable avg

Step 4: Perform the sub-step for every element in the array a[] using foreach loop

        (i) Assigh the computed value of 'v + pow((i - avg),2)' to the variable v

Step 5: Return the computed value of 'sqrt(v / count)'

PHP Source Code

                                          <?php
function find_sd($a)
{
    $count = count($a);
    $v = 0;
    $avg = array_sum($a) / $count;
    foreach ($a as $i) {
        $v += pow(($i - $avg), 2);
    }
    return sqrt($v / $count);
}
$a = array(5, 9, 4, 3, 2);
$num = '';
foreach ($a as $x) {
    $num = $num . " " . $x;
}
$sd = find_sd($a);
echo "The standard deviation of $num is $sd";
?>
                                      

OUTPUT

The standard deviation of  5 9 4 3 2 is 2.4166091947189