Chapter 2
Week 1 lecture 2
- Consider 101 as a base 2, 3, 4, or 5 representation. What does it represent in base 10?
- 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
- Repeat the above for 101.101
Solution
conv2dec2 <- function(x, B) {
a0 <- strsplit(x, '[.]')[[1]]
a_left <- as.numeric(strsplit(a0[1], '')[[1]])
a_right <- as.numeric(strsplit(a0[2], '')[[1]])
a <- c(a_left, a_right)
pows <- (length(a_left) - 1):(-length(a_right))
sum(a * B^pows)
}
x2 <- '101.101'
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\))
- 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}\)
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}\)
- 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}\)
Week 2 lecture 1
Challenges I
- Find to the nearest order of magnitude the smallest number that
Rcan represent.
[Hint: the following code
reduces a by a factor a 10, starting from 0.1, ten times.]
Challenges II
- Consider
Use the help files for functions %% and %/% together with x above to deduce what both operators do.
Solution
- Consider the function
range()and the vector
Use its help file to ascertain the difference between the following three calls.
Solution
- Consider the help file for function
mean(). What does its argumenttrimdo? Generate a small sample of data and use this to confirm that it does what you expect.
Week 2 lecture 2
Challenges I
- Compute the following to understand how
Rprioritises different symbols and operators
- 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
- Form a
listin 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.
Challenges II
- Form a \(10 \times 20\)
matrixcomprising N(0, 1) variates and find the median over each row.
- Complete the following
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
- Find the length of each vector in 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())
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 containNAs where the rolling mean cannot be computed, i.e., \(i = 1, 2, 3, n - 2, n - 1, n\).
Solution
- 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.]
- Familiarise yourself with the functions
any()andall().
Challenges II
- Create the following function
browser_test <- function(x) {
x1 <- 1
x2 <- 2
x3 <- 3
x4 <- 4
x5 <- 5
x6 <- 6
browser()
x7 <- 7
x8 <- log(x)
c(x1, x2, x3, x4, x5, x6, x7, x8)
}and then run browser_test(-1).
Then type
x1,x2,x3,x4,x5andx6.What happens if you type
x7?What happens if you hit
Entertwice and then typex7?What happens if you hit
Enteragain?
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.
Solution
- 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} =\)aand \(\mathbf{b} =\)b.
n <- 1e3
a <- rnorm(n) # using n above
b <- rnorm(n)
ab <- function(a, b) {
s <- 0
for (i in 1:n)
... # insert one line of code here
s
}Solution
- Then propose a vectorised alternative, and benchmark whether it’s better.