Created
July 21, 2011 00:12
-
-
Save noahhl/1096230 to your computer and use it in GitHub Desktop.
Fun with mortgage amortizations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Credit due to Thomas Girke ("http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/My_R_Scripts/mortgage.R") for original implementation of mortgage() | |
| # Definitions: | |
| # P = principal, the initial amount of the loan | |
| # I = annual interest rate | |
| # L = length of the loan in years, or at least the length over which the loan is amortized. | |
| # to use, run it as graphs <- amortizationScenarios(P, I, L), then graphs[1], graphs[2] | |
| amortizationScenarios <- function(P=500000, I = 6, L=30) { | |
| library(ggplot2) | |
| mortgage <- function(P=500000, I=6, L=30, extra = 0) { | |
| J <- I/(12 * 100) | |
| N <- 12 * L | |
| M <- (P*J/(1-(1+J)^(-N))) * (1 + extra/12) | |
| monthPay <<- M | |
| # Calculate Amortization for each Month | |
| Pt <- P # current principal or amount of the loan | |
| currP <- NULL | |
| while(Pt>=0) { | |
| H <- Pt * J # this is the current monthly interest | |
| C <- M - H # this is your monthly payment minus your monthly interest, so it is the amount of principal you pay for that month | |
| Q <- Pt - C # this is the new balance of your principal of your loan | |
| Pt <- Q # sets P equal to Q and goes back to step 1. The loop continues until the value Q (and hence P) goes to zero | |
| currP <- c(currP, Pt) | |
| } | |
| monthP <- c(P, currP[1:(length(currP)-1)])-currP | |
| aDFmonth <<- data.frame( | |
| Amortization=c(P, currP[1:(length(currP)-1)]), | |
| Monthly_Payment=monthP+c((monthPay-monthP)[1:(length(monthP)-1)],0), | |
| Monthly_Principal=monthP, | |
| Monthly_Interest=c((monthPay-monthP)[1:(length(monthP)-1)],0), | |
| Year=sort(rep(1:ceiling(N/12), 12))[1:length(monthP)] | |
| ) | |
| aDFyear <- data.frame( | |
| Amortization=tapply(aDFmonth$Amortization, aDFmonth$Year, max), | |
| Annual_Payment=tapply(aDFmonth$Monthly_Payment, aDFmonth$Year, sum), | |
| Annual_Principal=tapply(aDFmonth$Monthly_Principal, aDFmonth$Year, sum), | |
| Annual_Interest=tapply(aDFmonth$Monthly_Interest, aDFmonth$Year, sum), | |
| Year=as.vector(na.omit(unique(aDFmonth$Year))) | |
| ) | |
| aDFyear <<- aDFyear | |
| } | |
| alternatives = list() | |
| extraInterest <- c() | |
| amortizationCurves <- data.frame(stringsAsFactors=F) | |
| for(extra in 0:12) { | |
| mortgage(P, I, L, extra) | |
| alternatives[[extra+1]] <- aDFmonth | |
| cat("With", extra, "payments per year:\n") | |
| cat("Average monthly payment over life of loan: ", mean(aDFmonth$Monthly_Payment), "\n") | |
| cat("Months to payoff: ", nrow(aDFmonth), "\n") | |
| cat("Total payment vs. original principal:", sum(aDFmonth$Monthly_Payment)/aDFmonth$Amortization[1], "\n\n\n\n") | |
| extraInterest[extra+1] <- sum(aDFmonth$Monthly_Payment)/aDFmonth$Amortization[1] | |
| amortizationCurves <- rbind(amortizationCurves, data.frame(extra = extra, month = as.numeric(row.names(aDFmonth)), principal = aDFmonth$Amortization, stringsAsFactors=F)) | |
| } | |
| a <- ggplot(data.frame(extraPayments=0:12, premium = extraInterest), aes(x=extraPayments, y=premium, group=1)) + geom_line(colour='steelblue', size=I(2)) + opts(title="Total amount paid relative to original principal") + ylab("Total payments relative to original loan amount") + xlab("Number of extra payments made annually") | |
| b <- ggplot(amortizationCurves, aes(x=month, y=principal, group = extra)) + geom_line(aes(colour=extra)) + xlab("Months since origination") + ylab("Principal outstanding") + opts(title="Mortgage payoff relative to extra payments made") + scale_colour_continuous(name="Number of extra payments") | |
| return(list(a,b)) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment