Reputation: 5378
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?
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.
Upvotes: 16
Views: 28262
Reputation: 693
For anyone who stumbles on this, use something like this - which gives you a rounded column of which standard deviation based on some other column's values:
# get percentile and which standard deviation for daily decline pct change
def which_std_dev(row,df,col):
std_1 = round(df[col].mean() + 1 * df[col].std(),0)
std_2 = round(df[col].mean() + 2 * df[col].std(),0)
std_3 = round(df[col].mean() + 3 * df[col].std(),0)
std_4 = round(df[col].mean() + 4 * df[col].std(),0)
std_5 = round(df[col].mean() + 5 * df[col].std(),0)
std_6 = round(df[col].mean() + 6 * df[col].std(),0)
std_7 = round(df[col].mean() + 7 * df[col].std(),0)
std_8 = round(df[col].mean() + 8 * df[col].std(),0)
std_9 = round(df[col].mean() + 9 * df[col].std(),0)
std_10 = round(df[col].mean() + 10 * df[col].std(),0)
if row[col] <= std_1:
return 1
elif row[col] > std_1 and row[col] < std_2:
return 2
elif row[col] >= std_2 and row[col] < std_3:
return 3
elif row[col] >= std_3 and row[col] < std_4:
return 4
elif row[col] >= std_4 and row[col] < std_5:
return 5
elif row[col] >= std_6 and row[col] < std_6:
return 6
elif row[col] >= std_7 and row[col] < std_7:
return 7
elif row[col] >= std_8 and row[col] < std_8:
return 8
elif row[col] >= std_9 and row[col] < std_9:
return 9
else:
return 10
df_day['percentile'] = round(df_day['daily_decline_pct_change'].rank(pct=True),3)
df_day['which_std_dev'] = df_day.apply(lambda row: which_std_dev(row,df_day,'daily_decline_pct_change'), axis = 1)
Upvotes: 0
Reputation: 17942
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
Upvotes: 25