Standard deviation (SD, σ) is a measure of spread (or dispersion) of data. High standard deviation tells that data points are spread more above and below the mean.

I’ve edited an image from SlidePlay (Standard Error for AP Biology) where you can see a left image with a bigger standard deviation.

Bigger SD on the left, smaller SD on the right Bigger SD on the left, smaller SD on the right

In statistics, it depends if you have data about the whole population or if you just have a sample. You can also do tweaks depending on your sample size (if you have less than 10 data points).

I’ll use an estimator commonly known as the sample standard deviation.

Sample SD formula Sample SD formula

Ruby code is pretty much simple:

# smaller SD
a1 = [ 40, 45, 35, 42, 38, 39, 40, 41, 36 ]

# larger SD
a2 = [ 1, 100, 200, 50, 20, 400 ]

def sum a
  a.inject(:+)
end

def mean a
  sum(a) / a.length.to_f
end

def sample_variance a
  m = mean a
  sum = a.inject(0){|sum, i| sum + (i-m)**2 }
  sum / (a.length - 1).to_f
end

def sd a
  return Math.sqrt(sample_variance(a))
end

puts sd a1
#=> 3.0459444804161775

puts sd a2
#=> 150.88240454075486