Skip to content

Instantly share code, notes, and snippets.

@alexchinco
Last active September 7, 2016 15:47
Show Gist options
  • Save alexchinco/d7e3e2efa3bf158a8e30ae8fb950589f to your computer and use it in GitHub Desktop.
Save alexchinco/d7e3e2efa3bf158a8e30ae8fb950589f to your computer and use it in GitHub Desktop.
Mortgage Math
function Amortization(InitialBalance,R,T,Output){
var Interest = [];
var Principal = [];
var Balance = [InitialBalance];
var A = (1/R) * (1 - 1/((1+R)**T));
var Pmt = InitialBalance/A;
for(var t = 1; t <= T; t++){
Interest.push(Balance[t-1]*R)
Principal.push(Pmt - Balance[t-1]*R)
Balance.push(Balance[t-1] - Principal[t-1])
}
if (Output == 'InterestPayment') { return Interest }
if (Output == 'PrincipalPayment') { return Principal }
if (Output == 'RemainingBalance') { return Balance }
}
/// ################################
/// ################################
var InitialBalance = 500; ///++InitialBalance:++ **Amount you borrow** from the bank (in $1000s).
var R = 0.06 / 12; ///++R:++ Monthly **mortgage rate**.
var T = 30 * 12; ///++T:++ **How long** it takes **until you to pay off principal** you owe if you keep on making the **same monthly payments**? a.k.a., amortization period.
/// ################################
/// ################################
var A = (1/R) * (1 - 1/((1+R)**T)); ///++A:++ **Present value** of a **T-month $1 annuity** if the **monthly interest rate is R**.
var MonthlyPayment = InitialBalance / A; ///++MonthlyPayment:++ **Amount you pay** each month (in $1000s) for **next T months** if you **borrow InitialBalance** from the bank when the monthly **mortgage rate is R**.
/// ################################
///################################
var RemainingBalance = Amortization(InitialBalance,R,T,'RemainingBalance'); ///++RemainingBalance:++ **Amount of money** (in $1000s) that **you still owe** the bank **after month t payment**. Note that InitialBalance = RemainingBalance in month t=0.
var InterestPayment = Amortization(InitialBalance,R,T,'InterestPayment'); ///++InterestPayment:++ **Part of payment** in month t (in $1000s) **that does not affect remaining balance**.
var PrincipalPayment = Amortization(InitialBalance,R,T,'PrincipalPayment'); ///++PrincipalPayment:++ **Part of payment** in month t (in $1000s) that goes towards** lowering your remaining balance**.
function AnnuityPayments(T){
var AnnuityPayments = [];
for(var t = 1; t <= T; t++){
AnnuityPayments.push(1)
}
return AnnuityPayments
}
function PresentValue(Payments,R){
var PaymentsPV = [];
for(var t = 1; t <= T; t++){
PaymentsPV.push(Payments[t-1]/(1+R)**t)
}
return PaymentsPV
}
function Sum(PaymentsPV){
var SumOfPaymentsPV = 0;
var T = PaymentsPV.length;
for(var t = 0; t < T; t++) {
SumOfPaymentsPV += PaymentsPV[t];
}
return SumOfPaymentsPV
}
/// ################################
/// ################################
var R = 0.06 / 12; ///++R:++ Monthly **mortgage rate**.
var T = 30 * 12; ///++T:++ **How long** it takes **until you to pay off principal** you owe if you keep on making the **same monthly payments**? a.k.a., amortization period.
var AnnuityPayments = AnnuityPayments(T); ///++AnnuityPayments:++ Sequence of **$1 payments** for each of the **next **_**T**_** months**.
var AnnuityPaymentsPV = PresentValue(AnnuityPayments,R); ///++AnnuityPaymentsPV:++ The **present value** of each of the **$1 annuity payments** from month t=1 to month t=T.
/// ################################
/// ################################
Sum(AnnuityPaymentsPV); ///**Sum** of the **present value** of **$1 annuity payments** over **next T months**.
var A = (1/R) * (1 - 1/((1+R)**T)); ///++A:++ **Present value** of a **T-month $1 annuity** if the **monthly interest rate is R** computed using the annuity formula. Better be **equal to sum of present value of annuity payments**!!!
var InitialBalance = 500; ///++InitialBalance:++ **Amount you borrow** from the bank (in $1000s).
var R = 0.06 / 12; ///++R:++ Monthly **mortgage rate**.
var T = 30 * 12; ///++T:++ **How long** it takes **until you to pay off principal** you owe if you keep on making the **same monthly payments**? a.k.a., amortization period.
/// ################################
/// ################################
var A = (1/R) * (1 - 1/((1+R)**T)); ///++A:++ **Present value** of a **T-month $1 annuity** if the **monthly interest rate is R**.
/// ################################
/// ################################
var MonthlyPayment = InitialBalance / A; ///++MonthlyPayment:++ **Amount you pay** each month (in $1000s) for **next T months** if you **borrow InitialBalance** from the bank when the monthly **mortgage rate is R**.
var MonthlyPaymentsPV = MonthlyPayment * A; ///++MonthlyPaymentsPV:++ **Present value** of your **monthly payments** for the **next T months** if the **mortgage rate is R** (in $1000s). Better be **same as InitialBalance**, or else someone screwed up!!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment