# Bootstrapping Confidence Intervals: the basics If you are a data scientist, data analysts, product manager in a data-driven company, you experiment with your product a lot, and usually you do lots of AB tests.

Let’s assume you have an AB test with control and treatment groups. When analysing results, you compare the average number of orders/visits/views (or any other metric) in test groups. Obviously, you can calculate the mean of a metric, you can even say using t-test and p-value if there is statistically significant difference between two means. But usually, they say don’t look only at p-values, look at confidence intervals.

In this blog post, we discover how to calculate confidence intervals using bootstrapping. Bootstrapping is a cool method to estimate confidence intervals because it does not rely on any assumption of data distribution (compared to popular Welch t-test, for example) and it is quite easy to implement by yourself (I share a link to my implementation below).

## Bootstrap definition and principle

The bootstrap is a method for estimating standard errors and computing confidence intervals. Bootstrapping started in 1970th by Bradley Efron; it has already existed for more than 40 years, so many different types and methods of bootstrapping were developed since then.

But despite of many variations, all  bootstrap steps look as follows:

1. Obtain data sample x1,x2,…,xn drawn from a distribution F.
2. Define u – statistic computed from the sample (mean, median, etc).
3. Sample x*1, x*2, . . . , x*n with replacement from the original data sample. Let it be F*  –  the empirical distribution. Repeat n times (n is bootstrap iterations).
4. Compute u* –  the statistic calculated from each resample.

Then the bootstrap principle says that:

1. Empirical distribution from bootstraps is approximately equal to distribution of sample data:  F* ≈ F.
2. The variation of statistic computed from the sample u is well-approximated by the variation of statistic computed from each resample u*. So, computed statistic u* from bootstrapping is a good proxy for statistic of sample data.

As mentioned, bootstrap can be applied for different goals:

• Estimating Confidence intervals
• Hypothesis testing
• Bias elimination

Each topic deserves a separate post and today we concentrate only on bootstrapping confidence intervals.

## Methods for Bootstrapping Confidence Intervals

When I researched the topic, I haven’t found a single source with all bootstraps methods at once, every reference has its own types. Most common ones are presented below:

1. Empirical Bootstrapping

In a nutshell, this method approximates the difference between bootstrapped means and sample mean.

Let’s consider an example (it will continue for other methods as well): estimate the mean μ of the underlying distribution and give an 80% bootstrap confidence interval.

Empirical bootstrapping says:

• For each bootstrap calculate mean x*
• Compute δ* = x* − x for each bootstrap sample (x is mean of original data), sort them from smallest to biggest.
• Choose δ.1 as the 90th percentile, δ.9 as the 10th percentile of sorted list of δ*, which gives an 80% confidence interval of [x−δ.1, x−δ.9].

Wasserman’s “All of Statistics” book provides with the following math notation for empirical bootstrap:

where x is mean of original data and x* is bootstrap means.

2. Percentile bootstrap

Instead of computing the differences δ*, the bootstrap percentile method uses the distribu­tion of the bootstrap sample statistic as a direct approximation of the data sample statistic.

For Percentile bootstrap:

• For each bootstrap calculate mean x*, sort them from smallest to biggest.
• Choose x*.1 as the 10th percentile, x*.9 as the 90th percentile of sorted list of x*, which gives an 80% confidence interval of [x*.1, x*.9].

Or, in other words:

The bootstrap percentile method is quite simple, but it depends on the bootstrap distribution of x* based on a particular sample being a good approximation to the true distribution of x (because of that reason, this source advises not to use percentile bootstrap).

3. Normal bootstrap

Instead of taking percentiles of bootstrapped means, normal bootstrap method calculates confidence intervals for these bootstrapped means.

• For each bootstrap calculate mean x*.
• Calculate 80% confidence intervals for array of x* using for example, Student t-test:

where x* is the sample mean from bootstrap means, se is the standard error of the bootstrap means, z* is critical value (found from the distribution table of normal CDF).

When I compared this method to others, its CI were always very narrow compared to other methods which puzzled me a lot. As I understood, it is because the definition strongly relies on assumption about normal distribution of data.

Some time later, I stumbled upon slightly different wording of this method in Wasserman’s “All of Statistics”. Calculation differs by just one parameter, but it gives a huge difference.

4. Normal interval bootstrap

Normal interval bootstrap repeats all steps of normal bootstrap, but use the following formula for CI:

where se_boot is the bootstrap estimate of the standard error.

This interval is not accurate unless the distribution of x* is close to Normal.

Basically, here formula has no division by squared root of n. I haven’t found explanation why it is done in that way. If you have any ideas, please, share them in comments.

5. Parametric Bootstrap

Parametric bootstrap is very close to empirical approach, the only difference is in the source of the bootstrap sample. For the parametric bootstrap, we generate the bootstrap sample from a parametrized distribu­tion. It is often used for the efficient computation of Bayes posterior distributions, not for experimentation analysis, so I will not go into its details here.

## Comparing methods

As promised earlier, here you can find Python implementation of all methods. The goal was to estimate 95% bootstrap confidence interval for the mean of target metric. I played with bootstrap methods, number of bootstrap samples and sample size of data itself.

The main question I had: which bootstrap method would show more reliable results. First of all, normal bootstrap crearly produces too narrow CI (because of normality assumptions). Other 3 methods are usually close to each other given large enough sample. The advantage of percentile and empirical types is that they provide different intervals from left and right sides (in contrast to normal interval bootstrap). Usually, it is better because it accounts for distribution of data. As you can see on plot below,  intervals for percentile and empirical bootstraps are greeter from the left side – on the side where population mean lies.