Aishwarya Singh — Updated On November 28th, 2023
Algorithm Intermediate Machine Learning Python Structured Data Supervised Technique Time Series Time Series Forecasting


Time is the most critical factor in data science and machine learning that decides whether a business will rise or fall. That’s why we see sales in stores and e-commerce platforms aligning with festivals. These businesses analyze years of spending data to understand the best time to throw open the gates and see an increase in consumer spending.

But how can you, as a data scientist, perform this analysis? Don’t worry, you don’t need to build a time machine! Time Series modeling is a powerful technique that acts as a gateway to understanding and forecasting trends and patterns.

multivariate time series

But even a time series model has different facets. Most of the examples we see on the web deal with univariate time series. Unfortunately, real-world use cases don’t work like that. There are multiple variables at play, and handling all of them at the same time is where a data scientist will earn his worth.

Learning Objectives

  • Understand what a multivariate time series is and how to deal with it.
  • Understand the difference between univariate and multivariate time series.
  • Learn the implementation of multivariate time series in Python following a case study-based tutorial.

Univariate Vs. Multivariate Time Series Forecasting Python

This article assumes some familiarity with univariate time series, their properties, and various techniques used for forecasting. Since this article will be focused on multivariate time series, I would suggest you go through the following articles, which serve as a good introduction to univariate time series:

But I’ll give you a quick refresher on what a univariate time series is before going into the details of a multivariate time series. Let’s look at them one by one to understand the difference.

Univariate Time Series

A univariate time series, as the name suggests, is a series with a single time-dependent variable.

For example, have a look at the sample dataset below, which consists of the temperature values (each hour) for the past 2 years. Here, the temperature is the dependent variable (dependent on Time).

Univariate time series data set

If we are asked to predict the temperature for the next few days, we will look at the past values and try to gauge and extract a pattern. We would notice that the temperature is lower in the morning and at night while peaking in the afternoon. Also, if you have data for the past few years, you would observe that it is colder during the months of November to January while being comparatively hotter from April to June.

Such observations will help us in predicting future values. Did you notice that we used only one variable (the temperature of the past 2 years)? Therefore, this is called Univariate Time Series Analysis/Forecasting.

Multivariate Time Series (MTS)

A Multivariate time series has more than one time series variable. Each variable depends not only on its past values but also has some dependency on other variables. This dependency is used for forecasting future values. Sounds complicated? Let me explain.

Consider the above example. Suppose our dataset includes perspiration percent, dew point, wind speed, cloud cover percentage, etc., and the temperature value for the past two years. In this case, multiple variables must be considered to predict temperature optimally. A series like this would fall under the category of multivariate time series. Below is an illustration of this:

Multivariate time series dataset

Now that we understand what a multivariate time series looks like, let us understand how we can use it to build a forecast.

Dealing With a Multivariate Time Series – VAR

In this section, I will introduce you to one of the most commonly used methods for multivariate time series forecasting – Vector Auto Regression (VAR).

In a VAR algorithm, each variable is a linear function of the past values of itself and the past values of all the other variables. To explain this in a better manner, I’m going to use a simple visual example:

We have two variables, y1, and y2. We need to forecast the value of these two variables at a time ‘t’ from the given data for past n values. For simplicity, I have considered the lag value to be 1.

Multivariate time series, VAR

To compute y1(t), we will use the past value of y1 and y2. Similarly, to compute y2(t), past values of both y1 and y2 will be used.

Simple mathematical way of representing this relation:

multivariate time series, vector auto regression
multivariate time series, vector auto regression


  • a1 and a2 are the constant terms,
  • w11, w12, w21, and w22 are the coefficients,
  • e1 and e2 are the error terms

These equations are similar to the equation of an AR process. Since the AR process is used for univariate time series data, the future values are linear combinations of their own past values only. Consider the AR(1) process:

y(t) = a + w*y(t-1) +e

