2017-08-03 39 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