fascinating points of time sequence is the intrinsic complexity of such an apparently easy sort of knowledge.
On the finish of the day, in time sequence, you could have an x axis that normally represents time (t), and a y axis that represents the amount of curiosity (inventory value, temperature, site visitors, clicks, and so on…). That is considerably less complicated than a video, for instance, the place you may need 1000’s of photos, and every picture is a tensor of width, top, and three channels (RGB).
Nonetheless, the evolution of the amount of curiosity (y axis) over time (x axis) is the place the complexity is hidden. Does this evolution current a pattern? Does it have any knowledge factors that clearly deflect from the anticipated sign? Is it steady or unpredictable? Is the typical worth of the amount bigger than what we might anticipate? These can all by some means be outlined as anomalies.
This text is a group of a number of anomaly detection methods. The purpose is that, given a dataset of a number of time sequence, we are able to detect which time sequence is anomalous and why.
These are the 4 time sequence anomalies we’re going to detect:
- We’re going to detect any pattern in our time sequence (pattern anomaly)
- We’re going to consider how risky the time sequence is (volatility anomaly).
- We’re going to detect the purpose anomalies inside the time sequence (single-point anomaly).
- We’re going to detect the anomalies inside our financial institution of alerts, to know what sign behaves in another way from our set of alerts (dataset-level anomaly).
We’re going to theoretically describe every anomaly detection methodology from this assortment, and we’re going to present the Python implementation. The entire code I used for this weblog put up is included within the PieroPaialungaAI/timeseriesanomaly GitHub folder
0. The dataset
With a view to construct the anomaly collector, we have to have a dataset the place we all know precisely what anomaly we’re trying to find, in order that we all know if our anomaly detector is working or not. With a view to do this, I’ve created a knowledge.py script. The script incorporates a DataGenerator object that:
- Reads the configuration of our dataset from a config.json* file.
- Creates a dataset of anomalies
- Offers you the power to simply retailer the info and plot them.
That is the code snippet:

So we are able to see that we now have:
- A shared time axis, from 0 to 100
- A number of time sequence that type a time sequence dataset
- Every time sequence presents one or many anomalies.
The anomalies are, as anticipated:
- The pattern habits, the place the time sequence have a linear or polynomial diploma habits
- The volatility, the place the time sequence is extra risky and altering than regular
- The extent shift, the place the time sequence has the next common than regular
- Some extent anomaly, the place the time sequence has one anomalous level.
Now our purpose can be to have a toolbox that may determine every one in all these anomalies for the entire dataset.
*The config.json file permits you to modify all of the parameters of our dataset, such because the variety of time sequence, the time sequence axis and the sort of anomalies. That is the way it seems like:
1. Pattern Anomaly Identification
1.1 Principle
After we say “a pattern anomaly”, we’re searching for a structural habits: the sequence strikes upward or downward over time, or it bends in a constant means. This issues in actual knowledge as a result of drift typically means sensor degradation, altering consumer habits, mannequin/knowledge pipeline points, or one other underlying phenomenon to be investigated in your dataset.
We take into account two sorts of developments:
- Linear regression: we match the time sequence with a linear pattern
- Polynomial regression: we match the time sequence with a low-degree polynomial.
In follow, we measure the error of the Linear Regression mannequin. Whether it is too giant, we match the Polynomial Regression one. We take into account a pattern to be “vital” when the p worth is decrease than a set threshold (generally p < 0.05).
1.2 Code
The AnomalyDetector object in anomaly_detector.py will run the code described above utilizing the next features:
- The detector, which is able to load the info we now have generated in DataGenerator.
- detect_trend_anomaly and detect_all_trends detect the (eventual) pattern for a single time sequence and for the entire dataset, respectively
- get_series_with_trend returns the indices which have a major pattern.
We are able to use plot_trend_anomalies to show the time sequence and see how we’re doing:

Good! So we’re capable of retrieve the “stylish” time sequence in our dataset with none bugs. Let’s transfer on!
2. Volatility Anomaly Identification
2.1 Principle
Now that we now have a world pattern, we are able to deal with volatility. What I imply by volatility is, in plain English, how in every single place is our time sequence? In additional exact phrases, how does the variance of the time sequence evaluate to the typical one in all our dataset?
That is how we’re going to take a look at this anomaly:
- We’re going to take away the pattern from the timeseries dataset.
- We’re going to discover the statistics of the variance.
- We’re going to discover the outliers of those statistics
Fairly easy, proper? Let’s dive in with the code!
2.2 Code
Equally to what we now have achieved for the developments, we now have:
- detect_volatility_anomaly, which checks if a given time sequence has a volatility anomaly or not.
- detect_all_volatilities, and get_series_with_high_volatility, which verify the entire time sequence datasets for volatility anomaly and return the anomalous indices, respectively.
That is how we show the outcomes:

