This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/bootstrap/lecture/bootstrapsem.py

54 lines
1.6 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
plt.xkcd()
fig = plt.figure( figsize=(6,3.5) )
rng = np.random.RandomState(637281)
nsamples = 100
nresamples = 1000
db = 0.05;
bins = np.arange(-0.4, 0.41, db)
# draw a SRS (simple random sample, "Stichprobe") from the population:
x = rng.randn(nsamples)
# bootstrap the mean:
mus = []
for i in range(nresamples) :
mus.append(np.mean(x[rng.randint(0, nsamples, nsamples)]))
hmus, _ = np.histogram(mus, bins, density=True)
# many SRS:
musrs = []
for i in range(nresamples) :
musrs.append(np.mean(rng.randn(nsamples)))
hmusrs, _ = np.histogram(musrs, bins, density=True)
ax = fig.add_subplot(1, 1, 1)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.set_xlabel('Mean')
ax.set_xlim(-0.4, 0.4)
ax.set_ylabel('Probability density')
ax.set_ylim(0.0, 4.5)
ax.set_yticks(np.arange(0.0, 4.5, 1.0))
ax.annotate('sampling\ndistribution',
xy=(-0.12, 3.1), xycoords='data',
xytext=(-0.18, 3.5), textcoords='data', ha='right',
arrowprops=dict(arrowstyle="->", relpos=(1.0,0.5),
connectionstyle="angle3,angleA=20,angleB=120") )
ax.annotate('bootstrap\ndistribution',
xy=(0.13, 3.3), xycoords='data',
xytext=(0.25, 4), textcoords='data',
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5),
connectionstyle="angle3,angleA=20,angleB=60") )
ax.bar(bins[:-1]-0.25*db, hmusrs, 0.5*db, color='r')
ax.bar(bins[:-1]+0.25*db, hmus, 0.5*db, color='b')
plt.tight_layout()
plt.savefig('bootstrapsem.pdf')
#plt.show();