TaylorExpansionMultiplierCalculator { public static DoubleTimeSeries<?> getTimeSeries(final Map<UnderlyingType, DoubleTimeSeries<?>> underlyingData, final Underlying underlying) { Validate.notNull(underlying, "underlying"); Validate.notNull(underlyingData, "underlying data"); Validate.notEmpty(underlyingData, "underlying data"); Validate.noNullElements(underlyingData.keySet(), "underlying data keys"); Validate.noNullElements(underlyingData.values(), "underlying data values"); if (underlying instanceof NthOrderUnderlying) { final NthOrderUnderlying nthOrder = (NthOrderUnderlying) underlying; final int n = nthOrder.getOrder(); if (n == 0) { throw new UnsupportedOperationException(); } final UnderlyingType type = nthOrder.getUnderlying(); Validate.isTrue(underlyingData.containsKey(type)); DoubleTimeSeries<?> ts = underlyingData.get(type); ts = ts.power(n); return ts.multiply(getMultiplier(underlying)); } else if (underlying instanceof MixedOrderUnderlying) { final MixedOrderUnderlying mixedOrder = (MixedOrderUnderlying) underlying; DoubleTimeSeries<?> result = null; DoubleTimeSeries<?> multiplier = null; int size = 0; for (final NthOrderUnderlying underlyingOrder : mixedOrder.getUnderlyingOrders()) { if (result == null) { result = getTimeSeries(underlyingData, underlyingOrder); size = result.size(); } else { multiplier = getTimeSeries(underlyingData, underlyingOrder); if (multiplier.size() != size) { throw new IllegalArgumentException("Time series in map were not the same length"); } result = result.multiply(multiplier); if (result.size() != size) { throw new IllegalArgumentException("Time series in map did not contain the same times"); } } } return result; } throw new IllegalArgumentException("Order was neither NthOrderUnderlying nor MixedOrderUnderlying: have " + underlying.getClass()); }  static double getMultiplier(final Underlying underlying); static double getValue(final Map<UnderlyingType, Double> underlyingData, final Underlying underlying); static DoubleTimeSeries<?> getTimeSeries(final Map<UnderlyingType, DoubleTimeSeries<?>> underlyingData, final Underlying underlying);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullUnderlying() { getTimeSeries(UNDERLYING_DATA, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullMap() { getTimeSeries(null, FIRST_ORDER); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEmptyMap() { getTimeSeries(Collections.<UnderlyingType, DoubleTimeSeries<?>> emptyMap(), FIRST_ORDER); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testUnderlyingType2() { getTimeSeries(UNDERLYING_DATA, NEW_TYPE); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullKey() { getTimeSeries(Collections.<UnderlyingType, DoubleTimeSeries<?>> singletonMap(null, X), FIFTH_ORDER); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullValue() { getTimeSeries(Collections.<UnderlyingType, DoubleTimeSeries<?>> singletonMap(UnderlyingType.SPOT_PRICE, null), FIFTH_ORDER); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongTSLength() { final DoubleTimeSeries<?> z = ImmutableInstantDoubleTimeSeries.of(new long[] {1, 2, 3, 4, 5}, new double[] {1, 1, 1, 1, 1}); final Map<UnderlyingType, DoubleTimeSeries<?>> m = new HashMap<>(); m.put(UnderlyingType.SPOT_PRICE, X); m.put(UnderlyingType.IMPLIED_VOLATILITY, z); getTimeSeries(m, MIXED_ORDER); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongTSTimes() { final int n = 100; final long[] t = new long[n]; final double[] z = new double[n]; for (int i = 0; i < n; i++) { t[i] = i + 1; z[i] = Math.random() - 0.5; } final DoubleTimeSeries<?> ts = ImmutableInstantDoubleTimeSeries.of(t, z); final Map<UnderlyingType, DoubleTimeSeries<?>> m = new HashMap<>(); m.put(UnderlyingType.SPOT_PRICE, X); m.put(UnderlyingType.IMPLIED_VOLATILITY, ts); getTimeSeries(m, MIXED_ORDER); }