Last active
November 15, 2022 00:19
-
-
Save markscottwright/79489b22e00bce175bd4b189159db049 to your computer and use it in GitHub Desktop.
Financial functions, taken from Apache POI.
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
public class Financial { | |
/** | |
* Emulates 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 | |
* | |
* @param r | |
* - periodic interest rate represented as a decimal. | |
* @param nper | |
* - number of total payments / periods. | |
* @param pv | |
* - present value -- borrowed or invested principal. | |
* @param fv | |
* - future value of loan or annuity. | |
* @param type | |
* - when payment is made: beginning of period is 1; end, 0. | |
* @return <code>double</code> representing periodic payment amount. | |
*/ | |
static public double pmt(double r, int nper, double pv, double fv, int type) { | |
return -r * (pv * Math.pow(1 + r, nper) + fv) / ((1 + r * type) * (Math.pow(1 + r, nper) - 1)); | |
} | |
/** | |
* Overloaded pmt() call omitting type, which defaults to 0. | |
* | |
* @see #pmt(double, int, double, double, int) | |
*/ | |
static public double pmt(double r, int nper, double pv, double fv) { | |
return pmt(r, nper, pv, fv, 0); | |
} | |
/** | |
* Overloaded pmt() call omitting fv and type, which both default to 0. | |
* | |
* @see #pmt(double, int, double, double, int) | |
*/ | |
static public double pmt(double r, int nper, double pv) { | |
return pmt(r, nper, pv, 0); | |
} | |
/** | |
* Emulates Excel/Calc's FV(interest_rate, number_payments, payment, PV, Type) | |
* function, which calculates future value or principal at period N. | |
* | |
* @param r | |
* - periodic interest rate represented as a decimal. | |
* @param nper | |
* - number of total payments / periods. | |
* @param pmt | |
* - periodic payment amount. | |
* @param pv | |
* - present value -- borrowed or invested principal. | |
* @param type | |
* - when payment is made: beginning of period is 1; end, 0. | |
* @return <code>double</code> representing future principal value. | |
*/ | |
static public double fv(double r, int nper, double pmt, double pv, int type) { | |
return -(pv * Math.pow(1 + r, nper) + pmt * (1 + r * type) * (Math.pow(1 + r, nper) - 1) / r); | |
} | |
/** | |
* Overloaded fv() call omitting type, which defaults to 0. | |
* | |
* @see #fv(double, int, double, double, int) | |
*/ | |
static public double fv(double r, int nper, double c, double pv) { | |
return fv(r, nper, c, pv, 0); | |
} | |
/** | |
* Present value of an amount given the number of future payments, rate, amount | |
* of individual payment, future value and boolean value indicating whether | |
* payments are due at the beginning of period (false => payments are due at | |
* end of period) | |
* | |
* @param r | |
* rate | |
* @param n | |
* num of periods | |
* @param y | |
* pmt per period | |
* @param f | |
* future value | |
* @param t | |
* type (true=pmt at beginning of period, false=pmt at end of period) | |
*/ | |
public static double pv(double r, double n, double y, double f, boolean t) { | |
if (r == 0) { | |
return -1 * ((n * y) + f); | |
} else { | |
double r1 = r + 1; | |
return (((1 - Math.pow(r1, n)) / r) * (t ? r1 : 1) * y - f) / Math.pow(r1, n); | |
} | |
} | |
/** | |
* Present value of an amount given the number of future payments, rate, amount | |
* of individual payment, defaults to 0 future value and payments at the end of | |
* the period | |
* | |
* @param r | |
* rate | |
* @param n | |
* num of periods | |
* @param y | |
* pmt per period | |
*/ | |
public static double pv(double r, double n, double y) { | |
return pv(r, n, y, 0, false); | |
} | |
public static double mortgagePayment(double rate, int years, double mortgage) { | |
return -pmt(rate/12.0, years*12, mortgage); | |
} | |
public static double mortgageForPayment(double rate, int years, double monthlyPayment) { | |
return pv(rate/12.0, years*12, -monthlyPayment); | |
} | |
public static void main(String[] args) { | |
System.out.println(mortgagePayment(.06, 30, 1_000_000)); | |
System.out.println(mortgageForPayment(.06, 30, 6000)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment