Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FinanceLib.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula.functions;
  16. /**
  17. * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  18. *
  19. *
  20. * This class is a functon library for common fiscal functions.
  21. * <b>Glossary of terms/abbreviations:</b>
  22. * <br/>
  23. * <ul>
  24. * <li><em>FV:</em> Future Value</li>
  25. * <li><em>PV:</em> Present Value</li>
  26. * <li><em>NPV:</em> Net Present Value</li>
  27. * <li><em>PMT:</em> (Periodic) Payment</li>
  28. *
  29. * </ul>
  30. * For more info on the terms/abbreviations please use the references below
  31. * (hyperlinks are subject to change):
  32. * </br>Online References:
  33. * <ol>
  34. * <li>GNU Emacs Calc 2.02 Manual: http://theory.uwinnipeg.ca/gnu/calc/calc_203.html</li>
  35. * <li>Yahoo Financial Glossary: http://biz.yahoo.com/f/g/nn.html#y</li>
  36. * <li>MS Excel function reference: http://office.microsoft.com/en-us/assistance/CH062528251033.aspx</li>
  37. * </ol>
  38. * <h3>Implementation Notes:</h3>
  39. * Symbols used in the formulae that follow:<br/>
  40. * <ul>
  41. * <li>p: present value</li>
  42. * <li>f: future value</li>
  43. * <li>n: number of periods</li>
  44. * <li>y: payment (in each period)</li>
  45. * <li>r: rate</li>
  46. * <li>^: the power operator (NOT the java bitwise XOR operator!)</li>
  47. * </ul>
  48. * [From MS Excel function reference] Following are some of the key formulas
  49. * that are used in this implementation:
  50. * <pre>
  51. * p(1+r)^n + y(1+rt)((1+r)^n-1)/r + f=0 ...{when r!=0}
  52. * ny + p + f=0 ...{when r=0}
  53. * </pre>
  54. */
  55. public final class FinanceLib {
  56. private FinanceLib() {
  57. // no instances of this class
  58. }
  59. /**
  60. * Future value of an amount given the number of payments, rate, amount
  61. * of individual payment, present value and boolean value indicating whether
  62. * payments are due at the beginning of period
  63. * (false => payments are due at end of period)
  64. * @param r rate
  65. * @param n num of periods
  66. * @param y pmt per period
  67. * @param p future value
  68. * @param t type (true=pmt at end of period, false=pmt at begining of period)
  69. */
  70. public static double fv(double r, double n, double y, double p, boolean t) {
  71. double retval = 0;
  72. if (r == 0) {
  73. retval = -1*(p+(n*y));
  74. }
  75. else {
  76. double r1 = r + 1;
  77. retval =((1-Math.pow(r1, n)) * (t ? r1 : 1) * y ) / r
  78. -
  79. p*Math.pow(r1, n);
  80. }
  81. return retval;
  82. }
  83. /**
  84. * Present value of an amount given the number of future payments, rate, amount
  85. * of individual payment, future value and boolean value indicating whether
  86. * payments are due at the beginning of period
  87. * (false => payments are due at end of period)
  88. * @param r
  89. * @param n
  90. * @param y
  91. * @param f
  92. * @param t
  93. */
  94. public static double pv(double r, double n, double y, double f, boolean t) {
  95. double retval = 0;
  96. if (r == 0) {
  97. retval = -1*((n*y)+f);
  98. }
  99. else {
  100. double r1 = r + 1;
  101. retval =(( ( 1 - Math.pow(r1, n) ) / r ) * (t ? r1 : 1) * y - f)
  102. /
  103. Math.pow(r1, n);
  104. }
  105. return retval;
  106. }
  107. /**
  108. * calculates the Net Present Value of a principal amount
  109. * given the discount rate and a sequence of cash flows
  110. * (supplied as an array). If the amounts are income the value should
  111. * be positive, else if they are payments and not income, the
  112. * value should be negative.
  113. * @param r
  114. * @param cfs cashflow amounts
  115. */
  116. public static double npv(double r, double[] cfs) {
  117. double npv = 0;
  118. double r1 = r + 1;
  119. double trate = r1;
  120. for (int i=0, iSize=cfs.length; i<iSize; i++) {
  121. npv += cfs[i] / trate;
  122. trate *= r1;
  123. }
  124. return npv;
  125. }
  126. /**
  127. *
  128. * @param r
  129. * @param n
  130. * @param p
  131. * @param f
  132. * @param t
  133. */
  134. public static double pmt(double r, double n, double p, double f, boolean t) {
  135. double retval = 0;
  136. if (r == 0) {
  137. retval = -1*(f+p)/n;
  138. }
  139. else {
  140. double r1 = r + 1;
  141. retval = ( f + p * Math.pow(r1, n) ) * r
  142. /
  143. ((t ? r1 : 1) * (1 - Math.pow(r1, n)));
  144. }
  145. return retval;
  146. }
  147. /**
  148. *
  149. * @param r
  150. * @param y
  151. * @param p
  152. * @param f
  153. * @param t
  154. */
  155. public static double nper(double r, double y, double p, double f, boolean t) {
  156. double retval = 0;
  157. if (r == 0) {
  158. retval = -1 * (f + p) / y;
  159. } else {
  160. double r1 = r + 1;
  161. double ryr = (t ? r1 : 1) * y / r;
  162. double a1 = ((ryr - f) < 0)
  163. ? Math.log(f - ryr)
  164. : Math.log(ryr - f);
  165. double a2 = ((ryr - f) < 0)
  166. ? Math.log(-p - ryr)
  167. : Math.log(p + ryr);
  168. double a3 = Math.log(r1);
  169. retval = (a1 - a2) / a3;
  170. }
  171. return retval;
  172. }
  173. }