Anomaly Detection in Multivariate Time Series with VAR

Anomaly detection is a hot topic in machine learning. As we can guess, the definition of ‘anomaly’ is variable and domain related. In time series applications, when we face this kind of problem, we have to take into account also the temporal dimension. The history of a series contains a lot of information on its behavior and can suggest its future changes. This is particularly true for series not generated by a random walk process and that exhibits a cyclical/periodic pattern.

The simple known model that deals with time series and learns information from its past is the ARIMA. ARIMA models are great instruments to develop time series forecasting tools. Their ability to learn how series evolve could also be useful in anomaly detection tasks. In this sense, classical approaches consist of marking as anomaly an observation that goes outside a tolerance threshold. This approach is limited to singular series; if we would like to consider a more complex system we need another approach.

In this post, we introduce a methodology to detect anomaly in a complex system made by multiple correlated series. We use VAR models, the multivariate extension of ARIMA, to extract the correlation pattern from the series at our disposal. The learned information by VAR is then used to build a thresholding mechanism to flag alerts when our metric exceeds a critical value.


We take experimental data from Kaggle. Seattle Burke Gilman Trail is a dataset hosted by the city of Seattle which is part of its open data project. The dataset stores hourly counting series detected by sensors._ These sensors count both people riding bikes and pedestrians. Separate volumes are tallied for each travel mode. Wires in a diamond formation in the concrete detect bikes and an infrared sensor mounted on a wooden post detects pedestrians_.

Image for post

examples of daily aggregated time series at our disposal

In total, 5 counting series are supplied. 2 related to pedestrian count, 2 related to bike count, and the total which is the sum of the previous series. There are double counters for pedestrians and bikes because two directions of travel are registered.

Given this data, our anomaly detection journey is divided into two parts. Firstly we provide a classic univariate anomaly detection approach using ARIMA. At the end, we pass to a multivariate approach considering all the series and their interaction in the system. According to the scope of this post, we decide to aggregate the data at our disposal, passing from hourly to daily data.


In the univariate anomaly approach, we plan to use ARIMA to detect the presence of strange patterns. We decide to focus on the series of total counts. The first thing to deal with when developing ARIMA is to take care of stationarity, explosive trends, or seasonality. As we can easily check on the plot above and the autocorrelation below, the total count series presents a double seasonality: weekly and yearly.

Image for post

original autocorrelation

The long term seasonality can be very annoying. To remove it we subtract on each day the relative monthly mean computed on train data. In this way, we remain with only the weekly pattern which can be learned by our models without much problem.

Image for post

autocorrelation after removing long term seasonality

We fit the best ARIMA limiting the search around 7 autoregressive order while minimizing AIC. The final model seems to produce normal residuals without any autocorrelation degree.

Image for post

#data-science #towards-data-science #anomaly-detection #timeseries #machine-learning

What is GEEK

Buddha Community

Anomaly Detection in Multivariate Time Series with VAR
Lenora  Hauck

Lenora Hauck


Unsupervised Anomaly Detection on Time Series

To understand the normal behaviour of any flow on time axis and detect anomaly situations is one of the prominent fields in data driven studies. These studies are mostly conducted in unsupervised manner, since labelling the data in real life projects is a very tough process in terms of requiring a deep retrospective analyses if you already don’t have label information. Keep in mind that outlier detection and anomaly detection are used interchangeably most of the time.

There is not a magical silver bullet that performs well in all anomaly detection use cases. In this writing, I touch on fundamental methodologies which are mainly utilized while detecting anomalies on time series in an unsupervised way, and mention about simple working principles of them. In this sense, this writing can be thought as an overview about anomaly detection on time series including real life experiences.

Image for post

Photo by Jack Nagz on Unsplash

Probability Based Approaches

Using Z-score is one of the most straightforward methodology. Z-score basically stands for the number of standart deviation that sample value is below or above the mean of the distribution. It assumes that each features fits a normal distribution, and calculating the z-score of each features of a sample give an insight in order to detect anomalies. Samples which have much features whose values are located far from the means are likely to be an anomaly.

While estimating the z-scores, you should take into account the several factors that affect the pattern to get more robust inferences. Let me give you an example, you aim detecting anomalies in traffic values on devices in telco domain. Hour information, weekday information, device information(if multiple device exist in dataset) are likely to shape the pattern of traffic values. For this reason, z-score should be estimated by considering each device, hour and weekday for this example. For instance, if you expect 2.5 mbps average traffic on device A at 8 p.m. at weekends, you should take into consideration that value while making a decision for corresponding device and time information.

