Time series analysis in Python

I am a beginner to time-series analysis. I have the model below; y is sales of product and x is tweet-rate:

$y_t=ay_{t-1}+by_{t-2}+...+cy_{t-m}+dx_t+ex_{t-1}+...+fx_{t-n}$

1. What is this model called? I guess it's called an AR model but I am not sure since the dependent variable y is on R.H.S as well.
2. How do I fix the lag period, $m$ and $n$? Can $x$ and $y$ have different lags?
3. How can I use Python to build this model and also predict the sales for $t+1\ldots t+n$? Any solution for this without using rpy.
• I've changed your second RHS term to $by_{t-2}$, is that correct? – Peter Ellis Feb 11 '12 at 11:12
• @Vivek: If you're really serious about estimating that model (and it looks like an AR(m) model to me), you'll have more chance using R than Python. Especially because the estimating procedure for an AR model is fairly complex, and is built-in in R. – Joris Meys Feb 11 '12 at 14:01

1. The model you have there is called an Autoregressive Distributed Lag (ARDL) Model. To be specific, \begin{equation} y_t=ay_{t-1}+by_{t-2}+...+cy_{t-m}+dx_t+ex_{t-1}+...+fx_{t-n} \end{equation} can be called an ARDL(m,n) model and we can write the model in slightly more compact form as: \begin{equation} y_{t} = \delta + \sum_{i=1}^{m} \alpha_{i} y_{t-i} + \sum_{j=0}^{n} \beta_{j} x_{t-j} + u_{t} \end{equation} where $u_{t} \sim IID(o, \sigma^{2})~ \forall~ t$ and in this case $\delta = 0$.

2. The values of m and n do not have to be the same. That is, the lag length of the autoregressive term does not have to be equal to the lag length of the distributed lag term. Note also that it is possible to include a second (or more) distributed lag terms (for example, $z_{t-k}$). There are different ways of choosing the lag lengths and for a treatment of this issue, I refer you to Chapter 17 of Damodar Gujarati and Dawn Porter's Basic Econometrics (5th ed).

3. To build a model like this in python, it might be worth checking out statsmodels.tsa as well as the other packages mentioned in the other answers.

• +1. Some related terms that it may be worth the OP's time to look up are "Transfer function models" and "Time series regression models" – Glen_b May 24 '13 at 8:45

This answer should likely be a comment because I am not addressing the first two questions, but is too long...

You can do a lot of statistical work in Python these days, and with projects like statsmodels and pandas it is getting better and better. For time series analysis I think the best choice currently is using the PyIMSL package, which contains a good selection of functions all written in C for speed (and free for non-commercial use). Documentation can be found here. (Full disclosure, I used to work for Rogue Wave Software).

Now then, even though I use Python for most of my analytical work, for time series modeling I have turned to using the excellent forecast package in R by Rob Hyndman. It is hard to beat, especially for exploratory work.

• The Forecast package in R seems to be for univariate time series analysis. Hence, I ve doubts whether one can use it for ARDL. – user86509 Aug 21 '15 at 10:50

There is also Tidal Analysis on Sourceforge.net. Check this as well. Sometimes there is one thing in a free application and misses few things that you really want.

http://sourceforge.net/projects/tappy/?source=directory

Well, at first I suggest you to search on Google a Python package to manipulate time-series, like this one http://statsmodels.sourceforge.net/. On the other hand, if you MUST use python (instead of R, for example) you can try an optimization approach for find the best model parameters using as objective function the prediction error (MSE or RMSE).