In this case, we have only one variable – y, a constant term – a, an error term – e, and a coefficient – w. In order to accommodate the multiple variable terms in each equation for VAR, we will use vectors.  We can write equations (1) and (2) in the following form:

multivariate time series, vector auto regression | VAR

The two variables are y1 and y2, followed by a constant, a coefficient metric, a lag value, and an error metric. This is the vector equation for a VAR(1) process. For a VAR(2) process, another vector term for time (t-2) will be added to the equation to generalize for p lags:

multivariate time series, vector auto regression | VAR

The above equation represents a VAR(p) process with variables y1, y2 …yk. The same can be written as:

multivariate time series, vector auto regression | VAR
multivariate time series, vector auto regression | VAR equation

The term εt in the equation represents multivariate vector white noise. For a multivariate time series, εt should be a continuous random vector that satisfies the following conditions:

  1. E(εt) = 0
    The expected value for the error vector is 0
  2. E(εt1t2‘) = σ12
    The expected value of εt and εt‘ is the standard deviation of the series.

Why Do We Need Vector Autoregressive Models?

Recall the temperate forecasting example we saw earlier. An argument can be made for it to be treated as a multiple univariate series. We can solve it using simple univariate forecasting methods like AR. Since the aim is to predict the temperature, we can simply remove the other variables (except temperature) and fit a model on the remaining univariate series.

Another simple idea is to forecast values for each series individually using the techniques we already know. This would make the work extremely straightforward! Then why should you learn another forecasting technique? Isn’t this topic complicated enough already?

From the above equations (1) and (2), it is clear that each variable is using the past values of every variable to make predictions. Unlike AR, VAR is able to understand and use the relationship between several variables. This is useful for describing the dynamic behavior of the data and also provides better forecasting results. Additionally, implementing VAR is as simple as using any other univariate technique (which you will see in the last section).

The extensive usage of VAR models in finance, econometrics, and macroeconomics can be attributed to their ability to provide a framework for achieving significant modeling objectives. With VAR models, it is possible to elucidate the values of endogenous variables by considering their previously observed values.

Granger’s Causality Test

Granger’s causality test can be used to identify the relationship between variables prior to model building. This is important because if there is no relationship between variables, they can be excluded and modeled separately. Conversely, if a relationship exists, the variables must be considered in the modeling phase.

The test in mathematics yields a p-value for the variables. If the p-value exceeds 0.05, the null hypothesis must be accepted. Conversely, if the p-value is less than 0.05, the null hypothesis must be rejected.

Stationarity of a Multivariate Time Series

We know from studying the univariate concept that a stationary time series will, more often than not, give us a better set of predictions. If you are not familiar with the concept of stationarity, please go through this article first: A Gentle Introduction to handling non-stationary Time Series.

To summarize, for a given univariate time series:

y(t) = c*y(t-1) + ε t

The series is said to be stationary if the value of |c| < 1. Now, recall the equation of our VAR process:

multivariate time series, vector auto regression | VAR

Note: I is the identity matrix.

Representing the equation in terms of Lag operators, we have:

multivariate time series, vector auto regression | VAR

Taking all the y(t) terms on the left-hand side:

vector auto regression | VAR
vector auto regression | VAR
The coefficient of y(t) is called the lag polynomial. Let us represent this as Φ(L):

vector auto regression | VAR, lag polynomial
vector auto regression | VAR, lag polynomial

For a series to be stationary, the eigenvalues of |Φ(L)-1| should be less than 1 in modulus. This might seem complicated, given the number of variables in the derivation. This idea has been explained using a simple numerical example in the following video. I highly encourage watching it to solidify your understanding:

Similar to the Augmented Dickey-Fuller test for univariate series, we have Johansen’s test for checking the stationarity of any multivariate time series data. We will see how to perform the test in the last section of this article.

Train-Validation Split

If you have worked with univariate time series data before, you’ll be aware of the train-validation sets. The idea of creating a validation set is to analyze the performance of the model before using it for making predictions.

