代做MATH377: Financial and Actuarial Modelling in R Tutorial 2帮做R语言

- 首页 >> OS编程

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.

Exercise 3. Consider the matrix

a) Verify that A3 = 0.

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

Exercise 4. Consider the matrix

Replace the entries with even values with −1.

Exercise 5. Solve the linear system

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.

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.

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)

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

Using a while loop, find a value N such that

where ϵ = 0.00001.

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

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

where A0 is the identity matrix.

a) Consider the matrix

Write an R function to compute

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






站长地图