Program to implement standard deviation of grouped data

Given a class interval and frequency of the class and the task is to find standard deviation of grouped data.
Formula to find standard deviation

Standard Deviation = √( (∑(F x M2 – n x μ2)) / (n-1) )

Where,
F – frequency of the class.
M – mid value of class interval.
μ – Mean of the grouped data.
n – sum of frequency.



Examples:

Input : lower_limit[] = {50, 61, 71, 86, 96}
        upper_limit[] = {60, 70, 85, 95, 100}
        freq[] = {9, 7, 9, 12, 8}
Output : 15.757
Explanation : 
Standard deviation = sqrt(287127.75 - 45 * 
             78.3444 * 78.3444) / (45 - 1)
           = 15.757

Input : lower_limit[] = {37, 47, 57, 67}
        upper_limit[] = {46, 56, 66, 76}
        freq[] = {19, 23, 27, 28}
Output : 10.9817

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to implement standard 
// deviation of grouped data.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find mean of grouped data.
float mean(float mid[], int freq[], int n)
{
    float sum = 0, freqSum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + mid[i] * freq[i];
        freqSum = freqSum + freq[i];
    }
    return sum / freqSum;
}
  
// Function to find standard
// deviation of grouped data.
float groupedSD(float lower_limit[],
                float upper_limit[],
                int freq[], int n)
{
    float mid[n], sum = 0, freqSum = 0, sd;
    for (int i = 0; i < n; i++) {
        mid[i] = (lower_limit[i] + upper_limit[i]) / 2;
        sum = sum + freq[i] * mid[i] * mid[i];
        freqSum = freqSum + freq[i];
    }
  
    // Formula to find standard deviation
    // of grouped data.
    sd = sqrt((sum - freqSum * mean(mid, freq, n) * 
              mean(mid, freq, n)) / (freqSum - 1));
    return sd;
}
  
// Driver function.
int main()
{
    // Declare and initialize
    // the lower limit of interval.
    float lower_limit[] = { 50, 61, 71, 86, 96 };
  
    // Declare and initialize
    // the upper limit of interval.
    float upper_limit[] = { 60, 70, 85, 95, 100 };
    int freq[] = { 9, 7, 9, 12, 8 };
  
    // Calculating the size of array.
    int n = sizeof(lower_limit) / sizeof(lower_limit[0]);
  
    cout << groupedSD(lower_limit, upper_limit, freq, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement 
// standard deviation of grouped data.
import java.io.*;
  
class GFG {
      
    // Function to find mean of grouped data.
    static float mean(float mid[], int freq[], int n)
    {
        float sum = 0, freqSum = 0;
        for (int i = 0; i < n; i++) 
        {
            sum = sum + mid[i] * freq[i];
            freqSum = freqSum + freq[i];
        }
        return sum / freqSum;
    }
      
    // Function to find standard
    // deviation of grouped data.
    static float groupedSD(float lower_limit[],
                          float upper_limit[],
                            int freq[], int n)
    {
        float mid[] = new float[n];
        float sum = 0, freqSum = 0, sd;
        for (int i = 0; i < n; i++) 
        {
            mid[i] = (lower_limit[i] + upper_limit[i]) / 2;
            sum = sum + freq[i] * mid[i] * mid[i];
            freqSum = freqSum + freq[i];
        }
      
        // Formula to find standard deviation
        // deviation of grouped data.
        sd = (float)Math.sqrt((sum - freqSum * mean(mid, freq, n) * 
                        mean(mid, freq, n)) / (freqSum - 1));
        return sd;
    }
  
    // Driver function.
    public static void main (String[] args)
    {
        // Declare and initialize
        // the lower limit of interval.
        float lower_limit[] = { 50, 61, 71, 86, 96 };
      
        // Declare and initialize
        // the upper limit of interval.
        float upper_limit[] = { 60, 70, 85, 95, 100 };
        int freq[] = { 9, 7, 9, 12, 8 };
      
        // Calculating the size of array.
        int n = lower_limit.length;
      
        System.out.println( groupedSD(lower_limit, 
                            upper_limit, freq, n));
              
    }
}
  
// This code is contributed by vt_m

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program to implement standard
# deviation of grouped data.
  
import math
  
# Function to find mean of grouped data.
def mean( mid, freq, n):
  
    sum = 0
    freqSum = 0
    for i in range(0,n):
        sum = sum + mid[i] * freq[i]
        freqSum = freqSum + freq[i]
      
    return sum / freqSum
  
   
# Function to find standard
# deviation of grouped data.
def groupedSD(lower_limit, upper_limit ,freq , n):
  
    mid=[[0] for i in range(0,n)]
    sum = 0
    freqSum = 0
    sd=0
    for i in range(0,n):
        mid[i] = (lower_limit[i] + upper_limit[i]) / 2
        sum = sum + freq[i] * mid[i] * mid[i]
        freqSum = freqSum + freq[i]
   
    # Formula to find standard deviation
    # of grouped data.
    sd = math.sqrt((sum - freqSum * mean(mid, freq, n)* mean(mid, freq, n)) / (freqSum - 1))
    return sd
  
  
#  driver code
# Declare and initialize
# the lower limit of interval.
lower_limit= [ 50, 61, 71, 86, 96 ]
   
# Declare and initialize
# the upper limit of interval.
upper_limit= [ 60, 70, 85, 95, 100 ]
freq =[ 9, 7, 9, 12, 8 ]
   
# Calculating the size of array.
n = len(lower_limit)
   
print(groupedSD(lower_limit, upper_limit, freq, n))
  
# This code is contributed by Gitanjali.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement 
// standard deviation of grouped data.
using System;
  
class GFG {
      
    // Function to find mean of grouped data.
    static float mean(float []mid, int []freq, 
                                       int n)
    {
        float sum = 0, freqSum = 0;
        for (int i = 0; i < n; i++) 
        {
            sum = sum + mid[i] * freq[i];
            freqSum = freqSum + freq[i];
        }
        return sum / freqSum;
    }
      
    // Function to find standard
    // deviation of grouped data.
    static float groupedSD(float []lower_limit,
                           float []upper_limit,
                            int []freq, int n)
    {
        float []mid = new float[n];
        float sum = 0, freqSum = 0, sd;
  
        for (int i = 0; i < n; i++) 
        {
            mid[i] = (lower_limit[i] + upper_limit[i]) / 2;
  
            sum = sum + freq[i] * mid[i] * mid[i];
  
            freqSum = freqSum + freq[i];
        }
      
        // Formula to find standard deviation
        // deviation of grouped data.
        sd = (float)Math.Sqrt((sum - freqSum * mean(mid,
             freq, n) * mean(mid, freq, n)) / (freqSum - 1));
          
        return sd;
    }
  
    // Driver function.
    public static void Main ()
    {
        // Declare and initialize
        // the lower limit of interval.
        float []lower_limit = { 50, 61, 71, 86, 96 };
      
        // Declare and initialize
        // the upper limit of interval.
        float []upper_limit = { 60, 70, 85, 95, 100 };
        int []freq = { 9, 7, 9, 12, 8 };
      
        // Calculating the size of array.
        int n = lower_limit.Length;
      
        Console.WriteLine(groupedSD(lower_limit, 
                         upper_limit, freq, n));
              
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to implement standard 
// deviation of grouped data.
  
// Function to find mean of grouped data.
function mean($mid, $freq, $n)
{
    $sum = 0; $freqSum = 0;
    for ( $i = 0; $i <$n; $i++) 
    {
        $sum = $sum + $mid[$i] * 
                       $freq[$i];
        $freqSum = $freqSum + $freq[$i];
    }
    return $sum / $freqSum;
}
  
// Function to find standard
// deviation of grouped data.
function groupedSD($lower_limit,
                   $upper_limit,
                      $freq, $n)
{
    $mid=array(); $sum = 0; 
    $freqSum = 0; $sd;
    for ( $i = 0; $i < $n; $i++) 
    {
        $mid[$i] = ($lower_limit[$i] + 
                    $upper_limit[$i]) / 2;
        $sum = $sum + $freq[$i] * 
               $mid[$i] * $mid[$i];
        $freqSum = $freqSum + $freq[$i];
    }
  
    // Formula to find standard deviation
    // of grouped data.
    $sd = sqrt(($sum - $freqSum
          mean($mid, $freq, $n) * 
          mean($mid, $freq, $n)) / 
                   ($freqSum - 1));
    return $sd;
}
  
    // Driver Code
    // Declare and initialize
    // the lower limit of interval.
    $lower_limit = array(50, 61, 71, 86, 96);
  
    // Declare and initialize
    // the upper limit of interval.
    $upper_limit = array(60, 70, 85, 95, 100);
    $freq = array( 9, 7, 9, 12, 8 );
  
    // Calculating the size of array.
    $n = count($lower_limit);
  
    echo groupedSD($lower_limit, $upper_limit,
                                   $freq, $n);
                                     
// This code is contributed by anuj_67.
?>

chevron_right


Output:

15.757


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.