Calculate the Johansen test for cointegration among every ticker in the Toolkit instance.

The Engle-Granger test only handles two assets and imposes an arbitrary normalization (which asset is “dependent”). Johansen’s test generalizes this to N >= 2 assets at once by testing the rank of the long-run coefficient matrix in a Vector Error Correction Model (VECM) fit to all assets jointly. The estimated rank equals the number of independent cointegrating (long-run equilibrium) relationships among the assets: rank 0 means none of them are cointegrated, rank N means the whole system is already stationary in levels, and a rank in between means that many independent stationary combinations exist among the N individually non-stationary price series.

For more information about the method, see the following papers:

  • Johansen, S. (1988). “Statistical Analysis of Cointegration Vectors.” Journal of Economic Dynamics and Control, 12(2-3), 231-254.
  • Johansen, S. (1991). “Estimation and Hypothesis Testing of Cointegration Vectors in Gaussian Vector Autoregressive Models.” Econometrica, 59(6), 1551-1580.

Also known as: Johansen test, Johansen procedure, VECM rank test.

Johansen Cointegration in Python

get_johansen_cointegration is part of the Econometrics module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_johansen_cointegration as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY")

toolkit.econometrics.get_johansen_cointegration(period="quarterly")

Which returns:

(showing the trace-statistic columns; the max-eigenvalue-statistic columns follow the same pattern)

  Eigenvalue Trace Statistic Trace Critical Value 95% Reject (Trace, 5%)
r <= 0 0.5653 14.1993 15.4943 False
r <= 1 0.3674 5.0363 3.8415 True

Parameters

get_johansen_cointegration accepts the following parameters:

  • period (str, optional): The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”.
  • column (str, optional): The historical data column to test. Defaults to “Adj Close”.
  • det_order (int, optional): Which deterministic term to include: -1 (none), 0 (a constant, restricted to lie in the cointegrating relation) or 1 (a linear trend restricted to the cointegrating relation, alongside an unrestricted constant in the short-run dynamics). Defaults to 0.
  • k_ar_diff (int, optional): The number of lagged first differences to include as short-run dynamics. Defaults to 1.
  • include_benchmark (bool, optional): Whether to include “Benchmark” among the tickers tested jointly. Defaults to False.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.
Share