Java library for working with financial data. Based on the financials R package.
To use it add the following dependency to your pom
<dependency>
<groupId>se.alipsa</groupId>
<artifactId>jfinancials</artifactId>
<version>1.0.0</version>
</dependency>
All methods of the public api are in the se.alipsa.jfinancials.Financials class
double pmt(interestRate, nper, pv, fv = 0, type = 0)
Equivalent to Excel/Calc's PMT(interest_rate, number_payments, PV, FV, Type) function, which calculates the payments for a loan or the future value of an investment
- interestRate periodic interest rate represented as a decimal.
- nper number of total payments / periods.
- pv present value -- borrowed or invested principal.
- fv future value of loan or annuity, default to 0 (which is what you want for loans)
- type when payment is made: beginning of period is 1; end is 0. Default is 0
returns A double representing the periodic payment amount.
Calulate payment for a loan
The following data:
Item | amount |
---|---|
Loan Amount | 50000 |
Interest rate | 3.50% |
Periods | 60 |
Monthly payment | 909.59 |
Then the Monthly payment can be calculated as
pmt(3.5/100, 60, -50000)
≈ 2004.43
monthlyAnnuityAmount(loanAmount, interestRate, tenureMonths, amortizationFreemonths = 0, type = 0)
Calculate the monthly annuity amount i.e. the amortization and interest amount each payment period (month)
- loanAmount the total loan amount including capitalized fees (e.g. startup fee)
- interestRate the annual nominal interest
- tenureMonths the tenure of the loan in number of months
- amortizationFreeMonths the number of initial amortization free months, default to 0
- type - when payment is made: beginning of period is 1; end is 0. Default is 0
returns the monthly annuity amount
Assuming the following:
Item | amount |
---|---|
Loan Amount | 50000 |
Interest rate | 3.50% |
Tenure | 60 |
Amortization Free months | 6 |
The monthly annuity amount would be
monthlyAnnuityAmount(50000, 3.5/100, 60, 6)
== 1002.10
cashFlow(loanAmount, interestRate, tenureMonths, amortizationFreeMonths, invoiceFee)
- loanAmount the total loan amount including capitalized fees (e.g. startup fee)
- interestRate the annual nominal interest
- tenureYears the tenure of the loan in number of years
- amortizationFreeMonths the number of initial amortization free months, default to 0
- invoiceFee a fee for each statement invoiced, default to 0
returns an array of doubles of cachFlow entries for each period
var cf = Financials.cashFlow(50000, 0.035, 5, 6, 30)
paymentPlan(loanAmount, interestRate, tenureMonths, amortizationFreeMonths = 0, invoiceFee = 0)
- loanAmount the total loan amount including capitalized fees (e.g. startup fee)
- interestRate the annual nominal interest
- tenureMonths the total tenure of the loan in number of months
- amortizationFreeMonths the number of initial amortization free months, default to 0
- invoiceFee a fee for each statement invoiced, default to 0
returns a List of Payment with the initial payment plan based on the input, based on monthly payment periods
var loanAmt = 10000;
var tenureYears = (int)(1.5 * 12);
var amortizationFreeMonths = 6;
var interest = BigDecimal.valueOf(3.5 / 100);
var invoiceFee = BigDecimal.valueOf(30);
var paymentPlan = Financials.paymentPlan(loanAmt, interest, tenureYears, amortizationFreeMonths, invoiceFee);
System.out.println(paymentPlan);
which will the following output:
month | costOfCredit | interestAmt | amortization | invoiceFee | outgoingBalance | cashFlow |
---|---|---|---|---|---|---|
0 | 0.00 | 0.00 | 0.00 | 0.00 | 10000.00 | -10000.00 |
1 | 29.17 | 29.17 | 0.00 | 30.00 | 10000.00 | 59.17 |
2 | 29.17 | 29.17 | 0.00 | 30.00 | 10000.00 | 59.17 |
3 | 29.17 | 29.17 | 0.00 | 30.00 | 10000.00 | 59.17 |
4 | 29.17 | 29.17 | 0.00 | 30.00 | 10000.00 | 59.17 |
5 | 29.17 | 29.17 | 0.00 | 30.00 | 10000.00 | 59.17 |
6 | 29.17 | 29.17 | 0.00 | 30.00 | 10000.00 | 59.17 |
7 | 849.22 | 29.17 | 820.05 | 30.00 | 9179.95 | 879.22 |
8 | 849.22 | 26.77 | 822.44 | 30.00 | 8357.51 | 879.22 |
9 | 849.22 | 24.38 | 824.84 | 30.00 | 7532.67 | 879.22 |
10 | 849.22 | 21.97 | 827.25 | 30.00 | 6705.42 | 879.22 |
11 | 849.22 | 19.56 | 829.66 | 30.00 | 5875.76 | 879.22 |
12 | 849.22 | 17.14 | 832.08 | 30.00 | 5043.69 | 879.22 |
13 | 849.22 | 14.71 | 834.51 | 30.00 | 4209.18 | 879.22 |
14 | 849.22 | 12.28 | 836.94 | 30.00 | 3372.24 | 879.22 |
15 | 849.22 | 9.84 | 839.38 | 30.00 | 2532.86 | 879.22 |
16 | 849.22 | 7.39 | 841.83 | 30.00 | 1691.03 | 879.22 |
17 | 849.22 | 4.93 | 844.28 | 30.00 | 846.75 | 879.22 |
18 | 849.22 | 2.47 | 846.75 | 30.00 | 0.00 | 879.22 |
Financials.totalPaymentAmount(loanAmount, interestRate, tenureMonths, amortizationFreeMonths, invoiceFee)
Total Payment amount is the sum of all payments.
- loanAmount the total loan amount including capitalized fees (e.g. startup fee)
- interestRate the annual nominal interest
- tenureMonths the total tenure of the loan in number of months
- amortizationFreeMonths the number of initial amortization free months, default to 0
- invoiceFee a fee for each statement invoiced, default to 0
returns a double containing the sum of all payments
import static se.alipsa.jfinancials.Financials.*;
double loanAmt = 10000;
int tenureMonths = (int) (1.5 * 12);
int amortizationFreeMonths = 6;
double interest = 3.5 / 100;
double invoiceFee = 30
double totalAmt = totalPaymentAmount(loanAmt, interest, tenureMonths, amortizationFreeMonths, invoiceFee);
print(totalAmt);
11725.645213062116
double irr(PaymentPlan cf)
double irr(double[] cf)
- cf a cash flow array or PaymentPlan (see the cashFlow or paymentPlan functions)
returns a double containing the internal return rate
Given the cache flow above
import static se.alipsa.jfinancials.Financials.*;
var internalReturn = irr(paymentPlan.getColumn("cashFlow"));
System.out.println(internalReturn);
output:
0.00291665871251
double apr(double monthlyIrr)
- monthlyIrr the MONTHLY internal rate of return (monthly irr)
Returns a double with the annual percentage rate
import static se.alipsa.jfinancials.Financials.*;
double annualPercentage = apr(internalReturn)
print(annualPercentage)
0.08934409474458183
double npv(double[] cashFlow, double rate)
Net present value (NPV) is the difference between the present value of cash inflows and the present value of cash outflows over a period of time. This function produces the same results as Excel does.
- cashFlow cache flow e.g. the cashFlow list of the payment plan
- rate interest rate
Returns a double with the net present value
import se.alipsa.jfinancials.Financials.*
System.out.println(npv(List.of(-123400, 36200, 54800, 48100), 0.035));
5908.8656360761