[linalg] added covar examples
This commit is contained in:
parent
b0decc7df2
commit
072b9a7053
@ -2,8 +2,9 @@
|
||||
x=[-1:0.02:1]'; % column vector!
|
||||
y=4*x/3 + 0.1*randn( size( x ) );
|
||||
plot( x, y, '.b' );
|
||||
axis( 'equal' );
|
||||
hold on;
|
||||
plot( x(1), y(1), '.b' );
|
||||
axis( 'equal' );
|
||||
|
||||
% new coordinate system:
|
||||
% e1 = [ 3 4 ]
|
||||
|
28
linearalgebra/code/covarmatrix.m
Normal file
28
linearalgebra/code/covarmatrix.m
Normal file
@ -0,0 +1,28 @@
|
||||
% covariance in 2D:
|
||||
x = 2.0*randn(1000, 1);
|
||||
y = 0.5*x + 0.2*randn(1000, 1);
|
||||
scatter(x, y)
|
||||
var(x)
|
||||
var(y)
|
||||
cov( [x, y] )
|
||||
pause(1.0)
|
||||
|
||||
% covariance of independent coordinates in 2D:
|
||||
x = 2.0*randn(1000, 1);
|
||||
y = 0.5*randn(1000, 1);
|
||||
scatter(x, y)
|
||||
var(x)
|
||||
var(y)
|
||||
cov( [x, y] )
|
||||
pause(1.0)
|
||||
|
||||
% covariance in 3D:
|
||||
x = 2.0*randn(1000, 1);
|
||||
y = -0.5*x + 0.2*randn(1000, 1);
|
||||
z = 2.0*x + 0.6*y + 2.0*randn(1000, 1);
|
||||
scatter3(x, y, z, 'filled')
|
||||
var(x)
|
||||
var(y)
|
||||
var(z)
|
||||
cov( [x, y, z] )
|
||||
pause()
|
36
linearalgebra/code/covarmatrixeigen.m
Normal file
36
linearalgebra/code/covarmatrixeigen.m
Normal file
@ -0,0 +1,36 @@
|
||||
% covariance in 2D:
|
||||
x = 2.0*randn(1000, 1);
|
||||
y = 0.5*x + 0.6*randn(1000, 1);
|
||||
scatter(x, y)
|
||||
cv = cov( [x, y] );
|
||||
[v d] = eig(cv);
|
||||
hold on
|
||||
quiver([0 0], [0 0], v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', 'r', 'LineWidth', 2);
|
||||
axis( 'equal' );
|
||||
hold off
|
||||
pause
|
||||
|
||||
% covariance of independent coordinates in 2D:
|
||||
x = 1.5*randn(1000, 1);
|
||||
y = 0.8*randn(1000, 1);
|
||||
scatter(x, y)
|
||||
cv = cov( [x, y] );
|
||||
[v d] = eig(cv);
|
||||
hold on
|
||||
quiver([0 0], [0 0], v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', 'r', 'LineWidth', 2);
|
||||
axis( 'equal' );
|
||||
hold off
|
||||
pause
|
||||
|
||||
% covariance in 3D:
|
||||
x = 2.0*randn(1000, 1);
|
||||
y = -0.5*x + 0.2*randn(1000, 1);
|
||||
z = 2.0*x + 0.6*y + 2.0*randn(1000, 1);
|
||||
scatter3(x, y, z)
|
||||
cv = cov( [x, y, z] );
|
||||
[v d] = eig(cv);
|
||||
hold on
|
||||
quiver3([0 0 0], [0 0 0], [0 0 0], v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', v(3,:).*sqrt(diag(d))', 'r', 'LineWidth', 2);
|
||||
axis( 'equal' );
|
||||
hold off
|
||||
pause()
|
@ -43,7 +43,7 @@ function pca2d( x, y, pm, w, h )
|
||||
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 )
|
||||
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 )
|
||||
|
||||
xlabel( 'x' );
|
||||
ylabel( 'y' );
|
||||
|
@ -7,7 +7,7 @@ for r = 0.01:0.19:1
|
||||
clf( f );
|
||||
y = r*x + sqrt(1-r^2)*randn( n, 1 );
|
||||
pca2d( x, y, 0, 5.0, 3.0 );
|
||||
waitforbuttonpress;
|
||||
pause( 1.0 );
|
||||
end
|
||||
|
||||
% two distributions:
|
||||
@ -17,13 +17,11 @@ y1 = randn( n/2, 1 );
|
||||
x2 = randn( n/2, 1 );
|
||||
y2 = randn( n/2, 1 );
|
||||
f = figure( 1 );
|
||||
pause( 'on' );
|
||||
for d = 0:1:5
|
||||
fprintf( 'Distance = %g\n', d );
|
||||
clf( f );
|
||||
d2 = d / sqrt( 2.0 );
|
||||
x = [ x1; x2 ];
|
||||
y = [ y1+d2; y2-d2 ];
|
||||
x = [ x1+d; x2-d ];
|
||||
y = [ y1+d; y2-d ];
|
||||
pca2d( x, y, 0, 10.0, 7.0 );
|
||||
waitforbuttonpress;
|
||||
pause( 1.0 );
|
||||
end
|
||||
|
@ -15,7 +15,7 @@ function pca3d( x, y, z )
|
||||
|
||||
% scatter plot:
|
||||
view( 3 );
|
||||
scatter3( x, y, z ); % , 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
|
||||
scatter3( x, y, z ); % , 1.0, 'b', 'filled' ); %, 'MarkerEdgeColor', 'blue' );
|
||||
xlabel( 'x' );
|
||||
ylabel( 'y' );
|
||||
zlabel( 'z' );
|
||||
|
@ -1,6 +1,3 @@
|
||||
scrsz = get( 0, 'ScreenSize' );
|
||||
set( 0, 'DefaultFigurePosition', [ scrsz(3)/2 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2 ] );
|
||||
|
||||
n = 10000;
|
||||
|
||||
% three distributions:
|
||||
|
Reference in New Issue
Block a user