0% found this document useful (0 votes)
51 views4 pages

Lab 1 Sol

This document provides solutions to exercises from a statistical learning course. It includes solutions to questions about matrix operations like finding the inverse and product of matrices. It also provides R code to generate matrices and vectors, including an identity matrix and vectors of integers not divisible by certain numbers. Some R assignments are identified as successful while others would not be.

Uploaded by

izzy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views4 pages

Lab 1 Sol

This document provides solutions to exercises from a statistical learning course. It includes solutions to questions about matrix operations like finding the inverse and product of matrices. It also provides R code to generate matrices and vectors, including an identity matrix and vectors of integers not divisible by certain numbers. Some R assignments are identified as successful while others would not be.

Uploaded by

izzy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MAST90104: A first course in Statistical Learning

Week 1 Practical/Workshop Solutions

Workshop questions
1. Show that X T X is a symmetric matrix.
Solution:
(X T X)T = X T (X T )T = X T X.

2. (a) Let  
a b
A=
c d
be a nonsingular 2 × 2 matrix. Show by direct multiplication that
 
−1 1 d −b
A = .
ad − bc −c a

Solution:
    
1 d −b a b 1 da − bc db − bd
= = I.
ad − bc −c a c d ad − bc −ca + ac −cb + ad

(b) Find the inverse of  


3 1
.
4 −2
Solution: From above, the inverse is
 
1 −2 −1
− .
10 −4 3

3. Let  
−6 3 0
A=
1 −1 1
and  
0 6 −1
B= 8 −1 0 
0 2 3
(a) Find the product AB
Solution:  
24 −39 6
AB =
−8 9 2
(b) Does BA exist?
Solution: No, the dimensions do not match
(c) Can we calculate B T AT ? If so, what is it? Solution:
 
24 −8
B T AT = (AB)T =  −39 9 
6 2
 T  T
4. Let x = 1 6 −4 2 and y = 2 −1 −1 0

(a) What is the norm of x ?


p √
Solution: kxk = 12 + 62 + (−4)2 + 22 = 57 ≈ 7.55

1
(b) Are x and y orthogonal ?
Solution: Yes, because xT y = 0

5. Is  
1 1 −1 −1
 1 −1 1 −1 
X= 
 1 −1 −1 1 
1 1 1 1
orthogonal? If not, what value of c makes the matrix cX orthogonal?
Solution: The matrix is not orthogonal, as its columns do not form an orthonormal set (e.g. the
first column has norm > 1). However they do form an orthogonal set, so we can just normalise
each vector to produce an orthogonal matrix. This gives c = 21 .
6. For the following matrices, find the eigenvalues and eigenvectors
 
1 2
(a)
0 −1
Solution: To find the eigenvalues, we solve the equation

1−λ 2
= (1 − λ)(−1 − λ) = 0.
0 −1 − λ
This gives λ = 1 and λ = −1
To find the eigenvector associated with eigenvalue 1, we solve the system of equation
    
1 2 x1 x1
= .
0 −1 x2 x2
 
1
This implies x2 = 0 and x1 can be any value. One solution is x = .
0
 
1
Similarly, the eigenvector associated with eigenvalue -1 is x =
−1
 
2 1
(b) Solution: The eigenvalues are λ = 1 and λ = 3
1 2
 
1
The eigenvector associated with λ = 1 is x = .
−1
 
1
The eigenvector associated with λ = 3 is x = .
1

Practical exercises
The following are taken from Chapter 2 of spuRs (Introduction to Scientific Programming and Simulation
Using R).
1. Give R assignment statements that set the variable z to
b
(a) xa
(b) (xa )b
(c) 3x3 + 2x2 + 6x + 1 (try to minimise the number of operations required)
(d) the second-to-last digit of x before the decimal point (hint: use floor(x) and/or %%)
(e) z + 1
Solution:
> x <- 123
> a <- 1.1
> b <- 1.2
> # a
> (z <- x^(a^b))

2
[1] 220.3624

> (z <- x^a^b)

[1] 220.3624

> # b
> (z <- (x^a)^b)

[1] 573.6867

> # c
> (z <- 3*x^3 + 2*x^2 + 6*x + 1) #8 operations

[1] 5613598

> (z <- (3*x + 2)*(x^2 + 2) - 3) #6 operations

[1] 5613598

> (z <- sum((x^(3:0))*c(3, 2, 6, 1))) #vectorised

[1] 5613598

> # d
> y <- abs(x)
> (z <- (y %% 100 - y %% 10)/10)

[1] 2

> (z <- floor(y/10 - floor(y/100)*10))

[1] 2

> # e
> (z <- z + 1)

[1] 3

2. Give R expressions that return the following matrices and vectors


(a) (1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1)
(b) (1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5)
 
0 1 1
(c)  1 0 1 
1 1 0
 
0 2 3
(d)  0 5 0 
7 0 0
Solution:
> # a
> c(1:8, 7:1)

[1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

> # b
> rep(1:5, 1:5)

[1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

3
> # c
> matrix(1, 3, 3) - diag(3)

[,1] [,2] [,3]


[1,] 0 1 1
[2,] 1 0 1
[3,] 1 1 0

> # d
> matrix(c(0,0,7, 2,5,0, 3,0,0), 3, 3)

[,1] [,2] [,3]


[1,] 0 2 3
[2,] 0 5 0
[3,] 7 0 0

3. Use R to produce a vector containing all integers from 1 to 100 that are not divisible by 2, 3, or 7.
Solution:

> x <- 1:100


> idx <- (x %% 2 != 0) & (x %% 3 != 0) & (x %% 7 != 0)
> x[idx]

[1] 1 5 11 13 17 19 23 25 29 31 37 41 43 47 53 55 59 61 65 67 71 73 79 83 85
[26] 89 95 97

4. Which of the following assignments will be successful? What will the vectors x, y, and z look like
at each stage?

rm(list = ls())
x <- 1
x[3] <- 3
y <- c()
y[2] <- 2
y[3] <- y[1]
y[2] <- y[4]
z[1] <- 0
5. Build a 10 × 10 identity matrix. Then make all the non-zero elements 5. Do this latter step in at
least two different ways.
Solution:

> Id <- diag(10)


> Id <- 5*Id # one way, using the fact that Id is the identity
> Id[Id != 0] <- 5 # another way, using vector indexing of a matrix
> diag(Id) <- 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy