R-MATH377

MATH377: Financial and Actuarial Modelling in R
Tutorial 2 – Solutions
Exercise 1. Create three vectors of length 3, with content and names of your choice. Next, combine the
three vectors into a 3× 3 matrix where each column represents one of your vectors. Finally, compute the
determinant (det() – is your matrix invertible ) and the transpose of your matrix.
Solution.
x <- 1:3 y <- 4:6 z <- 7:9 my_matrix <- matrix(c(x, y, z), 3) my_matrix ## [,1] [,2] [,3] ## [1,] 1 4 7 ## [2,] 2 5 8 ## [3,] 3 6 9 det(my_matrix) # Not invertible ## [1] 0 t(my_matrix) ## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 4 5 6 ## [3,] 7 8 9 Exercise 2. Consider the following matrix: matrix(c(c(-4, 2, 1), c(0, -1, 0.5), c(1.5, 0.2, -2)), ncol = 3, byrow = TRUE) ## [,1] [,2] [,3] ## [1,] -4.0 2.0 1.0 ## [2,] 0.0 -1.0 0.5 ## [3,] 1.5 0.2 -2.0 Compute the sum of rows (that is, you have to return a vector of length three with first entry -1) via the following three methods: a) Using the rowSums() function (see help for more information). b) Using the apply() function. c) Using matrix multiplication. Hint: This can be done by multiplying the above matrix with an appropriate vector. Solution. S <- matrix(c(c(-4, 2, 1), c(0, -1, 0.5), c(1.5, 0.2, -2)), ncol = 3, byrow = TRUE) a) rowSums(S) ## [1] -1.0 -0.5 -0.3 1 b) apply(S, 1, sum) ## [1] -1.0 -0.5 -0.3 c) S %*% rep(1, nrow(S)) ## [,1] ## [1,] -1.0 ## [2,] -0.5 ## [3,] -0.3 Exercise 3. Write an R program to create a 3-dimensional array of three 4× 3 matrices with entries of your choice. Solution. entries <- seq(1, 10, length.out = (3 * 4 * 3)) array(entries, dim = c(4, 3, 3)) ## , , 1 ## ## [,1] [,2] [,3] ## [1,] 1.000000 2.028571 3.057143 ## [2,] 1.257143 2.285714 3.314286 ## [3,] 1.514286 2.542857 3.571429 ## [4,] 1.771429 2.800000 3.828571 ## ## , , 2 ## ## [,1] [,2] [,3] ## [1,] 4.085714 5.114286 6.142857 ## [2,] 4.342857 5.371429 6.400000 ## [3,] 4.600000 5.628571 6.657143 ## [4,] 4.857143 5.885714 6.914286 ## ## , , 3 ## ## [,1] [,2] [,3] ## [1,] 7.171429 8.200000 9.228571 ## [2,] 7.428571 8.457143 9.485714 ## [3,] 7.685714 8.714286 9.742857 ## [4,] 7.942857 8.971429 10.000000 Exercise 4. Write an R program to: a) Create a numeric vector called rates with values: 0.043, 0.045, 0.041, 0.049, 0.05, 0.055, 0.048, 0.0495, 0.051, 0.044, 0.045, 0.0455. b) Create a character vector called months with values: “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”. c) Create a data frame called monthly_rates using months and rates. d) Add a new column to your data frame called year with values 2021 for all rows. e) Extract the rows where the rates are above 5%. f) Extract the rows where the rates are below the mean of the whole year. Solution. a) 2 rates <- c(0.043, 0.045, 0.041, 0.049, 0.05, 0.055, 0.048, 0.0495, 0.051, 0.044, 0.045, 0.0455) b) months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec") c) monthly_rates <- data.frame(rates, months) monthly_rates ## rates months ## 1 0.0430 Jan ## 2 0.0450 Feb ## 3 0.0410 Mar ## 4 0.0490 Apr ## 5 0.0500 May ## 6 0.0550 Jun ## 7 0.0480 Jul ## 8 0.0495 Aug ## 9 0.0510 Sep ## 10 0.0440 Oct ## 11 0.0450 Nov ## 12 0.0455 Dec d) monthly_rates$year <- 2021 monthly_rates ## rates months year ## 1 0.0430 Jan 2021 ## 2 0.0450 Feb 2021 ## 3 0.0410 Mar 2021 ## 4 0.0490 Apr 2021 ## 5 0.0500 May 2021 ## 6 0.0550 Jun 2021 ## 7 0.0480 Jul 2021 ## 8 0.0495 Aug 2021 ## 9 0.0510 Sep 2021 ## 10 0.0440 Oct 2021 ## 11 0.0450 Nov 2021 ## 12 0.0455 Dec 2021 e) monthly_rates[monthly_rates[, "rates"] > 0.05, ]
## rates

💡 Buy the answer for only $12 Get it now →

months year
## 6 0.055 Jun 2021
## 9 0.051 Sep 2021
f)
mean_rate <- mean(monthly_rates[, "rates"]) mean_rate ## [1] 0.04716667 monthly_rates[monthly_rates[, "rates"] < mean_rate, ] ## rates months year 3 ## 1 0.0430 Jan 2021 ## 2 0.0450 Feb 2021 ## 3 0.0410 Mar 2021 ## 10 0.0440 Oct 2021 ## 11 0.0450 Nov 2021 ## 12 0.0455 Dec 2021 Exercise 5. Assume a group of students with ages age <- c(19, 20, 18, 19, 18, 20, 18, 19, 19, 20) and grades grade <- c(90, 75, 80, 87, 74, 93, 100, 66, 71, 89) Let us imagine that we want to compute the average grade by age. We can use the tapply() function to do so. Look at the documentation of tapply() ( tapply) and solve the above problem. Solution. age <- c(19, 20, 18, 19, 18, 20, 18, 19, 19, 20) grade <- c(90, 75, 80, 87, 74, 93, 100, 66, 71, 89) tapply(grade, age, mean) ## 18 19 20 ## 84.66667 78.50000 85.66667 Exercise 6. Consider a random variable X with probability density function (pdf) fX(x) = x 4 e x28 , x ≥ 0 a) Write an R function to compute the above pdf. b) Check whether this function is indeed a pdf (i.e., that it integrates 1) by: i. A sum approximation of the form ∑ f(xi) (x), where (x) is a “small” increment. Hint: create a sequence vector (over a relatively large interval and with a small increment), evaluate your function in a) on the sequence vector, multiply the evaluation by the increment and finally sum. ii. Using the integrate(f, lower, upper) function. Note: $value gives the value of the integral. c) Compute the expected value and the variance of X. d) Modify your function in a) so that an error message is displayed if a negative value is given as input. Solution. a) dens <- function(x) { x * exp(-x 2 / 8) / 4 } dens(1) ## [1] 0.2206242 b) i. increment <- 0.01 int_grid <- seq(0, 20, by = increment) sum(dens(int_grid) * increment) ## [1] 0.9999979 i. 4 integrate(dens, 0, Inf) ## 1 with absolute error < 4.2e-05 c) mean_aux <- function(x) { x 2 * exp(-x 2 / 8) / 4 } snd_aux <- function(x) { x 3 * exp(-x 2 / 8) / 4 } mean_rv <- integrate(mean_aux, 0, Inf)$value mean_rv ## [1] 2.506628 snd_rv <- integrate(snd_aux, 0, Inf)$value var_rv <- snd_rv - mean_rv 2 var_rv ## [1] 1.716815 d) dens <- function(x) { if (any(x < 0)) { stop("Negative input") } x * exp(-x 2 / 8) / 4 } dens(1) ## [1] 0.2206242 dens(-1) ## Error in dens(-1): Negative input Exercise 7. We know that 1 + 12 + 1 4 + 1 8 + · · · = ∞∑ n=0 1 2n = 2 . Using a while loop, find a value N such that 2 N∑ n=0 1 2n ≤ , where = 0.00001. Solution. epsilon <- 0.00001 N <- 0 series <- 0 while ((2 - series) > epsilon) {
series <- series + 2 (-N) N <- N + 1 } N - 1 # An extra 1 was added 5 ## [1] 17 series ## [1] 1.999992 Exercise 8. Write a function that performs the inner product of two vectors using for loops. Using microbenchmark() compare the performance of your implementation with the inner product using the %*% operator. Try with large vectors, let’s say length 100, and times = 10000. Solution. user_product <- function(x, y){ z <- 0 for (i in 1:length(x)) { z <- z + x[i] * y[i] } z } x <- 1:100 y <- 101:200 user_product(x, y) ## [1] 843350 r_product <- function(x, y) { x %*% y } r_product(x, y) ## [,1] ## [1,] 843350 library(microbenchmark) microbenchmark(user_product(x, y), r_product(x, y), times = 10000) ## Warning in microbenchmark(user_product(x, y), r_product(x, y), times = 10000): ## less accurate nanosecond times to avoid potential integer overflows ## Unit: nanoseconds ## expr min lq mean median uq max neval ## user_product(x, y) 3198 3321 3501.851 3403 3526 18983 10000 ## r_product(x, y) 656 779 1058.066 779 902 457970 10000 6

💡 Buy the answer for only $12 Get it now →