#outlier-detection #time-series-analysis #time-series-forecasting #python #anomaly-detection

Real-Time Time Series Anomaly Detection

As much as it has become easier over the years to collect vast amounts of data across different sources, companies need to ensure that the data they’re gathering can bring value. To aid insight collection from the data, machine learning and analytics have become trending tools. Since these domains require real-time insights, an abundance of unwelcome data can create real issues.

Before decisions are made, and critically, before actions are taken, we must ask: are there anomalies in our data that could skew the results of the algorithmic analysis? If anomalies do exist, it is critical that we automatically detect and mitigate their influence. This ensures that we get the most accurate results possible before taking action.

In this post, we explore different anomaly detection approaches that can scale on a big data source in real-time. The tsmoothie package can help us to carry out this task. Tsmoothie is a python library for time series smoothing and outlier detection that can handle multiple series in a vectorized way. It’s useful because it can provide the techniques we needed to monitor sensors over time.

#time-series-analysis #editors-pick #anomaly-detection #data-science #machine-learning

Michael  Hamill

Michael Hamill


These Tips Will Help You Step Up Anomaly Detection Using ML

In this article, you will learn a couple of Machine Learning-Based Approaches for Anomaly Detection and then show how to apply one of these approaches to solve a specific use case for anomaly detection (Credit Fraud detection) in part two.

A common need when you analyzing real-world data-sets is determining which data point stand out as being different from all other data points. Such data points are known as anomalies, and the goal of anomaly detection (also known as outlier detection) is to determine all such data points in a data-driven fashion. Anomalies can be caused by errors in the data but sometimes are indicative of a new, previously unknown, underlying process.

#machine-learning #machine-learning-algorithms #anomaly-detection #detecting-data-anomalies #data-anomalies #machine-learning-use-cases #artificial-intelligence #fraud-detection

Time Series Basics with Pandas

In my last post, I mentioned multiple selecting and filtering  in Pandas library. I will talk about time series basics with Pandas in this post. Time series data in different fields such as finance and economy is an important data structure. The measured or observed values over time are in a time series structure. Pandas is very useful for time series analysis. There are tools that we can easily analyze.

In this article, I will explain the following topics.

  • What is the time series?
  • What are time series data structures?
  • How to create a time series?
  • What are the important methods used in time series?

Before starting the topic, our Medium page includes posts on data science, artificial intelligence, machine learning, and deep learning. Please don’t forget to follow us on Medium 🌱 to see these posts and the latest posts.

Let’s get started.

#what-is-time-series #pandas #time-series-python #timeseries #time-series-data

What is Time Series Forecasting?

In this article, we will be discussing an algorithm that helps us analyze past trends and lets us focus on what is to unfold next so this algorithm is time series forecasting.

What is Time Series Analysis?

In this analysis, you have one variable -TIME. A time series is a set of observations taken at a specified time usually equal in intervals. It is used to predict future value based on previously observed data points.

Here some examples where time series is used.

  1. Business forecasting
  2. Understand the past behavior
  3. Plan future
  4. Evaluate current accomplishments.

Components of time series :

  1. Trend: Let’s understand by example, let’s say in a new construction area someone open hardware store now while construction is going on people will buy hardware. but after completing construction buyers of hardware will be reduced. So for some times selling goes high and then low its called uptrend and downtrend.
  2. **Seasonality: **Every year chocolate sell goes high during the end of the year due to Christmas. This same pattern happens every year while in the trend that is not the case. Seasonality is repeating same pattern at same intervals.
  3. Irregularity: It is also called noise. When something unusual happens that affects the regularity, for example, there is a natural disaster once in many years lets say it is flooded so people buying medicine more in that period. This what no one predicted and you don’t know how many numbers of sales going to happen.
  4. Cyclic: It is basically repeating up and down movements so this means it can go more than one year so it doesn’t have fix pattern and it can happen any time and it is much harder to predict.

Stationarity of a time series:

A series is said to be “strictly stationary” if the marginal distribution of Y at time t[p(Yt)] is the same as at any other point in time. This implies that the mean, variance, and covariance of the series Yt are time-invariant.

However, a series said to be “weakly stationary” or “covariance stationary” if mean and variance are constant and covariance of two-point Cov(Y1, Y1+k)=Cov(Y2, Y2+k)=const, which depends only on lag k but do not depend on time explicitly.

#machine-learning #time-series-model #machine-learning-ai #time-series-forecasting #time-series-analysis