檢測系統異常數量變化的方法

主要利用格拉布斯法則,檢查出最近一次的錯誤數有沒有明顯增多
參考博客 https://blog.csdn.net/weixin_42178492/article/details/81630707
參考文檔 http://www.docin.com/p-730815444.html
https://wenku.baidu.com/view/90cc05a7c8d376eeafaa3151.html

我的數據列表是按時間順序排列的,需求是隻要查看最近時間的錯誤數有沒有突然變多

實現方法

/**
     * 利用格拉布斯準則校驗異常數據
     * @param sampleList
     * @return
     */
    static boolean grubbsCalc(List<Double> sampleList) {
        //因爲格拉布斯準則只能對大於等於3個數據進行判斷,所以數據量小於3時,直接返回
        if (sampleList.size() < 3) {
            return false
        }
        Double currentValue = sampleList.get(sampleList.size() - 1)
        //求出數據平均值和標準差
        double average = calcAverage(sampleList)
        // 我們只關心錯誤數比平均數大的情況
        if(currentValue < average){
            return false
        }
        double standard = calcStandard(sampleList, sampleList.size(), average)

        //與平均值之差
        double diffAvg  = (currentValue>average)?(currentValue-average):(average-currentValue)
        double GCurrent = diffAvg / standard
        // 我的列表大小是10, 置信概率P選0.95  , 
        //查格拉布斯表獲得臨界值:根據選定的P值(此處爲0.95)和測量次數n(此處爲10),查格拉布斯表,橫豎相交得臨界值G95(10)=2.176
        //  private static final double G9510 = 2.176
        if(GCurrent > G9510){
            return true
        }
        return false
    }


   //求平均
   static double calcAverage(ArrayList<Double> sample) {
        double sum = 0
        int cnt = 0
        for (int i = 0; i < sample.size(); i++) {
            sum += sample.get(i)
            cnt++
        }

        return (double) sum / cnt
    }

   //求標準差
    static double calcStandard(ArrayList<Double> array, int n, double average) {
        double sum = 0
        for (int i = 0; i < n; i++) {
            sum += ((double) array.get(i) - average) * ((double) array.get(i) - average)
        }
        return (double) Math.sqrt((sum / (n - 1)))
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章