37 lines
883 B
Matlab
37 lines
883 B
Matlab
% 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()
|