Skip to content

[WIP] [GSoC 2016] VECM #3013 - #3070

Closed
yogabonito wants to merge 165 commits into
statsmodels:masterfrom
yogabonito:vecm-testing
Closed

yogabonito wants to merge 165 commits into
statsmodels:masterfrom
yogabonito:vecm-testing

Conversation

@yogabonito

@yogabonito yogabonito commented Jun 23, 2016 •

Copy link
Copy Markdown
Contributor

The VECM framework developed in this year's GSoC aims to add the support for non-stationary VAR processes to statsmodels.
Currently, the following topics are implemented (see also this Jupyter-notebook):

  • Parameter estimation,
  • forecasting,
  • testing for Granger-causality,
  • testing for instantaneous causality (not on my initial todo-list and may be moved to vector_ar.py to extend the VAR framework's functionality),
  • impulse-response analysis, and
  • lag order selection.

Some of them work without and with deterministic terms in the model. Some functions, however, make use of the existing VAR framework which lacks support for some deterministic terms, such as seasonal terms. As a consequence much of the VECM-functionality relying on the VAR framework is not working with deterministic terms yet. This limitation will be overcome with the refactoring of the VAR-related code.

Future development will also include the implementation of the following VECM-related functions:

  • Testing for the rank of cointegration,
  • checking for residual autocorrelation,
  • testing for non-normality (I have already begun working on this), and
  • tests for structural change.

It's been a great summer and I am looking forward to working on these open issues!


Original comment:
Estimation of parameters and tests have been added.
A failing test (related to the parameter matrix for the deterministic terms) indicates that deterministic seasonal terms are not implemented correctly yet.
@josef-pkt, @bashtage, can you have a look at it?

yogabonito added 11 commits June 3, 2016 12:03
added:
- read data
- parse JMulTi output
- compute statsmodels output (some amendments needed)
TODO: actual comparison between JMulTi and statsmodels
The necessary amendments were made, to make the code work in the tests-directory.
- The code worked correctly in a Jupyter notebook but not as methods of the VECM class (due to missing *self* as first parameter).
- Variable names have been changed and method signatures have been shortened to make use of instance attributes.
- Actual return of the estimated parameter matrices added.
add tests
- for 1 dataset (E6)
- for 3 parameters (alpha, beta, and Gamma)
- for 4 different deterministic terms ('', 'c', 'cs', 'clt')
That makes 12 tests, of which at the current tolerance 11 are passing. (The failing one would pass with a little more tolerance).
- output of JMulTi has been added and includes now information about the matrix \Sigma_u (files ending with Sigmau.txt).
- tests for \Sigma_u added
- tests for the matrix C added (deterministic terms)
- tests for deterministic terms within the cointegration relation (linear trends) added
- parsing of JMulTi output is now done with a function in another file to have a "cleaner" test file.
@bashtage

Copy link
Copy Markdown
Member

@yogabonito Thanks for this - looks very promising. Will give detailed feedback later today

@coveralls

coveralls commented Jun 23, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.01%) to 85.761% when pulling 9e3524b on yogabonito:vecm-testing into f060948 on statsmodels:master.

Comment thread statsmodels/tsa/vecm/vecm.py Outdated
if 's' in deterministic_terms: # TODO: How many seasons??
seasons = np.zeros((3, Delta_x.shape[1])) # 3 = 4-1 (4=number_of_seasons)
for i in range(seasons.shape[0]):
seasons[i, i::4] = i+1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICS, this should be assigning 1, i.e.
seasons[i, i::4] = 1

This should fix the test failure for non-seasonal parameters, even though it might not be the same coding that JMulti is using, and it still leaves periodicity hardcoded at 4.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have tried assigning 1 now but the test is still failing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have tried assigning 1 now but the test is still failing.

@josef-pkt

Copy link
Copy Markdown
Member

general
limit line length to <80, space around operator to make it easier to read
use temporary variables: both for readability, and to avoid repeated calculation of dot products (I saw one case when I was reading earlier, but cannot find it right now.)

we will drop all use of numpy.matrix and stick with arrays, even though we have to spell out dot right now (eventually it will be @)

add comment with equation or page number in Lutkepohl or whatever reference is used. VAR is doing that, and it makes it easier to compare notes during development.

renaming: most can wait, the main one right now is Delta_xxx to delta_xxx lower case
(right now we can use names that are close to Lutkepohl but eventually we want "informative" names. e.g. part of VAR cleanup #815 #3028 )

Given that the basic estimation is working, I think you should use a basic, stub (?)

I read through the main code a bit later.

Comment thread statsmodels/tsa/vecm/vecm.py Outdated
r"""
Fit VECM process and do lag order selection

.. math:: y_t = A_1 y_{t-1} + \ldots + A_p y_{t-p} + u_t

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math in notes, one liner to start class

…stic="nc"

Note that test cases for instantaneous causality fail. They would pass with
"self.p+1" instead of "self.p" at the beginning of the method body. I think this might be a bug in JMulTi, since Lutkepohl (2005) is explicitly advising to use a VAR(p)-model and not a VAR(p+1)-model on page 320 (chapter 7.6.4).
To this end, also equation names have been added to the model (and parameter names too). Also, methods for confidence intervals were added to be able to use existing summary-related code. Here is a summary for the dataset interest_inflation with the two variables "Dp" and "R" (available in my vecm-testing-branch) in the case of no deterministic terms:
Det. terms outside coint. relation & lagged endog. parameters for equation Dp
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
L1.Dp         -0.5877      0.144     -4.072      0.000      -0.871      -0.305
L1.R           0.1173      0.112      1.046      0.296      -0.103       0.337
L2.Dp         -0.7036      0.099     -7.124      0.000      -0.897      -0.510
L2.R           0.1875      0.113      1.653      0.098      -0.035       0.410
L3.Dp         -0.8253      0.053    -15.489      0.000      -0.930      -0.721
L3.R           0.0231      0.112      0.207      0.836      -0.196       0.242
 Det. terms outside coint. relation & lagged endog. parameters for equation R
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
L1.Dp         -0.0823      0.127     -0.646      0.518      -0.332       0.167
L1.R           0.1860      0.099      1.880      0.060      -0.008       0.380
L2.Dp         -0.0403      0.087     -0.462      0.644      -0.211       0.131
L2.R          -0.0447      0.100     -0.446      0.655      -0.241       0.151
L3.Dp          0.0091      0.047      0.194      0.846      -0.083       0.101
L3.R           0.1722      0.099      1.748      0.080      -0.021       0.365
                 Loading coefficients (alpha) for equation Dp
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
ec1           -0.5460      0.190     -2.880      0.004      -0.918      -0.174
                 Loading coefficients (alpha) for equation R
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
ec1            0.1057      0.167      0.632      0.527      -0.222       0.434
          Cointegration relations for loading-coefficients-column 1
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
beta.0         1.0000          0          0      0.000       1.000       1.000
beta.1        -0.1106      0.014     -7.798      0.000      -0.138      -0.083
==============================================================================
Point estimation is working for all tested combinations of deterministic terms. This has been achieved by making the VECMResults-class compatible with vector_ar/irf.py. To this end, VECMResults had to be refactored (which also means that it now conforms to statsmodels' naming conventions, using "neqs", "nobs", and "k_ar" instead of "K", "T", and "p").
Todo: refactor VAR (include arbitrary deterministic terms). Then also confidence intervals will work and not only point estimations.
This feature works in the absence of deterministic terms. When VAR-refactoring is done (allowing arbitrary deterministic terms in VAR models), this feature will also work with deterministic terms.
Plotting functions allow plotting:
- the input data
- forecasts
- impulse responses
When implementing the test for normality I saw that the current implementation in VAR is not producing the same results as JMulTi. For now, I have rewritten the code such that it produces the same results as JMulTi for VARs. The corresponding code for VECMs is not finished yet.
+ Allow for more than one caused variable. TODO: make this work in var_model.py too.
+ Allow for specifying variables via integers and strings (also sequences containing both these types are handled correctly).
+ Simplify API by removing the `caused`-argument. Having a separate `causing` and `caused` parameter was actually a bug because it allowed the user to specify a subset of all variables via these two parameters. (Lutkepohl's derivation of the Granger-test-theory is based on the fact that `causing` and `caused` form a partition of the set of all variables.) TODO: fix this bug in var_model.py too.
This includes the possibility to have now more than one caused variable and the
bug fix (i.e. restricting `caused` to always be the complement of `causing`.)
…t cases

Change test cases: Add JMulTi output of causality tests which were produced
using a lag order reduced by 1 in JMulTi (because JMulTi seems to base its
test for instantaneous causality on a VAR(p+1) model which does not correspond
to Lutkepohl where a VAR(p) model is used. Now the test cases are passing (for
deterministic="nc", further deterministic terms will be available after VAR is
refactored).
This function gives the same results as JMulTi for all 10 tested combinations of deterministic terms.
Added deterministic terms to VAR (via `exog`-parameter)

Added the test for instantaneous causality to VAR (by moving the code
from VECM to VAR)

Added 459 unit tests for VAR (all passing) covering:

 - parameter estimation (for endog and deterministic terms)

 - Sigma_u

 - Loglikelihood

 - forecasting

 - Granger- and instantaneous causality

 - todo: add tests for the remaining methods

Brought back "partial Granger causality" as discussed with Josef in statsmodels#3070

Unit tests so far in this PR:
VAR: 459 (all passing)
VECM: 422 / 426 passing (4 failures stem from t- & p-values for certain
                         parameters)
@yogabonito

Copy link
Copy Markdown
Contributor Author

This long list of commits suggests that something went wrong when trying to resolve the conflict. I have also a rebased branch (vecm-rebased) which is cleaner (only 55 instead of 112 commits but both branches contain the same files).
Is there a way to base this PR on the vecm-rebased branch?

@josef-pkt

Copy link
Copy Markdown
Member

@yogabonito Given that your rebased branch looks clean, you could continue with that one and open a new pull request.

According to the network only the last commit merges the original branch into the rebased version, but the comment/commit sequence in here still shows the original sequence besides the last merge commit. According to git only the last merge commit would need to be removed, but AFAIK that would change the github comment/merge sequence. (I'm not quite sure how this matches up.)

yogabonito added a commit to yogabonito/statsmodels that referenced this pull request Sep 26, 2016
Added deterministic terms to VAR (via `exog`-parameter)

Added the test for instantaneous causality to VAR (by moving the code
from VECM to VAR)

Added 459 unit tests for VAR (all passing) covering:

 - parameter estimation (for endog and deterministic terms)

 - Sigma_u

 - Loglikelihood

 - forecasting

 - Granger- and instantaneous causality

 - todo: add tests for the remaining methods

Brought back "partial Granger causality" as discussed with Josef in statsmodels#3070

Unit tests so far in this PR:
VAR: 459 (all passing)
VECM: 422 / 426 passing (4 failures stem from t- & p-values for certain
                         parameters)
@yogabonito yogabonito mentioned this pull request Oct 28, 2016
yogabonito added a commit to yogabonito/statsmodels that referenced this pull request Mar 23, 2017
Added deterministic terms to VAR (via `exog`-parameter)

Added the test for instantaneous causality to VAR (by moving the code
from VECM to VAR)

Added 459 unit tests for VAR (all passing) covering:

 - parameter estimation (for endog and deterministic terms)

 - Sigma_u

 - Loglikelihood

 - forecasting

 - Granger- and instantaneous causality

 - todo: add tests for the remaining methods

Brought back "partial Granger causality" as discussed with Josef in statsmodels#3070

Unit tests so far in this PR:
VAR: 459 (all passing)
VECM: 422 / 426 passing (4 failures stem from t- & p-values for certain
                         parameters)
yogabonito added a commit to yogabonito/statsmodels that referenced this pull request Jun 22, 2017
Added deterministic terms to VAR (via `exog`-parameter)

Added the test for instantaneous causality to VAR (by moving the code
from VECM to VAR)

Added 459 unit tests for VAR (all passing) covering:

 - parameter estimation (for endog and deterministic terms)

 - Sigma_u

 - Loglikelihood

 - forecasting

 - Granger- and instantaneous causality

 - todo: add tests for the remaining methods

Brought back "partial Granger causality" as discussed with Josef in statsmodels#3070

Unit tests so far in this PR:
VAR: 459 (all passing)
VECM: 422 / 426 passing (4 failures stem from t- & p-values for certain
                         parameters)
yogabonito added a commit to yogabonito/statsmodels that referenced this pull request Jun 27, 2017
Added deterministic terms to VAR (via `exog`-parameter)

Added the test for instantaneous causality to VAR (by moving the code
from VECM to VAR)

Added 459 unit tests for VAR (all passing) covering:

 - parameter estimation (for endog and deterministic terms)

 - Sigma_u

 - Loglikelihood

 - forecasting

 - Granger- and instantaneous causality

 - todo: add tests for the remaining methods

Brought back "partial Granger causality" as discussed with Josef in statsmodels#3070

Unit tests so far in this PR:
VAR: 459 (all passing)
VECM: 422 / 426 passing (4 failures stem from t- & p-values for certain
                         parameters)
@josef-pkt

Copy link
Copy Markdown
Member

continuation of this PR in #3246 has now been merged

@josef-pkt josef-pkt closed this Jun 28, 2017
@tongkewu

tongkewu commented Dec 2, 2017

Copy link
Copy Markdown

Hi, I cannot find vecm in the current vector_ar class. Is it removed? Thanks!

@josef-pkt

Copy link
Copy Markdown
Member

@tongkewu It has been merged, and is in master but not in any release yet

examples
Here https://gist.github.com/josef-pkt/fda96edd7b15892a058f58ae0948ed71
is an updated version of yogabonito's notebook (updated at time of merge, but it should be still up-to-date with master)

(there is also another gist that I used for merge review https://gist.github.com/josef-pkt/b36cd8458ab2ed20ff784dc8ed3d3a1c looks messy and I don't remember.)

@tongkewu

tongkewu commented Dec 2, 2017 via email

Copy link
Copy Markdown

@josef-pkt

Copy link
Copy Markdown
Member

Yes, If you want to use VECM, then you need to build from source or wait for the next release.
VECM itself is just one module, but it includes necessary changes and enhancements to VAR

@tongkewu

tongkewu commented Dec 2, 2017 via email

Copy link
Copy Markdown

@pskharade

Copy link
Copy Markdown

any timeline for the next release ( with VECM)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants