diff --git a/statistics/code/bootstrapsem.m b/statistics/code/bootstrapsem.m index b5a7cbe..18ebae3 100644 --- a/statistics/code/bootstrapsem.m +++ b/statistics/code/bootstrapsem.m @@ -1,23 +1,24 @@ -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)]); - +nsamples = 100; +nresamples = 1000; + +% draw a SRS (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 ) ) diff --git a/statistics/code/gaussianbins.m b/statistics/code/gaussianbins.m index effe1fd..717f4ea 100644 --- a/statistics/code/gaussianbins.m +++ b/statistics/code/gaussianbins.m @@ -1,18 +1,26 @@ x = randn( 100, 1 ); % generate some data -bins1 = -4:2:4; % large bins -bins2 = -4:0.5:4; % small bins +db1=2; +db2 = 0.5; +bins1 = -4:db1:4; % large bins +bins2 = -4:db2:4; % small bins +[h1,b1] = hist(x,bins1); +[h2,b2] = hist(x,bins2); + subplot( 1, 2, 1 ); -hold on; -hist( x, bins1 ); -hist( x, bins2 ); +bar(b1,hn1) +hold on +bar(b2,hn2, 'facecolor', 'r' ) xlabel('x') -ylabel('Frequeny') -hold off; -subplot( 1, 2, 2 ); -hold on; -% normalize to the rigtht bin size: -hist( x, bins1, 1.0/(bins1(2)-bins1(1)) ); -hist( x, bins2, 1.0/(bins2(2)-bins2(1)) ); +ylabel('Frequency') +hold off + +% normalize: +hn1 = h1/sum(h1)/db1; +hn2 = h2/sum(h2)/db2; +subplot( 1, 2, 2 ) +bar(b1,hn1) +hold on +bar(b2,hn2, 'facecolor', 'r' ) xlabel('x') ylabel('Probability density') -hold off; +hold off diff --git a/statistics/code/sprintfexamples.m b/statistics/code/sprintfexamples.m new file mode 100644 index 0000000..b8f6b75 --- /dev/null +++ b/statistics/code/sprintfexamples.m @@ -0,0 +1,48 @@ +% sprintf returns a string. +% This string can be used to annotate plots using the text() function. +s = sprintf( 'x=%f', pi ) + +% fprintf writes directly to console (or into files). +% for fprintf you usually want to add the line break '\n': + +% '%f' formats floating point numbers: +fprintf( 'x=%f\n', pi ) +% The '%f' formatting string can be anywhere in the string: +fprintf( 'x=%fms\n', pi ) +% There can be arbitrary many '%' formatting strings: +fprintf( 'x=%fms, y=%fkHz\n', pi, 2*pi ) +% The '%' itself is generated by '%%': +fprintf( 'x=%f%%\n', pi ) +% A point followed by a number sets the number of digits after the point: +fprintf( 'x=%.2fms\n', pi ) +% The numbers are appropriately rounded: +fprintf( 'x=%.3fms\n', pi ) +% A number right before the point sets the width of the generated output: +fprintf( 'x=%10.3fms\n', pi ) +% '%e' also formats floating point numbers but forces to write in +% exponential style: +fprintf( 'x=%e\n', pi ) +% again, a point and number set the number of digits after the point. +fprintf( 'x=%.1e\n', pi ) +% '%g% formats the floating point number to a given number of valid digits +% (default is 5): +fprintf( 'x=%g\n', pi ) +% The number of valid digits is not the number of digits after the point: +fprintf( 'x=%.2g\n', pi ) +fprintf( 'x=%.2g\n', 10.123 ) +fprintf( 'x=%.2g\n', 18765.123 ) +fprintf( 'x=%.5g\n', 18765.123 ) + +% '%d' formats integers: +fprintf( 'x=%d\n', 5 ) +% the number defines the width of the output: +fprintf( 'x=%3d\n', 5 ) +% precedig the width with a '0' fills up the space with leading zeros: +fprintf( 'x=%03d\n', 5 ) + +% '%s' formats a string: +fprintf( 'x=%s\n', 'hallo' ) +% ... aligned to the right: +fprintf( 'x=%10s\n', 'hallo' ) +% ... unless the width is negative: +fprintf( 'x=%-10s!\n', 'hallo' )