Skip to content
Related Articles

Related Articles

Improve Article

Program to implement standard error of mean

  • Last Updated : 21 May, 2018

Standard error of mean (SEM) is used to estimate the sample mean dispersion from the population mean. The standard error along with sample mean is used to estimate the approximate confidence intervals for the mean. It is also known as standard error of mean or measurement often denoted by SE, SEM or SE.

Examples:

Input : arr[] = {78.53, 79.62, 80.25, 81.05, 83.21, 83.46}
Output : 0.8063

Input : arr[] = {5, 5.5, 4.9, 4.85, 5.25, 5.05, 6.0}
Output : 0.1546

Sample mean



Sample Standard Deviation



Estimate standard error of mean



Explanation:

given an array arr[] = {78.53, 79.62, 80.25, 81.05, 83.21, 83.46}
and the task is to find standard error of mean.
mean = (78.53 + 79.62 + 80.25 + 81.05 + 83.21 + 83.46) / 6
= 486.12 / 6
= 81.02
Sample Standard deviation = sqrt((78.53 – 81.02)2 + (79.62- 81.02)2 + . . .
+ (83.46 – 81.02)2 / (6 – 1))
= sqrt(19.5036 / 5)
= 1.97502
Standard error of mean = 1.97502 / sqrt(6)
= 0.8063

C++




// C++ Program to implement 
// standard error of mean.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find sample mean.
float mean(float arr[], int n)
{   
    // loop to calculate 
    // sum of array elements.
    float sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + arr[i];
      
    return sum / n;
}
  
// Function to calculate sample
// standard deviation.
float SSD(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 calculate sample error.
float sampleError(float arr[], int n)
{    
    // Formula to find sample error.
    return SSD(arr, n) / sqrt(n);
}
  
// Driver function
int main()
{
    float arr[] = { 78.53, 79.62, 80.25,
                    81.05, 83.21, 83.46 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << sampleError(arr, n);    
    return 0;
}

Java




// Java Program to implement
// standard error of mean.
  
class GFG {
  
    // Function to find sample mean.
    static float mean(float arr[], int n)
    {
        // loop to calculate
        // sum of array elements.
        float sum = 0;
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
  
        return sum / n;
    }
  
    // Function to calculate sample
    // standard deviation.
    static float SSD(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 (float)Math.sqrt(sum / (n - 1));
    }
  
    // Function to calculate sample error.
    static float sampleError(float arr[], int n)
    {
        // Formula to find sample error.
        return SSD(arr, n) / (float)Math.sqrt(n);
    }
  
    // Driver function
    public static void main(String[] args)
    {
        float arr[] = { 78.53f, 79.62f, 80.25f, 
                       81.05f, 83.21f, 83.46f };
        int n = arr.length;
        System.out.println(sampleError(arr, n));
    }
}
  
// This code is contributed 
// by  prerna saini

Python3




# Python 3 Program to implement 
# standard error of mean.
import math
  
  
# Function to find sample mean.
def mean(arr, n) :
  
    # loop to calculate 
    # sum of array elements.
    sm = 0
    for i in range(0,n) :
        sm = sm + arr[i]
       
    return sm / n
  
  
# Function to calculate sample
# standard deviation.
def SSD(arr, n) :
    sm = 0
    for i in range(0,n) :
        sm = sm + (arr[i] - mean(arr, n)) * (arr[i] - mean(arr, n))
   
    return (math.sqrt(sm / (n - 1)))
   
   
# Function to calculate sample error.
def sampleError(arr, n) :
  
    # Formula to find sample error.
    return SSD(arr, n) / (math.sqrt(n))
  
   
# Driver function
arr = [ 78.53, 79.62, 80.25, 81.05, 83.21, 83.46]
n = len(arr)
print(sampleError(arr, n))
      
    
# This code is contributed
# by Nikita Tiwari.

C#




// C# Program to implement
// standard error of mean.
using System;
  
class GFG {
  
    // Function to find sample mean.
    static float mean(float []arr, int n)
    {
          
        // loop to calculate
        // sum of array elements.
        float sum = 0;
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
  
        return sum / n;
    }
  
    // Function to calculate sample
    // standard deviation.
    static float SSD(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 (float)Math.Sqrt(sum / (n - 1));
    }
  
    // Function to calculate sample error.
    static float sampleError(float []arr, int n)
    {
          
        // Formula to find sample error.
        return SSD(arr, n) / (float)Math.Sqrt(n);
    }
  
    // Driver code
    public static void Main()
    {
        float []arr = {78.53f, 79.62f, 80.25f, 
                       81.05f, 83.21f, 83.46f};
        int n = arr.Length;
        Console.Write(sampleError(arr, n));
    }
}
  
// This code is contributed by Nitin Mittal.

PHP




<?php
// PHP Program to implement 
// standard error of mean.
  
// Function to find sample mean.
function mean($arr,$n)
      
    // loop to calculate 
    // sum of array elements.
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
        $sum = $sum + $arr[$i];
      
    return $sum / $n;
}
  
// Function to calculate sample
// standard deviation.
function SSD($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 calculate
// sample error.
function sampleError($arr, $n)
      
    // Formula to find sample error.
    return SSD($arr, $n) / sqrt($n);
}
  
// Driver Code
{
    $arr = array(78.53, 79.62, 80.25,
                 81.05, 83.21, 83.46 );
    $n = sizeof($arr) / sizeof($arr[0]);
    echo sampleError($arr, $n); 
    return 0;
}
  
// This code is contributed by nitin mittal.
?>


Output:
0.8063

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :