Program for Coefficient of variation

Given an array of size n and the task is to find Coefficient of variation . Coefficient of variation is the ratio of standard deviation and mean. The main purpose of coefficient of variation is to find study of quality assurance by measuring the dispersion of the population data of a probability or frequency distribution, or by determining the content or quality of the sample data of substances. The method of measuring the ratio of standard deviation to mean is also known as relative standard deviation often abbreviated as RSD.

Examples :

Input : arr[] = {60.25, 62.38, 65.32, 61.41, 63.23}
Output : 0.0307144

Input : arr[] = {15, 36, 53.67, 25.45, 67.8, 56, 78.09}
Output : 0.48177



Approach:

Coefficient of Variation = Standard deviation / mean 
Example:
arr[] = {60.25, 62.38, 65.32, 61.41, 63.23}
mean = 62.518
Standard Deviation = 1.9202
Coefficient of Variation = Standard deviation / mean
                         = 1.9202 / 62.518
                         = 0.0307144

Below is the implementation of the above formula :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to find coefficient of 
// variation of given array.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find mean of given array.
float mean(float arr[], int n)
{
    float sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + arr[i];
    return sum / n;
}
  
// Function to find standard deviation 
// of given array.
float standardDeviation(float arr[], int n)
{
    float sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + (arr[i] - mean(arr, n)) *
                    (arr[i] - mean(arr, n));
  
    return sqrt(sum / (n - 1));
}
  
// Function to find coefficient of variation.
float coefficientOfVariation(float arr[], int n)
{
   return standardDeviation(arr, n) / mean(arr, n);
}
  
// Driver Program
int main()
{
    float arr[] = { 15, 36, 53.67, 25.45, 
                    67.8, 56, 78.09 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << coefficientOfVariation(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java Program to find coefficient of 
// variation of given array
import java.io.*;
  
class GFG {
  
    // Function to find mean of given array.
    static double mean(double arr[], int n)
    {
        double sum = 0;
          
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
        return sum / n;
    }
      
    // Function to find standard  
    // deviation of given array.
    static double standardDeviation(double arr[],
                                            int n)
    {
        double sum = 0;
          
        for (int i = 0; i < n; i++)
            sum = sum + (arr[i] - mean(arr, n)) *
                            (arr[i] - mean(arr, n));
      
        return Math.sqrt(sum / (n - 1));
    }
      
    // Function to find coefficient of variation.
    static double coefficientOfVariation(double arr[],
                                                int n)
    {
    return (standardDeviation(arr, n) / mean(arr, n));
    }
  
    // Driver Program
    public static void main (String[] args) {
          
    double arr[] = { 15, 36, 53.67, 25.45
                              67.8, 56, 78.09 };
    int n = arr.length;
      
    System.out.println( coefficientOfVariation(arr, n));
  
    }
}
  
  
//This article is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to find coefficient 
# of variation of given array.
import math
  
# Function to find mean of
# given array.
def mean(arr, n):
    sum = 0
      
    for i in range(0, n):
        sum = sum + arr[i]
    return (sum / n) 
  
# Function to find standard 
# deviation of given array.
def standardDeviation(arr, n):
    sum = 0
      
    for i in range(0, n):
        sum = (sum + (arr[i] - mean(arr, n)) *
                      (arr[i] - mean(arr, n))) 
  
    return math.sqrt(sum / (n - 1)) 
  
  
# Function to find coefficient
# of variation.
def coefficientOfVariation(arr, n):
    return (standardDeviation(arr, n) /
                          mean(arr, n)) 
  
  
# Driver Program
arr = [15, 36, 53.67, 25.45
            67.8, 56, 78.09
n = len(arr)
  
print(round(coefficientOfVariation(arr, n), 5))
  
# This code is contributed by Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

//C# Program to find coefficient of 
// variation of given array
using System;
  
class GFG {
  
    // Function to find mean of given array.
    static float mean(double []arr, int n)
    {
        double sum = 0;
          
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
        return (float)sum / n;
    }
      
    // Function to find standard 
    // deviation of given array.
    static float standardDeviation(double []arr,
                                            int n)
    {
        double sum = 0;
          
        for (int i = 0; i < n; i++)
            sum = sum + (arr[i] - mean(arr, n)) *
                        (arr[i] - mean(arr, n));
      
        return (float)Math.Sqrt(sum / (n - 1));
    }
      
    // Function to find coefficient of variation.
    static float coefficientOfVariation(double []arr,
                                                int n)
    {
        return(float) (standardDeviation(arr, n) / mean(arr, n));
    }
  
    // Driver Program
    public static void Main () {
          
    double []arr = { 15, 36, 53.67, 25.45, 
                            67.8, 56, 78.09 };
    int n = arr.Length;
      
    Console.WriteLine( coefficientOfVariation(arr, n));
  
    }
}
  
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to find coefficient of 
// variation of given array.
  
  
// Function to find mean
// of given array.
function mean($arr, $n)
{
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
        $sum = $sum + $arr[$i];
    return $sum /$n;
}
  
// Function to find standard 
// deviation of given array.
function standardDeviation($arr, $n)
{
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
        $sum = $sum + ($arr[$i] - 
                mean($arr, $n)) * 
               ($arr[$i] - mean($arr, $n));
  
    return sqrt($sum / ($n - 1));
}
  
// Function to find coefficient of variation.
function coefficientOfVariation($arr, $n)
{
return standardDeviation($arr, $n) / 
                    mean($arr, $n);
}
  
// Driver Code
$arr = array( 15, 36, 53.67, 25.45, 
                  67.8, 56, 78.09 );
$n = count($arr);
echo coefficientOfVariation($arr, $n);
  
// This code is contributed by vt_m.
?>

chevron_right


Output :

0.48177


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.