3. Single-point Anomaly
3.1 Principle
Okay, now let’s ignore all the opposite time sequence of the dataset and let’s deal with every time sequence at a time. For our time sequence of curiosity, we need to see if we now have one level that’s clearly anomalous. There are various methods to try this; we are able to leverage Transformers, 1D CNN, LSTM, Encoder-Decoder, and so on. For the sake of simplicity, let’s use a quite simple algorithm:
- We’re going to undertake a rolling window strategy, the place a hard and fast sized window will transfer from left to proper
- For every level, we compute the imply and customary deviation of its surrounding window (excluding the purpose itself)
- We calculate how many customary deviations the purpose is away from its native neighborhood utilizing the Z-score
We outline a degree as anomalous when it exceeds a hard and fast Z-score worth. We’re going to use Z-score = 3 which implies 3 occasions the usual deviations.
3.2 Code
Equally to what we now have achieved for the developments and volatility, we now have:
- detect_point_anomaly, which checks if a given time sequence has any single-point anomalies utilizing the rolling window Z-score methodology.
- detect_all_point_anomalies and get_series_with_point_anomalies, which verify the entire time sequence dataset for level anomalies and return the indices of sequence that include at least one anomalous level, respectively.
And that is how it’s performing:

4. Dataset-level Anomaly
4.1 Principle
This half is deliberately easy. Right here we’re not searching for bizarre time limits, we’re searching for bizarre alerts within the financial institution. What we need to reply is:
Is there any time sequence whose general magnitude is considerably bigger (or smaller) than what we anticipate given the remainder of the dataset?
To do this, we compress every time sequence right into a single “baseline” quantity (a typical stage), after which we evaluate these baselines throughout the entire financial institution. The comparability can be achieved when it comes to the median and Z rating.
4.2 Code
That is how we do the dataset-level anomaly:
- detect_dataset_level_anomalies(), finds the dataset-level anomaly throughout the entire dataset.
- get_dataset_level_anomalies(), finds the indices that current a dataset-level anomaly.
- plot_dataset_level_anomalies(), shows a pattern of time sequence that current anomalies.
That is the code to take action:

5. All collectively!
Okay, it’s time to place all of it collectively. We’ll use detector.detect_all_anomalies() and we are going to consider anomalies for the entire dataset based mostly on pattern, volatility, single-point and dataset-level anomalies. The script to do that may be very easy:
The df offers you the anomaly for every time sequence. That is the way it seems like:
If we use the next operate we are able to see that in motion:

Fairly spectacular proper? We did it. 🙂
6. Conclusions
Thanks for spending time with us, it means loads. ❤️ Right here’s what we now have achieved collectively:
- Constructed a small anomaly detection toolkit for a financial institution of time sequence.
- Detected pattern anomalies utilizing linear regression, and polynomial regression when the linear match is just not sufficient.
- Detected volatility anomalies by detrending first after which evaluating variance throughout the dataset.
- Detected single-point anomalies with a rolling window Z-score (easy, quick, and surprisingly efficient).
- Detected dataset-level anomalies by compressing every sequence right into a baseline (median) and flagging alerts that stay on a unique magnitude scale.
- Put every thing collectively in a single pipeline that returns a clear abstract desk we are able to examine or plot.
In lots of actual initiatives, a toolbox just like the one we constructed right here will get you very far, as a result of:
- It provides you explainable alerts (pattern, volatility, baseline shift, native outliers).
- It provides you a powerful baseline earlier than you progress to heavier fashions.
- It scales nicely when you could have many alerts, which is the place anomaly detection normally turns into painful.
Remember that the baseline is easy on function, and it makes use of quite simple statistics. Nonetheless, the modularity of the code permits you to simply add complexity by simply including the performance within the anomaly_detector_utils.py and anomaly_detector.py.
7. Earlier than you head out!
Thanks once more on your time. It means loads ❤️
My title is Piero Paialunga, and I’m this man right here:

I’m initially from Italy, maintain a Ph.D. from the College of Cincinnati, and work as a Information Scientist at The Commerce Desk in New York Metropolis. I write about AI, Machine Studying, and the evolving position of information scientists each right here on TDS and on LinkedIn. In the event you preferred the article and need to know extra about machine studying and comply with my research, you possibly can:
A. Comply with me on Linkedin, the place I publish all my tales
B. Comply with me on GitHub, the place you possibly can see all my code
C. For questions, you possibly can ship me an e-mail at piero.paialunga@hotmail
