Detecting anomalies in periodic timeseries using gradient descent
In periodic time series it’s possible make an aproximation of them using discrete fourier transform given by the following equation:
Also it’s possible express them using sin functions only:
Then we can express this model for example on pytorch to find the parameters , for the first model or , for the last model.
In my particular problem the tiemeseries has two seasonings daily and weekly and timeseries buckets are 10 minutes long, then has to be the amount of buckets of 10 minutes in one week.
One interesting thing is that if applying box-cox transformation it makes the learning faster.
The box-cox transformation is given by the following equation:
When then box-cox transformation is like function. must take values between 0 and 1.
The box-cox transformation maintains the anomalies and reduce the magnitude of the noise, because reduce the scale of timeseries. When using the transformed timeseries the function to learn in simpler than the original.
The tricky part to detect anomalies is make the learned function smoother. We can acomplish that not taking into account the high frequencies in the fourier model. Doing in that way the learned function has less noise, then is more easy to detect anomalies in the original timeseries. Filter frequencies has another benefit for learning, the amount of features decrease, then the learning is faster.
Pytorch implementation
Plot timeseries transformed, learned function and anomalies
To detect anomalies I calculated the noise (difference between timeseries and learned function), then calculate noise standard deviation and calculate the point where the noice obsolute value is greater that 3 times std. The blue line is the learned function, the red one is the transformed timeseries and the green point are the anomalies.
The noise
Plot anomalies in the original tiemseries
Here is the plot of the previous anomalies on the original timeseries
Forecasting
After learn parameters of fourier model it’s possible evaluate on futures points in time.
Code
You can find the code in this Notebook