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
2020-12-21 22:07:36 +01:00

48 lines
1.5 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
from plotstyle import *
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)
fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.1*figure_height))
fig.subplots_adjust(**adjust_fs(left=4.0, bottom=2.7, right=1.5))
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.22, 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, **fsB)
ax.bar(bins[:-1]+0.25*db, hmus, 0.5*db, **fsA)
plt.savefig('bootstrapsem.pdf')