# Issue when building VAR model using Python

Building on my Question here which involved predicting the closing price of a stock given the previous days closing price, opening price, high price, low price and the number of articles associated with the stock. I have been recommended to use a VAR Model due to the possibility of Cointegration between the exogenous variables. And I am differencing the terms to remove non-stationarity and also using returns rather than prices in the model. The model looks as follows:

$log(Close_{t}) = A_1*log(Close_{t-1}) + .... + A_p*log(Close_{t-p}) + u_{t}$ where I have a 21*5 multivariate VAR Time series, as I only have 21 data points and have closing price, opening price, high price, low price and the number of articles as the exogenous variables. So, I do this in Pythn and am using the statsmodels module using the documentation provided here.

The data I am using is as follows:

          date   sym    open      high       low     close  newscount
0   2014.08.01   IBM  191.20  191.6700  188.8400  189.1800          3
1   2014.08.01  NFLX  418.81  427.7600  412.5100  425.4200          0
2   2014.08.01   WMT   73.50   73.8900   73.2100   73.5500          3
3   2014.08.04   IBM  189.00  190.4800  188.6000  189.6300          2


Some of the code I have is given below where df_IBM_training is the dataframe associated with IBM and only considers the training set:

regressor_1 = np.log(df_IBM_training['close'])
regressor_2 = np.log(df_IBM_training.open) - np.log(df_IBM_training.open.shift(1))
regressor_3 = np.log(df_IBM_training.high) - np.log(df_IBM_training.open)
regressor_4 = np.log(df_IBM_training.low) - np.log(df_IBM_training.open)
regressor_5 = df_IBM_training.newscount


model = statsmodels.tsa.api.VAR(X_IBM) where

X_IBM = [10**(regressor_1), 10**(regressor_2), 10**(regressor_3), 10**(regressor_4), 10**(regressor_5)] #Variables

But, I then attempt to determine the lag of the model and get the error numpy.linalg.linalg.linalgerror: 8th leading minor not positive definite, which suggests to me that the model might be wrong or that I am not using the statsmodel VAR correctly and I need help in this regard:

model.select_order(2)

Thank You

Note: I realize I was told to use VECM, but this is not available in Python and unfortunately I am not well versed in R, so I thought using VAR my not be too bad.

• @RichardHardy I was wondering if you could please provide some help with this issue. – Jojo Oct 26 '15 at 19:23
• You can't predict tomorrow's closing price. Forget about it. You can "predict" to a certain degree the long term return, I means months or years ahead. Nothing you build is going to predict tomorrow's price. – Aksakal Oct 26 '15 at 19:27
• Are your time series only 21-observations long? That is actually a very, very short sample. Also, is VECM really not available i Python? That's a pity. But you could assume that each pair of variables $C$, $O$, $H$, $L$ (as in my previous answer) is cointegrated with a cointegration vector (1,-1) (that is, the difference between any pair is stationary), so you could manually build the error correction terms and supply them (lagged by 1) as exogenous regressors. The model would be readily suited for forecasting one step ahead. You would need a little work to forecast more than one step ahead. – Richard Hardy Oct 26 '15 at 20:04
• The VARX model with error correction terms (i.e. the manually-built VECM) as indicated in the above comment should have as many equations as there are endogenous variables -- if you intend to forecast more than one step ahead. The variables should be of the form $\Delta \text{log}(X)$ rather than $\text{log}(X)$, i.e. log-returns, except for the error correction terms that will be $\text{log}(C)-\text{log}(O)$, $\text{log}(C)-\text{log}(H)$, and $\text{log}(C)-\text{log}(L)$ (all other cointegrated pairs can be expressed in terms of these three, thus these three should suffice). – Richard Hardy Oct 26 '15 at 20:09
• Aksakal is most likely right, and I would not put my money on a VECM to forecast stock prices, but why not play along in this thought experiment. I don't know Python, but what might be going wrong (why you are getting an error) is (1) you are running out of degrees of freedom (too few data points, too many parameters to be estimated) or (2) you somehow got perfectly multicollinear variables (if you used not only logs of the original variables but also differences between them such as $\text{log}(H)-\text{log}(C)$ and $\text{log}(L)-\text{log}(C)$). But it might be something else, too. – Richard Hardy Oct 26 '15 at 20:14