0% found this document useful (0 votes)
61 views2 pages

R Programming Exam: Instructions

This document provides instructions for an R programming exam. It consists of 10 problems to be solved by writing an R script. For each problem, the script should generate an object (A1, A2, etc.) with the specified output. The problems include regressing mpg on wt for mtcars, generating random matrices, computing a minimum variance portfolio, simulating a stochastic process, loading and manipulating data frames and time series data, exploring autocorrelation, and pricing European call options using the Black-Scholes formula. The script must be saved with a specific naming convention and submitted as an attachment for grading.

Uploaded by

Micheal Go
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

R Programming Exam: Instructions

This document provides instructions for an R programming exam. It consists of 10 problems to be solved by writing an R script. For each problem, the script should generate an object (A1, A2, etc.) with the specified output. The problems include regressing mpg on wt for mtcars, generating random matrices, computing a minimum variance portfolio, simulating a stochastic process, loading and manipulating data frames and time series data, exploring autocorrelation, and pricing European call options using the Black-Scholes formula. The script must be saved with a specific naming convention and submitted as an attachment for grading.

Uploaded by

Micheal Go
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

R Programming Exam

20/09/2020

Instructions

The exam consists on writing an R script following the instructions. At the beginning of your R script, you
should write the following commands:
NIS <- # YOUR STUDENT ID NUMBER
set.seed(NIS)

Each problem requires creating an R object with a specific data structure. The object should be assigned to a
symbol A1 for problem 1, A2 for problem 2, and so on.
Example:
Problem 1: Create a 2 x 2 identity matrix. The output should be a 2x2 matrix.
Problem 2: Create the “hello world!” function. The output should be a function.
Your full R script should read
NIS <- # YOUR STUDENT ID NUMBER
set.seed(NIS)
# Problem 1
A1 <- diag(2)
# Problem 2
A2 <- function() print('hello world!')

Loading data files

You must assume that the data files are placed in the same directory as your R script.

Submission instructions You must save your R script with the name exam_[YOUR NIS].R. For example,
if your student number is 12345, the file must be named exam_12345.R. You must submit your exam script
as an attachment to this assignment.

Problems

Problem 1: Regress mpg on wt using the mtcars dataset.


The output A1 must be a 2 x 1 matrix.
Problem 2: Generate a 100 x 3 matrix where each entry is distributed t5 , which denotes the central
t-distribution with 5 degrees of freedom. Make sure you fill the matrix by columns, i.e first we fill in the
entire first column, then the second column, and so on.
The output A2 must be a 100 x 3 matrix.

1
Problem 3: Interpret the previously generated matrix as the returns of 3 different assets recorded during
100 days, and use it to compute the minimum variance portfolio. Recall that this is a 3 x 1 vector

S−1 1
w= ,
10 S−1 1
where S−1 is the inverse of the sample covariance matrix of the data and 1 is a 3-dimensional vector of ones.
Note that if you do this correctly, the sum of the entries should be equal to 1.
The output A3 must be a 3 x 1 matrix.
Problem 4: Simulate the process

Xt = .5Xt−1 + ut , ut ∼ N (0, 1),

where X0 = 0, and t = 1, ..., 100.


The output A4 must be a numeric vector of length 100.
Problem 5: Load the sp500ret.csv file as a data frame. Note that the first date is the last. Please fix so
that the first observation really corresponds to the first date.
The output A5 must be a data frame.
Problem 6: Load the SP500index.txt dataset. Extract the “GSPC.Adjusted” column Pt and compute the
log-returns, i.e. rt = log(Pt ) − log(Pt−1 ), t = 2, ..., T , where T is the number of observations in the dataset.
Hint: check the diff function.
The output A6 must be an xts object of length T − 1.
Problem 7: Compute the mean, standard deviation, minimum and maximum of the log-returns from the
previous question.
The output A7 must be a list with names “mean”, “std”, “min” and “max”.
Problem 8: Explore if there is any predictability in the S&P500 returns at the daily level. To do this, run
an autoregression: simply regress the series on its lagged value. Extract the slope and its standard error.
The output A8 must be a named vector with names “persistence” and “SE”.
Problem 9: Create a function that calculates the price of a European call option with the Black-Scholes
formula. Recall that this is given by

C(S, K, M, σ, r) = N (d1 )S − N (d2 )Ke−rM


σ2
     
1 S
d1 = √ log + r+ M
σ M K 2

d2 = d1 − σ M

where S is the price of the underlying asset, K is the strike price, M is the time to maturity, N (·) is the
standard normal cumulative distribution function, σ is the volatility, and r is the risk-free rate.
The output A9 should be the function C.
Problem 10: Suppose that S = 100, r = 0, and σ = 1. For each strike price in {80, 90, 100, 110, 120} and
time to maturity in

{10/360, 60/360, 110/360, 160/360, 210/360, 260/360, 310/360, 1},

compute the call price.


The output A10 must be a 5 x 8 matrix with the call prices for each combination of K and M .

You might also like