You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Mirr.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import java.util.Arrays;
  17. import org.apache.poi.ss.formula.eval.ErrorEval;
  18. import org.apache.poi.ss.formula.eval.EvaluationException;
  19. /**
  20. * Calculates Modified internal rate of return. Syntax is MIRR(cash_flow_values, finance_rate, reinvest_rate)
  21. *
  22. * <p>Returns the modified internal rate of return for a series of periodic cash flows. MIRR considers both the cost
  23. * of the investment and the interest received on reinvestment of cash.</p>
  24. *
  25. * Values is an array or a reference to cells that contain numbers. These numbers represent a series of payments (negative values) and income (positive values) occurring at regular periods.
  26. * <ul>
  27. * <li>Values must contain at least one positive value and one negative value to calculate the modified internal rate of return. Otherwise, MIRR returns the #DIV/0! error value.</li>
  28. * <li>If an array or reference argument contains text, logical values, or empty cells, those values are ignored; however, cells with the value zero are included.</li>
  29. * </ul>
  30. *
  31. * Finance_rate is the interest rate you pay on the money used in the cash flows.
  32. * Reinvest_rate is the interest rate you receive on the cash flows as you reinvest them.
  33. *
  34. * @author Carlos Delgado (carlos dot del dot est at gmail dot com)
  35. * @author Cedric Walter (cedric dot walter at gmail dot com)
  36. *
  37. * @see <a href="http://en.wikipedia.org/wiki/MIRR">Wikipedia on MIRR</a>
  38. * @see <a href="http://office.microsoft.com/en-001/excel-help/mirr-HP005209180.aspx">Excel MIRR</a>
  39. * @see Irr
  40. */
  41. public class Mirr extends MultiOperandNumericFunction {
  42. public Mirr() {
  43. super(false, false);
  44. }
  45. @Override
  46. protected int getMaxNumOperands() {
  47. return 3;
  48. }
  49. @Override
  50. protected double evaluate(double[] values) throws EvaluationException {
  51. double financeRate = values[values.length-1];
  52. double reinvestRate = values[values.length-2];
  53. double[] mirrValues = Arrays.copyOf(values, values.length - 2);
  54. boolean mirrValuesAreAllNegatives = true;
  55. for (double mirrValue : mirrValues) {
  56. mirrValuesAreAllNegatives &= mirrValue < 0;
  57. }
  58. if (mirrValuesAreAllNegatives) {
  59. return -1.0d;
  60. }
  61. boolean mirrValuesAreAllPositives = true;
  62. for (double mirrValue : mirrValues) {
  63. mirrValuesAreAllPositives &= mirrValue > 0;
  64. }
  65. if (mirrValuesAreAllPositives) {
  66. throw new EvaluationException(ErrorEval.DIV_ZERO);
  67. }
  68. return mirr(mirrValues, financeRate, reinvestRate);
  69. }
  70. private static double mirr(double[] in, double financeRate, double reinvestRate) {
  71. double value = 0;
  72. double numOfYears = in.length - 1;
  73. double pv = 0;
  74. double fv = 0;
  75. int indexN = 0;
  76. for (double anIn : in) {
  77. if (anIn < 0) {
  78. pv += anIn / Math.pow(1 + financeRate + reinvestRate, indexN++);
  79. }
  80. }
  81. for (double anIn : in) {
  82. if (anIn > 0) {
  83. fv += anIn * Math.pow(1 + financeRate, numOfYears - indexN++);
  84. }
  85. }
  86. if (fv != 0 && pv != 0) {
  87. value = Math.pow(-fv / pv, 1d / numOfYears) - 1;
  88. }
  89. return value;
  90. }
  91. }