# Time series analysis with recurring event (python)

I Have a vector of n values and I have event that occurs occasionally throughout this vector. So I have

V1 = [300, 120, 450, 700, 880, 400, 100, 60, 44, 91]


and the indexes of the start and end of the event. So indexes = [(2,4)] means that the event start at position #2 (when the value was 450) and ended at 4 (when the value was 880).

I want to check if there is a significance difference in v1 when event happens, meaning wether [450, 700, 880] has different distribution and higher mean then the rest of v1.

What is the best way to do this?

If your data are continuous, then you can use a Kolmogorov-Smirnov 2-sample test to test the hypothesis that they are the same. If your data are discrete, you can try the Anderson-Darling test, which does support continuous data in scipy as well. Another quick check is to use z-scores and set some threshold around the number SDs.
Not sure if you want to exclude your events' values when comparing (makes some assumptions about your data so that is up to you), but here is an example:

import numpy as np
from scipy import stats

V1 = [300, 120, 450, 700, 880, 400, 100, 60, 44, 91]
events = [(2,4), (7,8)]

for e in events:
e_vals = V1[e[0]:e[1]]
noe_vals = [V1[i] for i in range(0,len(V1)) if i not in range(e[0],e[1])]
print("Event ", e, ": ", stats.ks_2samp(noe_vals, e_vals))
print("Event ", e, ": ", stats.anderson_ksamp([noe_vals, e_vals]))
print("Event ", e, ":  z-scores(", stats.zscore(V1)[e[0]:e[1]], ")")
print(" ")


Event  (2, 4) :  Ks_2sampResult(statistic=0.875, pvalue=0.072496346049057803)
Event  (2, 4) :  Anderson_ksampResult(statistic=1.0583965447073287, critical_values=array([ 0.325,  1.226,  1.961,  2.718,  3.752]), significance_level=0.11909520945783537)
Event  (2, 4) :  z-scores( [ 0.48957677  1.39285493] )

Event  (7, 8) :  Ks_2sampResult(statistic=0.88888888888888884, pvalue=0.21744842363651484)
Event  (7, 8) :  Anderson_ksampResult(statistic=0.071240617635128525, critical_values=array([ 0.325,  1.226,  1.961,  2.718,  3.752]), significance_level=0.32330529012728515)
Event  (7, 8) :  z-scores( [-0.91953717] )


Lots of things to toy around with:
If your event index is inclusive, just add 1 when slicing: e_vals = V1[e[0]:e[1]+1], etc.;
if you want to exclude all other events, just add another loop to remove those before testing;
if you want to combine all events, slice using all the indexes.

If you want to go further and classify your event, you can utilize these values as additional data for a supervised classification model if you have past labeled events and other data, and build a better predictor that doesn't rely entirely on distribution assumptions.

Alternatively, you can use Monte Carlo simulation, GLM, OLS, etc. to fit your data and test the resulting statistics, similar to what IrishStat did.

I took your data and added an indicator series appropriately populated according to your hypothesis . I ran the automatic option which yielded an OLS model . The Actual,Fit and Forecast graph is here . The acf of the model's errors suggested sufficiency.

The conclusion is that the 3 periods values are collectively different from the other values.

In this case there were no one-time pulses to be worried about which (untreated) would distort the significance test.

I do not use Python but I am sure that this trivial case can be programmed therein. Identifying unspecified (latent) deterministic structure such as level shifts,pulses,seasonal pulses and.or local time trends might require some research http://docplayer.net/12080848-Outliers-level-shifts-and-variance-changes-in-time-series.html .