Skip to content

DPredict · Guide test 205

Johansen trace test

Tests whether the cointegration rank is at most a specified value in a multivariate system.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

  • Cointegration tests — Do several wandering (unit-root) series move together in the long run, so that some combination of them is stationary?

In Cointegration tests, choose Test = Johansen trace and maximum eigenvalue.

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Johansen trace test

80
1.50
0.60

1: the gap is a random walk too (no cointegration).

0.050

Your data

Simulated with known truth
—— y– – x—— gap y − 0.79 − 1.53·x

Cointegrated series wander together: the gap from their long-run line keeps returning to zero.

If H₀ were true

λ_trace = 30.10
  1. The reference: how λ_trace varies across samples when H₀ is true, trace of two independent random walks, n = 80 (Monte Carlo null: 999 samples simulated under H₀ (seed 20261107)).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .006).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

λtrace(0)=−T∑iln⁡(1−λ^i)\lambda_{\text{trace}}(0) = -T\sum_{i}\ln(1 - \hat\lambda_i)

With your numbers

λtrace=−78 [ln⁡(1−0.21)+ln⁡(1−0.14)]=30.10\lambda_{\text{trace}} = -78\,[\ln(1 - 0.21) + \ln(1 - 0.14)] = 30.10
λi\lambda_i
squared canonical correlations between differences and lagged levels
TT
effective sample size
rr
cointegrating rank; H₀ here: r = 0

The verdict

λ_trace = 30.10, p = .006

p ≤ α = .050: reject H₀

Test ranks in sequence (r = 0, then r ≤ 1) and stop at the first non-rejection. The deterministic terms change the critical values.

Statistic
30.100
p-value
0.0060
Johansen trace test on two series of length 80: λ_trace = 30.10, p = .006. The p-value is below α = .050, so the data are hard to reconcile with H0.

Johansen trace test on two series of length 80: λ_trace = 30.10, p = .006. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: cointegration rank is at most r; H1: rank exceeds r.

Assumptions

A correctly specified vector error-correction model for I(1) series, suitable innovations, chosen lag order and deterministic terms, and no unmodeled structural break.

Formula

Jtrace(r)=−T∑j=r+1plog⁡(1−λ^j)J_{trace}(r)=-T\sum_{j=r+1}^{p}\log(1-\widehat\lambda_j)

Parameters and notation

In Δyt=Πyt−1+ΣΓjΔyt−j+deterministic terms+ut, λ̂j are ordered squared canonical correlations after residualizing lagged levels and differences on short-run/deterministic regressors. p is system dimension,T effective sample.

Structure of the method

Fit a vector error-correction representation, partial out short-run lags and deterministic terms from levels and differences, and solve a generalized eigenvalue/canonical-correlation problem. The trace statistic aggregates evidence from all eigenvalues beyond a hypothesized rank. Rank testing requires a Johansen reference rather than an ordinary chi-square approximation.

Estimated and nuisance parameters

Short-run coefficient matrices, innovation covariance, canonical eigenvalues, and a normalized cointegrating space are estimated. Individual cointegration vectors are not uniquely identified without normalization/restrictions; rank is a tested structural dimension, not a continuously optimized weight.

Settings and design choices

Specify system variables, VAR level lag order, deterministic terms and their placement, seasonal dummies, break treatment, and sequential rank-testing protocol. R's K counts level lags while Python's k_ar_diff counts difference lags; constants inside and outside the cointegration relation are also distinct settings.

Fitting and adaptation

Fit the chosen system and test ranks in a declared sequence. If lag or deterministic specification is selected from data, disclose the selection and use appropriate validation/calibration. Conditional on a selected rank, refit the VECM and assess residuals and stability rather than assuming rank selection alone yields a valid forecasting model.

Sensitivity and robustness

Small samples relative to dimension/lag count, nonnormal innovations, autocorrelation, near-unit roots, breaks, and ill-conditioned covariance can distort rank conclusions. Different deterministic placements change critical values. Trace and maximum-eigenvalue tests can disagree; report that distinction and substantive uncertainty instead of choosing whichever rank best fits a narrative.

How to use and calibrate

Use Johansen-specific critical values for deterministic specification; sequential rank testing must follow a stated protocol.

Example application

Estimate how many stationary long-run relations exist in a multivariate system.

Interpretation and pitfalls

Watch out

Neither statistic has an ordinary χ² distribution in standard rank testing. Small samples and lag misspecification can distort rank selection.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

The two Johansen implementations use different deterministic-term parameterizations; compare only after aligning the model. Test ranks sequentially using the corresponding critical-value table.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy, statsmodels

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
x=np.cumsum(rng.normal(size=160));y=1.5*x+rng.normal(size=160);Z=np.column_stack([y,x])
from statsmodels.tsa.vector_ar.vecm import coint_johansen
r=coint_johansen(Z,det_order=0,k_ar_diff=1)
print("trace",r.lr1,r.cvt)
print("max eigenvalue",r.lr2,r.cvm)
Output
trace [63.05843318  3.78595986] [[13.4294 15.4943 19.9349]
 [ 2.7055  3.8415  6.6349]]
max eigenvalue [59.27247332  3.78595986] [[12.2971 14.2639 18.52  ]
 [ 2.7055  3.8415  6.6349]]
R

Validated — Executed successfully in the validation environment · needs urca

set.seed(2026)
x<-cumsum(rnorm(160));y<-1.5*x+rnorm(160);Z<-cbind(y,x)
library(urca)
print(summary(ca.jo(Z,type="trace",ecdet="const",K=2)))
Output
###################### 
# Johansen-Procedure # 
###################### 

Test type: trace statistic , without linear trend and constant in cointegration 

Eigenvalues (lambda):
[1] 4.037212e-01 1.828300e-02 1.344411e-17

Values of teststatistic and critical values of test:

          test 10pct  5pct  1pct
r <= 1 |  2.92  7.52  9.24 12.97
r = 0  | 84.61 17.85 19.96 24.60

Eigenvectors, normalised to first column:
(These are the cointegration relations)

               y.l2      x.l2  constant
y.l2      1.0000000    1.0000  1.000000
x.l2     -1.4723500  332.0156 -1.978132
constant  0.1278698 2125.9707 67.241592

Weights W:
(This is the loading matrix)

           y.l2          x.l2     constant
y.d -1.20217698 -0.0001670904 2.960932e-17
x.d -0.03771394 -0.0001142129 2.542438e-18

Sources

Statistics guide v4 (historical):
Python: Executed successfully in the validation environment · R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Unavailable local dependencies: statsmodels. Python: Not run in the reference's review: Unavailable local dependencies: statsmodels. No installation attempted. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 205, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT205.