Creating a validation set for time series problems is tricky because we have to take into account the time component. One cannot directly use the train_test_split or k-fold validation since this will disrupt the pattern in the series. The validation set should be created considering the date and time values.

Suppose we have to forecast the temperate diff, dew point, cloud percent, etc., for the next two months using data from the last two years. One possible method is to keep the data for the last two months aside and train the model for the remaining 22 months.

Once the model has been trained, we can use it to make predictions on the validation set. Based on these predictions and the actual values, we can check how well the model performed and the variables for which the model did not do so well. And for making the final prediction, use the complete dataset (combine the training data and validation sets).

Python Implementation

In this section, we will implement the Vector AR model on a toy dataset. I have used the Air Quality dataset for this and you can download it from here.

Python Code

Preparing the Data

The data type of the Date_Time column is object, and we need to change it to datetime. Also, for preparing the data, we need the index to have datetime. Follow the below commands:

df['Date_Time'] = pd.to_datetime(df.Date_Time , format = '%d/%m/%Y %H.%M.%S')
data = df.drop(['Date_Time'], axis=1)
data.index = df.Date_Time

Deal with Missing Values

The next step is to deal with the missing values. It is not always wise to use df.dropna. Since the missing values in the data are replaced with a value of -200, we will have to impute the missing value with a better number. Consider this – if the present dew point value is missing, we can safely assume that it will be close to the value of the previous hour. That makes sense, right? Here, I will impute -200 with the previous value.

You can choose to substitute the value using the average of a few previous values or the value at the same time on the previous day (you can share your idea(s) of imputing missing values in the comments section below).

#missing value treatment
cols = data.columns
for j in cols:
    for i in range(0,len(data)):
       if data[j][i] == -200:
           data[j][i] = data[j][i-1]

#checking stationarity
from statsmodels.tsa.vector_ar.vecm import coint_johansen
#since the test works for only 12 variables, I have randomly dropped
#in the next iteration, I would drop another and check the eigenvalues
johan_test_temp = data.drop([ 'CO(GT)'], axis=1)

Below is the result of the test:

array([ 0.17806667,  0.1552133 ,  0.1274826 ,  0.12277888,  0.09554265,
        0.08383711,  0.07246919,  0.06337852,  0.04051374,  0.02652395,
        0.01467492,  0.00051835])

Creating the Validation Set

We can now go ahead and create the validation set to fit the model and test its performance.

#creating the train and validation set
train = data[:int(0.8*(len(data)))]
valid = data[int(0.8*(len(data))):]

#fit the model
from statsmodels.tsa.vector_ar.var_model import VAR

model = VAR(endog=train)
model_fit =

# make prediction on validation
prediction = model_fit.forecast(model_fit.y, steps=len(valid))

Making it Presentable

The predictions are in the form of an array, where each list represents the predictions of the row. We will transform this into a more presentable format.

#converting predictions to dataframe
pred = pd.DataFrame(index=range(0,len(prediction)),columns=[cols])
for j in range(0,13):
    for i in range(0, len(prediction)):
       pred.iloc[i][j] = prediction[i][j]

#check rmse
for i in cols:
    print('rmse value for', i, 'is : ', sqrt(mean_squared_error(pred[i], valid[i])))


rmse value for CO(GT) is :  1.4200393103392812
rmse value for PT08.S1(CO) is :  303.3909208229375
rmse value for NMHC(GT) is :  204.0662895081472
rmse value for C6H6(GT) is :  28.153391799471244
rmse value for PT08.S2(NMHC) is :  6.538063846286176
rmse value for NOx(GT) is :  265.04913993413805
rmse value for PT08.S3(NOx) is :  250.7673347152554
rmse value for NO2(GT) is :  238.92642219826683
rmse value for PT08.S4(NO2) is :  247.50612831072633
rmse value for PT08.S5(O3) is :  392.3129907890131
rmse value for T is :  383.1344361254454
rmse value for RH is :  506.5847387424092
rmse value for AH is :  8.139735443605728

After the testing on the validation set, let’s fit the model on the complete dataset.

#make final predictions
model = VAR(endog=data)
model_fit =
yhat = model_fit.forecast(model_fit.y, steps=1)

We can test the performance of our model by using the following methods:

  1. Akaike information criterion (AIC): It quantifies the quality of a model by balancing the fit of the model to the data with the complexity of the model. AIC provides a way to compare different models and choose the one that best fits the data with the least complexity.
  2. Bayesian information criterion (BIC): This stats measure is used for model selection among a set of candidate models. Like the Akaike information criterion (AIC), BIC provides a trade-off between the goodness of fit and model complexity. However, BIC places a stronger penalty on the number of parameters than AIC does, which can help prevent overfitting.


Before I started this article, the idea of working with a multivariate time series seemed daunting in its scope. It is a complex topic, so take your time to understand the details. The best way to learn is to practice, so I hope the above Python implementations will be useful for you.

I encourage you to use this approach on a dataset of your choice. This will further cement your understanding of this complex yet highly useful topic. If you have any suggestions or queries, share them in the comments section.

Key Takeaways

  • Multivariate time series analysis involves the analysis of data over time that consists of multiple interdependent variables.
  • Vector Auto Regression (VAR) is a popular model for multivariate time series analysis that describes the relationships between variables based on their past values and the values of other variables.
  • VAR models can be used for forecasting and making predictions about the future values of the variables in the system.

Frequently Asked Questions

Q1. What is a Vector Auto Regression (VAR) model?

A. Vector Auto Regression (VAR) model is a statistical model that describes the relationships between variables based on their past values and the values of other variables. It is a flexible and powerful tool for analyzing interdependencies among multiple time series variables.

Q2. What is the order of a VAR model?

A. The order of a VAR model specifies the number of lags used in the model. It determines how many past observations of the variables are included in the model. The order is usually determined using information criteria such as AIC and BIC.

Q3. How can Granger causality tests be used in VAR modeling?

A. Granger causality tests can be used to determine whether one variable is useful in predicting another variable in a VAR model. It involves testing whether the inclusion of lagged values of one variable in a VAR model improves the forecasting accuracy of another variable.

About the Author

Aishwarya Singh
Aishwarya Singh

An avid reader and blogger who loves exploring the endless world of data science and artificial intelligence. Fascinated by the limitless applications of ML and AI; eager to learn and discover the depths of data science.

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

34 thoughts on "Multivariate Time Series Analysis With Python for Forecasting and Modeling (Updated 2023)"

