Few updates on todays lecture

This commit is contained in:
2015-10-19 18:53:52 +02:00
parent b5084b78f4
commit 1e3b02b9a1
6 changed files with 50 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
x = randn( 100, 1 );
bins1 = -4:2:4;
bins2 = -4:0.5:4;
x = randn( 100, 1 ); % generate some data
bins1 = -4:2:4; % large bins
bins2 = -4:0.5:4; % small bins
subplot( 1, 2, 1 );
hold on;
hist( x, bins1 );
@@ -10,6 +10,7 @@ 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)) );
xlabel('x')

View File

@@ -1,22 +1,30 @@
% plot Gaussian pdf:
dx=0.1
dx=0.1;
x = [-4.0:dx:4.0];
p = exp(-0.5*x.^2)/sqrt(2.0*pi);
hold on
plot(x,p, 'linewidth', 10 )
% compute integral between x1 and x2:
x1=1.0
x2=2.0
P = sum(p((x>=x1)&(x<x2)))*dx
% draw random numbers:
r = randn( 10000, 1 );
hist(r,x,1.0/dx)
% check P:
Pr = sum((r>=x1)&(r<x2))/length(r)
plot(x, p, 'linewidth', 10)
% show area of integral:
area(x((x>=x1)&(x<=x2)), p((x>=x1)&(x<=x2)), 'FaceColor', 'r' )
hold off
% compute integral between x1 and x2:
x1=1.0;
x2=2.0;
P = sum(p((x>=x1)&(x<x2)))*dx;
disp( [ 'The integral between ', num2str(x1, 1), ' and ', num2str(x2, 1), ' is ', num2str(P, 3) ] );
% draw random numbers:
%r = randn( 10000, 1 );
%hist(r,x,1.0/dx)
% check P:
Pr = sum((r>=x1)&(r<x2))/length(r);
disp( [ 'The probability of getting a number between ', num2str(x1, 1), ' and ', num2str(x2, 1), ' is ', num2str(Pr, 3) ] );
% infinite integral:
P = sum(p)*dx;
disp( [ 'The integral between -infinity and +infinity is ', num2str(P, 3) ] );
disp( [ 'I.e. the probability to get any number is ', num2str(P, 3) ] );

View File

@@ -1,13 +1,13 @@
function m = mymedian( x )
% returns the median of the vector x
xs = sort( x );
if ( length( xs ) == 0 )
if ( length( xs ) == 0 ) % empty input vector
m = NaN;
elseif ( rem( length( xs ), 2 ) == 0 )
elseif ( rem( length( xs ), 2 ) == 0 ) % even number of data values
index = length( xs )/2;
m = (xs( index ) + xs( index+1 ))/2;
else
index = (length( xs ) + 1)/2;
m = (xs( index ) + xs( index+1 ))/2; % average the two central elements
else % odd number of data values
index = (length( xs ) + 1)/2; % take the middle element
m = xs( index );
end
end

View File

@@ -1,13 +1,14 @@
function q = quartiles( x )
% returns a vector with the first, second, and third quartile of the vector x
% returns a vector with the first, second, and third quartile
% of the vector x
xs = sort( x );
if ( length( xs ) == 0 )
if ( length( xs ) == 0 ) % no data
q = [];
elseif ( rem( length( xs ), 2 ) == 0 )
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
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)) ];