# Cointegration Johansen test and Autocorrelation Durbin Watson Test

**URL:** <https://discourse.julialang.org/t/cointegration-johansen-test-and-autocorrelation-durbin-watson-test/103950>\
**Category:** New to Julia\
**Tags:** stats\
**Created:** [September 17, 2023, 12:21pm UTC](https://discourse.julialang.org/t/cointegration-johansen-test-and-autocorrelation-durbin-watson-test/103950 "2023-09-17T12:21:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ginger\_snaps](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginger_snaps/32/203688_2.png) [@ginger\_snaps](https://discourse.julialang.org/u/ginger_snaps)\
**Post date:** [September 17, 2023, 12:21pm UTC](https://discourse.julialang.org/t/cointegration-johansen-test-and-autocorrelation-durbin-watson-test/103950/1 "2023-09-17T12:21:36Z")

</div>

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:

```julia
from statsmodels.stats.stattools import durbin_watson
res = durbin_watson(x_norm) # 2.1573

```

```julia
if(!require(car)){install.packages('car')}
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 :

```julia
from statsmodels.tsa.vector_ar import vecm; vecm.coint_johansen(***)
library(ursa); ca.jo(***)

```

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

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [September 17, 2023, 7:05pm UTC](https://discourse.julialang.org/t/cointegration-johansen-test-and-autocorrelation-durbin-watson-test/103950/2 "2023-09-17T19:05:54Z")

</div>

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
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])
3.0

```

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](https://en.wikipedia.org/wiki/Durbin%E2%80%93Watson_statistic).

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

---

<div class="post-metadata">

**Author:** ![ginger\_snaps](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ginger_snaps/32/203688_2.png) [@ginger\_snaps](https://discourse.julialang.org/u/ginger_snaps)\
**Post date:** [September 18, 2023, 10:21am UTC](https://discourse.julialang.org/t/cointegration-johansen-test-and-autocorrelation-durbin-watson-test/103950/3 "2023-09-18T10:21:37Z")

</div>

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.

```julia
 using LinearAlgebra

mutable struct DWTestResult
    dw::Real
    corr::Bool
end

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)
end

```

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