Alex says: September 28, 2018 at 7:12 pm
HI. Thanks for sharing the knowledge and the great article! Could you pls add some details regarding the stationarity test process described in the article : the test is done and the results are presented but it is not clear if it could be concluded that the data is stationary; after the test is done no further actions to make the data stationary are performed...why so. Thanks Reply
John says: September 28, 2018 at 7:22 pm
Why not just use Random Forest for this? Thank you Reply
Aishwarya Singh
Aishwarya Singh says: September 29, 2018 at 10:17 pm
Hi Alex, under the stationarity section, we understood that if the modulus of eigenvalues are less than 1, the series would be classified as stationary. While implementation, since the condition is satisfied, I have not performed any transformation on the series. There is a complete article that describes dealing with non stationary time series (link provided in this article). Reply
Aishwarya Singh
Aishwarya Singh says: September 29, 2018 at 10:21 pm
Hi John, random forest can be used for supervised machine learning algorithms. In this case, we don't have a test set. Considering the example for weather prediction used in section 1 -if you consider temperature as target variable and the rest as independent variables, the test set must have the independent variables, which is not the case here. Using VAR , we predict all the variables . Reply
coolcoder001 says: November 06, 2018 at 8:27 am
Hi , I have applied the coint_johansen on my dataset. But , since most of the dependent variables are 0 , I am getting Singular Matrix error. How can I solve it ? Reply
G M says: November 08, 2018 at 10:22 pm
Can you give the csv used in this? Reply
Tharindu says: November 09, 2018 at 3:46 pm
Hi Aish, Thanks for the great article. Could you pl explain how a VAR and ECM differentiate. Thx Reply
Aishwarya Singh
Aishwarya Singh says: November 12, 2018 at 4:11 pm
Hi, Please share the notebook. Have dropped the mail Reply
Aishwarya Singh
Aishwarya Singh says: November 12, 2018 at 4:13 pm
The csv file is shared in the post itself. Refer section 6 of the article. Reply
Giuseppe says: November 14, 2018 at 9:20 pm
Hi! I was wondering about ranges of each column of the dataset. VAR models express every output as a linear combination of other variables weighted in a certain way. So, using absolute values changing in different ranges is probably not a good solution. RMSE high values seem to confirm this. Am I wrong? Reply
Bharath says: November 16, 2018 at 5:29 am
#missing value treatment cols = data.columns for j in cols: for i in range(0,len(data)): if data[j][i] == -200: data[j][i] = data[j][i-1] the above code can be replaced with data = data.fillna(method='ffill') Reply
Rick says: December 04, 2018 at 4:33 pm
Hello, It was a very instructive article, I have a question on your final prediction. Why do you fit a new VAR model on your whole dataset to make your prediction instead of taking the previous fitted model (with your training set) ? Thank you very much Reply
Aishwarya Singh
Aishwarya Singh says: December 05, 2018 at 11:11 am
Hi Rick, We build a new model for two reasons - Firstly, we must train the model on the complete set otherwise we loose some information. Secondly, the model makes prediction for dates after the training data. If we use only the train set, the predictions will be for dates present on the validation set. Reply
Soukaina says: December 07, 2018 at 3:09 am
Hi, Thank you for the tutorial, i want to ask you please about this line : # make prediction on validation prediction = model_fit.forecast(model_fit.y, steps=len(valid)) what does y in model_fit.y mean ? and can we apply this approach (test of stationnarity+VAR) if we have just 2 variables both of them dependent to time thank you so much Reply
Parth says: December 10, 2018 at 1:41 pm
Hello, The article is really great! I have understood the mathematics and reasoning behind the VAR, however, I still have a problem in compiling the same in python and I need basic codes for the same. Please help me regarding the same. Thank you very much. Reply
Aishwarya Singh
Aishwarya Singh says: December 10, 2018 at 3:57 pm
Hey Parth, The complete code is given in the article itself. Let me know which part are you facing an issue with. Reply
Aishwarya Singh
Aishwarya Singh says: December 10, 2018 at 4:09 pm
Hi Soukaina, prediction = model_fit.forecast(model_fit.y, steps=len(valid)) We first fit the model on the data and then forecast values for the length of validation set. Reply
Aishwarya Singh
Aishwarya Singh says: December 10, 2018 at 4:18 pm
Hi, I haven't worked with ECM yet. You can probably put the question on so that the community can help you clarify the doubt. Reply
Aishwarya Singh
Aishwarya Singh says: December 10, 2018 at 4:24 pm
That's a good point. Normalizing should reduce the rmse value. Have you tried applying it on this dataset? Reply
Maya says: December 30, 2018 at 5:54 pm
Hello Aishwarya I have 2 datasets that contains weather data, air pollution data, and all the variables measures in hours. How can I study the correlation between variables to do the features selection. I do not need all the variables in ny module,I need to identify the air pollution variables that effected by the weather variables How could i do that? Reply
GNEGONBE William says: January 23, 2019 at 2:19 pm
df = pd.read_csv("AirQualityUCI.csv", parse_dates=[['Date', 'Time']]); i have problem with parse_date function he doeesn't work. Can i have another fufnction which can permit to parse 'Date' and 'Time' ? Reply
Partha Kuila
Partha Kuila says: January 25, 2019 at 3:39 pm
I have to face the same type of problem. I have one target variable and other rests of variables are independent. Data is given day to day time series observation. Now I want to forecast the target variable for the next three months. It is helpful if you share this type of article. Reply
Partha Kuila
Partha Kuila says: February 08, 2019 at 11:34 am
Hi Aishwarya, I want to forecast the next 30 days then we have not validation set, then what we do? Reply
Cindy says: February 08, 2019 at 12:25 pm
How can i visualise the results? Reply
Aishwarya Singh
Aishwarya Singh says: February 20, 2019 at 2:40 pm
You can use the matplot library. Simply plot the actual values and the predictions on the same plot to compare. Reply
Aishwarya Singh
Aishwarya Singh says: February 20, 2019 at 2:54 pm
Hi Partha, There are a number of articles out these which cover this concept. You can use Algorithms like LSTM, or build two different models and combine the predictions. Reply
Aishwarya Singh
Aishwarya Singh says: February 20, 2019 at 2:58 pm
Hi Maya, You can use the data.corr() function to get the correlation between the variables. You can then select or remove the variables. Reply
Aishwarya Singh
Aishwarya Singh says: February 20, 2019 at 3:56 pm
Hi, You can read the dataset without using parse_dates, and then later convert the datatype of column from object to datetime using the pd.to_datetime function. Reply
Aishwarya Singh
Aishwarya Singh says: February 20, 2019 at 4:06 pm
hi partha, we are not using the validation set here. The only thing is that we are able to compare the results right now, but that won't be possible with a test set. Reply
Prabin Kumar Jena
Prabin Kumar Jena says: February 28, 2019 at 12:44 am
Hello Aishwarya, I have some doubt please help me out, in my data set there is test data and I want to predict for the test data but in my test data there is no dependent variable so how to predict for the test data? Thank You. Reply
Aishwarya Singh
Aishwarya Singh says: March 05, 2019 at 12:58 pm
Hi Prabin, You can go for other forecasting techniques like the ARIMA model or SARIMA model Reply
Marios says: March 20, 2019 at 7:33 am
Run as df = pd.read_csv("AirQualityUCI.csv",decimal=',',delimiter=';',parse_dates=[['Date', 'Time']]) Reply
Carlos says: December 03, 2021 at 3:34 am
Hi! Thank you for the article. I have a specific situation for which I have not found an answer anywhere thusfar (as in my situation I do not explain my time series by their values in the previous time period [T-1]). I have a dataset of 5 variables, with one of the independent variables (global temperature anomaly) showing nonstationarity and the rest of my time series showing stationarity. I like to explain the local temperature (dependent variable) with the global temperature, 3 wind vector variables (west-east component, south-north and vorticity which is the upward speed of wind and used as a proxy for cloud cover) and lastly a memory term for local temperature. Since I do monthly regressions (meaning that I investigate only 1 particular month over a span of 60 years to remove seasonality, for example 1950-2010 for the month June ONLY) this memory term is not a lag term in the econometric sense, but the previous month. So in the mentioned example this would be May between 1950-2010. How do I proceed to make a statistically solid regression if one of my dependent variables is nonstationary and the rest is stationary? Like mentioned, I solely look to local temperature as explained by the independent variables at that same T, and i dont explain any of my variables with their lagged values in time period T-1 (as loc temperature in May for 1972 would not have any influence on loc temperature for May 1973 since there are a lot of time steps in between that I do not include in the regression). Thank you, any help would be greatly appreciated. Reply
joe says: September 28, 2022 at 10:16 pm
Hi there! quick question what does the "(model_fit, steps=1)" mean in the code you used? make final predictions model = VAR(endog=data) model_fit = yhat = model_fit.forecast(model_fit.y, steps=1) print(yhat) Reply

Leave a Reply Your email address will not be published. Required fields are marked *