Reorganized statistic exercises and added soultions

This commit is contained in:
2015-10-21 18:06:55 +02:00
parent 69b9afbc80
commit 4bb3d6dc56
16 changed files with 321 additions and 483 deletions

View File

@@ -1,24 +0,0 @@
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)]);

View File

@@ -1,8 +0,0 @@
function x = randomwalk(n,p)
% returns a random wolk with n steps and
% probability p for positive steps.
r = rand(n,1);
r(r<p) = -1.0;
r(r>=p) = +1.0;
x = cumsum(r);
end

View File

@@ -1,25 +0,0 @@
p = 0.5;
nsteps = 100;
nwalks = 1000;
y = zeros( nwalks, nsteps/10 );
for k = 1:length( y )
x = randomwalk( nsteps, p );
for j = 1:nsteps/10
y(k,j) = x((j-1)*10+1);
end
%plot( x )
%pause( 1 )
if rem(k,100) == 0
%[h1,b1] = hist( y(1:k,1), [-50:2:50] );
%[h2,b2] = hist( y(1:k,2), [-50:2:50] );
%bar( b1, h1, 1.0, 'b' );
%hold on;
%bar( b2, h2, 'FaceColor', 'r' );
%hold off;
sdev = var( y(1:k,:), 1 );
plot( sdev )
pause( 1.0 );
end
end

View File

@@ -1,32 +0,0 @@
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;