/** * Obtains a time-series containing a single date and value. * * @param date the singleton date * @param value the singleton value * @return the time-series */ public static LocalDateDoubleTimeSeries of(LocalDate date, double value) { ArgChecker.notNull(date, "date"); return builder().put(date, value).build(); }
/** * Obtains the union of a pair of time series. * <p> * This returns a time-series with the union of the dates of the two inputs. * When the same date occurs in both time-series, the operator is invoked to combine the values. * * @param other the time-series to combine with * @param mapper the function to be used to combine the values * @return a new time-series containing the dates in common between the * input series with their values combined together using the function */ public default LocalDateDoubleTimeSeries union(LocalDateDoubleTimeSeries other, DoubleBinaryOperator mapper) { ArgChecker.notNull(other, "other"); ArgChecker.notNull(mapper, "mapper"); LocalDateDoubleTimeSeriesBuilder builder = new LocalDateDoubleTimeSeriesBuilder(stream()); other.stream().forEach(pt -> builder.merge(pt, mapper)); return builder.build(); }
@Override public LocalDateDoubleTimeSeries mapDates(Function<? super LocalDate, ? extends LocalDate> mapper) { List<LocalDate> dates = dates().map(mapper).collect(toImmutableList()); dates.stream().reduce(this::checkAscending); return LocalDateDoubleTimeSeries.builder().putAll(dates, Doubles.asList(points)).build(); }
public void test_putAll_toBuilder() { Collection<LocalDate> dates = Arrays.asList(date(2013, 1, 1), date(2014, 1, 1)); Collection<Double> values = Doubles.asList(2d, 3d); LocalDateDoubleTimeSeries base = LocalDateDoubleTimeSeries.builder().putAll(dates, values).build(); LocalDateDoubleTimeSeriesBuilder test = LocalDateDoubleTimeSeries.builder(); test.put(date(2012, 1, 1), 0d); test.put(date(2013, 1, 1), 1d); test.putAll(base.toBuilder()); assertEquals(test.get(date(2012, 1, 1)), OptionalDouble.of(0d)); assertEquals(test.get(date(2013, 1, 1)), OptionalDouble.of(2d)); assertEquals(test.get(date(2014, 1, 1)), OptionalDouble.of(3d)); }
/** * Creates an instance. * <p> * Use {@link LocalDateDoubleTimeSeries#toBuilder()}. * * @param dates the dates to initialize with * @param values the values to initialize with */ LocalDateDoubleTimeSeriesBuilder(LocalDate[] dates, double[] values) { for (int i = 0; i < dates.length; i++) { put(dates[i], values[i]); } }
/** * Obtains the intersection of a pair of time series. * <p> * This returns a time-series with the intersection of the dates of the two inputs. * The operator is invoked to combine the values. * * @param other the time-series to combine with * @param mapper the function to be used to combine the values * @return a new time-series containing the dates in common between the * input series with their values combined together using the function */ public default LocalDateDoubleTimeSeries intersection(LocalDateDoubleTimeSeries other, DoubleBinaryOperator mapper) { ArgChecker.notNull(other, "other"); ArgChecker.notNull(mapper, "mapper"); return new LocalDateDoubleTimeSeriesBuilder() .putAll(stream() .filter(pt -> other.containsDate(pt.getDate())) .map(pt -> LocalDateDoublePoint.of( pt.getDate(), mapper.applyAsDouble(pt.getValue(), other.get(pt.getDate()).getAsDouble())))) .build(); }
@Override public LocalDateDoubleTimeSeries filter(ObjDoublePredicate<LocalDate> predicate) { Stream<LocalDateDoublePoint> filteredPoints = stream().filter(pt -> predicate.test(pt.getDate(), pt.getValue())); // As we may have changed the density of the series by filtering // go via the builder to get the best implementation return new LocalDateDoubleTimeSeriesBuilder(filteredPoints).build(); }
public void test_NaN_is_not_allowed() { assertThrowsIllegalArg(() -> LocalDateDoubleTimeSeries.of(DATE_2015_01_02, Double.NaN)); assertThrowsIllegalArg(() -> LocalDateDoubleTimeSeries.builder().put(DATE_2015_01_02, Double.NaN)); assertThrowsIllegalArg(() -> LocalDateDoubleTimeSeries.builder().putAll( ImmutableMap.of(DATE_2015_01_02, Double.NaN))); assertThrowsIllegalArg(() -> LocalDateDoubleTimeSeries.builder().put( LocalDateDoublePoint.of(DATE_2015_01_02, Double.NaN))); assertThrowsIllegalArg(() -> LocalDateDoubleTimeSeries.builder().putAll( ImmutableList.of(DATE_2015_01_02), ImmutableList.of(Double.NaN))); assertThrowsIllegalArg(() -> LocalDateDoubleTimeSeries.builder().putAll( ImmutableList.of(LocalDateDoublePoint.of(DATE_2015_01_02, Double.NaN)))); LocalDateDoubleTimeSeries s1 = LocalDateDoubleTimeSeries.of(DATE_2015_01_02, 1d); LocalDateDoubleTimeSeries s2 = LocalDateDoubleTimeSeries.of(DATE_2015_01_02, 2d); assertThrowsIllegalArg(() -> s1.intersection(s2, (d1, d2) -> Double.NaN)); assertThrowsIllegalArg(() -> s1.mapValues(d -> Double.NaN)); }
public void test_get() { LocalDateDoubleTimeSeriesBuilder test = LocalDateDoubleTimeSeries.builder() .put(date(2014, 1, 1), 14) .put(date(2012, 1, 1), 12) .put(date(2013, 1, 1), 13); assertEquals(test.get(date(2012, 1, 1)), OptionalDouble.of(12d)); assertEquals(test.get(date(2013, 1, 1)), OptionalDouble.of(13d)); assertEquals(test.get(date(2014, 1, 1)), OptionalDouble.of(14d)); assertEquals(test.get(date(2015, 1, 1)), OptionalDouble.empty()); }
/** * Creates an empty builder, used to create time-series. * <p> * The builder has methods to create and modify a time-series. * * @return the time-series builder */ public static LocalDateDoubleTimeSeriesBuilder builder() { return new LocalDateDoubleTimeSeriesBuilder(); }
public void test_putAll_stream() { Collection<LocalDate> dates = Arrays.asList(date(2013, 1, 1), date(2014, 1, 1)); Collection<Double> values = Doubles.asList(2d, 3d); LocalDateDoubleTimeSeries base = LocalDateDoubleTimeSeries.builder().putAll(dates, values).build(); LocalDateDoubleTimeSeriesBuilder test = LocalDateDoubleTimeSeries.builder(); test.put(date(2012, 1, 1), 0d); test.put(date(2013, 1, 1), 1d); test.putAll(base.stream()); assertEquals(test.get(date(2012, 1, 1)), OptionalDouble.of(0d)); assertEquals(test.get(date(2013, 1, 1)), OptionalDouble.of(2d)); assertEquals(test.get(date(2014, 1, 1)), OptionalDouble.of(3d)); }
/** * Puts all the entries from the supplied map into this builder. * <p> * If a date is duplicated it will overwrite an earlier entry. * * @param map the map of points to be added * @return this builder */ public LocalDateDoubleTimeSeriesBuilder putAll(Map<LocalDate, Double> map) { ArgChecker.noNulls(map, "map"); map.entrySet().forEach(e -> put(e.getKey(), e.getValue())); return this; }
@Override public LocalDateDoubleTimeSeriesBuilder toBuilder() { return new LocalDateDoubleTimeSeriesBuilder(dates, values); }
private void assertLibor6mSeries(LocalDateDoubleTimeSeries actualSeries) { assertEquals(actualSeries.size(), 3); LocalDateDoubleTimeSeries expectedSeries = LocalDateDoubleTimeSeries.builder() .put(LocalDate.of(1971, 1, 4), 0.0681) .put(LocalDate.of(1971, 1, 5), 0.0675) .put(LocalDate.of(1971, 1, 6), 0.0669) .build(); assertEquals(actualSeries, expectedSeries); }
/** * Creates an instance. * <p> * Use {@link DenseLocalDateDoubleTimeSeries#toBuilder()}. * * @param points the stream of points to initialize with */ LocalDateDoubleTimeSeriesBuilder(Stream<LocalDateDoublePoint> points) { points.forEach(pt -> put(pt.getDate(), pt.getValue())); }
@Override public LocalDateDoubleTimeSeriesBuilder toBuilder() { return new LocalDateDoubleTimeSeriesBuilder(stream()); }
private void assertLibor3mSeries(LocalDateDoubleTimeSeries actualSeries) { assertEquals(actualSeries.size(), 3); LocalDateDoubleTimeSeries expectedSeries = LocalDateDoubleTimeSeries.builder() .put(LocalDate.of(1971, 1, 4), 0.065) .put(LocalDate.of(1971, 1, 5), 0.0638) .put(LocalDate.of(1971, 1, 6), 0.0638) .build(); assertEquals(actualSeries, expectedSeries); }