Vous êtes sur la page 1sur 21

# 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")

apply(Xd, 2, sd)
V1
V2
3.297128 15.571229
Xs = scale(X)# standardized data
Xs
V1
V2
1 -1.77427146 -1.96516286
2 -1.47097719 -0.87340572
:
:
:
20 1.86525974 2.01653967
apply(Xs, 2, mean)
V1
V2
-4.308077e-16 3.579493e-16
apply(Xs, 2, sd)
V1 V2
1 1
screen(2)
plot(Xs, pch=16, xlab="Height", ylab="Weight")
title("Standardized Data (Women's Heights and Weights)")
abline(h=0,v=0,col="grey")

3. Scalar multiplication
a
[1] 2 1
screen(2)
screen(1)
plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')
arrows(0, 0, a[1], a[2])
abline(h=0,v=0,col="grey")
title("Representation of (2, 1)")

r= .6
ra = r*a
ra
[1] 1.2 0.6
screen(2)
plot(ra[1],ra[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')
arrows(0, 0, ra[1], ra[2])
abline(h=0,v=0,col="grey")
title("Point Representation of 0.6*(2, 1)")

screen(2)
screen(1)
plot(Xs, pch=16, xlab="Height", ylab="Weight", xlim=c(-2,2), ylim=c(-2,2))
title("Standardized Data")
abline(h=0,v=0,col="grey")
screen(2)
plot(r*Xs, pch=16, xlab="Height", ylab="Weight", xlim=c(-2,2), ylim=c(-2,2))
title("Rescaled Standardized Data")
abline(h=0,v=0,col="grey")

## 4. Vector multiplication: projection

w1 = matrix(rep(1,2),nc=1)
w1
[,1]
[1,]
1
[2,]
1
w1^2
[,1]
[1,]
1
[2,]
1
lw1 = sqrt(sum(w1^2))# length of w1, ||w1||
w1 = w1/lw1# unit length
w1
[,1]
[1,] 0.7071068
[2,] 0.7071068
sqrt(sum(w1^2))
[1] 1
a
[1] 2 1
aw1 = a %*% w1
aw1
[,1]
[1,] 2.121320
screen(2)
screen(1)
plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
abline(0, w1[2]/w1[1])
points(aw1*w1[1], aw1*w1[2], pch=16, col="red")
segments(a[1],a[2],aw1*w1[1],aw1*w1[2],lty=3)
w2 = matrix(c(-1, 1),nc=1)
w2
[,1]
[1,]
-1
[2,]
1
w2^2
[,1]
[1,]
1
[2,]
1
lw2 = sqrt(sum(w2^2))
w2 = w2/lw2# unit length
w2
[,1]
[1,] -0.7071068
[2,] 0.7071068
sqrt(sum(w2^2))
[1] 1
aw2 = a %*% w2
aw2
[,1]
[1,] -0.7071068

screen(2)
plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')
abline(h=0,v=0,col="grey")
arrows(0, 0, w2[1], w2[2])
abline(0, w2[2]/w2[1])
points(aw2*w2[1], aw2*w2[2], pch=16, col="green")
segments(a[1],a[2],aw2*w2[1],aw2*w2[2],lty=3)

Xs
V1
V2
1 -1.77427146 -1.96516286
2 -1.47097719 -0.87340572
:
:
:
20 1.86525974 2.01653967
Z1 = Xs %*% w1
screen(2)
screen(1)
plot(Xs, xlim=c(-2,2), ylim=c(-2,2), pch=16, xlab='x', ylab='y')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
abline(0, w1[2]/w1[1])
points(Z1*w1[1], Z1*w1[2], pch=16, col="red")
segments(Xs[,1],Xs[,2],Z1*w1[1],Z1*w1[2],lty=3)
Z2 = Xs %*% w2
screen(2)
plot(Xs, xlim=c(-2,2), ylim=c(-2,2), pch=16, xlab='x', ylab='y')
abline(h=0,v=0,col="grey")
arrows(0, 0, w2[1], w2[2])
abline(0, w2[2]/w2[1])

## points(Z2*w2[1], Z2*w2[2], pch=16, col="green")

segments(Xs[,1],Xs[,2],Z2*w2[1],Z2*w2[2],lty=3)

5. Orthogonal vectors
t(w1) %*% w2
[,1]
[1,] 2.236167e-17
screen(2)
screen(1)
plot(0, 0, xlim=c(-3,3), ylim=c(-3,3), type="n", xlab='x', ylab='y')
arrows(0, 0, w1[1], w1[2])
arrows(0, 0, w2[1], w2[2])
abline(h=0,v=0,col="grey")

## 6. Matrix Multiplication: Rotation

screen(1)
plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
abline(0, w1[2]/w1[1])
arrows(0, 0, w2[1], w2[2])
abline(0, w2[2]/w2[1])
points(Z1*w1[1], Z1*w1[2], col="red")
segments(Xs[,1],Xs[,2],Z1*w1[1],Z1*w1[2],lty=3)
points(Z2*w2[1], Z2*w2[2], col="green")
segments(Xs[,1],Xs[,2],Z2*w2[1],Z2*w2[2],lty=3)
screen(2)
W = cbind(w1, w2)
Z = Xs %*% W
plot(Z, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')
abline(h=0,v=0)
arrows(0,0,1,0)
arrows(0,0,0,1)
points(Z1, rep(0,20), col="red")
segments(Z1,rep(0,20),Z1,Z2,lty=3)
points(rep(0,20), Z2, col="green")
segments(rep(0,20),Z2,Z1,Z2,lty=3)

screen(1)
plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
arrows(0, 0, w2[1], w2[2])
screen(2)
plot(Z, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')
abline(h=0,v=0,col="grey")
arrows(0,0,1,0)
arrows(0,0,0,1)

## plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')

abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
arrows(0, 0, w2[1], w2[2])
ZWt = Z %*% t(W)
points(ZWt, col="red")

## r1 = c(cos(pi/3),-sin(pi/3))# rotate axes pi/3

r2 = c(sin(pi/3),cos(pi/3))
R = cbind(r1,r2)
screen(2)
screen(1)
plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')
abline(h=0,v=0,col="grey")
arrows(0, 0, r1[1], r1[2])
arrows(0, 0, r2[1], r2[2])
screen(2)
plot(Xs %*% R, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')
abline(h=0,v=0,col="grey")
arrows(0,0,1,0)
arrows(0,0,0,1)

## 7. Matrix Multiplication: Streching and Shrinking

sd(Z)
[1] 1.3664086 0.3645923
Dinv = diag(1/sd(Z))
Dinv
[,1]
[,2]
[1,] 0.7318455 0.00000
[2,] 0.0000000 2.74279
Zs = Z %*% Dinv
screen(1)
plot(Z, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')
abline(h=0,v=0,col="grey")
arrows(0,0,1,0)

arrows(0,0,0,1)
screen(2)
plot(Zs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')
abline(h=0,v=0,col="grey")
arrows(0,0,1,0)
arrows(0,0,0,1)

## 8. Singular Value Decomposition

D = diag(sd(Z))
D %*% Dinv
[,1] [,2]
[1,]
1
0
[2,]
0
1
screen(2)
screen(1)
plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
arrows(0, 0, w2[1], w2[2])
X2 = Zs %*% D %*% t(W)
D;Zs;t(W)

## #print out the values

points(X2, col="red")
Xsvd = svd(Xs)
Xsvd
\$d

## [1] 5.956037 1.589221

\$u
[,1]
[,2]
[1,] -0.4439494504 0.084935083
[2,] -0.2783275263 -0.265883024
:
:
:
[20,] 0.4608511785 -0.067310382
\$v
[,1]
[,2]
[1,] 0.7071068 0.7071068
[2,] 0.7071068 -0.7071068

#
#
#
#
#
#

## 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.e., col length=1) and the singular
values are constant multiples of D = diag(sd(Z))
(i.e., 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, pch=16, xlab='U1', ylab='U2')
abline(h=0,v=0,col="grey")
cov(U)

[,1]
[,2]
[1,] 5.263158e-02 3.801841e-19
[2,] 3.801841e-19 5.263158e-02
t(U) %*% U
[,1]
[,2]
[1,] 1.000000e+00 4.948366e-18
[2,] 4.948366e-18 1.000000e+00

## 9. Matrix Computation of Covariance

V1
V2
1 -5.85 -30.6
2 -4.85 -13.6
:
:
:
20 6.15 31.4
cov(Xd)
V1
V2
V1 10.87105 44.51579
V2 44.51579 242.46316
n = nrow(Xd)
S = 1/(n-1) * t(Xd) %*% Xd
S
V1
V2
V1 10.87105 44.51579
V2 44.51579 242.46316

## 10. Matrix Determinant

det(S)
[1] 654.1742
screen(2)
screen(1)
plot(0, 0, xlim=c(0, 290), ylim=c(0,290), type="n", xlab='S1', ylab='S2')
abline(h=0,v=0,col="grey")
polygon(c(0,S[1,1],S[1,1]+S[1,2],S[1,2]),c(0,S[2,1],S[2,1]+S[2,2],S[2,2]),
col="red")

## arrows(0, 0, S[1,1], S[2,1])

arrows(0, 0, S[1,2], S[2,2])
arrows(S[1,1], S[2,1], S[1,1]+S[1,2], S[2,1]+S[2,2])
arrows(S[1,2], S[2,2], S[1,1]+S[1,2], S[2,1]+S[2,2])
Xd.svd = svd(Xd)
d = Xd.svd\$d
d
[1] 69.020117

7.040842

D = diag(d)
D
[,1]
[,2]
[1,] 69.02012 0.000000
[2,] 0.00000 7.040842
SD = 1/(n-1) * t(D) %*% D
SD
[,1]
[,2]
[1,] 250.7251 0.000000
[2,]
0.0000 2.609130
det(SD)
[1] 654.1742
prod(d^2/(n-1))
[1] 654.1742
screen(2)
plot(0, 0, xlim=c(0, 290), ylim=c(0,290), type="n", xlab='S1', ylab='S2')
abline(h=0,v=0,col="grey")
polygon(c(0,SD[1,1],SD[1,1]+SD[1,2],SD[1,2]),
c(0,SD[2,1],SD[2,1]+SD[2,2],SD[2,2]),col="red")
arrows(0, 0, SD[1,1], SD[2,1])
arrows(0, 0, SD[1,2], SD[2,2])
arrows(SD[1,1], SD[2,1], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])
arrows(SD[1,2], SD[2,2], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])

screen(2)
screen(1)
plot(0, 0, xlim=c(0, 55), ylim=c(0,290), type="n", xlab='S1', ylab='S2')
abline(h=0,v=0,col="grey")
polygon(c(0,S[1,1],S[1,1]+S[1,2],S[1,2]),c(0,S[2,1],S[2,1]+S[2,2],S[2,2]),
col="red")
arrows(0, 0, S[1,1], S[2,1])
arrows(0, 0, S[1,2], S[2,2])

## arrows(S[1,1], S[2,1], S[1,1]+S[1,2], S[2,1]+S[2,2])

arrows(S[1,2], S[2,2], S[1,1]+S[1,2], S[2,1]+S[2,2])
screen(2)
plot(0, 0, xlim=c(0, 255), ylim=c(0,3), type="n", xlab='S1', ylab='S2')
abline(h=0,v=0,col="grey")
polygon(c(0,SD[1,1],SD[1,1]+SD[1,2],SD[1,2]),
c(0,SD[2,1],SD[2,1]+SD[2,2],SD[2,2]),col="red")
arrows(0, 0, SD[1,1], SD[2,1])
arrows(0, 0, SD[1,2], SD[2,2])
arrows(SD[1,1], SD[2,1], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])
arrows(SD[1,2], SD[2,2], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])

## # R codes for Vectors and Matrices

split.screen(c(1,2))
horizontally
par(bg="white", 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 <- 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)")
# Data as a collection of vectors
X
apply(X, 2, mean)
apply(X, 2, sd)
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)
Xd
apply(Xd, 2, mean)
apply(Xd, 2, sd)
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")
Xs <- scale(X)
Xs
apply(Xs, 2, mean)
apply(Xs, 2, sd)

# standardized data

screen(2)
plot(Xs, pch=16, xlab="Height", ylab="Weight")
title("Standardized Data (Women's Heights and Weights)")
abline(h=0,v=0,col="grey")
# Scalar multiplication
a
screen(2)
screen(1)
plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')
arrows(0, 0, a[1], a[2])
abline(h=0,v=0,col="grey")
title("Representation of (2, 1)")
r<- .6
ra <- r*a
ra
screen(2)
plot(ra[1],ra[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')
arrows(0, 0, ra[1], ra[2])
abline(h=0,v=0,col="grey")
title("Point Representation of 0.6*(2, 1)")
screen(2)
screen(1)
plot(Xs, pch=16, xlab="Height", ylab="Weight", xlim=c(-2,2), ylim=c(-2,2))
title("Standardized Data")
abline(h=0,v=0,col="grey")
screen(2)
plot(r*Xs, pch=16, xlab="Height", ylab="Weight", xlim=c(-2,2), ylim=c(-2,2))
title("Rescaled Standardized Data")
abline(h=0,v=0,col="grey")
# Vector multiplication: projection
w1 <- matrix(rep(1,2),nc=1)
w1
w1^2
lw1 <- sqrt(sum(w1^2))
# length of w1, ||w1||
w1 <- w1/lw1 # unit length
w1

sqrt(sum(w1^2))
a
aw1 <- a %*% w1
aw1
screen(2)
screen(1)
plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
abline(0, w1[2]/w1[1])
points(aw1*w1[1], aw1*w1[2], pch=16, col="red")
segments(a[1],a[2],aw1*w1[1],aw1*w1[2],lty=3)
w2 <- matrix(c(-1, 1),nc=1)
w2
w2^2
lw2 <- sqrt(sum(w2^2))
w2 <- w2/lw2 # unit length
w2
sqrt(sum(w2^2))
a
aw2 <- a %*% w2
aw2
screen(2)
plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')
abline(h=0,v=0,col="grey")
arrows(0, 0, w2[1], w2[2])
abline(0, w2[2]/w2[1])
points(aw2*w2[1], aw2*w2[2], pch=16, col="green")
segments(a[1],a[2],aw2*w2[1],aw2*w2[2],lty=3)

Xs
Z1 <- Xs %*% w1
screen(2)
screen(1)
plot(Xs, xlim=c(-2,2), ylim=c(-2,2), pch=16, xlab='x', ylab='y')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
abline(0, w1[2]/w1[1])
points(Z1*w1[1], Z1*w1[2], pch=16, col="red")
segments(Xs[,1],Xs[,2],Z1*w1[1],Z1*w1[2],lty=3)
Z2 <- Xs %*% w2

screen(2)
plot(Xs, xlim=c(-2,2), ylim=c(-2,2), pch=16, xlab='x', ylab='y')
abline(h=0,v=0,col="grey")
arrows(0, 0, w2[1], w2[2])
abline(0, w2[2]/w2[1])
points(Z2*w2[1], Z2*w2[2], pch=16, col="green")
segments(Xs[,1],Xs[,2],Z2*w2[1],Z2*w2[2],lty=3)
# Orthogonal vectors
t(w1) %*% w2
screen(2)
screen(1)
plot(0, 0, xlim=c(-3,3), ylim=c(-3,3), type="n", xlab='x', ylab='y')
arrows(0, 0, w1[1], w1[2])
arrows(0, 0, w2[1], w2[2])
abline(h=0,v=0,col="grey")
# Matrix Multiplication: Rotation
screen(1)
plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
abline(0, w1[2]/w1[1])
arrows(0, 0, w2[1], w2[2])
abline(0, w2[2]/w2[1])
points(Z1*w1[1], Z1*w1[2], col="red")
segments(Xs[,1],Xs[,2],Z1*w1[1],Z1*w1[2],lty=3)
points(Z2*w2[1], Z2*w2[2], col="green")
segments(Xs[,1],Xs[,2],Z2*w2[1],Z2*w2[2],lty=3)
screen(2)
W <- cbind(w1, w2)
Z <- Xs %*% W
plot(Z, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')
abline(h=0,v=0)
arrows(0,0,1,0)
arrows(0,0,0,1)
points(Z1, rep(0,20), col="red")
segments(Z1,rep(0,20),Z1,Z2,lty=3)
points(rep(0,20), Z2, col="green")
segments(rep(0,20),Z2,Z1,Z2,lty=3)
screen(1)
plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')
abline(h=0,v=0,col="grey")

## arrows(0, 0, w1[1], w1[2])

arrows(0, 0, w2[1], w2[2])
screen(2)
plot(Z, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')
abline(h=0,v=0,col="grey")
arrows(0,0,1,0)
arrows(0,0,0,1)
screen(1)
plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
arrows(0, 0, w2[1], w2[2])
ZWt <- Z %*% t(W)
points(ZWt, col="red")
r1 <- c(cos(pi/3),-sin(pi/3))
r2 <- c(sin(pi/3),cos(pi/3))
R <- cbind(r1,r2)

## # rotate axes pi/3 clockwise

screen(2)
screen(1)
plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')
abline(h=0,v=0,col="grey")
arrows(0, 0, r1[1], r1[2])
arrows(0, 0, r2[1], r2[2])
screen(2)
plot(Xs %*% R, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')
abline(h=0,v=0,col="grey")
arrows(0,0,1,0)
arrows(0,0,0,1)
# Matrix Multiplication: Streching and Shrinking
sd(Z)
Dinv <- diag(1/sd(Z))
Dinv
Zs <- Z %*% Dinv
screen(1)
plot(Z, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')
abline(h=0,v=0,col="grey")
arrows(0,0,1,0)
arrows(0,0,0,1)
screen(2)

## plot(Zs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')

abline(h=0,v=0,col="grey")
arrows(0,0,1,0)
arrows(0,0,0,1)
# Singular Value Decomposition
D <- diag(sd(Z))
D %*% Dinv
screen(2)
screen(1)
plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')
abline(h=0,v=0,col="grey")
arrows(0, 0, w1[1], w1[2])
arrows(0, 0, w2[1], w2[2])
X2 <- Zs %*% D %*% t(W)
points(X2, col="red")
Xsvd <- svd(Xs)
Xsvd
U<-Xsvd\$u
screen(2)
plot(U, pch=16, xlab='U1', ylab='U2')
abline(h=0,v=0,col="grey")
cov(U)
t(U) %*% U
# Matrix Computation of the covariance Matrix
Xd

cov(Xd)
n <- nrow(Xd)
S <- 1/(n-1) * t(Xd) %*% Xd
S
# Matrix Determinant
det(S)
screen(2)
screen(1)
plot(0, 0, xlim=c(0, 290), ylim=c(0,290), type="n", xlab='S1', ylab='S2')
abline(h=0,v=0,col="grey")
polygon(c(0,S[1,1],S[1,1]+S[1,2],S[1,2]),c(0,S[2,1],S[2,1]+S[2,2],S[2,2]),col="
red")
arrows(0, 0, S[1,1], S[2,1])
arrows(0, 0, S[1,2], S[2,2])

## arrows(S[1,1], S[2,1], S[1,1]+S[1,2], S[2,1]+S[2,2])

arrows(S[1,2], S[2,2], S[1,1]+S[1,2], S[2,1]+S[2,2])
Xd.svd <- svd(Xd)
d <- Xd.svd\$d
d
D <- diag(d)
D
SD <- 1/(n-1) * t(D) %*% D
SD
det(SD)
prod(d^2/(n-1))
screen(2)
plot(0, 0, xlim=c(0, 290), ylim=c(0,290), type="n", xlab='S1', ylab='S2')
abline(h=0,v=0,col="grey")
polygon(c(0,SD[1,1],SD[1,1]+SD[1,2],SD[1,2]),c(0,SD[2,1],SD[2,1]+SD[2,2],SD[2,2
]),col="red")
arrows(0, 0, SD[1,1], SD[2,1])
arrows(0, 0, SD[1,2], SD[2,2])
arrows(SD[1,1], SD[2,1], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])
arrows(SD[1,2], SD[2,2], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])

screen(1)

## plot(0, 0, xlim=c(0, 55), ylim=c(0,290), type="n", xlab='S1', ylab='S2')

abline(h=0,v=0,col="grey")
polygon(c(0,S[1,1],S[1,1]+S[1,2],S[1,2]),c(0,S[2,1],S[2,1]+S[2,2],S[2,2]),col="
red")
arrows(0, 0, S[1,1], S[2,1])
arrows(0, 0, S[1,2], S[2,2])
arrows(S[1,1], S[2,1], S[1,1]+S[1,2], S[2,1]+S[2,2])
arrows(S[1,2], S[2,2], S[1,1]+S[1,2], S[2,1]+S[2,2])
screen(2)
plot(0, 0, xlim=c(0, 255), ylim=c(0,3), type="n", xlab='S1', ylab='S2')
abline(h=0,v=0,col="grey")
polygon(c(0,SD[1,1],SD[1,1]+SD[1,2],SD[1,2]),c(0,SD[2,1],SD[2,1]+SD[2,2],SD[2,2
]),col="red")
arrows(0, 0, SD[1,1], SD[2,1])
arrows(0, 0, SD[1,2], SD[2,2])
arrows(SD[1,1], SD[2,1], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])
arrows(SD[1,2], SD[2,2], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])