Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate rate of interest per period #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions finance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ export class Finance {
*/
public PMT(fractionalRate: number, numOfPayments: number, principal: number): number;

/**
* Caculate the rate of interest per period.
* @param numOfPayments
* @param pmt Loan Payment
* @param pv Present Value
* @param fv Future Value
* @param when begin for 1, end for 0
*/
public RATE(numOfPayments: number, pmt: number, pv: number, fv: number, when: number): number;

/**
* Inflation-adjusted Return (IAR)
* Measure the return taking into account the time period's inflation rate
Expand Down
31 changes: 31 additions & 0 deletions finance.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,37 @@ Finance.prototype.IAR = function(investmentReturn, inflationRate){
return 100 * (((1 + investmentReturn) / (1 + inflationRate)) - 1);
}

/**
* Caculate the rate of interest per period.
* @param numOfPayments
* @param pmt Loan Payment
* @param pv Present Value
* @param fv Future Value
* @param when begin for 1, end for 0
*/
Finance.prototype.RATE = function(numOfPayments, pmt, pv, fv, when) {

var guess = 0.1;
var limit = 100; //loop limit
var guess_last;

do {
guess_last = guess;
guess = guess_last - guess_diff(guess_last, numOfPayments, pmt, pv, fv, when);
limit--;
}while(guess_last.toFixed(5) != guess.toFixed(5) && limit > 0);

return guess;

function guess_diff(guess, nper, pmt, pv, fv, when) {
t1 = Math.pow((guess+1), nper);
t2 = Math.pow((guess+1), nper-1);
return ((fv + t1*pv + pmt*(t1 - 1)*(guess*when + 1)/guess) /
(nper*t2*pv - pmt*(t1 - 1)*(guess*when + 1)/Math.pow(guess,2) + nper*pmt*t2*(guess*when + 1)/guess +
pmt*(t1 - 1)*when/guess));
}
}

// XIRR - IRR for irregular intervals
Finance.prototype.XIRR = function(cfs, dts, guess) {
if (cfs.length != dts.length) throw new Error('Number of cash flows and dates should match');
Expand Down
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ describe('FinanceJS', function() {
cal.IAR(0.08, 0.03).should.equal(4.854368932038833);
});

it('should compute RATE', function() {
Number(cal.RATE(12, -933.33, 10000, 0, 0).toFixed(5)).should.equal(0.01788);
});

it('should compute XIRR', function() {
cal.XIRR([-1000, -100, 1200],[new Date(2015, 11, 1 ), new Date(2016, 7, 1 ), new Date(2016, 7, 19 )],0 ).should.equal(14.11);
});
Expand Down