# Vector and Matrix Geometry

split.screen(c(1,2)) # Divide the graphics device into two parts
horizontally
[1] 1 2
par(bg="white", pty="s")
# set background color (ensures proper
erasure)
screen(2)
par(pty="s")
# and square plot regions

1. Vector Representation
a = c(2, 1)
screen(1)
plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')
# point representation of a vector
abline(h=0,v=0,col="grey")
title("Point Representation of (2, 1)")
screen(2)
plot(0, 0, xlim=c(-3,3), ylim=c(-3,3), type="n", xlab='x', ylab='y')
# Empty plotting frame
arrows(0, 0, a[1], a[2]) # ray representation of a vector
abline(h=0,v=0,col="grey")
title("Ray Representation of (2, 1)")

2. Data as a collection of vectors
X
V1 V2
1 57 93
2 58 110
: :
:
20 69 155
mean(X)
V1
V2
62.85 123.60
sd(X)
V1
V2
3.297128 15.571229
screen(2)
screen(1)
plot(X, pch=16, xlab="Height", ylab="Weight")
title("Raw Data (Women's Heights and Weights)")
Xd = scale(X, scale=F)
# mean-centered data (deviations)
Xd
V1
V2
1 -5.85 -30.6
2 -4.85 -13.6
:
:
:
20 6.15 31.4
apply(Xd, 2, mean)
# Xd is matrix, not data frame
V1
V2
-1.421302e-15 5.685209e-15
screen(2)
plot(Xd, pch=16, xlab="Height", ylab="Weight")
title("Mean-Centered Data (Women's Heights and Weights)")
abline(h=0,v=0,col="grey")

xlab='x'.3). Scalar multiplication a [1] 2 1 screen(2) screen(1) plot(a[1]. pch=16.579493e-16 apply(Xs.a[2].77427146 -1.fg="grey". ylim=c(-3.3). pch=16.col="grey") 3.3). 0.47097719 -0.v=0.inches=F) title("Representation of (2.circ=1:3. 2. mean) V1 V2 -4.86525974 2. ylab='y') arrows(0.308077e-16 3.87340572 : : : 20 1. sd) V1 V2 3. xlim=c(-3.297128 15. 1)") . ylab="Weight") title("Standardized Data (Women's Heights and Weights)") abline(h=0. a[1].apply(Xd.96516286 2 -1.v=0. 2.3). a[2]) abline(h=0.rep(0. xlab="Height".01653967 apply(Xs.571229 Xs = scale(X)# standardized data Xs V1 V2 1 -1.col="grey") symbols(rep(0. sd) V1 V2 1 1 screen(2) plot(Xs.add=T. 2.

2 0. 1)") screen(2) screen(1) plot(Xs. pch=16.ra[2]. xlim=c(-3.2)) title("Rescaled Standardized Data") abline(h=0. ylab="Weight".6 ra = r*a ra [1] 1.6 screen(2) plot(ra[1].rep(0.3).v=0.col="grey") .circ=1:3. xlab="Height". ra[2]) abline(h=0. xlim=c(-2.6*(2. ylim=c(-3. ylab='y') arrows(0.col="grey") symbols(rep(0. xlab="Height".v=0. ylim=c(-2.2). 0. ylim=c(-2. ylab="Weight". xlab='x'. ra[1].v=0.3).r= .fg="grey".3).3).inches=F) title("Point Representation of 0.2). xlim=c(-2.2)) title("Standardized Data") abline(h=0. pch=16. pch=16.add=T.col="grey") screen(2) plot(r*Xs.

] -0. ylim=c(-3.3).121320 screen(2) screen(1) plot(a[1].aw1*w1[1].7071068 [2.1] [1. xlab='x'.] 0.1] [1. xlim=c(-3.] 1 w1^2 [. ylab='y') abline(h=0.a[2].inches=F) w2 = matrix(c(-1.2).col="grey") arrows(0.nc=1) w2 [.] 1 lw2 = sqrt(sum(w2^2)) w2 = w2/lw2# unit length w2 [.7071068 sqrt(sum(w1^2)) [1] 1 a [1] 2 1 aw1 = a %*% w1 aw1 [. w1[2]) abline(0.] 2.a[2].3).7071068 sqrt(sum(w2^2)) [1] 1 aw2 = a %*% w2 aw2 [. col="red") segments(a[1]. 1).] 1 [2.4. w1[1].1] [1.fg="grey".lty=3) symbols(rep(0.] 1 [2.] 0.1] [1.] 1 lw1 = sqrt(sum(w1^2))# length of w1.] 0.v=0.] 1 w2^2 [. 0.1] [1.7071068 [2.] -1 [2. Vector multiplication: projection w1 = matrix(rep(1.3).add=T.1] [1. ||w1|| w1 = w1/lw1# unit length w1 [.1] [1.1] [1.] 1 [2. w1[2]/w1[1]) points(aw1*w1[1]. pch=16.nc=1) w1 [.rep(0.circ=1:3. aw1*w1[2].7071068 .aw1*w1[2]. pch=16.3).] -0.

add=T. 0.screen(2) plot(a[1]. w2[2]/w2[1]) points(aw2*w2[1].v=0. w2[2]) abline(0.2). pch=16.col="grey") arrows(0.96516286 2 -1. w1[2]/w1[1]) points(Z1*w1[1].2]. xlim=c(-3. col="red") segments(Xs[. xlab='x'. w1[2]) abline(0.3).2).add=T.v=0.3). xlab='x'. ylim=c(-2.2).3). pch=16.col="grey") arrows(0. w2[2]/w2[1]) . w2[1].col="grey") arrows(0.aw2*w2[1]. pch=16. w2[1].lty=3) symbols(rep(0. col="green") segments(a[1]. ylab='y') abline(h=0. pch=16.fg="grey".aw2*w2[2]. aw2*w2[2].Xs[.1].87340572 : : : 20 1.3).3).lty=3) symbols(rep(0.77427146 -1.inches=F) Xs V1 V2 1 -1.inches=F) Z2 = Xs %*% w2 screen(2) plot(Xs. 0.Z1*w1[1]. Z1*w1[2].fg="grey". xlab='x'.a[2].rep(0. xlim=c(-2. ylab='y') abline(h=0. ylab='y') abline(h=0.2).Z1*w1[2].rep(0.86525974 2.3). ylim=c(-2.47097719 -0. w1[1]. 0. w2[2]) abline(0.circ=1:3.v=0.circ=1:3. ylim=c(-3. xlim=c(-2.01653967 Z1 = Xs %*% w1 screen(2) screen(1) plot(Xs. pch=16.a[2].

3).fg="grey".v=0.col="grey") symbols(rep(0.fg="grey".Z2*w2[2]. w1[2]) arrows(0. xlab='x'.3).1] [1. 0.Xs[.3).] 2. Orthogonal vectors t(w1) %*% w2 [.3). w1[1]. xlim=c(-3.rep(0.Z2*w2[1].points(Z2*w2[1]. pch=16. type="n". ylim=c(-3. w2[2]) abline(h=0. Z2*w2[2].circ=1:3. 0.lty=3) symbols(rep(0.3).236167e-17 screen(2) screen(1) plot(0.1].rep(0.circ=1:3. 0.add=T. col="green") segments(Xs[.add=T. ylab='y') arrows(0.2].inches=F) 5.inches=F) . w2[1].3).

pch=16. w2[2]/w2[1]) points(Z1*w1[1].rep(0.Z1*w1[2].3).rep(0.0.2].3). xlab='X1'.0. col="green") segments(rep(0.Z2*w2[2]. col="red") segments(Z1.20).2]. col="red") segments(Xs[. ylim=c(-3.circ=1:3.Z2. xlim=c(-3.fg="grey". xlab='Z1'. w2[2]) abline(0.lty=3) points(Z2*w2[1]. ylab='Z2') abline(h=0.1) points(Z1.Z2*w2[1].lty=3) symbols(rep(0.Z1*w1[1].Z2.1.add=T.Z1.inches=F) screen(2) W = cbind(w1.20). Z1*w1[2].Xs[.circ=1:3. Z2.3). w1[2]/w1[1]) arrows(0. Matrix Multiplication: Rotation screen(1) plot(Xs. rep(0.3).0) arrows(0.lty=3) symbols(rep(0. col="green") segments(Xs[.3).20).20). w2) Z = Xs %*% W plot(Z. 0. Z2*w2[2].add=T.Xs[. w1[2]) abline(0. xlim=c(-3. pch=16.rep(0.col="grey") arrows(0.1].6.v=0) arrows(0.inches=F) .3).1].0.Z2. ylab='X2') abline(h=0. 0. w1[1].3). w2[1].Z1. ylim=c(-3.v=0.fg="grey".lty=3) points(rep(0.3).

col="grey") arrows(0. ylim=c(-3. xlab='X1'. xlim=c(-3. xlab='Z1'. ylim=c(-3.circ=1:3. pch=16.rep(0. w2[1]. xlim=c(-3.v=0.0) arrows(0. xlab='X1'.3). ylab='X2') abline(h=0.3).3).1.screen(1) plot(Xs. w2[2]) symbols(rep(0.3).3).v=0.0.3).circ=1:3. w2[1]. w1[1]. 0.3). ylab='X2') abline(h=0.rep(0.rep(0.inches=F) ZWt = Z %*% t(W) points(ZWt.fg="grey".add=T.3). xlim=c(-3. ylim=c(-3.3). 0.0.0.col="grey") arrows(0. 0.fg="grey".3).col="grey") arrows(0.fg="grey".3). pch=16.add=T. col="red") . 0. w2[2]) symbols(rep(0. ylab='Z2') abline(h=0.inches=F) screen(2) plot(Z.inches=F) plot(Xs.add=T.1) symbols(rep(0. pch=16.3). w1[2]) arrows(0. w1[2]) arrows(0.v=0.circ=1:3. w1[1].

1] [.v=0.0) . ylab='Z2') abline(h=0. ylim=c(-3. pch=16. xlab='Z1'.0.2] [1.74279 Zs = Z %*% Dinv screen(1) plot(Z.fg="grey".1.add=T. pch=16. xlim=c(-3.0) arrows(0.inches=F) 7.3).3). ylab='Z2') abline(h=0. r1[2]) arrows(0. r1[1].3).v=0. ylim=c(-3. pch=16.0000000 2.3).fg="grey". xlab='Z1'.inches=F) screen(2) plot(Xs %*% R. 0.r1 = c(cos(pi/3).] 0. xlim=c(-3.1) symbols(rep(0.7318455 0. r2[1].0.circ=1:3.3).] 0.3).3664086 0.-sin(pi/3))# rotate axes pi/3 r2 = c(sin(pi/3).cos(pi/3)) R = cbind(r1.3). xlab='X1'.r2) screen(2) screen(1) plot(Xs.0.00000 [2.col="grey") arrows(0. xlim=c(-3.3645923 Dinv = diag(1/sd(Z)) Dinv [. ylab='X2') abline(h=0.v=0.rep(0.0.circ=1:3.3).col="grey") arrows(0.add=T. 0. Matrix Multiplication: Streching and Shrinking sd(Z) [1] 1. ylim=c(-3.3). r2[2]) symbols(rep(0.3).col="grey") arrows(0.1.rep(0.

t(W) #print out the values points(X2. ylim=c(-3. xlab='Z1'.col="grey") arrows(0.1) symbols(rep(0. xlim=c(-3.inches=F) X2 = Zs %*% D %*% t(W) D.fg="grey".circ=1:3.0.3). pch=16.fg="grey". 0.rep(0. ylim=c(-3.3).circ=1:3.1) symbols(rep(0.Zs.3). w1[2]) arrows(0.col="grey") arrows(0.3).3). ylab='Z2') abline(h=0.0. col="red") Xsvd = svd(Xs) Xsvd \$d . w1[1].3).0.add=T. w2[2]) symbols(rep(0.fg="grey". ylab='X2') abline(h=0.circ=1:3.0) arrows(0.add=T.3).inches=F) 8. 0.0.] 0 1 screen(2) screen(1) plot(Xs.1. xlab='X1'. xlim=c(-3.3).rep(0. pch=16.2] [1.3).1] [. Singular Value Decomposition D = diag(sd(Z)) D %*% Dinv [.3).inches=F) screen(2) plot(Zs.v=0.v=0.] 1 0 [2.arrows(0.add=T.0. w2[1].rep(0.

589221 \$u [.263158e-02 t(U) %*% U [.948366e-18 [2.948366e-18 1.956037 1.] 4.7071068 X2p = Xsvd\$u %*% diag(Xsvd\$d) %*% t(Xsvd\$v) # # # # # # X2p identical with X2 X2 is such that Zs has sd=1 and D = diag(sd(Z)) X2p is such that Xsvd\$u is orthonormal (i.1] [.] 1.e.1] [. diag(Xsvd\$d) = constant multiples * D ) of course Xsvd\$u %*% diag(Xsvd\$d) %*% t(Xsvd\$v) = Zs %*% D %*% t(W) U=Xsvd\$u screen(2) plot(U. col length=1) and the singular values are constant multiples of D = diag(sd(Z)) (i.7071068 -0.1] [.col="grey") cov(U) [.000000e+00 4.2] [1.000000e+00 .[1] 5.] -0. ylab='U2') abline(h=0.2783275263 -0.067310382 \$v [.] 0.1] [..2] [1.e..084935083 [2.801841e-19 [2.4439494504 0.] -0.v=0.2] [1.7071068 [2.7071068 0.] 0. xlab='U1'.265883024 : : : [20.] 5.] 0. pch=16.2] [1.801841e-19 5.263158e-02 3.4608511785 -0.] 3.

15 31.c(0. Matrix Computation of Covariance Xd# Start with mean-centered matrix V1 V2 1 -5. ylab='S2') abline(h=0.51579 V2 44.S[1.85 -13.51579 242.6 : : : 20 6.9.S[1.85 -30.1]+S[1.S[2.1]+S[2.2]).2].87105 44.6 2 -4.1].46316 10.col="grey") polygon(c(0.51579 242.4 cov(Xd) V1 V2 V1 10.51579 V2 44.1].2]. Matrix Determinant det(S) [1] 654. col="red") .S[2.S[2. ylim=c(0.S[1.87105 44. type="n".290). 0. xlab='S1'. 290).46316 n = nrow(Xd) S = 1/(n-1) * t(Xd) %*% Xd S V1 V2 V1 10. xlim=c(0.2]).1742 screen(2) screen(1) plot(0.v=0.

S[1. S[2.2] [1. xlim=c(0.040842 SD = 1/(n-1) * t(D) %*% D SD [.S[2. 0.S[1.1]+SD[1.00000 7.1]. 290).S[1. S[1. c(0.2] [1. 55).SD[1.] 0.1]+S[1.040842 D = diag(d) D [. SD[1.609130 det(SD) [1] 654.2]. S[1.2].SD[2.2]) screen(2) screen(1) plot(0.1]+S[1.] 69.svd = svd(Xd) d = Xd.1]. S[2. SD[2.1]. xlab='S1'. SD[2.2]) arrows(S[1.2]) .S[2.1742 prod(d^2/(n-1)) [1] 654.000000 [2.1].2]).1] [. xlim=c(0.SD[1.7251 0.2]) Xd.2].1].SD[2.2].1]+S[2.2].2]. 0.2]. S[1. ylab='S2') abline(h=0. S[2.SD[1.2].2].1]+SD[2.S[2.290).col="red") arrows(0.1]. S[2.1]) arrows(0. 0.2].1]+SD[1. S[2.1]+S[1.v=0.2]. SD[2. SD[1. SD[2.2].arrows(0.svd\$d d [1] 69.SD[2.020117 7.2].1]+SD[2.1].1].1]+S[2. S[2.0000 2.2]) arrows(S[1.1742 screen(2) plot(0. S[2.1]) arrows(0.1] [.1].1]+S[2.1]+SD[2.c(0. ylim=c(0.] 0.1]. 0.2]). SD[2.1]+SD[1.02012 0. type="n". S[1.2]) arrows(SD[1.290).2]). S[1. xlab='S1'. 0.2]. SD[1. SD[2.1]) arrows(0. 0.2]. 0. SD[1.col="grey") polygon(c(0. col="red") arrows(0.v=0.000000 [2.2]) arrows(SD[1. ylim=c(0.] 250.1]. 0. S[2. S[1. type="n".2]). ylab='S2') abline(h=0.col="grey") polygon(c(0.

1]+SD[2. 2.1].2]. pch=16. xlab='x'.table("http://www. 1) screen(1) plot(a[1]. S[1. S[2.2]) arrows(SD[1. 2.2]. SD[1.1]. a[1].1].2]. 255).v=0. 2. ylab='y') # Empty plotting frame arrows(0.2].SD[1.2]. xlim=c(-3. c(0.uidaho.2]. SD[2. ylab='y') # point representation of a vector abline(h=0.2]).1].SD[1. ylim=c(-3. scale=F) Xd apply(Xd.1]) arrows(0. SD[2.3). type="n".1]+SD[2.1]. S[2.col="grey") polygon(c(0.1]+SD[1. SD[2.v=0. type="n". 0. SD[2. 0.2]) # R codes for Vectors and Matrices split. a[2]) # ray representation of a vector abline(h=0.1]+SD[1. S[1. S[2.a[2].SD[2. sd) screen(2) # mean-centered data (deviations) .2]).2)) horizontally par(bg="white".2]) arrows(S[1.SD[2.1]+S[2. ylab='S2') abline(h=0.c(2.1]+S[2.2]) screen(2) plot(0.1]+S[1.col="grey") # add coordinate axes title("Point Representation of (2. SD[2. SD[1.3).2]. mean) apply(X.2]. 0.1]+SD[1. xlab='S1'. pch=16. ylim=c(-3.3). SD[1.1]+SD[2.txt") X apply(X. 2.3). ylab="Weight") title("Raw Data (Women's Heights and Weights)") Xd <. sd) screen(2) screen(1) plot(X. 0.3).SD[1.1]. 0.col="red") arrows(0. ylim=c(0.2].scale(X.v=0.1]+S[1. S[2.2]) arrows(SD[1.screen(c(1.col="grey") title("Ray Representation of (2. 1)") screen(2) plot(0. pty="s") screen(2) par(pty="s") # Divide the graphics device into two parts # set background color (ensures proper erasure) # and square plot regions # Vector representation a <. xlim=c(0.SD[2.edu/~stevel/519/Data/WOMEN. mean) apply(Xd.1]. xlab='x'. xlab="Height". SD[1.arrows(S[1. 1)") # Data as a collection of vectors X= read. SD[2.2].2]. xlim=c(-3.

fg="grey". a[2]) abline(h=0.2).v=0. sd) # standardized data screen(2) plot(Xs. xlab="Height".3).ra[2]. ra[2]) abline(h=0.plot(Xd.2)) title("Rescaled Standardized Data") abline(h=0. pch=16.col="grey") symbols(rep(0. xlim=c(-2. 1)") screen(2) screen(1) plot(Xs. a[1]. xlim=c(-2. 1)") r<.matrix(rep(1. pch=16.r*a ra screen(2) plot(ra[1]. ylab="Weight") title("Mean-Centered Data (Women's Heights and Weights)") abline(h=0. ||w1|| w1 <.3). ylim=c(-3. ra[1].col="grey") # Scalar multiplication a screen(2) screen(1) plot(a[1]. pch=16. xlim=c(-3.circ=1:3.2).a[2].circ=1:3. ylim=c(-2.rep(0.3).v=0.v=0.2)) title("Standardized Data") abline(h=0. 0.v=0.3). ylab='y') arrows(0. ylim=c(-2.inches=F) title("Point Representation of 0.scale(X) Xs apply(Xs..3). pch=16.w1/lw1 # unit length w1 .sqrt(sum(w1^2)) # length of w1. xlab="Height".v=0.col="grey") Xs <.v=0.fg="grey". ylab="Weight". ylab="Weight". 2. xlab="Height". 2.add=T. mean) apply(Xs. xlab='x'. pch=16. xlab='x'.3).rep(0.3).nc=1) w1 w1^2 lw1 <.col="grey") # Vector multiplication: projection w1 <.6*(2.add=T.col="grey") symbols(rep(0. xlim=c(-3. ylab="Weight") title("Standardized Data (Women's Heights and Weights)") abline(h=0. 0.inches=F) title("Representation of (2.col="grey") screen(2) plot(r*Xs. ylab='y') arrows(0. pch=16.2). ylim=c(-3.3).6 ra <. xlab="Height".

a %*% w1 aw1 screen(2) screen(1) plot(a[1].col="grey") arrows(0.sqrt(sum(w2^2)) w2 <. aw1*w1[2].1]. xlim=c(-2.fg="grey".3).add=T.Xs[.w2/lw2 # unit length w2 sqrt(sum(w2^2)) a aw2 <.a[2]. ylim=c(-3.3).circ=1:3. pch=16. 0.2).aw1*w1[2].fg="grey". w1[1]. xlab='x'. xlab='x'.3).col="grey") arrows(0.3).rep(0.circ=1:3.3).col="grey") arrows(0.Xs %*% w2 .v=0.inches=F) Xs Z1 <.fg="grey". col="red") segments(Xs[.circ=1:3.lty=3) symbols(rep(0. w1[2]/w1[1]) points(aw1*w1[1]. pch=16.2].3). xlab='x'. ylim=c(-3. Z1*w1[2].rep(0. pch=16. w2[2]) abline(0.3).add=T.add=T. ylim=c(-2.a[2].3).a %*% w2 aw2 screen(2) plot(a[1].matrix(c(-1.inches=F) w2 <.aw2*w2[2].a[2]. aw2*w2[2]. xlim=c(-3. col="red") segments(a[1].lty=3) symbols(rep(0.v=0. w1[2]/w1[1]) points(Z1*w1[1].2).aw1*w1[1].aw2*w2[1]. ylab='y') abline(h=0. pch=16. w1[2]) abline(0.3).a[2].Z1*w1[2]. col="green") segments(a[1]. w2[1].Xs %*% w1 screen(2) screen(1) plot(Xs.inches=F) Z2 <. w1[2]) abline(0.3). w1[1].Z1*w1[1]. 1). 0.sqrt(sum(w1^2)) a aw1 <. w2[2]/w2[1]) points(aw2*w2[1].lty=3) symbols(rep(0.v=0. ylab='y') abline(h=0. 0. pch=16.nc=1) w2 w2^2 lw2 <. pch=16.rep(0. ylab='y') abline(h=0. xlim=c(-3.

col="green") segments(Xs[. 0. w2[1]. w1[2]) abline(0.inches=F) # Matrix Multiplication: Rotation screen(1) plot(Xs.0.0) arrows(0.lty=3) symbols(rep(0.Z2*w2[1].2].3). pch=16. ylab='X2') abline(h=0. w2[2]/w2[1]) points(Z1*w1[1]. w2[2]) abline(0. col="red") segments(Xs[. ylab='Z2') abline(h=0.rep(0. xlim=c(-3. Z2*w2[2]. xlim=c(-3.3). w2[1].3).3). w2) Z <.3).2). ylab='y') abline(h=0.circ=1:3.3).rep(0. pch=16.Xs[. w1[2]) arrows(0.Z1*w1[1].lty=3) points(rep(0. xlim=c(-3. w1[1].Z2*w2[1].cbind(w1. 0.circ=1:3.screen(2) plot(Xs.20). w2[1]. w1[1].3). xlab='X1'. ylim=c(-3.2).v=0. xlab='x'.3).col="grey") arrows(0. xlim=c(-2.2].inches=F) screen(2) W <.fg="grey".lty=3) points(Z2*w2[1].fg="grey".fg="grey".lty=3) symbols(rep(0.Z1*w1[2]. w1[2]/w1[1]) arrows(0.add=T. ylim=c(-3.1]. rep(0.v=0) arrows(0.Xs %*% W plot(Z.v=0.Z2. ylim=c(-3. w2[2]) abline(h=0. type="n". 0. pch=16.Z2.v=0.rep(0.add=T. xlab='x'.20). xlab='Z1'.Z2*w2[2].Z2*w2[2].3).3).2]. Z2*w2[2]. pch=16. xlab='X1'.20).circ=1:3.circ=1:3.rep(0. Z1*w1[2].0.0.col="grey") . col="green") segments(rep(0. Z2.Z2.Z1.add=T.1) points(Z1.lty=3) symbols(rep(0.rep(0. col="green") segments(Xs[. 0.Xs[. ylab='X2') abline(h=0.col="grey") arrows(0.Z1.1]. pch=16.1. w2[2]) abline(0.inches=F) screen(1) plot(Xs. 0.Xs[. w2[2]/w2[1]) points(Z2*w2[1].fg="grey".1].20). ylab='y') arrows(0.3).inches=F) # Orthogonal vectors t(w1) %*% w2 screen(2) screen(1) plot(0.3).3). xlim=c(-3. ylim=c(-2.3).add=T.col="grey") symbols(rep(0. ylim=c(-3.3).v=0.3). col="red") segments(Z1. 0.

ylab='Z2') abline(h=0.0) arrows(0.3).inches=F) # Matrix Multiplication: Streching and Shrinking sd(Z) Dinv <. ylim=c(-3. ylim=c(-3.add=T. ylim=c(-3.arrows(0. r1[1].1.fg="grey".v=0. xlab='Z1'.3).col="grey") arrows(0.1) symbols(rep(0.3).add=T.rep(0.v=0.circ=1:3.v=0.0) arrows(0.add=T.0.0.1.diag(1/sd(Z)) Dinv Zs <. 0.0. ylab='X2') abline(h=0.add=T. pch=16.circ=1:3.fg="grey". r1[2]) arrows(0.inches=F) screen(1) plot(Xs.inches=F) screen(2) plot(Z.0.3). xlim=c(-3. 0.0) arrows(0.add=T. col="red") r1 <. w1[2]) arrows(0.3).rep(0. r2[1]. xlab='Z1'.1.Z %*% t(W) points(ZWt.col="grey") arrows(0.v=0.3).3). xlim=c(-3.v=0.3). pch=16.c(sin(pi/3).circ=1:3. w1[2]) arrows(0. w2[1].col="grey") arrows(0.fg="grey". xlim=c(-3.rep(0.cbind(r1. 0.c(cos(pi/3). pch=16. xlab='X1'. ylim=c(-3. r2[2]) symbols(rep(0.3). xlim=c(-3.0.circ=1:3.3). pch=16.fg="grey".circ=1:3.3).3).fg="grey".cos(pi/3)) R <.1) symbols(rep(0.rep(0. xlim=c(-3.inches=F) ZWt <.rep(0.0.-sin(pi/3)) r2 <. ylab='X2') abline(h=0.inches=F) screen(2) plot(Xs %*% R.0.fg="grey". xlab='Z1'.3). ylab='Z2') abline(h=0.3).3).inches=F) screen(2) .Z %*% Dinv screen(1) plot(Z. ylab='Z2') abline(h=0.3). w1[1].r2) # rotate axes pi/3 clockwise screen(2) screen(1) plot(Xs. w2[2]) symbols(rep(0.0.3).3). w1[1].col="grey") arrows(0. ylim=c(-3.circ=1:3. w2[1].3).0.3).3). pch=16.col="grey") arrows(0. w2[2]) symbols(rep(0.3).1) symbols(rep(0. 0. xlab='X1'. 0. 0.add=T.rep(0.

col="grey") cov(U) t(U) %*% U # Matrix Computation of the covariance Matrix Xd # Start with mean-centered matrix cov(Xd) n <.S[2.v=0. ylim=c(-3. w1[2]) arrows(0. S[1. ylab='S2') abline(h=0.S[2.3). ylim=c(-3.svd(Xs) Xsvd U<-Xsvd\$u screen(2) plot(U.0.add=T.3).c(0.v=0. pch=16. 0.col=" red") arrows(0.3).1) symbols(rep(0.1].rep(0. col="red") Xsvd <. ylab='Z2') abline(h=0.circ=1:3.S[1. xlim=c(0.3).inches=F) # Singular Value Decomposition D <. xlim=c(-3.2]. xlab='S1'. S[2. 290). w1[1]. xlab='Z1'.2]. xlab='X1'.2]) .1].add=T.col="grey") arrows(0.plot(Zs. pch=16. w2[1].0.1]. 0.3).S[2.fg="grey".v=0.Zs %*% D %*% t(W) points(X2. xlab='U1'.inches=F) X2 <.3).1]+S[1.col="grey") arrows(0.2]. xlim=c(-3. pch=16.0) arrows(0.fg="grey".1.col="grey") polygon(c(0.nrow(Xd) S <. ylab='U2') abline(h=0.S[1.2]). S[1.circ=1:3. 0. 0.1]+S[2.0. type="n".3). 0. w2[2]) symbols(rep(0.3).290).1]) arrows(0. ylim=c(0.rep(0. ylab='X2') abline(h=0. S[2.2]).1/(n-1) * t(Xd) %*% Xd S # Matrix Determinant det(S) screen(2) screen(1) plot(0.v=0.S[1.diag(sd(Z)) D %*% Dinv screen(2) screen(1) plot(Xs.

2].2].SD[2.c(0.SD[1.1]+S[2.S[1. 0.S[1.1].SD[2. S[2.1]) arrows(0. 0.2 ]). type="n". xlab='S1'. SD[2. ylim=c(0.c(0.1]. SD[1.2]). ylim=c(0.col="grey") polygon(c(0. 0.1]+S[1. ylab='S2') abline(h=0. 0.1]+SD[1.diag(d) D SD <.2]) arrows(SD[1.SD[1.1]+SD[1. S[2.2]. SD[1. S[1.2].2]. SD[2.SD[1.1].2]. SD[2.2].1]+S[1.1]. SD[2. 255). SD[2.1]+SD[1.1]. SD[1.2].2]) arrows(S[1.arrows(S[1. 0.SD[1. type="n". 55).2]) . S[2.1].S[2.col="grey") polygon(c(0.1]+SD[1. S[1. S[1.1]+SD[2.2]) arrows(SD[1.1]+S[2.SD[1. S[2.1]+SD[2.col="grey") polygon(c(0. S[2.S[1.2 ]).2].2]) arrows(SD[1.2]).2]).2]) arrows(S[1. S[2. xlab='S1'.svd\$d d D <.1]+SD[1.SD[1.2].290).2]. S[1. SD[1.2].1]+S[1.1]+S[2.col="red") arrows(0.2].2]) screen(1) # Rescale for better visibility plot(0.2].2].1].1]. SD[2. S[1. SD[2.1].1]+SD[2. ylim=c(0. SD[2.1]+S[2. 0.S[2.1].2].2].2]).2].1]+SD[2. SD[1.SD[2.1].2]) arrows(S[1.svd(Xd) d <. SD[1.1]) arrows(0. 290).1]+S[1.SD[2. type="n".1]+SD[2.col=" red") arrows(0. SD[1. 0. 0. SD[1.1]. SD[2.v=0. S[2.2]) Xd.290).2]. S[2. xlab='S1'.2].2].Xd.1]. 0.1]+S[1.SD[2.1].1].v=0. ylab='S2') abline(h=0.S[2.2]) arrows(SD[1.c(0. xlim=c(0.2].col="red") arrows(0. S[2.svd <.v=0.1/(n-1) * t(D) %*% D SD det(SD) prod(d^2/(n-1)) screen(2) plot(0.2]) screen(2) plot(0.1]+SD[1. SD[2. SD[2.1]. S[2.1]+S[2. S[1. xlim=c(0.SD[2.2].1]+SD[2.1]) arrows(0. ylab='S2') abline(h=0.2].1].2]. xlim=c(0. SD[2.3).