Search code examples
pythonalgorithmstatisticsmeanstandard-deviation

How do I calculate standard deviation in python without using numpy?


I'm trying to calculate standard deviation in python without the use of numpy or any external library except for math. I want to get better at writing algorithms and am just doing this as a bit of "homework" as I improve my python skills. My goal is to translate this formula into python but am not getting the correct result.

I'm using an array of speeds where speeds = [86,87,88,86,87,85,86]

When I run:

std_dev = numpy.std(speeds)
print(std_dev)

I get: 0.903507902905. But I don't want to rely on numpy. So...

My implementation is as follows:

import math

speeds = [86,87,88,86,87,85,86]

def get_mean(array):
    sum = 0
    for i in array:
        sum = sum + i
    mean = sum/len(array)
    return mean

def get_std_dev(array):
    # get mu
    mean = get_mean(array)
    # (x[i] - mu)**2
    for i in array:
        array = (i - mean) ** 2
        return array
    sum_sqr_diff = 0
    # get sigma
    for i in array:
        sum_sqr_diff = sum_sqr_diff + i
        return sum_sqr_diff
    # get mean of squared differences
    variance = 1/len(array)
    mean_sqr_diff = (variance * sum_sqr_diff)
    
    std_dev = math.sqrt(mean_sqr_diff)
    return std_dev

std_dev = get_std_dev(speeds)
print(std_dev)

Now when I run:

std_dev = get_std_dev(speeds)
print(std_dev)

I get: [0] but I am expecting 0.903507902905

What am I missing here?


Solution

  • speeds = [86,87,88,86,87,85,86]
    
    # Calculate the mean of the values in your list
    mean_speeds = sum(speeds) / len(speeds)
    
    # Calculate the variance of the values in your list
    # This is 1/N * sum((x - mean(X))^2)
    var_speeds = sum((x - mean_speeds) ** 2 for x in speeds) / len(speeds)
    
    # Take the square root of variance to get standard deviation
    sd_speeds = var_speeds ** 0.5
    
    >>> sd_speeds
    0.9035079029052513