簡體   English   中英

計算數組的第三個標准偏差

[英]Calculate the 3rd standard deviation for an array

說,我有一個數組:

import numpy as np

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

我如何計算它的第 3 個標准偏差,這樣我就可以得到+3sigma的值,如下圖所示?

在此處輸入圖像描述

通常,我使用std = np.std(x) ,但老實說,我不知道它返回的是1sigma值還是2sigma ,或者其他什么。 我會非常感謝你的幫助。 先感謝您。

NumPy的std產生標准偏差,通常用“sigma”表示。 要獲得2-sigma或3-sigma范圍,您可以簡單地將sigma乘以2或3:

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

輸出:

[-27.545797458510656, 52.315028227741429]

有關更多詳細信息,請參閱文檔,其中說明:

標准偏差是平均偏差平均值的平方根,即std = sqrt(平均值(abs(x-x.mean())** 2))。

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

對於任何偶然發現此問題的人,請使用類似這樣的東西 - 它會為您提供一個圓角列,其中標准偏差基於其他列的值:

# 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM