Professional Documents
Culture Documents
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")
# add coordinate axes
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)")
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")
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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")
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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")
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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])
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")
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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])
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
arrows(0,0,0,1)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
points(X2, col="red")
Xsvd = svd(Xs)
Xsvd
$d
[,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
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])
# 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")
# add coordinate axes
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= read.table("http://www.uidaho.edu/~stevel/519/Data/WOMEN.txt")
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)
# 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")
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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")
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
# 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")
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
# 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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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")
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])
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
# 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)
symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)
screen(2)
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])
screen(1)