Abstract
In this workshop we will learn about a) the List data structure, b) the xts class, c) subsetting dataset, and d) control-flow commands such as conditionals and loops.You will work in RStudio. Create an R Notebook document to write whatever is asked in this workshop.
You have to replicate all the steps explained in this workshop, and ALSO you have to do whatever is asked. Any QUESTION or any STEP you need to do will be written in CAPITAL LETTERS. For ANY QUESTION, you have to RESPOND IN CAPITAL LETTERS right after the question.
At the end you have to solve 2 CHALLENGE exercises.
It is STRONGLY RECOMMENDED that you write your OWN NOTES as if this were your notebook. Your own workshop/notebook will be very helpful for your further study.
Keep saving your .Rmd file, and ONLY SUBMIT the .html version of your .Rmd file.
Lists are R objects that can store elements of different
classes, oposite to vectors which can be used to store elements of the
same class
only. Each element of a list can be of any
object class such as data frames, numeric vectors, matrices or even
list. For example, we can create a list to store information of a
financial portfolio.
I will create a list that represents information of a financial portfolio. I first create the elements of the portfolio:
# Create vectors for stock tickers and expected stock returns:
<- c("AAPL","AMZN","WMT")
tickers <- c(0.020,0.015,0.010)
expected_stock_returns <- c(0.04,0.045,0.02) expected_stock_risk
Now I will indicate how much (in %) I will allocate for each asset in my portfolio. I will assign 50% to Apple, and 25% to Amazon and Wal-Mart. I create a vector with these weights (in decimal):
<- c(0.50,0.25,0.25) stock_weights
Now I will calculate the expected portfolio return as a weighted average the stock expected returns:
<- expected_stock_returns*stock_weights weighted_returns
When you multiply 2 vectors you get an element-by-element multiplication. This is called a “vectorization” operation. By default R “vectorizes” mathematical operations. This means the first element of the first vector is multiplied by the first element of the second vector. The same for the 2nd. and 3rd. elements of each vector.
Now I just sum all the elements of this vector that contains the weighted returns to get the expected return of my portfolio:
<- sum(weighted_returns) expected_portfolio_return
I used the function sum to the weighted_returns vector, so each element of the vector is added to get the expected portfolio return.
I now create a list with all these pieces of information of my
portfolio. I use the function list()
to create the
portfolio object as a list that contains the tickers vector, and the
vectors for expected stock return, expected stock risk, stock weights
and the expected portfolio return:
<- list(tickers, expected_stock_returns, expected_stock_risk,
portfolio1
stock_weights, expected_portfolio_return)# I assign names to each element of the portfolio:
names(portfolio1)<-c("Tickers","Stock_returns","Stock_risk","Stock_weights",
"Portfolio expected Return")
# I display the portfolio object:
portfolio1
## $Tickers
## [1] "AAPL" "AMZN" "WMT"
##
## $Stock_returns
## [1] 0.020 0.015 0.010
##
## $Stock_risk
## [1] 0.040 0.045 0.020
##
## $Stock_weights
## [1] 0.50 0.25 0.25
##
## $`Portfolio expected Return`
## [1] 0.01625
I can access the elements of the portfolio1 list using either the
name of each element, or the number of the element using double squared
brackets[[]]
:
# I display the expected stock tickers:
$Tickers portfolio1
## [1] "AAPL" "AMZN" "WMT"
# I can also display expected stock tickers using the element number and double
# brackets:
1]] portfolio1[[
## [1] "AAPL" "AMZN" "WMT"
# I display the class and length of the first element:
class(portfolio1$Tickers)
## [1] "character"
length(portfolio1$Tickers)
## [1] 3
R has different data classes (or R objects). Actually, you
can create your own data class, which is a set one or more data
structures. For time series datasets, which are very common in Finance,
the most popular data classes are: ts, zoo and xts. xts
stands for extensible time series class. An xts object contains a set of
columns that can be of any type (alphabetic, numeric, boolean) and it
has a time index. All xts objects are usually sorted
chronologically.
The index of an xts object is like the first column of the object, but it is actually its index; it is not considered a column of the object. An xts object has the advantage that we can apply several functions to that object for data merging, sorting, selecting, etc.
Try the following example. Download a sample dataset called edhec, which comes with the PerformanceAnalytics package. In R, we can use thousands of available packages to do different types of calculations and operations. PerformanceAnalytics package is a package with many functions related to Financial analytics. We will use some of these functions later in the course.
we need the PerformanceAnalytics package, so you can install it from the Package menu (right-bottom of your RStudio).
Once the package is installed in my computer, I do not need to re-install it again. However, each time I need to use the package, I need to load it into memory:
library(PerformanceAnalytics)
I download the sample dataset edhec, which contains monthly returns of different hedge fund indexes created by the EDHEC Risk Institute.
data("edhec")
The edhec dataset will appear in my Global Environment.
edhec is an xts R object that contains historical monthly returns of different hedge fund indexes. We can easily do different calculations with specific functions that apply to xts objects. For now, we will learn how to do subsetting with xts objects.
If you want to know more functions for the xts R objects, you can check the xts CheatSheet at: https://s3.amazonaws.com/assets.datacamp.com/blog_assets/xts_Cheat_Sheet_R.pdf
R has powerful indexing features for accessing object elements. These features can be used to select and exclude columns and rows (observations). In the next paragraphs we will learn how to extract the information we want from an xts object. Several of these functions also apply to data frames.
I see the structure of the edhec R dataset and the first and last rows:
str(edhec)
## An 'xts' object on 1997-01-31/2019-11-30 containing:
## Data: num [1:275, 1:13] 0.0119 0.0123 0.0078 0.0086 0.0156 0.0212 0.0193 0.0134 0.0122 0.01 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:13] "Convertible Arbitrage" "CTA Global" "Distressed Securities" "Emerging Markets" ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
head(edhec)
## Convertible Arbitrage CTA Global Distressed Securities
## 1997-01-31 0.0119 0.0393 0.0178
## 1997-02-28 0.0123 0.0298 0.0122
## 1997-03-31 0.0078 -0.0021 -0.0012
## 1997-04-30 0.0086 -0.0170 0.0030
## 1997-05-31 0.0156 -0.0015 0.0233
## 1997-06-30 0.0212 0.0085 0.0217
## Emerging Markets Equity Market Neutral Event Driven
## 1997-01-31 0.0791 0.0189 0.0213
## 1997-02-28 0.0525 0.0101 0.0084
## 1997-03-31 -0.0120 0.0016 -0.0023
## 1997-04-30 0.0119 0.0119 -0.0005
## 1997-05-31 0.0315 0.0189 0.0346
## 1997-06-30 0.0581 0.0165 0.0258
## Fixed Income Arbitrage Global Macro Long/Short Equity
## 1997-01-31 0.0191 0.0573 0.0281
## 1997-02-28 0.0122 0.0175 -0.0006
## 1997-03-31 0.0109 -0.0119 -0.0084
## 1997-04-30 0.0130 0.0172 0.0084
## 1997-05-31 0.0118 0.0108 0.0394
## 1997-06-30 0.0108 0.0218 0.0223
## Merger Arbitrage Relative Value Short Selling Funds of Funds
## 1997-01-31 0.0150 0.0180 -0.0166 0.0317
## 1997-02-28 0.0034 0.0118 0.0426 0.0106
## 1997-03-31 0.0060 0.0010 0.0778 -0.0077
## 1997-04-30 -0.0001 0.0122 -0.0129 0.0009
## 1997-05-31 0.0197 0.0173 -0.0737 0.0275
## 1997-06-30 0.0231 0.0198 -0.0065 0.0225
tail(edhec)
## Convertible Arbitrage CTA Global Distressed Securities
## 2019-06-30 0.0089 0.0240 0.0075
## 2019-07-31 0.0032 0.0206 -0.0032
## 2019-08-31 0.0019 0.0300 -0.0089
## 2019-09-30 0.0023 -0.0273 -0.0022
## 2019-10-31 0.0032 -0.0204 -0.0033
## 2019-11-30 0.0060 0.0058 -0.0043
## Emerging Markets Equity Market Neutral Event Driven
## 2019-06-30 0.0315 0.0038 0.0158
## 2019-07-31 0.0044 0.0029 0.0028
## 2019-08-31 -0.0348 0.0009 -0.0139
## 2019-09-30 0.0076 -0.0027 0.0008
## 2019-10-31 0.0204 -0.0014 0.0027
## 2019-11-30 -0.0008 0.0004 0.0090
## Fixed Income Arbitrage Global Macro Long/Short Equity
## 2019-06-30 0.0008 0.0221 0.0193
## 2019-07-31 0.0031 0.0065 0.0046
## 2019-08-31 -0.0033 0.0126 -0.0100
## 2019-09-30 0.0028 -0.0064 -0.0007
## 2019-10-31 0.0034 -0.0056 0.0104
## 2019-11-30 0.0041 -0.0017 0.0138
## Merger Arbitrage Relative Value Short Selling Funds of Funds
## 2019-06-30 0.0044 0.0075 -0.0073 0.0137
## 2019-07-31 0.0057 0.0039 -0.0033 0.0037
## 2019-08-31 -0.0003 -0.0031 0.0004 -0.0063
## 2019-09-30 0.0037 0.0054 -0.0035 -0.0033
## 2019-10-31 0.0052 0.0012 -0.0028 0.0035
## 2019-11-30 0.0053 0.0068 -0.0140 0.0071
From the results above, we see that this xts object has 13 columns, which are returns of different hedge fund indexes.
I can subset either rows, columns or both. For example I can select in which months the Emeging market fund has had negative returns:
<- edhec[edhec$`Emerging Markets`<0,4]
negative_returns_em # I display the first rows of this new object, which has only negative returns for Emerging Markets:
head(negative_returns_em)
## Emerging Markets
## 1997-03-31 -0.0120
## 1997-08-31 -0.0066
## 1997-10-31 -0.0572
## 1997-11-30 -0.0378
## 1998-01-31 -0.0429
## 1998-05-31 -0.0825
In this case I selected only rows that have negative monthly returns for Emerging Markets, and also I am selecting only the column #4, which is the Emerging Markets column. I use the currency sign $ to refer to a specific column of the xts object.
If I want to do a subsetting only of rows and keep ALL columns, I can do the following:
<- edhec[edhec$`Emerging Markets`<0,]
negative_returns_em_all head(negative_returns_em_all)
## Convertible Arbitrage CTA Global Distressed Securities
## 1997-03-31 0.0078 -0.0021 -0.0012
## 1997-08-31 0.0134 -0.0473 0.0147
## 1997-10-31 0.0100 -0.0098 -0.0064
## 1997-11-30 0.0000 0.0133 0.0054
## 1998-01-31 0.0145 0.0104 0.0095
## 1998-05-31 0.0056 0.0193 0.0006
## Emerging Markets Equity Market Neutral Event Driven
## 1997-03-31 -0.0120 0.0016 -0.0023
## 1997-08-31 -0.0066 0.0017 0.0071
## 1997-10-31 -0.0572 0.0095 0.0061
## 1997-11-30 -0.0378 0.0041 0.0134
## 1998-01-31 -0.0429 0.0060 0.0055
## 1998-05-31 -0.0825 0.0080 -0.0083
## Fixed Income Arbitrage Global Macro Long/Short Equity
## 1997-03-31 0.0109 -0.0119 -0.0084
## 1997-08-31 0.0087 -0.0180 0.0107
## 1997-10-31 -0.0032 -0.0142 0.0010
## 1997-11-30 0.0053 0.0106 -0.0026
## 1998-01-31 -0.0026 -0.0050 0.0013
## 1998-05-31 0.0040 0.0095 -0.0087
## Merger Arbitrage Relative Value Short Selling Funds of Funds
## 1997-03-31 0.0060 0.0010 0.0778 -0.0077
## 1997-08-31 0.0079 0.0103 -0.0072 0.0051
## 1997-10-31 0.0094 0.0079 0.0572 -0.0099
## 1997-11-30 0.0223 0.0111 0.0217 -0.0034
## 1998-01-31 0.0055 0.0132 0.0014 -0.0036
## 1998-05-31 -0.0009 0.0053 0.1437 -0.0072
As you see, to do subsetting I use squared brackets afher the object, and the first number refers to the ROWS to be selected, and the second parameter refers to the COLUMNS to be selected. In this case, after the coma I wrote NOTHING, indicating that I want ALL COLUMNS!
If I want to do a subsetting of specific rows using a range of dates, I can do the following. Since this is an xts object, it is very easy to subset using dates. I select the returns of the 12 months of the year 2008:
<-edhec["2008-01-01/2008-12-31",] edhec2008
I can also select those months where the Emerging Markets fund and the Convertible Arbitrage fund had positive returns in 2008:
<-edhec2008[edhec2008$`Convertible Arbitrage`>0 & edhec2008$`Emerging Markets`>0,]
positive_edhec2008 positive_edhec2008
## Convertible Arbitrage CTA Global Distressed Securities
## 2008-04-30 0.0076 -0.0078 0.0088
## 2008-05-31 0.0107 0.0162 0.0137
## Emerging Markets Equity Market Neutral Event Driven
## 2008-04-30 0.0190 0.0059 0.0118
## 2008-05-31 0.0163 0.0126 0.0176
## Fixed Income Arbitrage Global Macro Long/Short Equity
## 2008-04-30 0.0187 0.0078 0.0223
## 2008-05-31 0.0103 0.0114 0.0227
## Merger Arbitrage Relative Value Short Selling Funds of Funds
## 2008-04-30 0.0149 0.0130 -0.0461 0.0097
## 2008-05-31 0.0136 0.0159 -0.0142 0.0172
We can see that only 2 months had positive returns in these funds in 2008.
There are several logical operators to be used for the subsetting condition of rows. These operators are:
Control-flow commands or constructs are used to program a series of tasks using conditions and/or loops.
There are 7 main control structures:
The most simple ones are the conditionals if()
and
if/else
, which allow you to implement some basic algorithm
logic in your analysis. We will start with the conditionals statements
and then the loop statements.
The structure of an if
looks like the one below and
should be read as if this is TRUE, then do this …
if(<condition1>) {
## do stuff
}
Here is a simple example, that is self-explanatory:
<- 70
grade
if(grade > 69){
cat("You have passed this course. Well done!")
}
## You have passed this course. Well done!
In this case the condition will be true, so the message will be displayed. You can try different values for grade and re-run this code to see what happens.
Imagine that you have several conditions, not only one as the previous example. In this case, you need to use the else if statement. The structure looks like this:
if(<condition1>) {
## do something
else if(<condition2>) {
} ## do something different
}
So our previous example can be upgraded as follows. Try chaging the values in the input variables.
<- 80
gradePartial1 <- 60
gradePartial2 <- 100
gradeFinalExam
<- (gradePartial1*0.25 + gradePartial1*0.25 + gradeFinalExam*0.5)
FinalGrade
if(FinalGrade > 90){
"You did an Excellent job!"
else if(FinalGrade > 69){
} "You have passed this course. Well done."
else if(FinalGrade > 0){
} "Sorry you have failed the course."
}
## [1] "You have passed this course. Well done."
Now imagine that we have several conditionals but we want to do something with the remaining variables that don’t fulfill the previous conditionals, then we use else. The structure looks like:
if(<condition1>) {
## do something
else if(<condition2>) {
} ## do something different
else{
} ## do something different
}
Our example can be changed as follows to deal with negative grades.
<- -70
gradePartial1 <- 60
gradePartial2 <- -60
gradeFinalExam
<- (gradePartial1*0.25 + gradePartial1*0.25 + gradeFinalExam*0.5)
FinalGrade
if(FinalGrade > 90){
"You did awesome!"
else if(FinalGrade > 69){
} "You have passed this course. Well done."
else if(FinalGrade > 0){
} "Sorry you have failed the course."
else {
} "You have negative grades; it should be an error"
}
## [1] "You have negative grades; it should be an error"
In the next example we use the operator %in%
. This
operator checks whether a value exists inside a vector. For each element
of the vector it returns TRUE if the value is contained in the verctor
and 0 otherwise:
# Both elements in the first vector appears in the second vector
c(1,2) %in% c(6,4,8,3,2,1)
## [1] TRUE TRUE
# Only the last two elements of the first verctor appear in the second
c(6,4,8,3,2,1) %in% c(1,2)
## [1] FALSE FALSE FALSE FALSE TRUE TRUE
As you see, the result of using the %in% operator is a vector of Boolean values (TRUE OR FALSE). If the first element is TRUE, it means that the first number of the first vector is inside the second vector.
The for()
control statement is used to repeat certain
tasks a specific number of times. We can loop over a sequence of
numbers, or we can loop over specific elements of a vector. For example,
we create a simple loop that prints out the name of several
students:
<-c("Pedro","Laura","Bryan")
students
for(name in students){
##Instead of name you can write any variable just change it inside the loop
cat("Hi, my name is",name)
}
## Hi, my name is PedroHi, my name is LauraHi, my name is Bryan
In the next example we will use the quantmod
library to
download some financial data from online data sources such as Yahoo
finance and Google Finance. Our initial vector indicate the tickers to
download, in this case AAPL
,JPM
and
GE
.
You have to install the package in the Package tab of the bottom-right windows of RStudio.
Once you install the command, you have to load it with the library function. This quantmod library has the getSymbols function that is used to download online data from the web:
library(quantmod)
#Vector of tickers
<-c("AAPL","JPM","GE")
tickers
for(i in tickers){
getSymbols(i)
cat("the prices of the ticker",i,"have been downloaded")
}
## the prices of the ticker AAPL have been downloadedthe prices of the ticker JPM have been downloadedthe prices of the ticker GE have been downloaded
In the next example we do a similar process for a different ticker list. In the case of GE and AAPL we are going to download the data from yahoo, while INTGSTMXM193N from the FED. What happens with the remainning tickers?
<- c("AAPL","GM","INTGSTMXM193N","GE","JPM")
tickers
for(i in tickers){
if(i %in% c("AAPL","GE")){
getSymbols(i,src = "yahoo")
cat("the prices of the ticker",i,"have been downloaded from yahoo")
}else if(i=="INTGSTMXM193N"){
getSymbols(i,src = "FRED")
#Note that the source for the FED is called FRED
cat("the data of the ticker",i,"have been downloaded from the FED (Federal US Bank) ")
} }
## the prices of the ticker AAPL have been downloaded from yahoothe data of the ticker INTGSTMXM193N have been downloaded from the FED (Federal US Bank) the prices of the ticker GE have been downloaded from yahoo
We are going to use the previous example, but in this case we want also to add a condition to print the tickers that have NOT been downloaded.
<- c("AAPL","GM","INTGSTMXM193N","GE","JPM")
tickers
for(i in tickers){
if(i %in% c("AAPL","GE")){
getSymbols(i,src = "yahoo")
cat("the prices of the ticker",i,"have been downloaded from yahoo")
}else if(i=="INTGSTMXM193N"){
getSymbols(i,src = "FRED")
cat("the data of the ticker",i,"have been downloaded from the FED (Federal US Bank) ")
}else {
cat("The ticker",i,"has not been downloaded")
} }
## the prices of the ticker AAPL have been downloaded from yahooThe ticker GM has not been downloadedthe data of the ticker INTGSTMXM193N have been downloaded from the FED (Federal US Bank) the prices of the ticker GE have been downloaded from yahooThe ticker JPM has not been downloaded
In a similar manner than the for loop, it is possible to program a
loop with while
and repeat
control-flow
statements. The main difference is that in both you need to specify what
is the exit condition, and it may be possible that NOT all the
itereations has to be performed (like in the for case).
For the while
you need to specify the condition at the
beginning. Here an example. If you want to know how many years you need
to keep an investment with a fixed interest rate in order to duplicate
your initial investment:
<-0.10
APR# I define Annual Percentage Rate to be equal to 10%
<-100
INV# Initial investment equal to $100
<-2
MULTIPLE# Multiple = 2 to check when the investment double
<-INV
BALANCE# I start assigning the balance equal to the initial investment
<-0
year# I start with year equal to zero
while (BALANCE<MULTIPLE*INV) {
# the exit condition means that while the balance is less than the initial investment
# multiplied by the multiple, then continue with the iterations of the loop
<-year+1
year# I increase the value of year by 1
<-BALANCE*(1+APR)
BALANCE# I multiply the current balance times the growth factor (1+APR) using to the
# Annual Percentage Rate
}cat(
"To multiply your investment times ", MULTIPLE, "you need ", year, " years.")
## To multiply your investment times 2 you need 8 years.
cat(
"Your balance after ", year, " years will be $",BALANCE)
## Your balance after 8 years will be $ 214.3589
Unlike the while statement, you can use the repeat
statement to do a loop, but in the case of repeat, you have to specify
the exit condition using if and the break statement any place within the
loop. Here is the same loop we did above but using repeat:
<-0.10
APR# I define Annual Percentage Rate to be equal to 10%
<-100
INV# Initial investment equal to $100
<-2
MULTIPLE# Multiple = 2 to check when the investment double
<-INV
BALANCE# I start assigning the balance equal to the initial investment
<-0
year# I start with year equal to zero
repeat {
<-year+1
year# I increase the value of year by 1
<-BALANCE*(1+APR)
BALANCE# I multiply the current balance times the growth factor (1+APR) using to the
# Annual Percentage Rate
if (BALANCE>=MULTIPLE*INV) {
break
}# If the balance is greater than the multiple times the investment, then
# the break statement is executed, so the program stops the interations
}cat(
"To multiply your investment times ", MULTIPLE, "you need ", year, " years.")
## To multiply your investment times 2 you need 8 years.
cat(
"Your balance after ", year, " years will be $",BALANCE)
## Your balance after 8 years will be $ 214.3589
It is up to you to practice with the different types of loops. When
you have to write a loop, you first have to understand in detail what
you are looking for. Then, you have to identify the logic you need to do
the iterations and which type of calculations you need to do. It is very
recommended first write a pseudo-code
with your own words
specifying the details of your logic. The pseudo-code
is
just a list of tasks with your own words. You can use some names of
commands, but if you do not remember, you can just write what you want
to do line by line, and specify what are the itereations and conditions
of your algorithm.
Most R programmers believe that loops must be AVOIDED! This is wierd since most of the time programming requires to do repetitive tasks. However, in R, unlike other traditional computer languages such as C, there is an alternative way to do loops without writing loops!
This sounds wierd. The way you can do a loop without writing a loop is using “vectorization” in R. R can performs vectorization in a natural way. For example, if you want to do a sum of 2 vectors or matrices in a traditional language, you might think in a loop to do an element-by-element operation. Imagine you want calculate the net present value of a new project in your company. The project involves 2 new products, A and B. Each product will bring its own income. You already estimated the expected annual free cash flow (income minus all expenses) of both products for the next 5 years. The following table shows the expected free cash flow for each year for each product (in millions MXP )
Product | Year 1 | Year 2 | Year 3 | Year 4 | Year 5 |
---|---|---|---|---|---|
A | 3 | 3 | 4 | 6 | 8 |
B | -2 | -3 | -1 | 3 | 5 |
The discount rate (or cost of capital) for this project (annual) is 15%. The initial investment of the project is 10 million pesos.
How can you estimate the present value of these cash flows?
You first have to add the cash flows of each product and get an annual free cash flow for the project. You can use a loop that for each year, you do the sum of the 2 corresponding free-cash flows. Something like:
<-c(3,3,4,6,8)
A <-c(-2,-3,-1,3,5)
B # A and B are vectors with the annual free cash flows of each product
<-vector(length=5)
sumcashflowsfor (i in 1:length(A)) {
<-A[i] + B[i]
sumcashflows[i]# We sum the element i of both vectors and leave the result
# in the position i of the vector sumcashflows
} sumcashflows
## [1] 1 0 3 9 13
Now, instead of using a loop, we can use vectorization in a natural way to do the sum of both vectors:
<-c(3,3,4,6,8)
A <-c(-2,-3,-1,3,5)
B <-A+B
sumcashflows# We just sum both vectors as if they were numbers
sumcashflows
## [1] 1 0 3 9 13
# The actual operation perfored is an element-wise operation;
# it do a sum for each pair of elements of the vectors, and store the result in a vector
Now, how can you calculate the Present Value of these cash flows? You have to remember the basics of time value of money. To calculate the present value (\(PV_0\)) of a sequence of cash flows in the future, you just have to apply the following formula for the sequence of cash flows:
\[PV_{0}=CF_{0}+\frac{CF_{1}}{\left(1+R\right)^{1}}+\frac{CF_{2}}{\left(1+R\right)^{2}}+...+\frac{CF_{N}}{\left(1+R\right)^{N}}=\sum_{t=0}^{N}\left(\frac{CF_{t}}{\left(1+R\right)^{t}}\right)\]
Where R is annual discount rate, also called cost of capital when valuing projects in a company. \(CF_i\) refers to the cash flow of the year i. In our example, we do not have cash flow in the period zero (today). In sum, you have to bring all future cash flows to today, and then you sum all these cash flows to get the present value of all the expected cash flows. How can we do this in R? In a traditional computer language such as C, you usually do this using a loop. You can do the following loop in R to get the present value:
# I assign 0 the the variable PV, present value
<- 0
PV # I defined the discount rate as R:
<- 0.15
R for (i in 1:length(sumcashflows)) {
# I calculate the present value of each cash flow i:
<- sumcashflows[i] / (1 + R)^i
PVCF # I sum each corresponding cash flow i to the cumulative variable PV
<- PV + PVCF
PV
}# The loop iterates 5 times, one for each cash flow and will ends with the sum of all
cat("The Present Value of all cash flows is ", PV)
## The Present Value of all cash flows is 14.45119
How can I do the same process, but without writing a loop? I use the vectorization of R:
# I assign a sequence from 1 to 5, for the exponents of the formula of present value
# for each cash fow
<- seq(1,5)
exponents # The seq is a function that generates a sequence. Here I specified to start the
# sequence in 1 and finish in 5
# I calculate a vector with the present value for each cash flow. I use vectorization:
<- sumcashflows / (1 + R)^exponents
PVvector # Note that this mathematical expression applies to each of the element of the vector.
# Also, each vector has the same dimension.
# I finally sum all elements of the vector using the function sum:
<-sum(PVvector)
PVcat("The Present Value of all cash flows is ", PV)
## The Present Value of all cash flows is 14.45119
Go to datacamp.com site. You have to take (view and do the exercises) of the following chapters of the following courses:
Course 1: Intermediate R for Finance, complete the chapters: a) If Statement and Operators, and b) Loops.
Course 2: Bond Valuation and Analysis in R, complete the first chapter: Introduction to Plain Vanilla Bond Valuation
You will receive points for each activity you do in these chapters.
You have to write a program to calculate the number of months needed to finish paying a mortgage loan. The information about the loan is the following:
Loan amount = $3,000,000.00 pesos
APR (Annual % rate) = 11% (compounded monthly)
Monthly Fixed Payment = $40,000.00 (includes interests and capital)
Your program has to provide 2 results: the number of months needed to finish paying the loan, and the amount of the last payment if the payment is less than the fixed payment amount. Your program has to be able to run with any change in any of the values of the above variables.
This is a quite challenging exercise!
Hint: if you are familiar with Excel, start solving the problem in Excel, and then try to write your program in R.
Write a program that calculates the price of the following bond issued by the company ABC. ABC needs to finance an important project to develop a new technological device.
To get the money, ABC issued a bond with the following characteristics:
Calculate the price of this bond for each of the following annual interest rates:
You have to get the price for the bond for each of these 3 interest rates.
Remember that the price of a bond is the present value of its future cash flows.
You have to submit your .html file of this workshop through Canvas BEFORE NEXT CLASS.
The grade of this Workshop will be the following:
Remember that you have to submit your .html file of your workshop.