1 Workshop 2- Challenge 1 Solution

Regarding Challenge 1 of previous workshop, you have to find a peer and share your attempt. Improve your attempt with this feedback. If you solved the problem with an analytic formula, change the solution writing a loop.

Now modify your solution and write your code as a function. As any function, you need inputs, which are also called parameters of the function. Inside your function you need to process the inputs and then end up with an output or a set of outputs as results. It is said that each function also **returns* the values of the output.

For this challenge the inputs are:

Loan amount

Annual percentage rate

Frequency of annual compounding (we set as 12 a year, since we used months)

Fixed payment per period

The outputs have to be:

Number of periods needed to fully pay the loan

The amount of the last payment

Once you finish your function, test it with at least 2 different set of inputs, run your function and display your results.

1.1 COMPARISON WITH MATE

Challenge 1 INPUT VECTORS

PV <- 6000000 MFP <- 50000 i <- 0.12/12 CALCULATE TIME. I calculated the time manually at first to see if my programming was correct

t <- -log(1-(PV*i/MFP))/log(1+i) t

First I rewrite the inputs to make it run only this chunk when needed to change it.

PV <- 4000000 MFP <- 50000 i <- 0.12/12 EB <- PV T <- 0

Now I use while to create a loop as if I was making an amortization chart and get what I want. 

while (EB > 0) { First start counting the months T <- T+1 Then calculate interest I <- EB*i Then the payment(principal) of the month Pay <- MFP - I I designate my new ending balance EB <- EB - Pay I calculate the Principal paid of the month PPaid <- Pay + EB } print(paste( “It will take”, T, “months to pay and your last payment will be”, PPaid)) [1] “It will take 162 months to pay and your last payment will be 37035.3823180185” Challenge 2 COUPON

C <-5000000*.11/2 C [1] 275000 WITH ANNUAL INTEREST RATE = 8%

AR <- 0.08 exponentsb <- seq(1,40) PVbond <- C / (1 + AR)^exponentsb PVb<-sum(PVbond) cat(“The Price of the Bond is”, PVb) The Price of the Bond is 3279269 WITH ANNUAL INTEREST RATE = 11%

AR <- 0.11 exponentsb <- seq(1,40) PVbond <- C / (1 + AR)^exponentsb PVb<-sum(PVbond) cat(“The Price of the Bond is”, PVb) The Price of the Bond is 2461539 WITH ANNUAL INTEREST RATE = 13%

AR <- 0.13 exponentsb <- seq(1,40) PVbond <- C / (1 + AR)^exponentsb PVb<-sum(PVbond) cat(“The Price of the Bond is”, PVb) The Price of the Bond is 2099453

 Challenges about Bond Valuation

Include - General approach (in CAPITAL LETTERS) - Sequence of steps needed

2 Challenge 1

An investor purchasing a special Bonus from the UK Government is entitled to receive annual payments from the Government forever. What is the price of the bonus that pays $1,500 US annually if the next payment occurs one year from today? The market interest rate is 12%

2.1 General Approach

To calculate the price we would need to use the present value formula, in which we divide the payment between the rate used, un this case it should be 1500/0.12

2.2 Steps needed

# To start we should debine the variables 
payment<- 1500
rate<- 0.12
# Once we define , we need to compute the operation
present_value<- payment/rate
#Finally we add the output sentence
cat("The price of the special Bonus is","$", present_value)
## The price of the special Bonus is $ 12500

3 Challenge 2

Find the bond value of the following 2 exercises:

The $1,000 face value if bond XYZ has a coupon rate of 6%, with interest paid semi-annually, and matures in 5 years. If the bond is priced to yield 8%, what is the bond’s value today?

The ABC bond has a 8% coupon rate (with interest paid semi-annually), a maturity value of $1,000, and matures in 5 years. If the bond is priced to yield 6%, what is the bond’s current price?

You have to write one FUNCTION to solve these 2 problems.

3.1 General approach

To calculate the bond value of this 2 bonds, we need to use 2 formulas, the Annuity:

