Math 1140 - R tutorial - Nicolas Fourrier

... It might become useful to manipulate matrix and vectors. Try the following commands .... f utureV alue2=(1+ i2/m2) n2 = time ∗ m2 while(idx < n2){ idx = idx + 1.
113KB taille 7 téléchargements 723 vues
MATH 1140 - Section 001 Nicolas Fourrier

Fall 2012

Financial Mathematics - R Tutorial

Download R: Go on R-project website and follow the instructions from the website for the installation. Starting: • Create a folder to save all your R-work in you userspace • Open R, go to File → Change dir → Choose the desired folder → OK. • Go to file → Save Workspace → .RData → Save • In the prompt, to exit R, type: > q() And answer: yes to save your workspace image. • In your userspace, open your R-folder and double click on .RData to open R. Now, each time you would like to work with R, you only need to do the last step. Most important function: Programming requires a lot of curiosity. After you discover the first function > q() , you will need some help to understand pre-programmed function. The first functions that you will used will be extremely easy to understand and manipulate, but soon you will need more explanation: > help(yourF unction)

Play around: In the main window in R, there is the prompt > This is where you will type your command. Compute different usual operations to get familliar to the prompt. Then discover few functions: > c(1, 4, 0, 4) > c(2 : 12) > var < −c(2, 5, 4, 1) > var[3] > var[1] > var[c(1, 3)] > ls() > rm(var)

Matrix: It might become useful to manipulate matrix and vectors. Try the following commands to discover how to create a matrix. > newM atrix < −matrix(c(1, 4, 4, 23, 56, 4), 2, 3) > >

newM atrix[2, ] newM atrix[, c(1, 2)]

Create your own function: Instead of repeating many times the same step, you can create your own function: > myF irstF unction < −f unction(variable){variable ∗ 0.05 ∗ 25/360} Then, you can use this function:

>

myF irstF unction(2000)

Working with files: You can create script file by doing File -¿ New Script. You can write any Rcommand in this file before you save it in your current directory. It will create a .R file. Then in the command window, you must load the file by the command > source(“N ameOf Y ourF ile.R00 ) Problem 1 You will have to produce two functions. The first will compute the net present value given an interest rate of any problem from the textbook. (you will have to modify this function everytime you start a new problem). The second function is more challenging, you will have to find the internal rate of return (IRR) for any NPV. To this end, you need to get familiar with the loop > while This function allows you to repeat an action (a sequence of R command) until you reach the desired result. For instance, you may wish to compute the NPV function until its returning value get close to 0. Note that reaching exactly 0 in such a situation is not necessarly easy, as a consequence, instead of reaching exactly 0, you say that you the absolute value of the NPV to be less than 0.01. Computing the NPV many times will not be sufficient to achieve this problem, you will also need to define before a strategy, i.e., how do you choose the value of the parameter (rate of interest) of the NPV function. Solution to problem 1 > N P V < −f unction(interest){ i = interest/100 x < − − 10000 + 5000/(1 + i ∗ 6/12) + 3000/(1 + i ∗ 9/12) + 4000/(1 + i ∗ 15/12) return(x) }

>

}

F indIRR < −f unction(){ interest = 99 upperBound = 100 lowerBound = 0 y = N P V (interest) while(abs(y) > 0.1){ if (y > 0){ lowerBound = interest interest = interest + (upperBound − interest)/2 y = N P V (interest) cat(interest) } else{ upperBound = interest interest = interest − (interest − lowerBound)/2 y = N P V (interest) } } return(interest)

Problem 2 Graph the curve representing a the evolution of the future value in function of time for the following interest: • 5%(12) • 8% simple interest • 7%(1) To create a simple graph, you will need to use the following commands. First, we already know that > var < −c(1, 2, 3) creates a row vector with the number 1, 2 and 3. Observe that > var < −c(var, 4, 5) adds the number 4 and 5 to the previous row. The function > plot(variable) will plot ”variable” a graph. Solution > F V graph < −f unction(i, m){ idx = 0 f utureV alue = (1 + i/m) while(idx < 1001){ idx = idx + 1 S = (1 + i/m)idx f utureV alue < −c(f utureV alue, S) } plot(f utureV alue, type = ”o”, col = ”blue”, ann = F ALSE) box() title(main = ”F uturevalueinf unctionof time”, col.main = ”red”, f ont.main = 4) title(xlab = ”T ime − N umberof Conversionperiods”, col.lab = rgb(0, 0.5, 0)) title(ylab = ”F utureV alue”, col.lab = rgb(0, 0.5, 0)) }

The following function plots the graph of the 2 future values. It is a little bit longer but it is simply twice the previous functions. Note that the function is still incomplete since the x-axis corresponds to the number of conversion periods for the first case, and note the time (5 years in this example). > F V 2graph < −f unction(i1, m1, i2, m2){ time = 5 idx = 0 f utureV alue1 = (1 + i1/m1) n1 = time ∗ m1 while(idx < n1){ idx = idx + 1 S = (1 + i1/m1)idx f utureV alue1 < −c(f utureV alue1, S) } idx = 0 f utureV alue2 = (1 + i2/m2) n2 = time ∗ m2 while(idx < n2){ idx = idx + 1 S = (1 + i2/m2)idx f utureV alue1 < −c(f utureV alue2, S) } plot(f utureV alue1, type = ”o”, col = ”blue”, ann = F ALSE) lines(f utureV alue2, type = ”o”, pch = 22, lty = 2, col = ”red”) box() title(main = ”F uturevalueinf unctionof time”, col.main = ”red”, f ont.main = 4) title(xlab = ”T ime − N umberof Conversionperiods”, col.lab = rgb(0, 0.5, 0)) title(ylab = ”F utureV alue”, col.lab = rgb(0, 0.5, 0)) g range < −range(0, f utureV alue1, f utureV alue2) legend(1, g range[2], c(”Case1”, ”Case2”), cex = 0.8, col = c(”blue”, ”red”), pch = 21 : 22, lty = 1 : 2) }

Problem 3: Julie purchases a $5, 000 car with 9% discount loan and monthly payments for 3 years. Find the APR (express as AP R%(12)). Solution: AP R = 0.01811 ∗ 12 = 23.73%(12) First create a function which computes the difference between the the cash value and the present value of the payment. (AddOn function). Then create the FindAPR function based on FindIRR, which seek for the approximate APR. You can now proceed the same way (creating a small function such as AddOn, and use FindAPR) to find any interest rate you want when it is not possible to solve analytically. > AddOn < −f unction(interest){ x = 101.39 ∗ (1 − (1 + interest)( − 24))/interest − 3000 return(x) } > F indAP R < −f unction(){ interest = 99 upperBound = 100 lowerBound = 0 y = AddOn(interest) while(abs(y) > 0.1){ if (y > 0){ lowerBound = interest interest = interest + (upperBound − interest)/2 y = AddOn(interest) } else{ upperBound = interest interest = interest − (interest − lowerBound)/2 y = AddOn(interest) } } return(interest) }