83 lines
2.3 KiB
Matlab
83 lines
2.3 KiB
Matlab
function covareigen( x, y, pm, w, h )
|
|
% computes covariance matrix from the pairs x, y
|
|
% diagonalizes covariance matrix
|
|
% x and y are column vectors
|
|
% pm: 0 - scatter of data, 1 histogram, 2 multivariate gauus from
|
|
% covariance matrix, 1 and 2 require w and h
|
|
% w and h are the width and the height (in data units) for plotting
|
|
% histograms instead of scatter
|
|
|
|
% covariance matrix:
|
|
cv = cov( [ x y ] );
|
|
|
|
% eigen values:
|
|
[ v , d] = eig( cv )
|
|
s = sign( v );
|
|
s(1,:) = s(2,:);
|
|
v = v .* s;
|
|
|
|
% plots:
|
|
subplot( 2, 2, 1 );
|
|
hold on;
|
|
|
|
if (pm > 0) & (nargin == 5)
|
|
if pm == 1
|
|
% histogram of data:
|
|
xp = -w:0.5:w;
|
|
yp = -h:0.5:h;
|
|
[n,c] = hist3([x y], { xp, yp } );
|
|
contourf( c{1}, c{2}, n' );
|
|
else
|
|
xp = -w:0.1:w;
|
|
yp = -h:0.1:h;
|
|
[xg,yg] = meshgrid( xp, yp );
|
|
xy = [ xg(:) yg(:) ];
|
|
gauss = reshape( exp(-0.5*diag(xy*inv(cv)*xy'))/sqrt((2.0*pi)^2.0*det(cv)), size( xg ) );
|
|
contourf( xp, yp, gauss )
|
|
end
|
|
colormap( 'gray' );
|
|
else
|
|
% scatter plot:
|
|
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
|
end
|
|
|
|
% plot eigenvectors:
|
|
quiver( ones( 1, 2 ).*mean( x ), ones( 1, 2 )*mean(y), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', 'r', 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
|
|
|
|
axis( 'equal' );
|
|
hold off;
|
|
|
|
subplot( 2, 2, 3 );
|
|
hist( x, 50, 'b' );
|
|
|
|
% sort the eigenvalues:
|
|
[d,inx] = sort( diag(d), 'descend' );
|
|
|
|
subplot( 2, 2, 2 );
|
|
hold on;
|
|
% subtract means:
|
|
x = x - mean( x );
|
|
y = y - mean( y );
|
|
% project onto eigenvectors:
|
|
nx = [ x y ] * v(:,inx(1));
|
|
ny = [ x y ] * v(:,inx(2));
|
|
cv = cov( [nx, ny] )
|
|
[ v , d] = eig( cv )
|
|
if (pm > 0) & (nargin == 5)
|
|
if pm == 1
|
|
[n,c] = hist3([nx ny], { xp, yp } );
|
|
contourf( c{1}, c{2}, n' );
|
|
else
|
|
gauss = reshape( exp(-0.5*diag(xy*inv(cv)*xy'))/sqrt((2.0*pi)^2.0*det(cv)), size( xg ) );
|
|
contourf( xp, yp, gauss )
|
|
end
|
|
else
|
|
scatter( nx, ny, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
|
end
|
|
axis( 'equal' );
|
|
hold off;
|
|
|
|
subplot( 2, 2, 4 );
|
|
hist( nx, 50, 'b' );
|
|
end
|