A=(c/i)*[1-(1/((1+i)^n)]

and the present value of a future value

PV= FV/((1+i)^n)

Once we get the result of this 2, we need to sum the result, and that would provide us the bond value

3.2 Steps needed

#We start by providing our function, in which we need to use the future value (1000,1000), time (5,5), yield (8%, 6%), coupon rate (6%, 8%), and the number of periods( in this case is semi annually, this represents 2 capitalizations per year) 
bond_value <- function(FV,t,Y,i,n){

# We start with the annuity formula to get the present value of the cash flows 
      #  A=(c/i)*[1-(1/((1+i)^n)]
  # We calculate first of all the fixed payment
  
  FP<- i/(n*FV)
  # with the fixed payment, we calculate the present value of the cash flows
  
  PV_cashF <- FP/(Y/n)*(1-(1/((1+(Y/n))^(t*n))))
  
# Now we use the second formula to calculate the present value of a future value
      #  PV= FV/((1+i)^n)
  
  PV_FutVal <- FV/((1+(Y/n))^(t*n))

# Finally, as I mentioned, I need to sum both outputs
  
  return(PV_cashF+PV_FutVal)
  
}
# For the XYZ bond:
  #The $1,000 face value if bond XYZ has a coupon rate of 6%, with interest paid semi-annually, and matures in 5 years. If the bond is priced to yield 8%, what is the bond’s value today?

cat("The value of the XYZ bond is", bond_value(FV=1000, t=5, Y=0.08, i=0.06, n=2))
## The value of the XYZ bond is 675.5644
#For the ABC bond:
  #The ABC bond has a 8% coupon rate (with interest paid semi-annually), a maturity value of $1,000, and matures in 5 years. If the bond is priced to yield 6%, what is the bond’s current price?

cat("The value of the ABC bond is", bond_value(FV=1000, t=5, Y=0.06, i=0.08, n=2))
## The value of the ABC bond is 744.0943

4 Challenge 3

The HIJ bond has a current price of $800, a maturity value of $1,000, and matures in 5 years. If interest is paid semi-annually and the bond is priced to yield 8%, what is the bond’s annual coupon rate?

You have to write a function to solve this problem.

4.1 General Approach

We need to get the coupon rate from the bond price formula

BP= (iFV/(Y/n))[1-(1/(1+(Y/n)^(t*n))]+ (FV/(1+(Y/n)^(t*n)))

This takes us to

i= (BP- (FV/(1+(Y/n)^(tn))))(Y/n)/[1-(1/(1+(Y/n)^(t*n))]*FV

4.2 Steps needed

#Now let's start by generating our function
coupon_rate <- function(BP,FV,t,Y,n) {

# We use the formula we get to obtain the coupon rate
  
 cr<- (BP- (FV/(1+(Y/n)^(t*n))))*(Y/n)/(FV*((1-(1/(1+(Y/n)^(t*n))))))
  
 # Finally we multiply the coupon rate times the periodicity which is 2 cause it's semiannually
 
 return(cr*n)
}
# Now we put the output as we want it

cat("The coupon rate of the HIJ bond is", coupon_rate(BP =800,FV=1000,t=5,Y=0.08,n=2),"%")
## The coupon rate of the HIJ bond is -1.53314e+12 %
# watch out, the result is not correct

4.3 Challenge 4

You are planning to save for retirement over the next 30 years. To do this, you will invest $6,000 a month in a stock account and $3,000 a month in a bond account. The return of the stock account is expected to be 12% compounded monthly, and the bond account will pay 5% annual interest rate also compounded monthly. When you retire, you will combine your money into an account with a 9% annual return. How much can you withdraw each month from your account assuming a 25-year withdrawal period? At the end of 25 years your balance must be zero.

Hint: check if you can use the functions you wrote in the previous challenges to solve this challenge.

4.3.1 General Approach

To get our output we may need to calculate the present value

A=(c/i)*[1-(1/((1+i)^n)]

and later on get the future value from this formula

PV= FV/((1+i)^n)

Meaning,

FV=PV*((1+i)^n)

4.3.2 Steps needed

#We should start with the present value
# We formulate our function
pv_retirement <- function(c,t,i,n) {
  
  pv <- c/(i/n) * (1 - 1/(1+i/n)^(t*n))
  return(pv)
}
# I change according to the values I got for the investments

stock <- pv_retirement(c=6000,t=30,i=0.12,n=12) 
bond <- pv_retirement(c=3000,t=30,i=0.05,n=12)

# We get the future value according to the formula previously mentioned

    #  FV=PV*((1+i)^n)

stock_2 <- stock*(1+0.12/12)^(30*12)
bond_2 <- bond*(1+0.05/12)^(30*12)

# Sum both
balance <- stock_2 + bond_2

cat("The balance of 30 years would be", "$",balance)
## The balance of 30 years would be $ 23466561
# Now I get the monthly withdrawal
i_account<- 0.09
wtdwl<- balance*(i_account/12)/(1-(1/(1+i_account/12)^(25*12)))
cat("the monthly withdrawal shoul be",wtdwl)
## the monthly withdrawal shoul be 196930.5