YieldCurveUtils { public static YieldCurve withPointShifts(YieldCurve curve, List<Double> t, List<Double> shifts, ShiftType shiftType) { return withPointShifts(curve, t, shifts, shiftType, POINT_SHIFT_NAME); }  static YieldCurve withParallelShift(YieldCurve curve, double shift, ShiftType shiftType); static YieldCurve withParallelShift(YieldCurve curve, double shift, ShiftType shiftType, String nameSuffix); static YieldCurve withBucketedShifts(YieldCurve curve,
                                              List<DoublesPair> buckets,
                                              List<Double> shifts,
                                              ShiftType shiftType); static YieldCurve withBucketedShifts(YieldCurve curve,
                                              List<DoublesPair> buckets,
                                              List<Double> shifts,
                                              ShiftType shiftType,
                                              String nameSuffix); static YieldCurve withPointShifts(YieldCurve curve, List<Double> t, List<Double> shifts, ShiftType shiftType); static YieldCurve withPointShifts(YieldCurve curve,
                                           List<Double> t,
                                           List<Double> shifts,
                                           ShiftType shiftType,
                                           String nameSuffix); static final String PARALLEL_SHIFT_NAME; static final String BUCKETED_SHIFT_NAME; static final String POINT_SHIFT_NAME; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullCurve3() { YieldCurveUtils.withPointShifts(null, new ArrayList<Double>(), new ArrayList<Double>(), ShiftType.ABSOLUTE); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullPoints() { YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, null, new ArrayList<Double>(), ShiftType.ABSOLUTE); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullShifts2() { YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, new ArrayList<Double>(), null, ShiftType.ABSOLUTE); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullShiftType3() { YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, new ArrayList<Double>(), new ArrayList<Double>(), null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testDifferentLengthShifts2() { YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, Arrays.asList(3., 5.), new ArrayList<Double>(), ShiftType.ABSOLUTE); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testConstantCurvePointsShift() { YieldCurveUtils.withPointShifts(new YieldCurve("NAME", ConstantDoublesCurve.from(0.)), new ArrayList<Double>(), new ArrayList<Double>(), ShiftType.ABSOLUTE); }
@Test public void testEmptyPointAbsoluteShifts() { final List<Double> points = new ArrayList<>(); final List<Double> shifts = new ArrayList<>(); final YieldCurve shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.ABSOLUTE); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertFalse(ORIGINAL_CURVE.equals(shiftedCurve)); assertCurveEquals(ORIGINAL_CURVE.getCurve(), shiftedCurve.getCurve(), 1e-15); }
@Test public void testEmptyPointRelativeShifts() { final List<Double> points = new ArrayList<>(); final List<Double> shifts = new ArrayList<>(); final YieldCurve shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.RELATIVE); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertFalse(ORIGINAL_CURVE.equals(shiftedCurve)); assertCurveEquals(ORIGINAL_CURVE.getCurve(), shiftedCurve.getCurve(), 1e-15); }
@Test public void testPointZeroAbsoluteShifts() { List<Double> points = Arrays.asList(1., 2., 3., 4.); final List<Double> shifts = Arrays.asList(0., 0., 0., 0.); YieldCurve shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.ABSOLUTE); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertFalse(ORIGINAL_CURVE.equals(shiftedCurve)); assertCurveEquals(ORIGINAL_CURVE.getCurve(), shiftedCurve.getCurve(), 1e-9); points = Arrays.asList(1.1, 2.1, 3.1, 4.1); final double[] newT = new double[] {1, 1.1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 6, 7}; final double[] newY = new double[] {0.01, INTERPOLATED_CURVE.getYValue(1.1), 0.015, INTERPOLATED_CURVE.getYValue(2.1), 0.017, INTERPOLATED_CURVE.getYValue(3.1), 0.02, INTERPOLATED_CURVE.getYValue(4.1), 0.025, 0.035, 0.05}; final DoublesCurve expectedCurve = InterpolatedDoublesCurve.from(newT, newY, INTERPOLATOR); shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.ABSOLUTE); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertCurveEquals(expectedCurve, shiftedCurve.getCurve(), 1e-9); }
@Test public void testPointZeroRelativeShifts() { List<Double> points = Arrays.asList(1., 2., 3., 4.); final List<Double> shifts = Arrays.asList(0., 0., 0., 0.); YieldCurve shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.RELATIVE); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertFalse(ORIGINAL_CURVE.equals(shiftedCurve)); assertCurveEquals(ORIGINAL_CURVE.getCurve(), shiftedCurve.getCurve(), 1e-9); points = Arrays.asList(1.1, 2.1, 3.1, 4.1); final double[] newT = new double[] {1, 1.1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 6, 7}; final double[] newY = new double[] {0.01, INTERPOLATED_CURVE.getYValue(1.1), 0.015, INTERPOLATED_CURVE.getYValue(2.1), 0.017, INTERPOLATED_CURVE.getYValue(3.1), 0.02, INTERPOLATED_CURVE.getYValue(4.1), 0.025, 0.035, 0.05}; final DoublesCurve expectedCurve = InterpolatedDoublesCurve.from(newT, newY, INTERPOLATOR); shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.RELATIVE); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertCurveEquals(expectedCurve, shiftedCurve.getCurve(), 1e-9); }
@Test public void testPointAbsoluteShifts() { List<Double> points = Arrays.asList(1., 2., 3., 4.); final List<Double> shifts = Arrays.asList(2., 3., 4., 5.); final double[] y = new double[Y.length]; System.arraycopy(Y, 0, y, 0, Y.length); y[0] += 2; y[1] += 3; y[2] += 4; y[3] += 5; YieldCurve shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.ABSOLUTE); DoublesCurve expectedCurve = InterpolatedDoublesCurve.from(T, y, INTERPOLATOR); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertCurveEquals(expectedCurve, shiftedCurve.getCurve(), 1e-9); shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.ABSOLUTE); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertCurveEquals(expectedCurve, shiftedCurve.getCurve(), 1e-9); final List<Double> inverseShifts = Arrays.asList(-2., -3., -4., -5.); assertCurveEquals(ORIGINAL_CURVE.getCurve(), YieldCurveUtils.withPointShifts(shiftedCurve, points, inverseShifts, ShiftType.ABSOLUTE).getCurve(), 1e-9); points = Arrays.asList(1.1, 2.1, 3.1, 4.1); double[] newT = new double[] {1, 1.1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 6, 7}; double[] newY = new double[] {0.01, INTERPOLATED_CURVE.getYValue(1.1) + 2, 0.015, INTERPOLATED_CURVE.getYValue(2.1) + 3, 0.017, INTERPOLATED_CURVE.getYValue(3.1) + 4, 0.02, INTERPOLATED_CURVE.getYValue(4.1) + 5, 0.025, 0.035, 0.05}; expectedCurve = InterpolatedDoublesCurve.from(newT, newY, INTERPOLATOR); shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.ABSOLUTE); assertCurveEquals(expectedCurve, shiftedCurve.getCurve(), 1e-9); newT = new double[] {1, 1.1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 6, 7}; newY = new double[] {0.01, INTERPOLATED_CURVE.getYValue(1.1), 0.015, INTERPOLATED_CURVE.getYValue(2.1), 0.017, INTERPOLATED_CURVE.getYValue(3.1), 0.02, INTERPOLATED_CURVE.getYValue(4.1), 0.025, 0.035, 0.05}; expectedCurve = InterpolatedDoublesCurve.from(newT, newY, INTERPOLATOR); assertCurveEquals(expectedCurve, YieldCurveUtils.withPointShifts(shiftedCurve, points, inverseShifts, ShiftType.ABSOLUTE).getCurve(), 1e-9); }
@Test public void testPointRelativeShifts() { List<Double> points = Arrays.asList(1., 2., 3., 4.); final List<Double> shifts = Arrays.asList(2., 3., 4., 5.); final double[] y = new double[Y.length]; System.arraycopy(Y, 0, y, 0, Y.length); y[0] *= 3; y[1] *= 4; y[2] *= 5; y[3] *= 6; YieldCurve shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.RELATIVE); DoublesCurve expectedCurve = InterpolatedDoublesCurve.from(T, y, INTERPOLATOR); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertCurveEquals(expectedCurve, shiftedCurve.getCurve(), 1e-9); shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.RELATIVE); assertEquals("ORIGINAL_WithPointShifts", shiftedCurve.getName()); assertCurveEquals(expectedCurve, shiftedCurve.getCurve(), 1e-9); final List<Double> inverseShifts = Arrays.asList(-2. / 3, -0.75, -0.8, -5 / 6.); assertCurveEquals(ORIGINAL_CURVE.getCurve(), YieldCurveUtils.withPointShifts(shiftedCurve, points, inverseShifts, ShiftType.RELATIVE).getCurve(), 1e-9); points = Arrays.asList(1.1, 2.1, 3.1, 4.1); double[] newT = new double[] {1, 1.1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 6, 7}; double[] newY = new double[] {0.01, INTERPOLATED_CURVE.getYValue(1.1) * 3, 0.015, INTERPOLATED_CURVE.getYValue(2.1) * 4, 0.017, INTERPOLATED_CURVE.getYValue(3.1) * 5, 0.02, INTERPOLATED_CURVE.getYValue(4.1) * 6, 0.025, 0.035, 0.05}; expectedCurve = InterpolatedDoublesCurve.from(newT, newY, INTERPOLATOR); shiftedCurve = YieldCurveUtils.withPointShifts(ORIGINAL_CURVE, points, shifts, ShiftType.RELATIVE); assertCurveEquals(expectedCurve, shiftedCurve.getCurve(), 1e-9); newT = new double[] {1, 1.1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 6, 7}; newY = new double[] {0.01, INTERPOLATED_CURVE.getYValue(1.1), 0.015, INTERPOLATED_CURVE.getYValue(2.1), 0.017, INTERPOLATED_CURVE.getYValue(3.1), 0.02, INTERPOLATED_CURVE.getYValue(4.1), 0.025, 0.035, 0.05}; expectedCurve = InterpolatedDoublesCurve.from(newT, newY, INTERPOLATOR); assertCurveEquals(expectedCurve, YieldCurveUtils.withPointShifts(shiftedCurve, points, inverseShifts, ShiftType.RELATIVE).getCurve(), 1e-9); }