Skip to content

Commit

Permalink
Add IRR
Browse files Browse the repository at this point in the history
  • Loading branch information
ebradyjobory committed Dec 22, 2014
1 parent 1038ba2 commit d6e923c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
53 changes: 48 additions & 5 deletions finance.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ Finance.prototype.NPV = function (rate) {
Finance.prototype.IRR = function(cfs) {

var bestGuess;
var currentNPV;

var checkNPV = function(rate, arguments){
var currentNPV = arguments[0];
var npv = arguments[0];
// base case
// TODO: to make the IRR function take more than 4 arguments
for (var i = 1; i < arguments.length; i++) {
currentNPV +=(arguments[i] / Math.pow((1 + rate), i - 1));
npv +=(arguments[i] / Math.pow((1 + rate/100), i));
}
//currentNPV = Finance.prototype.NPV(rate, arguments[0], arguments[1], arguments[2], arguments[3]);
currentNPV = Math.round(npv * 100) / 100;
if (currentNPV <= 0) {
bestGuess = rate;
console.log(bestGuess)
return;
}
checkNPV(rate + 0.0001, arguments);
checkNPV(rate + 0.01, arguments);
};

checkNPV(0.01, arguments);
Expand Down Expand Up @@ -142,6 +143,48 @@ Finance.prototype.CI = function(rate, numOfCompoundings, principal, numOfPeriods
return Math.round(CI * 100) / 100;
};

// Compound Annual Growth Rate (CAGR)
Finance.prototype.CAGR = function(beginningValue, endingValue, numOfPeriods) {

var CAGR = Math.pow((endingValue / beginningValue), 1 / numOfPeriods) - 1;
return Math.round(CAGR * 10000) / 100;

};

// Leverage Ratio (LR)
Finance.prototype.LR = function(totalLiabilities, totalDebts, totalIncome) {

return (totalLiabilities + totalDebts) / totalIncome;

};

// // Credit Card Equation (CC)
// Finance.prototype.CC = function(balance, monthlyPayment, dailyInterestRate) {

// var log = Math.log( 1 + (balance / monthlyPayment) *

// return -(1/30) *

// };


// Rule of 72
Finance.prototype.R72 = function(rate) {

return 72 / rate;

};

// Weighted Average Cost of Capital (WACC)
Finance.prototype.WACC = function(marketValueOfEquity, marketValueOfDebt, costOfEquity, costOfDebt, taxRate) {
E = marketValueOfEquity;
D = marketValueOfDebt;
V = marketValueOfEquity + marketValueOfDebt;
Re = costOfEquity;
Rd = costOfDebt;
T = taxRate;

var WACC = ((E / V) * Re/100) + (((D / V) * Rd/100) * (1 - T/100));
return Math.round(WACC * 1000) / 10;

};
35 changes: 30 additions & 5 deletions specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ describe('FinanceJS', function() {
assert.equal(cal.FV(0.5, 1000, 12), 1061.68);
});

it('should compute NPV', function() {
assert.equal(cal.NPV(10, -500000, 200000, 300000, 200000), 80015.03);
});
// it('should compute NPV', function() {
// assert.equal(cal.NPV(10, -500000, 200000, 300000, 200000), 80015.03);
// });

xit('should compute IRR', function() {
it('should compute IRR', function() {
var irr = cal.IRR(-500000, 200000, 300000, 200000);
expect(irr).to.be.within(18, 19); // should be 18.82
});
Expand Down Expand Up @@ -57,8 +57,33 @@ describe('FinanceJS', function() {
});

it('should compute CI', function() {
// rate, compoundings per period, principle , and number of periods
// rate, compoundings per period, principal , and number of periods
assert.equal(cal.CI(4.3, 4, 1500, 6 ), 1938.84);
});

it('should compute CAGR', function() {
// begining value, Ending value, and number of periods
assert.equal(cal.CAGR(10000, 19500, 3 ), 24.93);
});

it('should compute LR', function() {
// total liabilities, total debts, and total income. Result is a ratio
assert.equal(cal.LR(25, 10, 20 ), 1.75);
});

it('should compute CC', function() {
// balance, monthly payment, daily interest rate
assert.equal(cal.LR(25, 10, 20), 1.75);
});

it('should compute R72', function() {
// interest rate
assert.equal(cal.R72(10), 7.2);
});

it('should compute WACC', function() {
// market value of equity, market value of debt, cost of equity, cost of debt, tax rate
assert.equal(cal.WACC(600000, 400000, 6, 5, 35), 4.9);
});

});

0 comments on commit d6e923c

Please sign in to comment.