Google Ad sense



All source codes in this blog are also available in Gregory Choi's Github Source code Repository


  

Saturday, March 19, 2016

How to calculate annuity value in R (Mortgage) & plot

[Question]

I want to buy a new house, but I don't have enough money. I decide to take out the loan from the bank. The house that I want to buy is worth $150,000. Currently, the bank offers the loan with 12% APR monthly compounded. I want to pay the mortgage for 30 years. What would be the monthly payment?

Plus, show the monthly interest payment and principal payment as time goes by.

[Answer]
We are going to solve this problem with annuity formula. And then, we are going to make a table for each payment period. Be sure that APR should be divided by 12 as it is a monthly payment.


Picture is from http://www.financeformulas.net/ 

[Code]

#Mortgage & Annuity
#Let's suppose that you want to buy a house that is worth $150,000
#Current APR rate is 12%, monthly compounded. (monthly rate = 1%)
#You want to take out the loan for 30 years (360 months)
#What would be the monthly payment?
#What would be the table that describes monthly interest payment & principal payment?

principal <- 150000
period <- 12 * 30 #30 years * 12 months
#Getting monthly rate
rate <- (0.12 / 12)
#Getting monthly payment with annuity formula
payment <- principal / ((1-(1+rate)^(-period))/rate)

#In this situation payment would be 1542.92
payment

#Let's build a table
#I use the sequance (1:360), meaning that please make a collection that has the value from 1 to 360.
mortgage <- data.frame(period=(1:360), balance=150000, payment=payment, interest_pmt=(1:360), principal=(1:360))

for(i in 1:360) {
  if(i==1) {
    #First payment
    mortgage$interest_pmt[i] <- principal * rate #Interest payment
    #Payment - Interest payment = principal amount that we can pay back this month
    mortgage$principal[i] <- mortgage$payment[i] - mortgage$interest_pmt[i]
    mortgage$balance[i] <- mortgage$balance[i] - mortgage$principal[i]
  } else {
    #After second payment
    mortgage$interest_pmt[i] <- mortgage$balance[(i-1)] * rate
    mortgage$principal[i] <- mortgage$payment[i] - mortgage$interest_pmt[i]
    mortgage$balance[i] <- mortgage$balance[i-1] - mortgage$principal[i]  
  }
}

#In R, there is a predefined palette. "rainbow" is the most usual one. You don't need to think about which color should be used.
myColor <- rainbow(2)
plot(mortgage$period, mortgage$interest_pmt, xlab = "Period", ylab="Cash Flow", col=myColor[1], type="l")
#Using command "lines" is the easiest way to add one more line on the existing graph.
lines(mortgage$period, mortgage$principal, col=myColor[2], type="l")
#We are going to add legend
legend(1,1200,
       #X, #Y position of the legend. The higher Y value, the higher position in the monitor.
       c("Interest Payment", "Principal payment"),
       lty=c(1,1), #As this is a line graph, we are going to use line as a symbol
       lwd=c(1,1), #Thickness of the line
       col=myColor, #color
       cex=0.6 #If it is 1.0 it's too big. Basically it is a scale factor
       )

[Result]
Monthly payment: $1542.919

#head() command allows you to have a peek at data frame if it is really large.
> head(mortgage)
  period  balance  payment interest_pmt principal
1      1 149957.1 1542.919     1500.000  42.91890
2      2 149913.7 1542.919     1499.571  43.34808
3      3 149870.0 1542.919     1499.137  43.78157
4      4 149825.7 1542.919     1498.700  44.21938
5      5 149781.1 1542.919     1498.257  44.66157
6      6 149736.0 1542.919     1497.811  45.10819

Graph

No comments:

Post a Comment