Search code examples
pythonliststandard-deviationdeviation

Deviation of a list from a reference value


I've got a reference value:

ref = 0.5

And a list:

my_list = [0.4, 0.5, 0.6, 0.5]

I'm trying to calculate how much, on average, the elements of my list differ from my reference value. Basically, it would be the same as a standard deviation, but instead of comparing with the mean of the list I'd like to compare with my reference value.

How could I do that the most efficiently in Python? Knowing that my lists are huge.


Solution

  • You could use numpy:

    import numpy as np
    my_list = [0.4, 0.5, 0.6, 0.5]
    ref = 0.5
    
    my_array = np.array(my_list)
    sd = np.sqrt(np.mean((my_array - ref)**2))
    print(sd) #prints 0.07071067811865474