Program for Mean Absolute Deviation

Given an array of size n, find mean absolute deviation.
Prerequisite : Mean, Variance and Standard Deviation

Examples:

Input : arr[] = {10, 15, 15, 17, 18, 21}
Output : 2.66667

Input : arr[] = {23.54, 56, 34.56, 67, 45.34,  
                              56.78, 78, 21}
Output : 16.6675



Mean absolute deviation or Average absolute deviation of data set is the average of absolute difference from mean or central point.Mean absolute deviation can be calculated by using the method given below.
Let arr[n] be an array of size n and the task is to find mean absolute deviation.
Mean absolute deviation = (abs(arr[0] – mean) + abs(arr[1] – mean) + . . . + abs(arr[n-1] – mean) / n;
where i = 0, 1, 2, . . . n-1 and “abs” is absolute difference.
mean = (arr[0] + arr[1] + arr[2] + . . . + arr[n-1]) / n;

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find mean absolute
// deviation of given array.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find mean
// of the array elements.
float Mean(float arr[], int n)
{   
    // Calculate sum of all elements.
    float sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + arr[i];
    return sum / n;
}
  
// Function to find mean absolute
// deviation of given elements.
float meanAbsoluteDeviation(float arr[], int n)
{   
    // Calculate the sum of absolute
    // deviation about mean.
    float absSum = 0;
    for (int i = 0; i < n; i++)
        absSum = absSum + abs(arr[i] - Mean(arr, n));
  
    // Return mean absolute deviation about mean.
    return absSum / n;
}
  
// Driver function.
int main()
{
    float arr[] = { 10, 15, 15, 17, 18, 21 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << meanAbsoluteDeviation(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java Program to find mean absolute
// deviation of given array
import java.io.*;
  
class GFG {
          
    // Function to find mean
    // of the array elements.
    static float Mean(float arr[], int n)
    
        // Calculate sum of all elements.
        float sum = 0;
          
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
          
        return sum / n;
    }
      
    // Function to find mean absolute
    // deviation of given elements.
    static float meanAbsDevtion(float arr[],
                                       int n)
    
        // Calculate the sum of absolute
        // deviation about mean.
        float absSum = 0;
          
        for (int i = 0; i < n; i++)
            absSum = absSum + Math.abs(arr[i]
                                - Mean(arr, n));
      
        // Return mean absolute 
        // deviation about mean.
        return absSum / n;
    }
      
        // Driver function.
        public static void main (String[] args) {
          
        float arr[] = { 10, 15, 15, 17, 18, 21 };
        int n = arr.length;
          
            System.out.println(meanAbsDevtion(arr, n));
        }
}
  
  
// This code is contributed by vt_m

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to find
# mean absolute deviation 
# of given array.
  
# Function to find mean
# of the array elements.
def Mean(arr, n) :
  
    # Calculate sum of all
    # elements.
    sm = 0
      
    for i in range(0, n) :
        sm = sm + arr[i]
    return sm // n
      
# Function to find mean 
# absolute deviation of 
# given elements.
def meanAbsoluteDeviation(arr, n) :
      
    # Calculate the sum of 
    # absolute deviation 
    # about mean.
    absSum = 0
    for i in range(0, n ):
        absSum = absSum + abs(arr[i] -
                         Mean(arr, n))
  
    # Return mean absolute 
    # deviation about mean.
    return absSum / n
  
# Driver function.
arr = [ 10, 15, 15, 17, 18, 21 ]
n = len(arr)
  
print(meanAbsoluteDeviation(arr, n))
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find mean absolute
// deviation of given array
using System;
  
class GFG {
          
    // Function to find mean
    // of the array elements.
    static float Mean(float []arr, int n)
    
        // Calculate sum of all elements.
        float sum = 0;
          
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
          
        return sum / n;
    }
      
    // Function to find mean absolute
    // deviation of given elements.
    static float meanAbsDevtion(float []arr,
                                    int n)
    
        // Calculate the sum of absolute
        // deviation about mean.
        float absSum = 0;
          
        for (int i = 0; i < n; i++)
            absSum = absSum + Math.Abs(arr[i]
                                - Mean(arr, n));
      
        // Return mean absolute 
        // deviation about mean.
        return absSum / n;
    }
      
        // Driver function.
        public static void Main () 
        {
              
            float []arr = { 10, 15, 15, 17, 18, 21 };
            int n = arr.Length;
          
            Console.WriteLine(meanAbsDevtion(arr, 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 find mean absolute
// deviation of given array.
  
// Function to find mean
// of the array elements.
function Mean($arr, $n)
      
    // Calculate sum of
    // all elements.
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
        $sum = $sum + $arr[$i];
    return $sum / $n;
}
  
// Function to find mean absolute
// deviation of given elements.
function meanAbsoluteDeviation($arr, $n)
      
    // Calculate the sum of absolute
    // deviation about mean.
    $absSum = 0;
    for ($i = 0; $i < $n; $i++)
        $absSum = $absSum + abs($arr[$i] - 
                         Mean($arr, $n));
  
    // Return mean absolute 
    // deviation about mean.
    return $absSum / $n;
}
  
    // Driver Code
    $arr = array(10, 15, 15, 17, 18, 21);
    $n = sizeof($arr);
    echo meanAbsoluteDeviation($arr, $n);
  
// This code is contributed by nitin mittal.
?>

chevron_right



Output:

2.66667


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 : nitin mittal



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.