AdfTest Function Enhanced With Rcpp Armadillo

In my previous post about rewriting my code to run in parallel part one I mentioned that we will make a small change to adfTest() function as well. In this post we will perform this small but performance-dramatic change.

When you take a closer look at the source code of this particular function fromĀ fUnitRoots package you will surely spot this part of code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (lags > 1) {
        y.diff.lag = z[,2:lags]
        if (type == "nc"){
            res = lm(y.diff ~ y.lag.1 - 1 + y.diff.lag)
        }
        if (type == "c"){
            res = lm(y.diff ~ y.lag.1 + 1 +  y.diff.lag) }
        if (type == "ct") {
            res = lm(y.diff ~ y.lag.1 + 1 + tt + y.diff.lag) }
    } else {
        if (type == "nc") {
            res = lm(y.diff ~ y.lag.1 - 1)
        }
        if (type == "c"){
            res = lm(y.diff ~ y.lag.1 + 1) }
        if (type == "ct") {
            res = lm(y.diff ~ y.lag.1 + 1  + tt)  }
    }

 

As we have discussed earlier the lm() is inferior to fastLm() function in terms of performance. So why not use this function instead? The codeĀ snippet after the change looks like this:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (lags > 1) {
        y.diff.lag = z[,2:lags]
        if (type == "nc"){
            res = fastLm(cbind(y.lag.1,y.diff.lag), y.diff)
        }
        if (type == "c"){
            res = fastLm(cbind(1, y.lag.1, y.diff.lag), y.diff) }
        if (type == "ct") {
            res = fastLm(cbind(1, y.lag.1, tt, y.diff.lag), y.diff) }
    } else {
        if (type == "nc") {
            res = fastLm(y.lag.1, y.diff)
        }
        if (type == "c"){
            res = fastLm(cbind(1, y.lag.1), y.diff) }
        if (type == "ct") {
            res = fastLm(cbind(1, y.lag.1, tt), y.diff) }
    }

 

Important note: do not forget to add this line before you use fastLm() function:

 

1
require(RcppArmadillo)

 

Using this modified function, call it adfTest2(), in the parallel test we performed previously, the code finishes in 386.57 seconds. In comparison, the fastest code finished in 820.143 seconds before! That's an amazing improvement.

To sum it up, we started with our serial code using old adfTest() function what took 1454.935 seconds. Using parallel code and improved adfTest2() it took 386.57 seconds. We saved 1068.365 seconds, i.e. we reduced the computational time by roughly 73.4%!

Huraaa

Huraaa!

Tags: , ,

Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.