Mike
Mike

Reputation: 31

simple variance & standard deviation for a numbers array

I've been reading all about standard deviation and variance in PHP, found many examples online here and there, but still none of them gave me the same result as of the Excel.

Say I have:

  $array1 = array(5,46,37,21,8,55,1);

I just want the variance and standard deviation of these numbers within the array...

Any simple straight forward clue?

Upvotes: 0

Views: 2452

Answers (2)

Levac Onlajn
Levac Onlajn

Reputation: 271

https://www.mathsisfun.com/data/standard-deviation.html

class SomeClass
{
    public $variance = 0.0;
    public $deviation = 0.0;

    public function run(array $array = [])
    {
        if (empty($array)) {
            echo "Passed array can't be empty and must contain only numbers.";
            die();
        }

        $this->variance = $this->getVariance($array);
        $this->deviation = $this->getStdDeviation($this->variance);
    }

    /**
    * Calculate variance.
     * Note: If no huge number of items we will not use "Sample" variance (but n items instead: n-1 items)
     *
     * @param array $arrayOfNumbers
     * @return float
     */
    private function getVariance(array $arrayOfNumbers)
    {
        $variance = 0.0;
        $totalElementsInArray = count($arrayOfNumbers);
        // Calc Mean.
        $averageValue = array_sum($arrayOfNumbers) / $totalElementsInArray;

        foreach ($arrayOfNumbers as $item) {
            $variance += pow(abs($item - $averageValue), 2);
        }

        return $variance;
    }

    /**
    * Simple deviation.
    * 
    * @param float $variance
    * @return float
    */
    private function getStdDeviation($variance)
    {
        return (float)sqrt($variance);
    }
}

$stat = new SomeClass;
$stat->run([5,46,37,21,8,55,1]);

echo "\n\nRESULTS:\n";
echo "\nVariance: " . $stat->variance;
echo "\nDeviation: " . $stat->deviation;

Upvotes: 2

user9654671
user9654671

Reputation:

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:

  1. Work out the Mean (the simple average of the numbers)
  2. Then for each number: subtract the Mean and square the result.
  3. Then work out the mean of those squared differences.
  4. 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));

?>

Upvotes: 0

Related Questions