Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

MATH377: Financial and Actuarial Modelling in R

Tutorial 2

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.

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.

A =  l 5(1)

1

2

1

6(3)  3l .

a) Verify that A3  = 0.

b)  Replace the third column with the sum of column 1 and column 2.


l33

20 

地(地)15

M =  

15 

地(地)14 10

 

51

35

27

21

25

53

 

60

17 l l

24 l(l) l

28 l . l

29 l(l)

24l

Replace the entries with even values with 1.

Exercise 5. Solve the linear system

('2y + x z = 1 ,

 2z y = 0 ,

'(x + 1 + y + 2z = 1 .

Hint: Look at the documentation of solve() (?solve).

Exercise 6. Write an R program to create a 3-dimensional array of three 4 × 3 matrices with entries of your choice.

Exercise 7. 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.

 

f)  Extract the rows where the rates are below the mean of the whole year.

Exercise 8. 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.

Exercise 9. Consider a random variable X with probability density function (pdf)

fX (x) =  ,  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.

Exercise 10. We know that

1 +  +  +  + · · · =  = 2 .

n=0

Using a while loop, find a value N such that

N

2  ϵ ,

n=0

where ϵ = 0.00001.

Exercise 11. Write a function that performs the inner product of two vectors using for loops.  Then,

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.

Exercise 12. Recall that the Taylor expansion of the exponential function is given by

exp(x) =   .

In a similar way, we can define the exponential of a square matrix A using the infinite sum representation

exp(A) =   ,

where A0  is the identity matrix.

a)  Consider the matrix

A =  l  2

Write an R function to compute

10

n=0

−3  .

An

.

b) Install the R package expm and compute exp(A) for A in a) using the expm() function.

c)  Compute the maximum entry-wise difference (in absolute value) between the computations in a) and b).