1
$\begingroup$

I am trying to figure out a way, in Python, how to identify when an event occurs, based on a jump in the rolling standard deviation.

As shown in the plot below, around the 12000th sample, an event occurs. In my Python script, I am currently using a threshold of 0.00051 to signify when that event occurs. However, sometimes the event occurs at 0.0005 and other times the event occurs at 0.000495.

enter image description here

My question is - how can I algorithmically, in Python, detect this jump in the rolling standard deviation to create the event altert? Because if I have a threshold set too low, I don't want it to be triggered too early. And if I run another test, and the threshold is too high, then I don't want the event to not be triggered at all.

Any advice is greatly appreciated!

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()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.