[statistics] imporved code style

This commit is contained in:
Jan Benda 2020-12-07 19:53:13 +01:00
parent 08382f94d4
commit e1c6c32db0
11 changed files with 61 additions and 61 deletions

View File

@ -304,7 +304,7 @@
%
\newboolean{showexercisesolutions}
\setboolean{showexercisesolutions}{true}
\newcommand{\exercisesolutions}{end} % 0: here, 1: chapter, 2: end
\newcommand{\exercisesolutions}{here} % 0: here, 1: chapter, 2: end
% we need this also as numbers:
\ifthenelse{\equal{\exercisesolutions}{end}}{\newcommand{\exercisesolutionsnum}{2}}{%
\ifthenelse{\equal{\exercisesolutions}{chapter}}{\newcommand{\exercisesolutionsnum}{1}}{%

View File

@ -43,7 +43,7 @@ filllw = 1.0
fillec = colors['white']
fillalpha = 0.4
filledge = {'linewidth': filllw, 'joinstyle': 'round'}
if int(mpl.__version__.split('.')[0]) < 2:
if mpl_major < 2:
del filledge['joinstyle']
# helper lines:

View File

@ -1,11 +1,11 @@
% check whether the median returned by mymedian
% really separates a vector into two halfs
for i = 1:140 % loop over different length
for k = 1:10 % try several times
a = randn( i, 1 ); % generate some data
m = mymedian( a ); % compute median
if length( a(a>m) ) ~= length( a(a<m) ) % check
disp( 'error!' )
for i = 1:140 % loop over different length
for k = 1:10 % try several times
a = randn(i, 1); % generate some data
m = mymedian(a); % compute median
if length(a(a>m)) ~= length(a(a<m)) % check
disp('error!')
end
end
end

View File

@ -1,5 +1,5 @@
n = 200;
corrs = [ 1.0, 0.6, 0.0, -0.9 ];
corrs = [1.0, 0.6, 0.0, -0.9];
for k = [1:length(corrs)]
r = corrs(k);
x = randn(n, 1);
@ -9,8 +9,8 @@ for k = [1:length(corrs)]
% compute correlation coefficient of data:
rho = corr(x, y);
subplot(2, 2, k)
scatter( x, y )
text( -2, 2.5, sprintf('r=%.1f', rho) )
scatter(x, y)
text(-2, 2.5, sprintf('r=%.1f', rho))
xlabel('x')
ylabel('y')
xlim([-3.0, 3.0])

View File

@ -5,7 +5,7 @@ plot(xs, cdf);
hold on;
dx = 0.01;
xx = [-4:dx:4]; % x-values for Gaussian pdf
xx = [-4:dx:4]; % x-values for Gaussian pdf
gauss = exp(-0.5*xx.^2)/sqrt(2.0*pi); % Gaussian pdf
gausscdf = cumsum(gauss)*dx;
plot(xx, gausscdf);

View File

@ -20,5 +20,5 @@ for i = [1:length(nrolls)]
bar(b, h, 'facecolor', 'b')
hold off
title(sprintf('N=%d', length(d)))
pause( 2.0 )
pause(2.0)
end

View File

@ -12,15 +12,15 @@ hold off
% compute integral between x1 and x2:
P = sum(p((x>=x1)&(x<x2)))*dx;
fprintf( 'Integral between %.2g and %.2g: %.3g\n', x1, x2, P );
fprintf('Integral between %.2g and %.2g: %.3g\n', x1, x2, P);
% draw random numbers:
r = randn( 10000, 1 );
r = randn(10000, 1);
% check P:
Pr = sum((r>=x1)&(r<x2))/length(r);
fprintf( 'Probability of a number between %.2g and %.2g: %.3g\n', x1, x2, Pr );
fprintf('Probability of a number between %.2g and %.2g: %.3g\n', x1, x2, Pr);
% infinite integral:
P = sum(p)*dx;
fprintf( 'Integral between -infinity and +infinity: %.3g\n', P );
fprintf('Integral between -infinity and +infinity: %.3g\n', P);

View File

@ -1,24 +1,24 @@
% generate data:
x = randn( 1, 100000 );
x = randn(1, 100000);
% histogram:
[h,b] = hist( x, 100 );
[h,b] = hist(x, 100);
% normalize:
bs = b(2)-b(1);
h = h/sum(h)/bs;
% plot:
bar( b, h );
xlabel( 'x' );
bar(b, h);
xlabel('x');
% median, quartile:
q = quartiles( x );
%q = quantile( x, [0.25, 0.5, 0.75 ] );
q = quartiles(x);
%q = quantile(x, [0.25, 0.5, 0.75 ]);
% plot:
hold on;
bar( b(b<q(1)), h(b<q(1)), 'FaceColor', [0.5 0 0.5] );
bar( b((b>=q(1)) & (b<q(2))), h((b>=q(1)) & (b<q(2))), 'FaceColor', [0.9 0 0] );
bar( b((b>=q(2)) & (b<q(3))), h((b>=q(2)) & (b<q(3))), 'FaceColor', [0 0 0.9] );
bar( b(b>=q(3)), h(b>=q(3)), 'FaceColor', [0.5 0 0.5] );
bar(b(b<q(1)), h(b<q(1)), 'FaceColor', [0.5 0 0.5]);
bar(b((b>=q(1)) & (b<q(2))), h((b>=q(1)) & (b<q(2))), 'FaceColor', [0.9 0 0]);
bar(b((b>=q(2)) & (b<q(3))), h((b>=q(2)) & (b<q(3))), 'FaceColor', [0 0 0.9]);
bar(b(b>=q(3)), h(b>=q(3)), 'FaceColor', [0.5 0 0.5]);
hold off;

View File

@ -1,16 +1,16 @@
function q = quartiles( x )
function q = quartiles(x)
% returns a vector with the first, second, and third quartile
% of the vector x
xs = sort( x );
if ( length( xs ) == 0 ) % no data
xs = sort(x);
if (length(xs) == 0) % no data
q = [];
elseif ( rem( length( xs ), 2 ) == 0 ) % even number of data
index = length( xs )/2;
m = (xs( index ) + xs( index+1 ))/2;
q = [ round( xs(length(xs)/4) ), m, xs(round(3*length(xs)/4)) ];
else % odd number of data
index = (length( xs ) + 1)/2;
m = xs( index );
q = [ round( xs(length(xs)/4) ), m, xs(round(3*length(xs)/4)) ];
elseif (rem(length(xs), 2) == 0) % even number of data
index = length(xs)/2;
m = (xs(index) + xs(index+1))/2;
q = [round(xs(length(xs)/4)), m, xs(round(3*length(xs)/4))];
else % odd number of data
index = (length(xs) + 1)/2;
m = xs(index);
q = [round(xs(length(xs)/4)), m, xs(round(3*length(xs)/4))];
end
end

View File

@ -1,4 +1,4 @@
function x = rollthedie( n )
function x = rollthedie(n)
% return a vector with the result of rolling a die n times
x = randi( [1, 6], n, 1 );
x = randi([1, 6], n, 1);
end

View File

@ -1,48 +1,48 @@
% sprintf returns a string.
% This string can be used to annotate plots using the text() function.
s = sprintf( 'x=%f', pi )
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 )
fprintf('x=%f\n', pi)
% The '%f' formatting string can be anywhere in the string:
fprintf( 'x=%fms\n', pi )
fprintf('x=%fms\n', pi)
% There can be arbitrary many '%' formatting strings:
fprintf( 'x=%fms, y=%fkHz\n', pi, 2*pi )
fprintf('x=%fms, y=%fkHz\n', pi, 2*pi)
% The '%' itself is generated by '%%':
fprintf( 'x=%f%%\n', pi )
fprintf('x=%f%%\n', pi)
% A point followed by a number sets the number of digits after the point:
fprintf( 'x=%.2fms\n', pi )
fprintf('x=%.2fms\n', pi)
% The numbers are appropriately rounded:
fprintf( 'x=%.3fms\n', pi )
fprintf('x=%.3fms\n', pi)
% A number right before the point sets the width of the generated output:
fprintf( 'x=%10.3fms\n', pi )
fprintf('x=%10.3fms\n', pi)
% '%e' also formats floating point numbers but forces to write in
% exponential style:
fprintf( 'x=%e\n', pi )
fprintf('x=%e\n', pi)
% again, a point and number set the number of digits after the point.
fprintf( 'x=%.1e\n', pi )
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 )
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 )
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 )
fprintf('x=%d\n', 5)
% the number defines the width of the output:
fprintf( 'x=%3d\n', 5 )
fprintf('x=%3d\n', 5)
% precedig the width with a '0' fills up the space with leading zeros:
fprintf( 'x=%03d\n', 5 )
fprintf('x=%03d\n', 5)
% '%s' formats a string:
fprintf( 'x=%s\n', 'hallo' )
fprintf('x=%s\n', 'hallo')
% ... aligned to the right:
fprintf( 'x=%10s\n', 'hallo' )
fprintf('x=%10s\n', 'hallo')
% ... unless the width is negative:
fprintf( 'x=%-10s!\n', 'hallo' )
fprintf('x=%-10s!\n', 'hallo')