MATH377: Financial and Actuarial Modelling in R
Financial and Actuarial Modelling in R
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
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 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.