.chapter2<-function(i=0){ " i Chapter 2: Functions - ---------------------------------------- 1 What is a function? 2 The simplest function 3 PV function for one given future cash flow 4 Present value formula for perpetuity/annuity 5 Perpetuity due and annuity due 6 R editor 7 Three ways to input values 8 Two types of comments 9 Future value of one present value 10 How to activate an R function 11 magic use of tab key 12 show and change our working directory 13 Definition of NPV and NPV rule 14 Files under the working directory 15 Function with default values 16 Definitions of IRR and IRR rule 17 Well-indented codes are more readable 18 Total risk and standard deviation 19 Videos 20 Links Example #1:> .c2 # get the above list Example #2:> .c2(1) # see the first explanation ";.zchapter2(i)} .n2chapter<-38 .zchapter2<-function(i){ if(i==0){ print(.c2) }else{ .printEachQ(2,i,.n2chapter) } } .c2<-.chapter2 .C2EXPLAIN1<-"Definition of a function //////////////////////////////// In programming and mathematics, a function is a named block of code or a mathematical expression that performs a specific task or computation. Functions take input values (arguments or parameters) and return a result or perform actions based on those inputs. The primary purpose of functions is to encapsulate reusable pieces of code, making programs more modular, readable, and easier to maintain. In mathematics, a function is a relation between a set of inputs (domain) and possible outputs (range), where each input is related to exactly one output. In programming, functions serve a similar purpose, allowing developers to break down complex tasks into smaller, more manageable units. Functions are defined using a name, a list of parameters, and a body of code that specifies what the function does when called. When a function is called, the specified code block is executed, and the function may return a value or perform specific actions. Functions promote code reuse, and abstraction, and help organize code into logical units, enhancing the clarity and efficiency of software development. //////////////////////////////// " .C2EXPLAIN2<-"The simplest function written in R //////////////////////////////// Objective: double any input value dd<-function(x)x^2 where dd is function name function is the reserve word x is the input x^ is the output //////////////////////////////// " .C2EXPLAIN3<-"pv function for one given future flow //////////////////////////////// Write an R program to calculate the present value for one given future cash flow. ------------------------------------------------------------ The present value for a given future value is FV pv(one future cash flow)= ---------- (1 + R) ^n where FV is the future value R is the period rate n is the number of periods Note: R and n should be consistent, i.e., with the same frequencies. For example, what is the present value of $100 received at the end of year 5 with an annual discount of 5%? //////////////////////////////// " .C2EXPLAIN4<-"present value formula for perpetuity //////////////////////////////// Write an R program to calculate present value for perpetuity The definition of perpetuity: same cash flows at the same interval forever. Example #1: c c -> infinity |-------|-------|-------| ... | 0 1 2 3 -> infinity Example #2: c c c c -> infinity |-------|-------|-------| ... | 0 1 2 3 -> infinity When the first cash flow occurs at the end of the first period, we have the following formula C pv(perpetuity) = ------ R where C is the constant cash flow R is the period discount rate present value formula for annuity ------------------------------ Definition of annuity: same cash flows at the same interval for n periods Example #1: c c |-------|-------|-------| ... | 0 1 2 3 n Example #2: c c c c |-------|-------|-------| ... | 0 1 2 3 n When cash flows happen at the end of each period and the first cash flow happens at the end of the first period C 1 pv(annuity)= ---- * [1 - -------- ] R (1+R)^n where C is the period cash flow R is the period discount rate n is the number of periods //////////////////////////////// " .C2EXPLAIN5<-"perpetuity due and annuity due //////////////////////////////// For normal perpetuity or annuity, each cash flow would happen at the end of each period. For perpetuity due or annuity due, each cash flow would happen at the beginning of each period. There are two ways to calculate present (future) value of annuity due. Method I: choose type=1 when use Excel pv() or fv() functions =pv(rate,nper,pmt,fv,1) =fv(rate,nper,pmt,pv,1) Method 2: try annuity as normal annuity Then type your result by (1+R) pv(annuity due) = pv(annuity)* (1+R) fv(annuity due) = fv(annuity)* (1+R) //////////////////////////////// " .C2EXPLAIN6<-"R editors //////////////////////////////// Method I: Notepad ----------------- We know that we could use Notepad as our text editor to write and edit our R programs. Method II: R editor ------------------ Step 1: click \"File\" -> \"New Script\" Step 2: enter your program, such as dd<-function(x)2*x Step 3: click \"File\" -> \"Save as\" Note #1: One of the advantages: Click \"Edit\" -> Run line or selection [ctl-R] or -> Run All //////////////////////////////// " .C2EXPLAIN7<-"Three ways to input values //////////////////////////////// Method 1: input values according to their order Method 2: input according to keyword Method 3: mixed //////////////////////////////// " .C2EXPLAIN8<-"Two types of comments //////////////////////////////// The first type command starts with # >pv<-100 # pv for present value The second type of command are included by a pair of double quotation marks \" Objective : estimate present value fv formula used: pv= -------- (1+r)^n pv : present value fv : future value r : effective period rate n : number of periods Example 1: > .pv_f(0.1,1,100) # meaning of inputs depend on order [1] 90.90909 Example 2: > .pv_f(0.045,5,225) [1] 180.5515 Example 3: > .pv_f(fv=100,r=0.1,n=1) # meaning depend on keywords > .pv_f(r=0.1,fv=100,n=1) > .pv_f(n=1,r=0.1,fv=100) Note: The above three give the same result \" //////////////////////////////// " .C2EXPLAIN9<-"future value formulae //////////////////////////////// Future value of one cash flow fv=pv(1+R)^n Future value of annuity C fv= -----*[(1+R)^n -1 ] R Future value of annuity due C fv= -----*[(1+R)^n -1 ]*(1+R) R //////////////////////////////// " .C2EXPLAIN10<-"How to activate an R function //////////////////////////////// Method #1: type it directly Method #2: copy and paste For example, we could use Notepad to write our program then, copy and paste the code to the R console Method #3: Use the R editor Click \"File\" --> \"New Script\" and type your code a) click \"Editor\" -> \"Run line or selection\" <=> Ctrl-R b) click \"Editor\" -> \"Run all\" Method #4 use the source() function For example, we have an R file called test1.R under c:/temp source(\"c:/temp/test1.R\") //////////////////////////////// " .C2EXPLAIN11<-"Magic use of tab key //////////////////////////////// From chapter 1, we know that we should use meaningful names for our variables. Because of this, the variables name might be quite long, such as DollarRealizedSpread_LR_SW The disadvantage of using a long name including more typing. Sometimes, we could use tab key to make our typing easier. After we type just enough letters, we could hit tab key to show a complete name. //////////////////////////////// " .C2EXPLAIN12<-"show and change our working directory //////////////////////////////// getwd() # get our current working directory setwd(\"c:/temp/\") # change our working director to c:/temp //////////////////////////////// " .C2EXPLAIN13<-"Definition of NPV (Net Present Value) and NPV rule //////////////////////////////// Method I: NPV = PV(all benefits) - pv(all costs) Method II: if cash outflow is defined as a negative value cash inflow is defined as a positive value NPV = PV(all cash flows) NPV Rule: for an independent project if NPV(Project) > 0 accept if NPV(Project) < 0 reject //////////////////////////////// " .C2EXPLAIN14<-"List file under the current working directory //////////////////////////////// The R fucntion used is > dir() //////////////////////////////// " .C2EXPLAIN15<-"Function without any input value/default value //////////////////////////////// Function without any input value ------------------------- such as q() Default input value ------------------- We could give a default value to an input value See an example below. dd<-function(x=10)x^2 //////////////////////////////// " .C2EXPLAIN16<-"definition of IRR (Internal Rate of Return) and IRR rule //////////////////////////////// IRR is the discount rate makes NPV equal zero. IRR rule: if IRR(project) > Rc accept if IRR(project) < Rc reject Rc is the cost of capital of the project //////////////////////////////// " .C2EXPLAIN17<-"well-indented codes are more readable //////////////////////////////// It is a good idea to indent our program that we could read it easily. See the difference between the following two examples. Well indented one ---------------- Objective : estimate present value fv formula used: pv= -------- (1+r)^n pv : present value fv : future value r : effective period rate n : number of periods Example 1: > .pv_f(0.1,1,100) # meaning of inputs depend on order [1] 90.90909 Example 2: > .pv_f(0.045,5,225) [1] 180.5515 Example 3: > .pv_f(fv=100,r=0.1,n=1) # meaning depend on keywords > .pv_f(r=0.1,fv=100,n=1) > .pv_f(n=1,r=0.1,fv=100) Note: The above three give the same result Well indented one ---------------- Objective: estimate present value FV formula used: pv= -------- (1+r)^n pv : present value fv : future value r : effective period rate n : number of periods Example 1: > .pv_f(0.1,1,100) # meaning of inputs depend on order [1] 90.90909 Example 2: > .pv_f(0.045,5,225) [1] 180.5515 Example 3: > .pv_f(fv=100,r=0.1,n=1) # meaning depend on keywords > .pv_f(r=0.1,fv=100,n=1) > .pv_f(n=1,r=0.1,fv=100) Note: The above three give the same result //////////////////////////////// " .C2EXPLAIN18<-"total risk vs. standard deviation /////////////////////////////////////////////// To estimate standard deviation for n observations, we have two formulae: 1) for a sample: (x1-mean)^2 + (x2-mean)^2 + ... + (xn-mean)^2 var = ------------------------------------------- n-1 std=sqrt(var) 2) for population: (x1-mean)^2 + (x2-mean)^2 + ... + (xn-mean)^2 var = ------------------------------------------- n std=sqrt(var) ////////////// " .C2EXPLAIN19<-"Videos /////////////////////////////// Boone,Ed,2010, Introduction to Functions in R (v35k,s5.7k,t6:29) https://www.youtube.com/watch?v=gl9opYcRxO8 LearnR, 2014, R Tutorial 35: Writing functions (v24k,s28k,t4:44) https://www.youtube.com/watch?v=-CTXR8qxTUI Tutorials Point (India) Pvt. Ltd., 2018, R programming -Functions (v5k,s980k,t3:00) https://www.youtube.com/watch?v=Vl9gSqi71Cs ///////////////////////////////// " .C2EXPLAIN20<-"Links /////////////////////////////// R examples http://www.rexamples.com/ R Programming Examples https://www.programiz.com/r-programming/examples ///////////////////////////////// "