HOMEWORD CHAPTER 3
EX_3.1
X
## [,1] [,2]
## [1,] 3 4
## [2,] 6 -2
## [3,] 3 1
sample_mean
## [1] 4 1
Graph the scatter plot in p = 2 dimensions, and locate the sample mean
plot(X,pch=19,col=3,main = "Scatter plot in p = 2 dimensions,
and locate the sample mean",xlab="X1",ylab="X2")
points(4,1,col="red",pch=19)
+3.2b
mean_matrix = matrix(c(4,4,4,1,1,1),ncol=2)
d = X-mean_matrix
colnames(d)=c("d1","d2")
vector = rbind(5*diag(3),t(d))
rownames(vector) <- c("X", "Y", "Z", "d1","d2")
col = c(rep("black",3))
Matrix devivation
d
## d1 d2
## [1,] -1 3
## [2,] 2 -3
## [3,] -1 0
The deviation vectors plot
3.2c Lengths and the cosine of the angle
length_d1 = sqrt(sum(d[,1]^2))
length_d1
## [1] 2.44949
length_d2= sqrt(sum(d[,2]^2))
length_d2
## [1] 4.242641
R = cor(X)
R
## [,1] [,2]
## [1,] 1.0000000 -0.8660254
## [2,] -0.8660254 1.0000000
Length of d1 = 2.4494897, Length of d2 = 4.2426407
We have cosin(d1,d2)=R12 with R is sample correlation coefficient so cosin(d1,d2) is: R12 = -0.8660254
EX 3.6
h=(matrix(rep(1,2),ncol = 1))
X=c(-1,3,-2,2,4,2,5,2,3)%>%matrix(.,ncol =3)%>%t()
one = matrix(c(1,1,1),ncol = 1)
mean(X[,2])*one
## [,1]
## [1,] 3
## [2,] 3
## [3,] 3
mean = cbind(mean(X[,1])*one, mean(X[,2])*one, mean(X[,3])*one)
mean
## [,1] [,2] [,3]
## [1,] 2 3 1
## [2,] 2 3 1
## [3,] 2 3 1
d=X-mean
S = cov(X)
S%*%matrix(c(1,1,-1),ncol = 1)
## [,1]
## [1,] 0
## [2,] 0
## [3,] 0
det(S)
## [1] 5.995204e-15
3.6a Calculate the matrix of deviations,Is this matrix of full rank? Explain. Matrix mean
mean
## [,1] [,2] [,3]
## [1,] 2 3 1
## [2,] 2 3 1
## [3,] 2 3 1
Matrix of deviations
d
## [,1] [,2] [,3]
## [1,] -3 0 -3
## [2,] 0 1 1
## [3,] 3 -1 2
a=d%*%matrix(c(1,1,-1),ncol = 1)
a
We have d * [1,1,-1] = 0 so matrix isn’t full rank
3.6b Calculate the generalized sample variance Interpret the latter geometrically. (Unbiased) Sample Variance-Covariance Matrix S
S
## [,1] [,2] [,3]
## [1,] 9.0 -1.5 7.5
## [2,] -1.5 1.0 -0.5
## [3,] 7.5 -0.5 7.0
det(S)
## [1] 5.995204e-15
We have det(S) = 0 so matrix (X) is not full rank. 3.6c Total sample variance = det(S) = 0
3.10
X=c(3,1,0,6,4,6,4,2,2,7,0,3,5,3,4)%>%matrix(.,ncol = 5)%>%t()
X
## [,1] [,2] [,3]
## [1,] 3 1 0
## [2,] 6 4 6
## [3,] 4 2 2
## [4,] 7 0 3
## [5,] 5 3 4
one = matrix(c(1,1,1,1,1),ncol = 1)
one
## [,1]
## [1,] 1
## [2,] 1
## [3,] 1
## [4,] 1
## [5,] 1
mean = cbind(mean(X[,1])*one, mean(X[,2])*one, mean(X[,3])*one)
a=d%*%matrix(c(1,1,-1),ncol = 1)
S = cov(X)
S%*%matrix(c(1,1,-1),ncol = 1)
## [,1]
## [1,] 0
## [2,] 0
## [3,] 0
det(S)
## [1] 0
Matrix X
X
## [,1] [,2] [,3]
## [1,] 3 1 0
## [2,] 6 4 6
## [3,] 4 2 2
## [4,] 7 0 3
## [5,] 5 3 4
3.10a:
Same EX3.8 We computed (Unbiased) Sample
Variance-Covariance Matrix S and matrix deviation d.
Matrix S
S
## [,1] [,2] [,3]
## [1,] 2.5 0.0 2.5
## [2,] 0.0 2.5 2.5
## [3,] 2.5 2.5 5.0
det(S)
## [1] 0
Matrix deviation d
d
## [,1] [,2] [,3]
## [1,] -3 0 -3
## [2,] 0 1 1
## [3,] 3 -1 2
a=d%*%matrix(c(1,1,-1),ncol = 1)
a
## [,1]
## [1,] 0
## [2,] 0
## [3,] 0
3.10b: Defind that : b = transpose(1,1,-1)
a= d*b = 0
and det(S) = 0
So the columns are linearly dependent
EX 3.15 3.15a use the sample mean, variance, and covariance formulas
X=c(1,4,3,6,2,6,8,3,3)%>%matrix(.,ncol=3)%>%t()
meanX = rbind(mean(X[,1]),mean(X[,2]),mean(X[,3]))
b=c(1,1,1,1,2,-3)
b=matrix(b,ncol = 2)%>%t()
bmean = b%*%meanX
bmean
## [,1]
## [1,] 12
## [2,] -1
S=cov(X)
S_b=b%*%S%*%t(b)
S
## [,1] [,2] [,3]
## [1,] 13.0 -2.5 1.5
## [2,] -2.5 1.0 -1.5
## [3,] 1.5 -1.5 3.0
#sample covalation
S_b
## [,1] [,2]
## [1,] 12 -3
## [2,] -3 43
We have sample mean b’X = 12,c’MeanX = -1
Sample sample covalation is S_b
3.15b Using the result to computing Sample mean and Sample variance of
X=c(1,4,3,6,2,6,8,3,3)%>%matrix(.,ncol=3)%>%t()
meanX = rbind(mean(X[,1]),mean(X[,2]),mean(X[,3]))
X_sum = X%*%t(b)
mean(X_sum[,1])
## [1] 12
mean(X_sum[,2])
## [1] -1
We have sample mean b’X = 12,c’MeanX = -1
Sample covarianc
S_b
## [,1] [,2]
## [1,] 12 -3
## [2,] -3 43
3.18 3.18a The sample mean and variance of a state’s total energy consumption for these majo rsources mean that b= c(1,1,1,1)
b=c(1,1,1,1)
b
## [1] 1 1 1 1
meanx= c(0.766,0.508,0.438,0.161)
meanx
## [1] 0.766 0.508 0.438 0.161
S=c(0.856,0.635,0.173,0.096,0.635,0.568,0.128,0.067,0.173,0.127,0.172,0.039,0.096,0.067,0.039,0.043)%>%matrix(.,ncol=4)%>%t()
sample_mean = b%*%meanx
variance = t(b)%*%S%*%b
sample_mean
## [,1]
## [1,] 1.873
variance
## [,1]
## [1,] 3.914
The sample mean = 1.873
Variance = 3.914
Determine the sample mean and variance of the excess of petroleum consumption over natural gas consumption Also find the sample covariance of this variable withthe total variable in part a. mean that b’=c(1,-1,0,0), and c’=(1,1,1,1)
b=c(1,-1,0,0,1,1,1,1)%>%matrix(.,ncol=2)%>%t()
b
## [,1] [,2] [,3] [,4]
## [1,] 1 -1 0 0
## [2,] 1 1 1 1
sample_mean = b%*%meanx
variance = b%*%S%*%t(b)
sample_mean
## [,1]
## [1,] 0.258
## [2,] 1.873
variance
## [,1] [,2]
## [1,] 0.154 0.362
## [2,] 0.363 3.914
The excess of sample mean = 0.258
Excess of Variance = 0.362