2017-08-03 58 views
0

我想在Python中找出一種方法,基於滾動標準偏差中的跳轉,如何識別事件何時發生。基於Python中的滾動差異跳轉識別事件

如下圖所示,在第12000個樣本中,出現event。在我的Python腳本中,我目前使用閾值0.00051來表示該事件何時發生。然而,有時event出現在0.0005和其他時間event發生在0.000495

enter image description here

我的問題是 - 我怎樣才能算法,在Python中,檢測到這一跳在滾動標準偏差以創建事件altert?因爲如果我的門檻太低,我不希望太早觸發它。如果我運行另一個測試,並且閾值太高,那麼我不希望事件不會被觸發。

任何意見非常感謝!

def animate(i): 
    data = pd.read_csv("C:\\Users\\Desktop\\data.txt", sep="\[|\]\[|\]",engine = 'python', header = None) 
    data = data.iloc[0, ::4] 
    data = data.astype(str).apply(lambda x: x.split(',')[-1]).astype(float) 
    data.pop(0) 
    xar = range(len(data)) 
    yar = pd.DataFrame(data) 
    # Starting from sample 1050 to get rid of any initial noise 
    yar = yar[1050:12500] 
    xar = xar[1050:12500] 
    std = yar.rolling(window=2500).std() 


    if (np.any(std>.00051)): 
     choices = ["Confirm Event"] 
     reply = easygui.buttonbox("Event Alert!, image, choices) 
     if reply == "Confirm Event": 
      sys.exit(0) 

    ax1.clear() 
    ax1.plot(xar,std) 
    ax1.set_title('Rolling Standard Deviation')  


fig, (ax1) = plt.subplots(1, sharex = True) 
ani = animation.FuncAnimation(fig, animate, interval=.01) 
plt.show() 

編輯瓦特/代碼

import pandas as pd 
import scipy 
import sys 
import numpy as np 
import easygui 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 


def animate(i): 

    data = pd.read_csv("C:\\Users\\Desktop\\data.txt", sep="\[|\]\[|\]",engine = 'python', header = None) 
    data = data.iloc[0, ::4] 
    data = data.astype(str).apply(lambda x: x.split(',')[-1]).astype(float) 
    data.pop(0) 
    xar = range(len(data)) 
    #yar = data.as_matrix() 
    yar = pd.DataFrame(data) 

    L=2500 
    # Starting from sample 1050 to get rid of any initial noise 
    yar = yar[1050:len(data)] 
    xar = xar[1050:len(data)] 

    std = yar.rolling(window=2500).std() 
    std = std.as_matrix() 
    #s = [np.std(yar[ii:ii+L]) for ii in range(1050,len(data))] 

    yar = data.as_matrix() 
    yar = yar[1050:len(data)] 
    d = np.diff(yar) 
    d2 = d*d 

    ## rolling mean of the diff squared: 
    SS = [np.mean(d2[ii:ii+L]) for ii in range(1050, len(d2)-L)] 

    # compute the F statistic 
    F = np.array([SS[ii]/SS[ii+L] for ii in range(1050, len(SS)-L)]) 
    w = np.where(np.less(F,1)) 
    F[w]=1/F[w] 

    # the x coordinate of the point where shift happens is offset by L from the computed F: 
    xi = np.arange(0,len(F))+L 

    ax1.clear() 
    ax2.clear() 
    ax3.clear() 
    ax1.plot(xar, std) 
    ax2.plot(xi, F) 
    ax3.plot(xar, yar) 
    ax1.set_title('Rolling Standard Deviation') 
    ax2.set_title('F - values') 
    ax3.set_title('Original Data') 



fig, (ax1, ax2, ax3) = plt.subplots(3, sharex = True) 
fig.subplots_adjust(hspace=1.5) 
ani = animation.FuncAnimation(fig, animate, interval=.01) 
plt.show() 

我的數據如下所示:

[0.013671875, -0.9599609375, -0.005859375][0.013671875, -0.9599609375, -0.005859375][0.013671875, -0.9599609375, -0.005859375][0.0068359375, -0.8193359375, -0.0029296875][0.0068359375, -0.8193359375, -0.0029296875][0.0068359375, -0.8193359375, -0.0029296875][0.0068359375, -0.8193359375, -0.0029296875][0.0068359375, -0.8193359375, -0.0029296875][0.0068359375, -0.8193359375, -0.0029296875][0.0068359375, -0.8193359375, -0.0029296875][0.0068359375, -0.8193359375, -0.0029296875][0.0087890625, -0.990234375, -0.0048828125][0.0087890625, -0.990234375, -0.0048828125][0.0087890625, -0.990234375, -0.0048828125][0.0087890625, -0.990234375, -0.0048828125][0.0087890625, -0.990234375, -0.0048828125][0.0087890625, -0.990234375, -0.0048828125][0.0068359375, -0.951171875, -0.00390625][0.0068359375, -0.951171875, -0.00390625][0.0068359375, -0.951171875, -0.00390625][0.0068359375, -0.951171875, -0.00390625][0.0068359375, -0.951171875, -0.00390625][0.0068359375, -0.951171875, -0.00390625][0.0068359375, -0.951171875, -0.00390625][0.0068359375, -0.951171875, -0.00390625][0.009765625, -0.9560546875, -0.0048828125][0.009765625, -0.9560546875, -0.0048828125][0.009765625, -0.9560546875, -0.0048828125][0.009765625, -0.9560546875, -0.0048828125][0.009765625, -0.9560546875, -0.0048828125][0.009765625, -0.9560546875, -0.0048828125][0.009765625, -0.9560546875, -0.0048828125][0.009765625, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0068359375, -0.9560546875, -0.0048828125][0.0068359375, -0.9560546875, -0.0048828125][0.0068359375, -0.9560546875, -0.0048828125][0.0068359375, -0.9560546875, -0.0048828125][0.0068359375, -0.9560546875, -0.0048828125][0.0068359375, -0.9560546875, -0.0048828125][0.0068359375, -0.9560546875, -0.0048828125][0.0068359375, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.005859375][0.0087890625, -0.9560546875, -0.005859375][0.0087890625, -0.9560546875, -0.005859375][0.0087890625, -0.9560546875, -0.005859375][0.0087890625, -0.9560546875, -0.005859375][0.0087890625, -0.9560546875, -0.005859375][0.0087890625, -0.9560546875, -0.005859375][0.0087890625, -0.9560546875, -0.005859375][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0087890625, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0087890625, -0.955078125, -0.0048828125][0.0087890625, -0.955078125, -0.0048828125][0.0087890625, -0.955078125, -0.0048828125][0.0087890625, -0.955078125, -0.0048828125][0.0087890625, -0.955078125, -0.0048828125][0.0087890625, -0.955078125, -0.0048828125][0.0087890625, -0.955078125, -0.0048828125][0.0087890625, -0.955078125, -0.0048828125][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.955078125, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125] 
+0

是事件相當罕見?您可以在興趣點前後進行N個點的直線擬合 - 當斜率發生變化時,擬合效果最好。如果坡度的變化發生得早或晚,則配合會變差。這意味着一個「合適的善良」的陰謀將有一個明確的最低限度。這會有幫助嗎? – Floris

+0

是的,該事件只發生一次。你知道一個與你所描述的內容類似的例子嗎? – Gary

+0

我會創建一個例子。你能否給出更多關於如何計算滾動標準偏差的背景?平均有多少個數據點? 「事件」有何不同 - 它是源數據中的單一點還是數據中的噪聲變化? – Floris

回答

1

我想了很長一段時間。事實上,我相信你問的是錯誤的問題(對不起)。正如評論中已經明確指出的那樣,你所尋找的「事件」是「會使我的信號中的噪音增加很多」。那麼檢測這種情況的正確方法是對兩組數據之間的標準偏差(方差)進行統計檢驗。

有一件事情叫做F測試,完全是這樣做的。如果你有兩個樣本並計算它們的方差,你可以回答「F方差與F檢驗不同」這個問題。爲此,需要測試統計(方差的比率)和自由度數(樣本數減1)。然後你可以計算出這個差異是由於偶然性的概率(「你多久看一次同樣分佈的兩個隨機樣本中這麼大的跳躍?) - 一種叫做p值的東西,如果p值足夠小,那麼你可以說「這不僅僅是偶然」,當然,當你運行1000次比較時,你會「偶然」平均獲得0.1%的概率事件一次;所以你需要準備好一些誤報(或接受你將只會看到很大的變化,這是不可能偶然發生的)

我實現了這個方法 - 並且Python代碼如下圖所示,信號變化「難以看到「;但經過分析後,發生變化的點突出於其他點之上(即使我將標準偏差僅增加了10%)

需要注意的一點是:我使用序列的diff作爲我輸入的計算值;這在很大程度上取消了緩慢變化的基礎價值的影響,所以你真的在看純粹的噪音。如果採樣是隨後的採樣是獨立的(即只有在採樣沒有經過低通濾波時才真正起作用;否則,就不採用差分),這樣做效果最好。

我希望代碼是不言自明的;讓我知道你是否需要進一步澄清。您的門檻現在可以根據您願意接受的誤報數量進行概率性設定。

enter image description here

# detect a sudden change in standard deviation of a sequence of measurmeents 
import numpy as np 
import matplotlib.pyplot as plt 
import scipy 

# a sequence of values with a mean and standard deviation 
# and then a sudden change in the standard deviation 
mu = 1000  # mean of signal 
sigma = 100  # standard deviation of signal 
increase = 1.1 # increase in standard deviation 
N1 = 10000  # number of datapoints before change 
L = 2500  # size of rolling window 

# create a series with a slight increase in noise: 
before = np.random.normal(mu,sigma,N1) 
after = np.random.normal(mu, increase*sigma, N1) 

sequence = np.concatenate((before, after), axis=0) 

twoD_img = np.histogram2d(range(0,2*N1), sequence, bins=(50,100)) 

plt.figure(); 
plt.subplot(4,1,1) 
plt.imshow(twoD_img[0].T,aspect='auto', extent = (0, 2*N1, np.min(sequence), np.max(sequence))); 
plt.title('input signal') 

# rolling standard deviation 
s = [np.std(sequence[ii:ii+L]) for ii in range(0,2*N1-L)] 

plt.subplot(4,1,2) 
plt.plot(s) 
plt.title('rolling standard deviation') 

# take the differences, and compute the average noise from that 
d = np.diff(sequence) 
d2 = d*d 

## rolling mean of the diff squared: 
SS = [np.mean(d2[ii:ii+L]) for ii in range(0, len(d2)-L)] 

# compute the F statistic 
F = np.array([SS[ii]/SS[ii+L] for ii in range(0, (len(d2)-2*L))]) 
w = np.where(np.less(F,1)) 
F[w]=1/F[w] 

# the x coordinate of the point where shift happens is offset by L from the computed F: 
xi = np.arange(0,len(F))+L 

plt.subplot(4,1,3) 
plt.plot(xi, F); 
plt.title('F values') 
plt.xlabel('datapoint #') 

# compute log of probability that this is by chance: 
logProb = np.log(1-scipy.stats.f.cdf(F, dfn=L-1, dfd=L-1)) 

plt.subplot(4,1,4) 
plt.plot(xi, logProb) 
plt.title('log probability plot') 
plt.xlabel('datapoint #') 

# make some space for the labels 
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.1, hspace=0.4) 

# and draw the result: 
plt.show() 
+0

偉大的寫作 - 謝謝!您是否建議我在F值上使用閾值來觸發「事件警報」? – Gary

+0

是的,這正是我所推薦的。您選擇的級別將決定您可以檢測到的變化有多少,但也會確定您會得到多少誤報。越敏感,誤報越多。說得通? – Floris

+0

或者,如果您使用p值,您將更直接地瞭解誤報的概率 - 在給定的樣本量下,您只需計算一次相應的F值。 – Floris