1   /**
2    * Investor, the open-source investment library
3    *
4    * (C) Copyright 2008, by individual contributors as indicated by the @author tag.
5    *
6    * This library is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of
9    * the License, or (at your option) any later version.
10   *
11   * This software is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this software; if not, write to the Free
18   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20   */
21  package com.nickokiss.investor.functioncalc;
22  
23  import java.math.BigDecimal;
24  
25  import com.nickokiss.investor.calc.MathCalc;
26  import com.nickokiss.investor.function.Function;
27  import com.nickokiss.investor.util.Range;
28  import com.nickokiss.investor.util.TkFinConstructionException;
29  
30  /**
31   * 
32   * @author Tomasz Koscinski <tomasz.koscinski@nickokiss.com>
33   */
34  public class IterativeFunctionCalc implements FunctionCalc {
35  
36    private MathCalc mathCalc = new MathCalc();
37    
38    @Override
39    public Range getParameterRange(BigDecimal expectedValue, Function function, BigDecimal maxSize, Range initialRange) throws TkFinConstructionException {
40      int steps = mathCalc.div(initialRange.getSize(), maxSize).intValue();
41      BigDecimal closestPar = initialRange.getStart();
42      BigDecimal smallestDiff = expectedValue.subtract(function.getValue(closestPar)).abs();
43      for (int i = 0; i <= steps; i++) {
44        BigDecimal addition = maxSize.multiply(new BigDecimal(i));
45        BigDecimal parameter = initialRange.getStart().add(addition);
46        BigDecimal result = function.getValue(parameter);
47        BigDecimal diff = expectedValue.subtract(result).abs();
48        if (diff.compareTo(smallestDiff) < 0) {
49          closestPar = parameter;
50          smallestDiff = diff;
51        }
52      }
53      BigDecimal half = mathCalc.div(maxSize, mathCalc.TWO);
54      Range resultRange = new Range(closestPar.subtract(half), closestPar.add(half));
55      return resultRange;
56    }
57  
58  }