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
// 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; } |
Java
// 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 |
Python3
# 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. |
C#
// 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 |
PHP
<?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. ?> |
15.757
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program for Variance and Standard Deviation of an array
- Mathematics | Mean, Variance and Standard Deviation
- Given N and Standard Deviation, find N elements
- Variance and standard-deviation of a matrix
- Program to implement standard error of mean
- Program for Mean Absolute Deviation
- Standard Normal Distribution (SND) - Java Program
- Program to implement Collatz Conjecture
- Program to implement Simpson's 3/8 rule
- Program to implement t-test
- Program to implement Linear Extrapolation
- Program to implement Inverse Interpolation using Lagrange Formula
- Calculate the IST : Indian Standard Time
- Set in C++ Standard Template Library (STL)
- Implement *, - and / operations using only + arithmetic operator
- Skewness of statistical data
- Data Science - Solving Linear Equations
- Data Science | Solving Linear Equations
- C++ Data Types
- Write a program to calculate pow(x,n)
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

