/* * Created on Mar 9, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package test; /** * @author vijay_dharap * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class RateTest { public static void main(String[] args) { RateTest rt = new RateTest(); double rate = rt.rate(36, -38035, 4894.9333, 21252, 12, 12, 0, 1); System.out.println(rate); } /*************************************************/ /** The Rate Function and related functions */ /*************************************************/ /** * ratio used in iterative solution for interest */ private double ratio = 1e4; /** * Method _fi_calc_interest. Method to calculate the interest rate depending on all the params * @param per number of periods * @param pv present value * @param pmt periodic payment * @param fv future value * @param CF compounding frequency * @param PF payment frequency * @param disc discrete/continuous compounding * @param bep beginning/end of period payment * @return double */ private double rate(int per,/* number of periods */ double pv, /* present value */ double pmt, /* periodic payment */ double fv, /* future value */ int CF, /* compounding frequency */ int PF, /* payment frequency */ int disc, /* discrete/continuous compounding */ int bep) /* beginning/end of period payment */ { double eint; double a, dik; int ri; if (pmt == 0.0) eint = Math .pow((Math.abs(fv) / Math.abs(pv)), (1.0 / (double) per)) - 1.0; else { if ((pmt * fv) < 0.0) { if (pv != 0) a = -1.0; else a = 1.0; eint = Math.abs((fv + a * (double) per * pmt) / (3.0 * (((double) per - 1.0) * ((double) per - 1.0) * pmt + pv - fv))); } else { if ((pv * pmt) < 0.0) { eint = Math.abs(((double) per * pmt + pv + fv) / ((double) per * pv)); } else { a = Math.abs(pmt / (Math.abs(pv) + Math.abs(fv))); eint = a + 1.0 / (a * (double) per * (double) per * (double) per); } } do { dik = fi(per, eint, pv, pmt, fv, bep) / fip(per, eint, pv, pmt, fv, bep); eint -= dik; ri = (int) Math.floor(ratio * (dik / eint)); } while (ri != 0); } /* endif */ return eint ; // i need only eint and not nom_int // so commented // return 100.0 * nom_int(eint, CF, PF, disc); } /* _fi_calc_interest */ // /* compute Nominal Interest Rate from Effective Interest Rate */ // private double nom_int(double eint, int CF, int PF, int disc) { // double nint; // // if (disc != 0) { // if (CF == PF) { // nint = CF * eint; // } else { // nint = CF // * (Math.pow((1.0 + eint), ((double) PF / (double) CF)) - 1.0); // } /* * endif */ // } else // nint = Math.log(Math.pow(1.0 + eint, PF)); // // return nint; // } /* nom_int */ /* calculation used in interest computation */ private double fi(int per, double eint, double pv, double pmt, double fv, int bep) { return interestCalc1(eint, per) * (pv + interestCalc3(eint, pmt, bep)) + pv + fv; } /* fi */ /* * calculation used in interest computation */ private double fip(int per, double eint, double pv, double pmt, double fv, int bep) { double AA = interestCalc1(eint, per); double CC = interestCalc3(eint, pmt, bep); double D = (AA + 1.0) / (1.0 + eint); return (double) per * (pv + CC) * D - (AA * CC) / eint; } /* fip */ /* Compute constant used in calculations */ private double interestCalc1(double eint, int per) { return Math.pow((1.0 + eint), (double) per) - 1.0; } /* interestCalc1 */ /* Compute constant used in calculations */ private double interestCalc2(double eint, int beg) { /* the following should call an error routine to report the error */ if (eint == 0.0) { System.err.println("Zero Interest.!!\n"); System.exit(1); } /* endif */ return (1.0 + eint * (double) beg) / eint; } /* interestCalc2 */ /* Compute constant used in calculations */ private double interestCalc3(double eint, double pmt, int beg) { return pmt * interestCalc2(eint, beg); } /* interestCalc3 */ /****************************************************/ /** end of The Rate Function and related functions */ /****************************************************/ }