Program for Variance and Standard Deviation of an array

16 Jul, 2021
Given an array, we need to calculate the variance and standard deviation of the elements of the array. Examples :  
Input  : arr[] = [1, 2, 3, 4, 5]
Output : Variance = 2
         Standard Deviation = 1   

Input  : arr[] = [7, 7, 8, 8, 3]
Output : Variance = 3
         Standard Deviation = 1
 
We have discussed program to find mean of an array. 
Mean is average of element. Mean of arr[0..n-1] = ∑(arr[i]) / n where 0 <= i < nVariance is sum of squared differences from the mean divided by number of elements.Variance = ∑(arr[i] – mean)2 / nStandard Deviation is square root of variance Standard Deviation =  √(variance)Please refer Mean, Variance and Standard Deviation for details.  
Below is the implementation of above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// CPP program to find variance// and standard deviation of// given array.#include <bits/stdc++.h>using namespace std; // Function for calculating varianceint variance(int a[], int n){    // Compute mean (average of elements)    int sum = 0;    for (int i = 0; i < n; i++)        sum += a[i];    double mean = (double)sum /                  (double)n;     // Compute sum squared    // differences with mean.    double sqDiff = 0;    for (int i = 0; i < n; i++)        sqDiff += (a[i] - mean) *                  (a[i] - mean);    return sqDiff / n;} double standardDeviation(int arr[],                         int n){    return sqrt(variance(arr, n));} // Driver Codeint main(){    int arr[] = {600, 470, 170, 430, 300};    int n = sizeof(arr) / sizeof(arr[0]);    cout << "Variance: "         << variance(arr, n) << "\n";    cout << "Standard Deviation: "         << standardDeviation(arr, n) << "\n";    return 0;}
// Java program to find variance// and standard deviation of// given array.import java.io.*; class GFG{     // Function for calculating    // variance    static double variance(double a[],                           int n)    {        // Compute mean (average        // of elements)        double sum = 0;                 for (int i = 0; i < n; i++)            sum += a[i];        double mean = (double)sum /                      (double)n;             // Compute sum squared        // differences with mean.        double sqDiff = 0;        for (int i = 0; i < n; i++)            sqDiff += (a[i] - mean) *                      (a[i] - mean);                 return (double)sqDiff / n;    }         static double standardDeviation(double arr[],                                    int n)    {        return Math.sqrt(variance(arr, n));    }         // Driver Code    public static void main (String[] args)    {         double arr[] = {600, 470, 170, 430, 300};    int n = arr.length;         System.out.println( "Variance: " +                         variance(arr, n));    System.out.println ("Standard Deviation: " +                         standardDeviation(arr, n));         }} // This code is contributed by vt_m.
# Python 3 program to find variance# and standard deviation of# given array.import math # Function for calculating variancedef variance(a, n):     # Compute mean (average of    # elements)    sum = 0    for i in range(0 ,n):        sum += a[i]    mean = sum /n     # Compute sum squared    # differences with mean.    sqDiff = 0    for i in range(0 ,n):        sqDiff += ((a[i] - mean)                * (a[i] - mean))    return sqDiff / n  def standardDeviation(arr, n):     return math.sqrt(variance(arr, n)) # Driver Codearr = [600, 470, 170, 430, 300]n = len(arr)print("Variance: ", int(variance(arr, n)))print("Standard Deviation: ",      round(standardDeviation(arr, n), 3)) # This code is contributed by Smitha
// C# program to find variance and// standard deviation of given array.using System; class GFG{     // Function for calculating    // variance    static float variance(double []a,                          int n)    {                 // Compute mean (average        // of elements)        double sum = 0;                 for (int i = 0; i < n; i++)            sum += a[i];                     double mean = (double)sum /                      (double)n;             // Compute sum squared        // differences with mean.        double sqDiff = 0;                 for (int i = 0; i < n; i++)            sqDiff += (a[i] - mean) *                      (a[i] - mean);                 return (float)sqDiff / n;    }         static float standardDeviation(double []arr,                                   int n)    {        return (float)Math.Sqrt(variance(arr, n));    }         // Driver Code    public static void Main ()    {             double []arr = {600, 470, 170, 430, 300};        int n = arr.Length;                 Console.WriteLine( "Variance: " +                            variance(arr, n));                                         Console.WriteLine ("Standard Deviation: " +                            standardDeviation(arr, n));             }} // This code is contributed by vt_m.
<?php// PHP program to find variance// and standard deviation of// given array. // Function for calculating.// variancefunction variance( $a, $n){    // Compute mean (average    // of elements)    $sum = 0;    for ( $i = 0; $i < $n; $i++)        $sum += $a[$i];    $mean = $sum / $n;     // Compute sum squared    // differences with mean.    $sqDiff = 0;    for ( $i = 0; $i < $n; $i++)        $sqDiff += ($a[$i] - $mean) *                   ($a[$i] - $mean);    return $sqDiff / $n;} function standardDeviation($arr, $n){    return sqrt(variance($arr, $n));} // Driver Code$arr = array(600, 470, 170, 430, 300);$n = count($arr);echo "Variance: " ,      variance($arr, $n) , "\n";echo"Standard Deviation: " ,     standardDeviation($arr, $n) ,"\n"; // This code is contributed by anuj_67.?>
<script> // JavaScript program to find variance and// standard deviation of given array.     // Function for calculating    // variance         function variance(a,  n)    {                 // Compute mean (average of elements)                 var sum = 0;                 for (var i = 0; i < n; i++){            sum += a[i];         }                     var mean = sum / n;             // Compute sum squared        // differences with mean.                 var sqDiff = 0;                 for (var i = 0; i < n; i++) {            sqDiff += (a[i] - mean) * (a[i] - mean);        }                 return sqDiff / n;    }         function standardDeviation(arr , n)    {        return Math.sqrt(variance(arr, n));    }         // Driver Code              var arr = [600, 470, 170, 430, 300]        var n = arr.length;                 document.write( "Variance: " +         variance(arr, n) + "<br>");                                         document.write ("Standard Deviation: " +        standardDeviation(arr, n).toFixed(3));              </script>
Output:  
Variance: 21704
Standard Deviation: 147.323
Time complexity of the program is O(n).This article is contributed by Himanshu Ranjan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
Smitha Dinesh Semwal
Akanksha_Rai
bunnyram19
2019ucp1423
statistical-algorithms
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Introduction to Arrays
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Subset Sum Problem | DP-25
Introduction to Data Structures
Python | Using 2D arrays/lists the right way