## Introduction

(There is a sequel to this in the next post and talks about making things parallel using TBB)

At work, I often need to nonparametrically compare two distributions $X$ and $Y$. A visual and effective approach is the shift plot which plots the difference of the percentiles against the percentiles of $X$. However, since decisions are made based on these figures, confidence bands are required. In practice, the Doksum and Sievers confidence band for all the deciles is too wide. The book “Introduction to Robust Estimation and Hypothesis Testing” by Rand Wilcox has an algorithm to compute the 95% confidence band for the deciles. The band is therefore much sharper. My intuition tells me it will cover the regions between deciles with the same coverage probability. The routine uses bootstrap samples to compute the terms of

where $n=m$ and $c = \frac{80.1}{n^2}+2.73$. The constant $c$ is chosen to attain the 95% coverage probability. The estimates of the standard error are compute via Harrell Davis estimators of the quantiles and bootstrap.If you load the package WRS , the function shifthd contains the code.

Bootstrapping can be slow when $n=m=60K$. Can we speed it up using Terra? This post will take us through the code.

## Beginnings

I encourage the reader to skim through the source of shifthd and hd. The following code mimics it very closely. We compute 4 things

• the estimate $\hat{\theta}_{qy}$ using the Harrel-Davis estimator (the hd function)
• the estimate $\hat{\theta}_{qx}$ using the Harrel-Davis estimator
• the bootstrap estimates $\hat{\sigma}^2_{qy}$
• the bootstrap estimate $\hat{\sigma}^2_{qx}$

We have some data

and enter Terra like this

### shifthd

So far straight forward! We load the smisc Terra module from this post.

The function shifthd

• converts the objects x_, y_, nboot_ to Terra R equivalents after having made duplicates (we don’t want to modify the original)
• computes the value of $c$ (called crit).
• Initiates the GSL random number generator and uses LuaJIT’s GC mechanism for FFI objects to clean on exit.
• Creates temporary arrays xarray and yarray which will be used for bootstrap samples.
• ret will contain the values to be returned. It is an R vector
• For the 9 deciles,
• computes the bootstrap estimate of standard error of $\theta_{qx},\theta_{qy}$
• the estimate of the difference in quantiles
• creates a numeric vector with the lower, and upper bounds and the diff

### hd

Let’s take a look at the hd function. This is written in Terra. The function will call pbeta which returns $Prob(X<x)$ where $X \sim Beta(pin,qin)$.

the function hd takes the sample x, it’s length n and the p-value for which to compute the quantile for. If you take a look at WRS’s hd function, this mimics that very closely.

### bsHDVariance

And finally, a Lua function to compute the boostrap estimates

• loops through nboot bootstrap iterations
• computes a simple random sample with replacement from src (original data)
• computes the Harrel-Davis estimate

## Performance

The following code compares the two approaches

And the timings are

 terra elapsed    22.342 10.331 10.290 10.325 10.286
r     elapsed    40.431 39.106 39.101 39.051 39.388


The drop in timings after Terra’s first run occurs on OS X, not so on Linux. I cannot explain why.

### Improvement

Both codes compute $P_{beta}(i/n)-P_{beta}((i-1)/n)$ repeatedly. One could precompute this instead. Using this approach, the Lua code would be (note hd has another definition). Terra can interpret overloaded function definitions.

The timings drop a lot to

terra elapsed    5.386 3.198 3.198 3.244 3.280


## Summary

The code in the bootstrap section (lines 10-12 in the above code), can be done in parallel. In the next post, we’ll parallelize this quite easily using Intel TBB. The above code is in package form and can be found at https://github.com/saptarshiguha/rterramisc

The next post talks about making things parallel using TBB