-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinancejs.js
165 lines (145 loc) · 4.78 KB
/
financejs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Write your package code here!
//
//Finance.js
//For more information, visit http://financejs.org
//Copyright 2014 - 2015 Essam Al Joubori, MIT license
// Instantiate a Finance class
Finance = function() {};
// Present Value (PV)
Finance.prototype.PV = function (rate, cf1) {
var rate = rate/100, pv;
pv = cf1 / (1 + rate);
return Math.round(pv * 100) / 100;
};
// Future Value (FV)
Finance.prototype.FV = function (rate, cf0, numOfPeriod) {
var rate = rate/100, fv;
fv = cf0 * Math.pow((1 + rate), numOfPeriod);
return Math.round(fv * 100) / 100;
};
// Net Present Value (NPV)
Finance.prototype.NPV = function (rate) {
var rate = rate/100, npv = arguments[1];
for (var i = 2; i < arguments.length; i++) {
npv +=(arguments[i] / Math.pow((1 + rate), i - 1));
}
return Math.round(npv * 100) / 100;
};
// Internal Rate of Return (IRR)
Finance.prototype.IRR = function(cfs) {
var bestGuess, currentNPV;
var checkNPV = function(rate, arguments){
var npv = arguments[0];
// base case
for (var i = 1; i < arguments.length; i++) {
npv +=(arguments[i] / Math.pow((1 + rate/100), i));
}
currentNPV = Math.round(npv * 100) / 100;
if (currentNPV <= 0) {
bestGuess = rate;
return;
}
checkNPV(rate + 0.01, arguments);
};
checkNPV(0.01, arguments);
return Math.round(bestGuess * 100) / 100;
};
// Payback Period (PP)
Finance.prototype.PP = function(numOfPeriods, cfs) {
// for even cash flows
if (numOfPeriods === 0) {
return Math.abs(arguments[1]) / arguments[2];
}
// for uneven cash flows
var cumulativeCashFlow = arguments[1];
var yearsCounter = 1;
for (i = 2; i < arguments.length; i++) {
cumulativeCashFlow += arguments[i];
if (cumulativeCashFlow > 0) {
yearsCounter += (cumulativeCashFlow - arguments[i]) / arguments[i];
return yearsCounter;
} else {
yearsCounter++;
}
}
};
// Return on Investment (ROI)
Finance.prototype.ROI = function(cf0, earnings) {
var roi = (earnings - Math.abs(cf0)) / Math.abs(cf0) * 100;
return Math.round(roi * 100) / 100;
};
// Amortization
Finance.prototype.AM = function (principal, rate, period, yearOrMonth) {
var ratePerPeriod = rate / 12 / 100;
// for inputs in years
if (!yearOrMonth) {
var numerator = ratePerPeriod * Math.pow((1 + ratePerPeriod), period * 12);
var denominator = Math.pow((1 + ratePerPeriod), period * 12) - 1;
var am = principal * (numerator / denominator);
return Math.round(am * 100) / 100;
// for inputs in months
} else if (yearOrMonth === 1) {
var numerator = ratePerPeriod * Math.pow((1 + ratePerPeriod), period);
var denominator = Math.pow((1 + ratePerPeriod), period) - 1;
var am = principal * (numerator / denominator);
return Math.round(am * 100) / 100;
} else {
console.log('not defined');
}
};
// Profitability Index (PI)
Finance.prototype.PI = function(rate, cfs){
var totalOfPVs = 0, PI;
for (var i = 2; i < arguments.length; i++) {
var discountFactor;
// calculate discount factor
discountFactor = 1 / Math.pow((1 + rate/100), (i - 1));
totalOfPVs += arguments[i] * discountFactor;
}
PI = totalOfPVs/Math.abs(arguments[1]);
return Math.round(PI * 100) / 100;
};
// Discount Factor (DF)
Finance.prototype.DF = function(rate, numOfPeriods) {
var dfs = [], discountFactor;
for (var i = 1; i < numOfPeriods; i++) {
discountFactor = 1 / Math.pow((1 + rate/100), (i - 1));
roundedDiscountFactor = Math.ceil(discountFactor * 1000)/1000;
dfs.push(roundedDiscountFactor);
}
return dfs;
};
// Compound Interest (CI)
Finance.prototype.CI = function(rate, numOfCompoundings, principal, numOfPeriods) {
var CI = principal * Math.pow((1 + ((rate/100)/ numOfCompoundings)), numOfCompoundings * 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;
};
// 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;
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
module.exports = Finance;
}
}