Chapter 2
Week 1 lecture 2
- Consider 101 as a base 2, 3, 4, or 5 representation. What does it represent in base 10?
Solution
# bookwork
- Repeat the calculation above using
R
. [Hint: consider forming a vector for \((a_0, a_1, a_2)^\text{T}\) and another for \((B^0, B^1, B^2)\)]
Solution
<- function(x, B) {
conv2dec <- as.numeric(strsplit(x, '')[[1]])
a <- (length(a) - 1):0
pows sum(a * B^pows)
}
<- '101'
x conv2dec(x, 2)
conv2dec(x, 3)
conv2dec(x, 4)
conv2dec(x, 5)
- Repeat the above for 101.101
Solution
<- function(x, B) {
conv2dec2 <- strsplit(x, '[.]')[[1]]
a0 <- as.numeric(strsplit(a0[1], '')[[1]])
a_left <- as.numeric(strsplit(a0[2], '')[[1]])
a_right <- c(a_left, a_right)
a <- (length(a_left) - 1):(-length(a_right))
pows sum(a * B^pows)
}
<- '101.101'
x2 conv2dec2(x2, 2)
conv2dec2(x2, 3)
conv2dec2(x2, 4)
conv2dec2(x2, 5)
Week 1 lecture 3
Challenges I
- Find the largest value of \(E\) that can be represented with single-precision arithmetic, given \(E = \sum_{i = 1}^8 b_{i + 1} 2^{8 - i}\) (i.e., find \(E\) when \(b_2 = b_3 = \ldots = b_9 = 1\))
Solution
sum(2^c(0:7))
- Find the smallest value of \(F\) greater than zero that can be represented with single-precision arithmetic, given \(F = \sum_{i = 1}^{23} b_{i + 9} 2^{-i}\)
Solution
2^(-23)
Challenges II
- Find the largest value of \(E\) that can be represented with double-precision arithmetic, given \(E = \sum_{i = 1}^{11} b_{i + 1} 2^{11 - i}\)
Solution
sum(2^c(0:10))
- Find the smallest value of \(F\) greater than zero that can be represented with double-precision arithmetic, given \(F = \sum_{i = 1}^{52} b_{i + 9} 2^{-i}\)
Solution
2^(-52)
Week 2 lecture 1
Challenges I
- Find to the nearest order of magnitude the smallest number that
R
can represent.
[Hint: the following code
<- .1
a for (i in 1:10) {
<- .1 * a
a print(a)
}
reduces a
by a factor a 10, starting from 0.1, ten times.]
Solution
<- .1
a for (i in 1:330) {
<- .1 * a
a print(a)
}
Challenges II
- Consider
<- 1:12 x
Use the help files for functions %%
and %/%
together with x
above to deduce what both operators do.
Solution
<- 1:12
x # %% 3 divides by three and then gives the remainder
%% 3
x # %/% 3 divides by three and then gives the integer part
%/% 3 x
- Consider the function
range()
and the vector
<- c(1, 2, NA, Inf) x
Use its help file to ascertain the difference between the following three calls.
range(x)
range(x, na.rm = TRUE)
range(x, finite = TRUE)
Solution
<- c(1, 2, NA, Inf)
x range(x) # gives NA because x includes NA
range(x, na.rm = TRUE) # recognises that Inf is big and ignores the NA
range(x, finite = TRUE) # ignores the NA and Inf, i.e. anything non-finite
- Consider the help file for function
mean()
. What does its argumenttrim
do? Generate a small sample of data and use this to confirm that it does what you expect.
Solution
<- c(1, 1:10)
x mean(x)
mean(x, trim = .1) # calculate the mean from the middle 90% of the data
Week 2 lecture 2
Challenges I
- Compute the following to understand how
R
prioritises different symbols and operators
1:3^2
c(1:3)^2
1+2^3
1 + 2)^3
(
1 + 2 * 3
1 + 2) * 3 (
Solution
1:3^2
1:3)^2
(
1 + 2^3
1 + 2*3
1 + 2)*3 (
- Calculate \(\mathbf{Ba}\) and \(\mathbf{cB}\) where \[ \mathbf{a} = \left(\begin{array}{c}2\\ 4\\ 6\end{array}\right),~ \mathbf{B} = \left(\begin{array}{ccc} 2 & 3 & 1\\ 4 & 3 & 1\\ 2 & 2 & 3 \end{array}\right) \text{ and } \mathbf{c} = \left(\begin{array}{ccc} 2 & 4 & 6 \end{array}\right). \]
Solution
<- c(2, 4, 6)
a <- matrix(c(2, 4, 4, 3, 3, 2, 1, 1, 3), 3, 3)
B <- matrix(c(2, 4, 6), 1)
c
%*% a # Ba
B %*% B # cB
c <- t(a) # creates c as the tranpose of a
c t(a) %*% B # avoids creating c
- Form a
list
in which the first element is a \(3 \times 6 \times 2\) array, the second is a 4-vector, and the third is a \(4 \times 3\) matrix, each of which are filled with random Uniform([-1, 1]) variates.
Solution
<- list(
x array(runif(36, -1, 1), c(3, 6, 2)),
runif(4, -1, 1),
matrix(runif(12, -1, 1), 4, 3)
) x
Challenges II
- Form a \(10 \times 20\)
matrix
comprising N(0, 1) variates and find the median over each row.
Solution
<- matrix(rnorm(200), 10, 20)
x apply(x, 1, median)
- Complete the following
<- lapply(1:4, function(i) ...) x
to a form a four-element list
in which each element comprises a vector of variates generated according to
\[
Y_1, \ldots, Y_N \text{ with } N - 1 \sim \text{Poisson}(2)\text{ and }Y_i \sim N(3, 2^2),~i = 1, \ldots, N
\]
Solution
<- lapply(1:4, function(i) rnorm(1 + rpois(1, 2), 3, 2))
x lapply(1 + rpois(4, 2), function(x) rnorm(x, 3, 2))
- Find the length of each vector in the
list
.
Solution
sapply(x, length)
Challenges III
- Consider the following
list
<- list(list(1:3, 4:6), list(7:9, 10:12)) lst2
What’s the difference between the following?
unlist(lst2)
unlist(lst2, recursive = FALSE)
Solution
# THe first returns a list of vectors.
# The second returns a vector, i.e. drops the structure
# of what's in each element of the list.
Week 2 lecture 3
Challenges I
- On the website www.mathsgear.co.uk you can buy a crooked die
- Let \(X \in \{1, 2, 3, 4, 5, 6\}\) denote the number rolled on a die, where \[ \text{Pr}(X = x) = \left\{\begin{array}{cl} 0.10 & \text{if }x = 1\\ 0.05 & \text{if }x \in \{2, 3\}\\ 0.20 & \text{if }x \in \{4, 5\}\\ 0.40 & \text{if }x = 6\\ \end{array}\right. \]
- Simulate 15 rolls of the die in
R
(perhaps usingsample()
)
Solution
<- 15
n
# as a loop
<- integer(n)
x for (i in 1:n) {
<- sample(1:6, 1, prob = c(.1, .05, .05, .2, .2, .4))
x[i]
}
x
# more tidily with sample()
<- sample(1:6, n, prob = c(.1, .05, .05, .2, .2, .4), replace = TRUE)
x x
Week 3 lecture 1
Challenges I
- Consider computing the rolling mean of a vector \((y_1, \ldots, y_n)^\text{T}\). Let’s suppose it’s a seven-day rolling mean, as often used to convey Covid-19 data, so that
\[
z_i = \dfrac{1}{7} \sum_{j = 1}^7 y_{i + j - 4}, \text{ for } i = 4, 5, \ldots, n - 3.
\]
Generate a vector of length
n
\(= 35\) comprising Gamma(2, 3) random variates, and then compute its rolling mean. Your result should containNA
s where the rolling mean cannot be computed, i.e., \(i = 1, 2, 3, n - 2, n - 1, n\).
Solution
<- 35
n <- rgamma(n, 3, 2)
y <- rep(NA, n)
z for (i in 4:(n - 3)) z[i] <- mean(y[i + seq(-3, 3)])
matplot(seq_len(n), cbind(y, z))
- The function
filter()
can do this in a vectorised way. Obtain the same result usingfilter()
. [Hint: the examples forfilter()
may help illustrate how its arguments work.]
Solution
<- filter(y, rep(1/7, 7))
z2 all.equal(z, z2)
<- as.vector(z2)
z3
all.equal(z, z3)
- Familiarise yourself with the functions
any()
andall()
.
Solution
all(runif(10) > .5)
any(runif(10) > .5)
Challenges II
- Create the following function
<- function(x) {
browser_test <- 1
x1 <- 2
x2 <- 3
x3 <- 4
x4 <- 5
x5 <- 6
x6 browser()
<- 7
x7 <- log(x)
x8 c(x1, x2, x3, x4, x5, x6, x7, x8)
}
and then run browser_test(-1)
.
Then type
x1
,x2
,x3
,x4
,x5
andx6
.What happens if you type
x7
?What happens if you hit
Enter
twice and then typex7
?What happens if you hit
Enter
again?
Solution
# No sketch solutions for Q2-5 given.
To exit browser()
mode type and execute Q
.
Week 3 lecture 2
Challenges I
- Confirm that the following give the same answer, and then benchmark which is quicker.
<- 1e3
n <- runif(n)
y sort(y)[1]
min(y)
Solution
library(microbenchmark)
microbenchmark(
apply(A, 1, sum),
rowSums(A)
)
# minima
<- 1e3
n <- runif(1e3)
y microbenchmark(
sort(y)[1],
min(y)
)
- Complete the function based on a
for()
loop so that it computes \(s = \sum_{i = 1}^n a_i b_i\), for \(i =1, \ldots, n\), given vectors \(\mathbf{a} =\)a
and \(\mathbf{b} =\)b
.
<- 1e3
n <- rnorm(n) # using n above
a <- rnorm(n)
b <- function(a, b) {
ab <- 0
s for (i in 1:n)
# insert one line of code here
...
s }
Solution
<- rnorm(n)
a <- rnorm(n)
b <- function(a, b) {
ab <- 0
s for (i in 1:length(a))
<- s + a[i] * b[i]
s
s
}ab(a, b)
- Then propose a vectorised alternative, and benchmark whether it’s better.
Solution
sum(a * b)
microbenchmark(
ab(a, b),
sum(a * b)
)