Program to implement standard error of mean
Last Updated :
11 Mar, 2024
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++
#include <bits/stdc++.h>
using namespace std;
float mean( float arr[], int n)
{
float sum = 0;
for ( int i = 0; i < n; i++)
sum = sum + arr[i];
return sum / n;
}
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));
}
float sampleError( float arr[], int n)
{
return SSD(arr, n) / sqrt (n);
}
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
class GFG {
static float mean( float arr[], int n)
{
float sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum = sum + arr[i];
return sum / n;
}
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 ));
}
static float sampleError( float arr[], int n)
{
return SSD(arr, n) / ( float )Math.sqrt(n);
}
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));
}
}
|
C#
using System;
class GFG {
static float mean( float []arr, int n)
{
float sum = 0;
for ( int i = 0; i < n; i++)
sum = sum + arr[i];
return sum / n;
}
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));
}
static float sampleError( float []arr, int n)
{
return SSD(arr, n) / ( float )Math.Sqrt(n);
}
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));
}
}
|
JavaScript
<script>
function mean(arr, n)
{
let sm = 0
for ( var i = 0; i < n; i++)
sm = sm + arr[i]
return sm / n
}
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 sampleError(arr, n)
{
return SSD(arr, n) / (Math.sqrt(n))
}
let arr = [ 78.53, 79.62, 80.25, 81.05, 83.21, 83.46]
let n = arr.length
console.log(sampleError(arr, n))
</script>
|
PHP
<?php
function mean( $arr , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum = $sum + $arr [ $i ];
return $sum / $n ;
}
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 sampleError( $arr , $n )
{
return SSD( $arr , $n ) / sqrt( $n );
}
{
$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;
}
?>
|
Python3
import math
def mean(arr, n) :
sm = 0
for i in range ( 0 ,n) :
sm = sm + arr[i]
return sm / n
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 )))
def sampleError(arr, n) :
return SSD(arr, n) / (math.sqrt(n))
arr = [ 78.53 , 79.62 , 80.25 , 81.05 , 83.21 , 83.46 ]
n = len (arr)
print (sampleError(arr, n))
|
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):
- Importing statistics: The code begins by importing the statistics module, which provides functions for mathematical statistics of numeric data.
- sample_error Function: This function takes an array (arr) containing numeric data as input and calculates the sample error.
- 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.
- 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
def sample_error(arr):
std_dev = statistics.stdev(arr)
sample_err = std_dev / ( len (arr) * * 0.5 )
return sample_err
arr = [ 78.53 , 79.62 , 80.25 , 81.05 , 83.21 , 83.46 ]
print (sample_error(arr))
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...