Open In App

Program to implement standard error of mean

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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


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.


JavaScript




<script>
 
// JavaScript Program to implement
// standard error of mean.
 
// Function to find sample mean.
function mean(arr, n)
{
    // loop to calculate
    // sum of array elements.
    let sm = 0
    for (var i = 0; i < n; i++)
        sm = sm + arr[i]
     
    return sm / n
}
 
 
// Function to calculate sample
// standard deviation.
function SSD(arr, n)
{
    let sm = 0
    for (var i = 0; i < n; i++)
        sm = sm + (arr[i] - mean(arr, n)) * (arr[i] - mean(arr, n))
 
    return (Math.sqrt(sm / (n - 1)))
}
 
 
// Function to calculate sample error.
function sampleError(arr, n)
{
    // Formula to find sample error.
    return SSD(arr, n) / (Math.sqrt(n))
}
 
 
// Driver function
 
let arr = [ 78.53, 79.62, 80.25, 81.05, 83.21, 83.46]
let n = arr.length
console.log(sampleError(arr, n))
     
     
// This code is contributed by phasing17
</script>


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.
?>


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.


Output

0.8063

Time Complexity: O(N2), for calculation of mean N times while calculating Sample Standard Deviation.
Auxiliary Space: O(1), as constant extra space is required.

Python Solution(Using Statistics):

  1. Importing statistics: The code begins by importing the statistics module, which provides functions for mathematical statistics of numeric data.
  2. sample_error Function: This function takes an array (arr) containing numeric data as input and calculates the sample error.
  3. Calculating Sample Standard Deviation: Inside the sample_error function, the sample standard deviation is calculated using the statistics.stdev() function. This function computes the sample standard deviation for the given data.
  4. Calculating Sample Error: Once the sample standard deviation is obtained, the sample error is computed by dividing the standard deviation by the square root of the sample size (len(arr)). This follows the formula for sample error calculation, where sample error equals standard deviation divided by the square root of the sample size.

Python3




import statistics
 
# Function to calculate the sample error
def sample_error(arr):
    # Calculate the sample standard deviation using statistics.stdev() for an unbiased estimator
    std_dev = statistics.stdev(arr)
    # Divide the standard deviation by the square root of the sample size to get the sample error
    sample_err = std_dev / (len(arr) ** 0.5)
    return sample_err
 
# Sample data
arr = [78.53, 79.62, 80.25, 81.05, 83.21, 83.46]
 
# Calculate and print the sample error
print(sample_error(arr))


Time Complexity: O(N)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads