MATH377: Financial and Actuarial Modelling in R
Tutorial 1
Exercise 1. Write an R code to determine the result of the following computation:
Exercise 2. Without using R, determine the result of the following logical computation
((!(4 == 3) | (abs(-3) <= 2)) & ((2^2 > 4) & (TRUE))) | ((!FALSE | (4+2) == 5) & (0.5 >= (1/2)))
Verify your result by typing the code in R.
Exercise 3. Find the errors in the following lines of code:
a)
2 + 3 *4 + sqrt[100]
b)
(2 + i) / 3 + {1e1 + 4.0i}
c)
time.To.Maturity <- 6
Interest.rate <- 0.05
{1e-0 + interest.rate}^{-time.To.Maturity}
Exercise 4. Without using R, determine the result of the following computation:
x <- c(1, 2, 3)
x[2] / x[2]^2 - 1 + 3 * x[3] - x[2 - 1]
Verify your result by typing the code in R.
Exercise 5. Write an R code to calculate the amount of money owed after n years, where n varies from 1 to 10 in yearly increments, assuming that the money lent originally is 2350, and the interest rate remains constant throughout the period at 5% compounded annually.
Exercise 6.
a) Is there any difference between 10:6*3 and 10:(6*3)? Explain why. Compute all the multiples of 3 between 3 to 30.
b) Is there any difference between 10:4ˆ3 and 10:(4ˆ3)? Explain why. Compute the square of the numbers 10,. . . ,4.
Exercise 7. Consider the vector 1:N, where N is a positive integer. Write an R code that determines how many elements in the vector are exactly divisible by 4. Test your code with N <- 40.
Exercise 8. Create a vector containing the following student grades: 70, 80, 55, 67, 90, 92, 83, 74, 100, 87, 49. Using logical operators and vector functions, answer the following question:
a) What is the average grade of the whole group?
b) How many students have grades less than 65?
c) What is the average grade of those students with grades between 60 and 80 (including 60 and 80)?
d) Assume that we add three new students with grades 65, 98, 54. Repeat questions a)-c) with the new vector of grades.
Exercise 9. Write an R program to compute the alternating harmonic series
up to a finite number of summands N. Test your code with N = 100 and compare with log(2).
Exercise 10. The function cov() computes the sample covariance of two vectors. Recall that for two vectors x = (x1, . . . , xn) and y = (y1, . . . , yn) the sample covariance is given by
Write an R program to compute the sample correlation without using the cov() function. Test your code with the following vectors: x <- c(1, 2, 3, 4) and y <- c(2, 2, 3, 5). Verify your result using cov(x, y).
Exercise 11. Using only rep() and seq() (or :) as needed, create the vectors
a)
0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
b)
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
c)
1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
Hint: Look into the help of rep() (?rep). You will find that the argument times can be a vector and that there are additional arguments named each and length.out.