Skip to content

Instantly share code, notes, and snippets.

@kebman
Last active May 17, 2018 14:18
Show Gist options
  • Save kebman/ebb14c314d6f7c778c4f25a8eaa6d871 to your computer and use it in GitHub Desktop.
Save kebman/ebb14c314d6f7c778c4f25a8eaa6d871 to your computer and use it in GitHub Desktop.
Example of how to calculate the Standard Deviation of a dataset using Python 2.7
#!/usr/bin/env python2
import math
# An imaginary dataset of 50 respondents graded from 1-9, so we have an example to play with:
dataset = [1,1,1,2,3,1,1,4,4,5,6,1,4,9,9,7,7,6,7,5,4,3,6,8,9,5,5,6,6,6,5,5,5,5,4,4,4,6,6,4,7,3,7,3,7,4,4,4,4,9]
# Amounts of each: 1*6,2*1,3*4,4*12,5*8,6*8,7*6,8*1,9*4
# Control: 6+1+4+12+8+8+6+1+4=50
# the mean average (the arithmetic mean) is the sum of all the grades added together, divided by the amount of grades given
mean = sum(dataset)/len(dataset)
# the deviation is calculated as (grade-mean)^2 for each grade in the dataset
deviation = [ (grade-mean)**2 for grade in dataset ]
# the variance is simply the mean of the deviations
variance = sum(deviation)/len(deviation)
# lastly, the standard deviation is the square root of the variance
standard_deviation = math.sqrt(variance)
print (standard_deviation)
# You can now use the standard deviation in the integration of Gaussian distributions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment