# Double Bootstrap Example

I am working on the double (iterated) Bootstrap. The idea is to take resamples from a given sample, compute the statistic of interest for this sample, and then standardize the result. When there is no analytic formula to compute the standard error, one must take another resample from the first resample. I tried to code this but my results seem strange. Here a part of my results:

1.1801,-1.775
1.2573,-1.4308
1.2813,-1.3563
1.3505,-1.3315
1.3631,-1.2758
1.2691,-1.2697
1.3084,-1.2621
1.2499,-1.1705
1.3931,-1.1314
1.2581,-1.1085
1.3545,-1.0996
1.3341,-1.0961
1.2215,-1.0782
1.3475,-1.0703
1.3616,-1.07
1.3236,-1.069
1.2662,-1.022
1.3391,-1.0124
1.3707,-1.0003
1.3267,-0.9939
1.4106,-0.9889
1.3328,-0.9836
1.4318,-0.9693
1.3528,-0.96
1.3569,-0.9547
1.3639,-0.9481
1.3294,-0.9327
1.3867,-0.9318


Each line represents one bootstrap result. The first col contains the statistic of interest for this bootstrap sample, the second col the calculated z. The results are sorted by z. The strange thing here is that the order is not always logical. See

1.3631,-1.2758
1.2691,-1.2697


While the order of the zs is sorted, the values are not. 1.26 is more extreme than 1.36 (its further from the mean). Can this happen? I thought yes because of the random component and same results may be identical, but the variation within is larger. Below I can show you my Python3 code so you can inspect my logic. Thanks a lot.

def doubleboot(func, reps1, reps2, data, prec=4):
thetahat = func(data)
results = []
for x in range(reps1):          #Loop that takes resamples from the main sample
B1 = []                     #Holds resampled values
for d in range(len(data)):
randomn1 = random.randint(0,len(data)-1)
B1.append(data[randomn1])
thetahatB1 = func(B1)

#From here on the inner boot that samples from Bootstrap1
thetasinner = []
for i in range(reps2):      #Loop that takes resamples from the resample
B2 = []                 #Holds resampled values from the resample
for i in range(len(B1)):
randomn2 = random.randint(0,len(B1)-1)
B2.append(B1[randomn2])
thetasinner.append(func(B2))
varsinner = sd(thetasinner)     #sd calculates the standard deviation of a list
z = (thetahatB1 - thetahat) / varsinner
results.append((round(thetahatB1,prec), round(z,prec)))
return sorted(results, key=lambda x: x[1])


EDIT1 Just again for the logic, here is a summary: https://freakonometrics.github.io/documents/teaching/slides-econometrics-2018-graduate-2.pdf The idea is as follows: take one regular bootstrap sample from your data and calculate the statistic of interest. By chance, the bootstrap result (thetahatB1) will differ from your sample result (thetahat). Now you want to standardize (studentize) this difference. If there is no analytical way to do so (if you want a median or whatever), you have to take a second round of bootstrap from your first one. This being done, you calculate this standard error and divide the difference by this factor, The results kinda make sense as they are in the correct range (from zero to +- 2). However, I dont really understand this problem of ordering. As you see, there are some "outliers" that have quite extreme results, but their z-values (the second col) are not that far out. I just wonder if this makes sense conceptually.

I am quite a beginner with statistics, so I cannot say much about bootstrapping. But checking your code, there are two nested loops that use the same variable (i). This may be altering the number of loops. Have you tried changing one of the i by j, for example?