One of my reasons for picking Python as the programming language I wanted to learn was because I would quite like to be able to do data sciences. I’ve spent some time working with micro-controllers with the intent of one day building a weather station that I can use for studying our local climate.
Because of that wish to be able to use python for data sciences, I wanted to try to improve my mathematics skills, so took up the ‘College Algebra with Python’ course. Prior to taking the first lesson, I also decided to familiarize myself with the use of a scientific calculator in case I ended up needing it during the course. (I have been making use of my calculator here and there to check my math and make sure that my algebra is correct when working with pencil and paper, and also to make sure my code outputs make sense.)
The book I was working from while learning to use the scientific calculator had, of course, a section on statistics. Inspired by one of the exercises in the book, and also by the ‘College Algebra with Python’ course, I decided to have a try at writing some code to calculate the standard deviation using the formula provided by the book.
I realize, of course, that there are easier ways of doing this – I know that numpy can be used for statistics as well – but I just thought it might be neat to have a try at coding something using the formula for the standard deviation. =)
So, I thought I’d share – I hope it looks OK! =)
# Program get the standard deviation
import math
def sample_sd(rainfall_values):
summation = 0
rfallsquared = []
total_values = len(rainfall_values)
for values in rainfall_values:
summation += values
rfallsquared.append(values**2)
rfallsquaresum = math.fsum(rfallsquared)
sample_sdev = math.sqrt((rfallsquaresum - (summation)**2 / total_values)/ (total_values - 1))
return sample_sdev
print(f"The standard deviation for the sample is:", sample_sd([1340, 990, 1120, 1736, 260, 1100, 1379, 1125, 1430, 1446, 1459, 1678, 1345, 978, 1002, 1110, 1546, 1672, 1467, 1123]), "mm")