Search code examples
pythonnumpystandard-deviation

Calculate the 3rd standard deviation for an array


Say, I have an array:

import numpy as np

x = np.array([0, 1, 2, 5, 6, 7, 8, 8, 8, 10, 29, 32, 45])

How can I calculate the 3rd standard deviation for it, so I could get the value of +3sigma as shown on the picture below?

enter image description here

Typically, I use std = np.std(x), but to be honest, I don't know if it returns the 1sigma value or maybe 2sigma, or whatever. I'll very grateful for you help. Thank you in advance.


Solution

  • NumPy's std yields the standard deviation, which is usually denoted with "sigma". To get the 2-sigma or 3-sigma ranges, you can simply multiply sigma with 2 or 3:

    print [x.mean() - 3 * x.std(), x.mean() + 3 * x.std()]
    

    Output:

    [-27.545797458510656, 52.315028227741429]
    

    For more detailed information, you might refer to the documentation, which states:

    The standard deviation is the square root of the average of the squared deviations from the mean, i.e., std = sqrt(mean(abs(x - x.mean())**2)).

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html