"Mortgage" # # All based on this formula: # # fv (1 + i) ^ -n = pv + pmt * (1 - (1 + i) ^ -n) / i; # # pv = Present Value # fv = Future Value # i = Interest Rate # n = Period # pmt = Payment # define fvf (fvf) (i, n) { fvf = (1 + i) ^ -n; } define pff (pf,fvf) (i, n) { fvf = fvf(i,n); pf = (1 - fvf(i,n)) / i; } define pf (pf) (i, n) { pf = (1 - fvf(i,n)) / i; } define pmt "Loan Payment" (pmt "Payment", cost "Interest Paid") (pv "Present Value", fv "Future Value", i "Interest", n "Period") { pmt = - (fv * fvf (i, n) + pv) / pf(i,n); cost = pmt * n + (pv - fv); } define private abs (a) (i) { if (i < 0) a = -i; else a=i; } define i "Interest Rate" (i "Interest", cost "Interest Paid") (pv "Present Value", fv "Future Value", n "Period", pmt "Payment") { local oi, r, dr, p1, p2, p3, dmort, mort; i = 0.01; do { oi = i; r = pmt * pf (i, n) - fv * fvf(i,n) + pv; p1 = i * (-n) * (1 + i) ^ (-n - 1) - (1-(1+i)^-n); p2 = pmt * p1 / (i ^ 2); p3 = -fv * (-n) * (1 + i) ^ (-n - 1); dr = p2 + p3 + pv; i = i - r / dr; } while (abs (i - oi) > 1/10000000); cost = pmt * n + (pv - fv); } define fv "Future Value" (fv "Future Value", cost "Interest Paid") (pv "Present Value", i "Interest", n "Period", pmt "Payment") { fv = (pmt * pf(i,n) + pv) / fvf (i, n); cost = pmt * n + (pv - fv); } define pv "Present Value" (pv "Present Value") # cost "Interest Paid") (fv "Future Value", i "Interest", n "Period", pmt "Payment") { pv = fv * fvf (i, n) - pmt * pf(i,n); # cost = pmt * n + (pv - fv); } define n "Loan Period" (n "Period", cost "Interest Paid") (pv "Present Value", fv "Future Value", i "Interest", pmt "Payment") { local num, den; if (pmt - fv * i < 0) num = log (-pmt + fv * i) - log (-pmt - pv * i); else num = log (pmt - fv * i) - log (pmt + pv * i); den = log (1 + i); n = num / den; cost = pmt * n + (pv - fv); } define loan "Loan Cost" (loan "Total cost", shortcost "Interest Paid", inv_fv "Investment Value") (pv "Loan Amount", ai "Annual Percent", pts "Points Percent", ny "Loan Years", cny "Cost Years") { local pmt, i, n, cn, cfv, inv_pv, inv_cost, cost; i = ai / 1200; n = ny * 12; cn = cny * 12; pmt, cost = pmt (pv, 0, i, n); cfv, shortcost = fv (pv, i, cn, pmt); inv_pv = pv * pts / 100; inv_fv, inv_cost = fv (inv_pv, i, cn, pmt); loan = inv_fv + cfv - pv; } define thirty "30 year fixed" (pmt "Payment", cost "Cost") (pv "Amount", iy "Yearly %") { pmt, cost = pmt(pv, 0, iy/1200, 360); }