New bootstrap exercises
This commit is contained in:
23
statistics/code/bootstrapsem.m
Normal file
23
statistics/code/bootstrapsem.m
Normal file
@@ -0,0 +1,23 @@
|
||||
nsamples = 1000
|
||||
resample = 500
|
||||
|
||||
x = randn( nsamples, 1 );
|
||||
sem = std(x)/sqrt(nsamples);
|
||||
|
||||
mu = zeros( resample, 1 );
|
||||
for i = 1:resample
|
||||
% resample:
|
||||
xr = x(randi(nsamples, nsamples, 1));
|
||||
% compute statistics on sample:
|
||||
mu(i) = mean(xr);
|
||||
end
|
||||
bootsem = std( mu );
|
||||
|
||||
hold on
|
||||
hist( x, 20 );
|
||||
hist( mu, 20 );
|
||||
hold off
|
||||
|
||||
disp(['bootstrap standard error: ', num2str(bootsem)]);
|
||||
disp(['standard error: ', num2str(sem)]);
|
||||
|
||||
24
statistics/code/bootstraptymus.m
Normal file
24
statistics/code/bootstraptymus.m
Normal file
@@ -0,0 +1,24 @@
|
||||
resample = 500
|
||||
|
||||
load( 'thymusglandweights.dat' );
|
||||
x = thymusglandweights;
|
||||
nsamples = length( x );
|
||||
sem = std(x)/sqrt(nsamples);
|
||||
|
||||
mu = zeros( resample, 1 );
|
||||
for i = 1:resample
|
||||
% resample:
|
||||
xr = x(randi(nsamples, nsamples, 1));
|
||||
% compute statistics on sample:
|
||||
mu(i) = mean(xr);
|
||||
end
|
||||
bootsem = std( mu );
|
||||
|
||||
hold on
|
||||
hist( x, 20 );
|
||||
hist( mu, 20 );
|
||||
hold off
|
||||
|
||||
disp(['bootstrap standard error: ', num2str(bootsem)]);
|
||||
disp(['standard error: ', num2str(sem)]);
|
||||
|
||||
3
statistics/code/boxwhisker.m
Normal file
3
statistics/code/boxwhisker.m
Normal file
@@ -0,0 +1,3 @@
|
||||
x = randn(40, 10);
|
||||
boxplot(x, 'whisker', 100.0 );
|
||||
|
||||
14
statistics/code/correlations.m
Normal file
14
statistics/code/correlations.m
Normal file
@@ -0,0 +1,14 @@
|
||||
n = 1000
|
||||
x = randn( n, 1 );
|
||||
y = randn( n, 1 ) + 0.2*x;
|
||||
r = corr(x,y)
|
||||
|
||||
nsamples = 500;
|
||||
rs = zeros( nsamples, 1 );
|
||||
for i = 1:nsamples
|
||||
xs = x(randi(n,n,1));
|
||||
ys = x(randi(n,n,1));
|
||||
rs(i) = corr(xs,ys);
|
||||
end
|
||||
|
||||
hist( rs, 20 )
|
||||
32
statistics/code/tdistribution.m
Normal file
32
statistics/code/tdistribution.m
Normal file
@@ -0,0 +1,32 @@
|
||||
n = 100000
|
||||
x=randn(n, 1);
|
||||
|
||||
nsamples = 3;
|
||||
nmeans = 10000;
|
||||
means = zeros( nmeans, 1 );
|
||||
sdevs = zeros( nmeans, 1 );
|
||||
students = zeros( nmeans, 1 );
|
||||
for i=1:nmeans
|
||||
sample = x(randi(n, nsamples, 1));
|
||||
means(i) = mean(sample);
|
||||
sdevs(i) = std(sample);
|
||||
students(i) = mean(sample)/std(sample)*sqrt(nsamples);
|
||||
end
|
||||
sdev = 1.0
|
||||
msdev = std(means)
|
||||
|
||||
% scatter( means, sdevs )
|
||||
|
||||
hold on;
|
||||
dxg=0.01;
|
||||
xmax = 10.0
|
||||
xmin = -xmax
|
||||
xg = [xmin:dxg:xmax];
|
||||
pg = exp(-0.5*(xg/sdev).^2)/sqrt(2.0*pi)/sdev;
|
||||
hold on
|
||||
plot(xg, pg, 'r', 'linewidth', 4)
|
||||
|
||||
bins = xmin:0.1:xmax;
|
||||
hist(means, bins, 1.0/(bins(2)-bins(1)) );
|
||||
hold off;
|
||||
|
||||
Reference in New Issue
Block a user