Cointegration Johansen test and Autocorrelation Durbin Watson Test

Hello. I’m new in Julia, but I need some performance so I’m rewriting from R and Python to Julia but I have problems with built-in functions. I know that Julia is not as wide as R and Python, but I was sure that I would find typical stats tests.

But I have problems with 2 of them:

  • Johansen cointegration test and VECM modeling
  • Durbin Watson autocorrelation test

Could you please give me some links for repos or packages for that?

  1. DW in Python and R:
from statsmodels.stats.stattools import durbin_watson
res = durbin_watson(x_norm) # 2.1573
res <- durbinWatsonTest(x_norm) # 2.1573

Julia has DurbinWatsonTest from HypothesisTests.jl says “X is the matrix of regressors from the original regression model and e the vector of residuals. Note that the Durbin-Watson test is not valid if X includes a lagged dependent variable.”

DurbinWatsonTest(X::AbstractArray, e::AbstractVector; p_compute::Symbol = :ndep)

What? I just need to run something like dw([1, -1, 1, -1]) on the vector and get results. Why regression, why all this stuff?

  1. About cointegration and VECM - nothing at all. I have got some code that was written in Julia 0.3 almost 10 years ago with even another keyword for struct - “type”, and it doesn’t work. And it contains a few thousands of lines.
    In Python and R :
from statsmodels.tsa.vector_ar import vecm; vecm.coint_johansen(***)

Maybe you know where I can find this tests, and not rewriting them from scratch?

Regarding the Durbin-Watson test, there is a difference between calculating the Durbin-Watson statistic, and using it with a cutoff to get a meaningful bound on the statistical error of a hypothesis test. If, as the OP suggests, the statistic is important, i.e. having dw([1, -1, 1, -1]), then this is super-simple:

julia> using LinearAlgebra

julia> dw(v) = (t = diff(v); dot(t,t)/dot(v,v))
dw (generic function with 1 method)

julia> dw([-1,1,-1,1])

Now what does the 3.0 mean? That requires the regressors, etc. and it is implemented in HypothesisTests package. See also the Wikipedia article: Durbin–Watson statistic.

I’m not addressing the second part of the question. Perhaps someone else will step up.

1 Like

Ok.Thank you. I think about just testing function like. I know that it’s not related to p-value, but usially it’s normal to use just 1.5 - 2.5 bounds, how do you think? I don’t want to build distribution just for this simple test.

 using LinearAlgebra

mutable struct DWTestResult

function dw_stat(resid, lower= 1.5, upper=2.5)
    t = diff(resid) 
    dw = dot(t,t)/dot(resid,resid)
    corr = lower < dw < upper
    return DWTestResult(dw, corr)

It’s weird that Julia does not have something about cointegration… The second problem is much more complicated (((