26 lines
1.1 KiB
Matlab
26 lines
1.1 KiB
Matlab
nsamples = 100;
|
|
nresamples = 1000;
|
|
|
|
% draw a simple random sample ("Stichprobe") from the population:
|
|
x = randn(1, nsamples);
|
|
fprintf('%-30s %-5s %-5s %-5s\n', '', 'mean', 'stdev', 'sem')
|
|
fprintf('%30s %5.2f %5.2f %5.2f\n', 'single SRS', mean(x), std(x), std(x)/sqrt(nsamples))
|
|
|
|
% bootstrap the mean:
|
|
mus = zeros(nresamples,1); % vector for storing the means
|
|
for i = 1:nresamples % loop for generating the bootstraps
|
|
inx = randi(nsamples, 1, nsamples); % range, 1D-vector, number
|
|
xr = x(inx); % resample the original SRS
|
|
mus(i) = mean(xr); % compute statistic of the resampled SRS
|
|
end
|
|
fprintf('%30s %5.2f %5.2f -\n', 'bootstrapped distribution', mean(mus), std(mus))
|
|
|
|
% many SRS (we can do that with the random number generator,
|
|
% but not in real life!):
|
|
musrs = zeros(nresamples,1); % vector for the means of each SRS
|
|
for i = 1:nresamples
|
|
x = randn(1, nsamples); % draw a new SRS
|
|
musrs(i) = mean(x); % compute its mean
|
|
end
|
|
fprintf('%30s %5.2f %5.2f -\n', 'sampling distribution', mean(musrs), std(musrs))
|