InterestRateCurveSensitivityUtils { static final List<DoublesPair> clean(final List<DoublesPair> old, final double relTol, final double absTol) { ArgumentChecker.notNull(old, "null list"); ArgumentChecker.isTrue(relTol >= 0.0 && absTol >= 0.0, "Tolerances must be greater than zero"); if (old.size() == 0) { return new ArrayList<>(); } final List<DoublesPair> res = new ArrayList<>(); final DoublesPair[] sort = old.toArray(new DoublesPair[old.size()]); Arrays.sort(sort, FirstThenSecondDoublesPairComparator.INSTANCE); final DoublesPair pairOld = sort[0]; double tOld = pairOld.first; double sum = pairOld.getSecond(); double scale = Math.abs(sum); double t = tOld; for (int i = 1; i < sort.length; i++) { final DoublesPair pair = sort[i]; t = pair.first; if (t > tOld) { if (Math.abs(sum) > absTol && Math.abs(sum) / scale > relTol) { res.add(DoublesPair.of(tOld, sum)); } tOld = t; sum = pair.getSecondDouble(); scale = Math.abs(sum); } else { sum += pair.getSecondDouble(); scale += Math.abs(pair.getSecondDouble()); } } if (Math.abs(sum) > absTol && Math.abs(sum) / scale > relTol) { res.add(DoublesPair.of(t, sum)); } return res; } protected  InterestRateCurveSensitivityUtils(); static Map<String, List<DoublesPair>> clean(final Map<String, List<DoublesPair>> old, final double relTol, final double absTol); static List<DoublesPair> addSensitivity(final List<DoublesPair> sensi1, final List<DoublesPair> sensi2); static Map<String, List<DoublesPair>> addSensitivity(final Map<String, List<DoublesPair>> sensi1, final Map<String, List<DoublesPair>> sensi2); static Map<String, List<DoublesPair>> addSensitivity(final Map<String, List<DoublesPair>> sensi, final String curveName, final List<DoublesPair> list); static Map<String, List<DoublesPair>> addSensitivity(final Map<String, List<DoublesPair>> sensi1, final Map<String, List<DoublesPair>> sensi2, final Map<String, List<DoublesPair>> sensi3); static Map<String, List<DoublesPair>> multiplySensitivity(final Map<String, List<DoublesPair>> sensitivity, final double factor); static List<DoublesPair> multiplySensitivity(final List<DoublesPair> sensitivity, final double factor); static boolean compare(final List<DoublesPair> sensi1, final List<DoublesPair> sensi2, final double tolerance); static boolean compare(final Map<String, List<DoublesPair>> sensi1, final Map<String, List<DoublesPair>> sensi2, final double tolerance);  }
@Test public void testCleanList1() { final List<DoublesPair> old = new ArrayList<>(); old.add(DoublesPair.of(0.23, 12.3)); final List<DoublesPair> res = clean(old, 1e-9, 1e-12); assertEquals(old.size(), res.size()); for (int i = 0; i < old.size(); i++) { assertEquals(old.get(i).first, res.get(i).first, 0.0); assertEquals(old.get(i).second, res.get(i).second, 1e-9); } }
@Test public void testCleanList2() { final List<DoublesPair> old = new ArrayList<>(); old.add(DoublesPair.of(0.23, 12.3)); old.add(DoublesPair.of(0.231, -12.3)); old.add(DoublesPair.of(1.23, 1.0)); old.add(DoublesPair.of(0.23, -12.3)); old.add(DoublesPair.of(1.23, 3.24)); old.add(DoublesPair.of(1.78, -3.24)); old.add(DoublesPair.of(1.23, -1.0)); old.add(DoublesPair.of(1.23, 1.0)); final List<DoublesPair> expected = new ArrayList<>(); expected.add(DoublesPair.of(0.231, -12.3)); expected.add(DoublesPair.of(1.23, 4.24)); expected.add(DoublesPair.of(1.78, -3.24)); final List<DoublesPair> res = clean(old, 1e-9, 1e-12); assertEquals(expected.size(), res.size()); for (int i = 0; i < expected.size(); i++) { assertEquals(expected.get(i).first, res.get(i).first, 0.0); assertEquals(expected.get(i).second, res.get(i).second, 1e-9); } }
@Test public void testCleanMap() { final String c1 = "c1"; final String c2 = "c2"; final List<DoublesPair> l1 = new ArrayList<>(); l1.add(DoublesPair.of(0.23, 12.3)); l1.add(DoublesPair.of(0.231, -12.3)); l1.add(DoublesPair.of(1.23, 1.0)); final Map<String, List<DoublesPair>> m = new HashMap<>(); m.put(c1, l1); final List<DoublesPair> l2 = new ArrayList<>(); l2.add(DoublesPair.of(0.23, -12.3)); l2.add(DoublesPair.of(1.23, 3.24)); l2.add(DoublesPair.of(1.78, -3.24)); l2.add(DoublesPair.of(1.23, -1.0)); l2.add(DoublesPair.of(1.23, 1.0)); m.put(c2, l2); final List<DoublesPair> e2 = new ArrayList<>(); e2.add(DoublesPair.of(0.23, -12.3)); e2.add(DoublesPair.of(1.23, 3.24)); e2.add(DoublesPair.of(1.78, -3.24)); final Map<String, List<DoublesPair>> res = clean(m, 1e-9, 1e-12); assertSensitivityEquals(l1, res.get(c1), 1e-9); assertSensitivityEquals(e2, res.get(c2), 1e-9); }