Created
April 17, 2018 13:29
-
-
Save HunderlineK/1189da59d8f6cb4da4175fbe745961df to your computer and use it in GitHub Desktop.
Loan with fixed payments
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
const math = require('mathjs'); | |
const last = require('lodash/last'); | |
const range = (n) => new Array(n).fill(0); | |
const duration = 238; | |
const monthlyRate = 0.0549 / 12; | |
const amount = 40020 * Math.pow(1 + monthlyRate, 2); | |
const coefMatrix = math.matrix( | |
range(duration + 1).reduce((matrix, emptyRow, row) => { | |
matrix.push( | |
range(duration + 1).reduce((coef, emptyCol, col) => { | |
switch (col) { | |
case duration: | |
coef.push(1); | |
break; | |
case row: | |
coef.push(1); | |
break; | |
case row - 1: | |
coef.push(-1 - monthlyRate); | |
break; | |
default: | |
coef.push(0); | |
} | |
return coef; | |
}, []) | |
); | |
return matrix; | |
}, []) | |
); | |
const constMatrix = math.matrix( | |
range(duration + 1).reduce((results, empty, row) => { | |
switch (row) { | |
case 0: | |
results.push((1 + monthlyRate) * amount); | |
break; | |
case 17: | |
results.push(-amount * 0.3); | |
break; | |
default: | |
results.push(0); | |
} | |
return results; | |
}, []) | |
); | |
// p = r * a / ( 1 - ( 1 + r )^-n ) | |
// a = p / r * ( 1 - ( 1 + r )^-n ) | |
console.log(last(math.multiply(math.inv(coefMatrix), constMatrix)._data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment