standard deviation
data analysis
statistics
mathematical calculations
tutorial

How to calculate a standard deviation array

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Standard deviation is a statistical measure that quantifies the amount of variation or dispersion in a set of data values. It provides insight into how spread out the data points are from the mean (average) of the dataset. Understanding standard deviation is crucial in fields such as finance, research, and engineering since it helps in understanding the variability and reliability of data.

In this article, we will explore how to calculate standard deviation for an array, the mathematical underpinnings, and practical examples to clarify the process. Additionally, we will discuss how standard deviation can be applied in different scenarios.

Definition and Formula

The standard deviation is the square root of the variance. The variance itself is the average of the squared differences from the mean. The standard deviation is denoted by σσ (sigma) for a population and ss for a sample.

Formula for Calculating Standard Deviation

For a population:

σ=_i=1N(x_iμ)2Nσ = \sqrt{\frac{\sum\_{i=1}^{N} (x\_i - \mu)^2}{N}}

For a sample:

s=_i=1n(x_ixˉ)2n1s = \sqrt{\frac{\sum\_{i=1}^{n} (x\_i - \bar{x})^2}{n-1}}

where: • NN is the number of data points in the population. • nn is the number of data points in the sample. • xix_i is each data point in the dataset. • μ\mu is the mean of the population. • xˉ\bar{x} is the mean of the sample.

Step-by-Step Calculation

Example

Let's calculate the standard deviation for the following array of numbers considering it as a sample: [4, 8, 6, 5, 3].

  1. Calculate the Mean:

xˉ=4+8+6+5+35=5.2\bar{x} = \frac{4 + 8 + 6 + 5 + 3}{5} = 5.2

  1. Calculate Each Deviation from the Mean:
    • 4: (45.2)=1.2(4 - 5.2) = -1.2 • 8: (85.2)=2.8(8 - 5.2) = 2.8 • 6: (65.2)=0.8(6 - 5.2) = 0.8 • 5: (55.2)=0.2(5 - 5.2) = -0.2 • 3: (35.2)=2.2(3 - 5.2) = -2.2
  2. Square Each Deviation:
    -1.22=1.44\text{-1.2}^2 = 1.442.82=7.842.8^2 = 7.840.82=0.640.8^2 = 0.64-0.22=0.04\text{-0.2}^2 = 0.04-2.22=4.84\text{-2.2}^2 = 4.84
  3. Calculate the Variance:

s2=1.44+7.84+0.64+0.04+4.844=3.70s^2 = \frac{1.44 + 7.84 + 0.64 + 0.04 + 4.84}{4} = 3.70

  1. Calculate the Standard Deviation:

s=3.701.92s = \sqrt{3.70} \approx 1.92

Summary Table

StepDescription
Calculate MeanCompute the average of the data
Find DeviationsSubtract the mean from each data point
Square DeviationsSquare each deviation value
Calculate VarianceFind average of squared deviations
Calculate Standard DeviationTake square root of the variance

Considerations

Population vs. Sample: Choose the right formula based on whether you have the entire population or a sample. The divisor in the variance formula differs (NN for population, n1n-1 for sample, known as Bessel's correction). • Units: The units of standard deviation are the same as the dataset, unlike variance which is squared. • Outliers: Be cautious as extreme values can affect the standard deviation.

Applications

Quality Control: In manufacturing, ensure product consistency. • Risk Assessment: In finance, measure investment risk. • Scientific Research: Assess experiment reliability and data precision.

Conclusion

Standard deviation is a fundamental statistical tool that aids in understanding the variability within a dataset. By following a structured approach to compute standard deviation, you can gain insights into the data's spread and make more informed decisions. Whether working with a full population or a sample, applying the correct formula ensures accurate analysis.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.