Updated some files

This commit is contained in:
Jan Benda 2015-10-22 09:37:36 +02:00
parent fd575f912b
commit b5a3de0fdb
3 changed files with 93 additions and 36 deletions

View File

@ -1,23 +1,24 @@
nsamples = 1000 nsamples = 100;
resample = 500 nresamples = 1000;
x = randn( nsamples, 1 ); % draw a SRS (simple random sample, "Stichprobe") from the population:
sem = std(x)/sqrt(nsamples); x = randn( 1, nsamples );
fprintf('%-30s %-5s %-5s %-5s\n', '', 'mean', 'stdev', 'sem' )
mu = zeros( resample, 1 ); fprintf('%30s %5.2f %5.2f %5.2f\n', 'single SRS', mean( x ), std( x ), std( x )/sqrt(nsamples) )
for i = 1:resample
% resample: % bootstrap the mean:
xr = x(randi(nsamples, nsamples, 1)); mus = zeros(nresamples,1); % vector for storing the means
% compute statistics on sample: for i = 1:nresamples % loop for generating the bootstraps
mu(i) = mean(xr); inx = randi(nsamples, 1, nsamples); % range, 1D-vector, number
end xr = x(inx); % resample the original SRS
bootsem = std( mu ); mus(i) = mean(xr); % compute statistic of the resampled SRS
end
hold on fprintf('%30s %5.2f %5.2f -\n', 'bootstrapped distribution', mean( mus ), std( mus ) )
hist( x, 20 );
hist( mu, 20 ); % many SRS (we can do that with the random number generator, but not in real life!):
hold off musrs = zeros(nresamples,1); % vector for the means of each SRS
for i = 1:nresamples
disp(['bootstrap standard error: ', num2str(bootsem)]); x = randn( 1, nsamples ); % draw a new SRS
disp(['standard error: ', num2str(sem)]); musrs(i) = mean( x ); % compute its mean
end
fprintf('%30s %5.2f %5.2f -\n', 'sampling distribution', mean( musrs ), std( musrs ) )

View File

@ -1,18 +1,26 @@
x = randn( 100, 1 ); % generate some data x = randn( 100, 1 ); % generate some data
bins1 = -4:2:4; % large bins db1=2;
bins2 = -4:0.5:4; % small bins 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 ); subplot( 1, 2, 1 );
hold on; bar(b1,hn1)
hist( x, bins1 ); hold on
hist( x, bins2 ); bar(b2,hn2, 'facecolor', 'r' )
xlabel('x') xlabel('x')
ylabel('Frequeny') ylabel('Frequency')
hold off; hold off
subplot( 1, 2, 2 );
hold on; % normalize:
% normalize to the rigtht bin size: hn1 = h1/sum(h1)/db1;
hist( x, bins1, 1.0/(bins1(2)-bins1(1)) ); hn2 = h2/sum(h2)/db2;
hist( x, bins2, 1.0/(bins2(2)-bins2(1)) ); subplot( 1, 2, 2 )
bar(b1,hn1)
hold on
bar(b2,hn2, 'facecolor', 'r' )
xlabel('x') xlabel('x')
ylabel('Probability density') ylabel('Probability density')
hold off; hold off

View File

@ -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' )