From 072b9a705371b6fa4bc25d333eef98cc5591e9b2 Mon Sep 17 00:00:00 2001
From: Jan Benda <jan.benda@uni-tuebingen.de>
Date: Sun, 6 Jan 2019 19:30:23 +0100
Subject: [PATCH] [linalg] added covar examples

---
 linearalgebra/code/coordinatetrafo.m  |  3 ++-
 linearalgebra/code/covarmatrix.m      | 28 +++++++++++++++++++++
 linearalgebra/code/covarmatrixeigen.m | 36 +++++++++++++++++++++++++++
 linearalgebra/code/pca2d.m            |  2 +-
 linearalgebra/code/pca2dexamples.m    | 10 +++-----
 linearalgebra/code/pca3d.m            |  2 +-
 linearalgebra/code/pca3dexamples.m    |  3 ---
 7 files changed, 72 insertions(+), 12 deletions(-)
 create mode 100644 linearalgebra/code/covarmatrix.m
 create mode 100644 linearalgebra/code/covarmatrixeigen.m

diff --git a/linearalgebra/code/coordinatetrafo.m b/linearalgebra/code/coordinatetrafo.m
index a1fd836..de62642 100644
--- a/linearalgebra/code/coordinatetrafo.m
+++ b/linearalgebra/code/coordinatetrafo.m
@@ -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 ]
diff --git a/linearalgebra/code/covarmatrix.m b/linearalgebra/code/covarmatrix.m
new file mode 100644
index 0000000..ff4368e
--- /dev/null
+++ b/linearalgebra/code/covarmatrix.m
@@ -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()
diff --git a/linearalgebra/code/covarmatrixeigen.m b/linearalgebra/code/covarmatrixeigen.m
new file mode 100644
index 0000000..c0a6f74
--- /dev/null
+++ b/linearalgebra/code/covarmatrixeigen.m
@@ -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()
diff --git a/linearalgebra/code/pca2d.m b/linearalgebra/code/pca2d.m
index 84e217f..64c3d21 100644
--- a/linearalgebra/code/pca2d.m
+++ b/linearalgebra/code/pca2d.m
@@ -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' );
diff --git a/linearalgebra/code/pca2dexamples.m b/linearalgebra/code/pca2dexamples.m
index 15552f3..52ae0b9 100644
--- a/linearalgebra/code/pca2dexamples.m
+++ b/linearalgebra/code/pca2dexamples.m
@@ -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
diff --git a/linearalgebra/code/pca3d.m b/linearalgebra/code/pca3d.m
index e967faf..c485638 100644
--- a/linearalgebra/code/pca3d.m
+++ b/linearalgebra/code/pca3d.m
@@ -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' );
diff --git a/linearalgebra/code/pca3dexamples.m b/linearalgebra/code/pca3dexamples.m
index 7abc511..5637c1a 100644
--- a/linearalgebra/code/pca3dexamples.m
+++ b/linearalgebra/code/pca3dexamples.m
@@ -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: