Resampling Techniques, Bootstrap and Blocking
Contents
7. Resampling Techniques, Bootstrap and Blocking¶
7.1. Why resampling methods ?¶
Statistical analysis.
* Our simulations can be treated as *computer experiments*. This is particularly the case for Monte Carlo methods
* The results can be analysed with the same statistical tools as we would use analysing experimental data.
* As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors.
7.2. Statistical analysis¶
* As in other experiments, many numerical experiments have two classes of errors:
* Statistical errors
* Systematical errors
* Statistical errors can be estimated using standard tools from statistics
* Systematical errors are method specific and must be treated differently from case to case.
7.3. Statistics, wrapping up from last week¶
Let us analyze the problem by splitting up the correlation term into partial sums of the form:
The correlation term of the error can now be rewritten in terms of
The value of
which gives us a useful measure of pairwise correlations
starting always at
7.4. Statistics, final expression¶
The sample error can now be written in terms of the autocorrelation function:
and we see that
7.5. Statistics, effective number of correlations¶
For a correlation free experiment,
We can interpret a sequential
correlation as an effective reduction of the number of measurements by
a factor
To neglect the autocorrelation time
7.6. Can we understand this? Time Auto-correlation Function¶
The so-called time-displacement autocorrelation
which can be rewritten as
where
7.7. Time Auto-correlation Function¶
One should be careful with times close to
One should therefore choose
Note that the variable
The time-correlation function gives a measure of the correlation between the various values of the variable
at a time
7.8. Time Auto-correlation Function¶
We can derive the correlation time by observing that our Metropolis algorithm is based on a random
walk in the space of all possible spin configurations.
Our probability
distribution function
with
resulting in
with
the eigenvector
7.9. Time Auto-correlation Function¶
If we assume that
We can relate this property to an observable like the mean energy.
With the probabilty
or as the scalar of a vector product
with
7.10. Time Auto-correlation Function¶
We rewrite this relation as
If we define
Since we have that in the limit
We define the quantity
and rewrite the last expectation value as
7.11. Time Auto-correlation Function¶
The quantities
resulting in
which is appropriate for all times.
7.12. Correlation Time¶
If the correlation function decays exponentially
then the exponential correlation time can be computed as the average
If the decay is exponential, then
which suggests another measure of correlation
called the integrated correlation time.
7.13. Resampling methods: Jackknife and Bootstrap¶
Two famous resampling methods are the independent bootstrap and the jackknife.
The jackknife is a special case of the independent bootstrap. Still, the jackknife was made popular prior to the independent bootstrap. And as the popularity of the independent bootstrap soared, new variants, such as the dependent bootstrap.
The Jackknife and independent bootstrap work for
independent, identically distributed random variables.
If these conditions are not
satisfied, the methods will fail. Yet, it should be said that if the data are
independent, identically distributed, and we only want to estimate the
variance of
7.14. Resampling methods: Jackknife¶
The Jackknife works by making many replicas of the estimator
which equals the vector
7.15. Resampling methods: Jackknife estimator¶
To get an estimate for the bias and
standard error of
7.16. Jackknife code example¶
from numpy import *
from numpy.random import randint, randn
from time import time
def jackknife(data, stat):
n = len(data);t = zeros(n); inds = arange(n); t0 = time()
## 'jackknifing' by leaving out an observation for each i
for i in range(n):
t[i] = stat(delete(data,i) )
# analysis
print("Runtime: %g sec" % (time()-t0)); print("Jackknife Statistics :")
print("original bias std. error")
print("%8g %14g %15g" % (stat(data),(n-1)*mean(t)/n, (n*var(t))**.5))
return t
# Returns mean of data samples
def stat(data):
return mean(data)
mu, sigma = 100, 15
datapoints = 10000
x = mu + sigma*random.randn(datapoints)
# jackknife returns the data sample
t = jackknife(x, stat)
Runtime: 0.0904138 sec
Jackknife Statistics :
original bias std. error
99.9498 99.9399 0.151413
7.17. Resampling methods: Bootstrap¶
Bootstrapping is a nonparametric approach to statistical inference that substitutes computation for more traditional distributional assumptions and asymptotic results. Bootstrapping offers a number of advantages:
The bootstrap is quite general, although there are some cases in which it fails.
Because it does not require distributional assumptions (such as normally distributed errors), the bootstrap can provide more accurate inferences when the data are not well behaved or when the sample size is small.
It is possible to apply the bootstrap to statistics with sampling distributions that are difficult to derive, even asymptotically.
It is relatively simple to apply the bootstrap to complex data-collection plans (such as stratified and clustered samples).
7.18. Resampling methods: Bootstrap background¶
Since
7.19. Resampling methods: More Bootstrap background¶
In the case that
Drawing lots of numbers from
, suppose we call one such set of numbers .Then using these numbers, we could compute a replica of
called .
By repeated use of (1) and (2), many
estimates of
7.20. Resampling methods: Bootstrap approach¶
But
unless there is enough information available about the process that
generated
Instead of generating the histogram for the relative
frequency of the observation
7.21. Resampling methods: Bootstrap steps¶
The independent bootstrap works like this:
Draw with replacement
numbers for the observed variables .Define a vector
containing the values which were drawn from .Using the vector
compute by evaluating under the observations .Repeat this process
times.
When you are done, you can draw a histogram of the relative frequency of
7.22. Code example for the Bootstrap method¶
The following code starts with a Gaussian distribution with mean value
%matplotlib inline
%matplotlib inline
from numpy import *
from numpy.random import randint, randn
from time import time
from scipy.stats import norm
import matplotlib.pyplot as plt
# Returns mean of bootstrap samples
def stat(data):
return mean(data)
# Bootstrap algorithm
def bootstrap(data, statistic, R):
t = zeros(R); n = len(data); inds = arange(n); t0 = time()
# non-parametric bootstrap
for i in range(R):
t[i] = statistic(data[randint(0,n,n)])
# analysis
print("Runtime: %g sec" % (time()-t0)); print("Bootstrap Statistics :")
print("original bias std. error")
print("%8g %8g %14g %15g" % (statistic(data), std(data),\
mean(t), \
std(t)))
return t
mu, sigma = 100, 15
datapoints = 10000
x = mu + sigma*random.randn(datapoints)
# bootstrap returns the data sample t = bootstrap(x, stat, datapoints)
# the histogram of the bootstrapped data
t = bootstrap(x, stat, datapoints)
# the histogram of the bootstrapped data
n, binsboot, patches = plt.hist(t, bins=50, density='true',histtype='bar', color='red', alpha=0.75)
# add a 'best fit' line
y = norm.pdf( binsboot, mean(t), std(t))
lt = plt.plot(binsboot, y, 'r--', linewidth=1)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.axis([99.5, 100.6, 0, 3.0])
plt.grid(True)
plt.show()
Runtime: 1.23216 sec
Bootstrap Statistics :
original bias std. error
100.039 14.8853 100.039 0.148237

7.23. Resampling methods: Blocking¶
The blocking method was made popular by Flyvbjerg and Pedersen (1989)
and has become one of the standard ways to estimate
Assume
The strength of the blocking method is when the number of
observations,
7.24. Blocking Transformations¶
We now define
blocking transformations. The idea is to take the mean of subsequent
pair of elements from
The quantity
We can then compute the autocovariance, the variance, sample mean, and
number of observations for each
7.25. Blocking Transformations¶
Using the
definition of the blocking transformation and the distributive
property of the covariance, it is clear that since
The quantity
7.26. Blocking Transformations, getting there¶
We have
The term
We can show that
7.27. Blocking Transformations, final expressions¶
We can then wrap up
By repeated use of this equation we get
Flyvbjerg and Petersen demonstrated that the sequence
For an elegant solution and proof of the blocking method, see the recent article of Marius Jonsson (former MSc student of the Computational Physics group).