@Test public void testReturnsWithDividendsAtDifferentTimes() { final int n = 20; final LocalDate[] times = new LocalDate[n]; final double[] data = new double[n]; final double[] returns = new double[n - 1]; double random; for (int i = 0; i < n; i++) { times[i] = LocalDate.ofEpochDay(i); random = RANDOM.nextDouble(); data[i] = random; if (i > 0) { returns[i - 1] = Math.log(random / data[i - 1]); } } final LocalDateDoubleTimeSeries dividendTS = ImmutableLocalDateDoubleTimeSeries.of( new LocalDate[] {LocalDate.ofEpochDay(300)}, new double[] {3}); final LocalDateDoubleTimeSeries priceTS = ImmutableLocalDateDoubleTimeSeries.of(times, data); final LocalDateDoubleTimeSeries returnTS = ImmutableLocalDateDoubleTimeSeries.of(Arrays.copyOfRange(times, 1, n), returns); assertTrue( CALCULATOR .evaluate(new LocalDateDoubleTimeSeries[] {priceTS, dividendTS}) .equals(returnTS)); }
@Test public void testReturnsWithZeroesInSeries() { final int n = 20; final LocalDate[] times = new LocalDate[n]; final double[] data = new double[n]; final double[] returns = new double[n - 3]; double random; for (int i = 0; i < n - 2; i++) { times[i] = LocalDate.ofEpochDay(i); random = RANDOM.nextDouble(); data[i] = random; if (i > 0) { returns[i - 1] = Math.log(random / data[i - 1]); } } times[n - 2] = LocalDate.ofEpochDay(n - 2); data[n - 2] = 0; times[n - 1] = LocalDate.ofEpochDay(n - 1); data[n - 1] = RANDOM.nextDouble(); final LocalDateDoubleTimeSeries priceTS = ImmutableLocalDateDoubleTimeSeries.of(times, data); final LocalDateDoubleTimeSeries returnTS = ImmutableLocalDateDoubleTimeSeries.of(Arrays.copyOfRange(times, 1, n - 2), returns); final TimeSeriesReturnCalculator strict = new ContinuouslyCompoundedTimeSeriesReturnCalculator(CalculationMode.STRICT); final LocalDateDoubleTimeSeries[] tsArray = new LocalDateDoubleTimeSeries[] {priceTS}; try { strict.evaluate(tsArray); Assert.fail(); } catch (final TimeSeriesException e) { // Expected } final TimeSeriesReturnCalculator lenient = new ContinuouslyCompoundedTimeSeriesReturnCalculator(CalculationMode.LENIENT); assertTrue(lenient.evaluate(tsArray).equals(returnTS)); }
public void testCycle4() { ImmutableLocalDateDoubleTimeSeries cycleObject = cycleObject(ImmutableLocalDateDoubleTimeSeries.class, ts); assertEquals(ImmutableLocalDateDoubleTimeSeries.class, cycleObject.getClass()); assertEquals(ts, cycleObject); cycleObject = cycleObject( ImmutableLocalDateDoubleTimeSeries.class, ImmutableLocalDateDoubleTimeSeries.EMPTY_SERIES); assertEquals(ImmutableLocalDateDoubleTimeSeries.EMPTY_SERIES, cycleObject); }
@Test(expectedExceptions = TimeSeriesException.class) public void testWithBadInputs() { final LocalDateDoubleTimeSeries ts = ImmutableLocalDateDoubleTimeSeries.of( new LocalDate[] {LocalDate.ofEpochDay(1)}, new double[] {4}); CALCULATOR.evaluate(new LocalDateDoubleTimeSeries[] {ts}); }
@BeforeMethod public void setUp() { dates = new LocalDate[] {LocalDate.of(2012, 6, 30), LocalDate.of(2012, 7, 1)}; values = new double[] {1.1d, 2.2d}; ts = ImmutableLocalDateDoubleTimeSeries.of(dates, values); empty = ImmutableLocalDateDoubleTimeSeries.EMPTY_SERIES; }
@Override protected LocalDateDoubleTimeSeries getReturnSeries( LocalDateDoubleTimeSeries ts, ValueRequirement desiredValue) { LocalDateDoubleTimeSeries differenceSeries = super.getReturnSeries(ts, desiredValue); double lambda = Double.parseDouble( desiredValue.getConstraint( VolatilityWeightingFunctionUtils.VOLATILITY_WEIGHTING_LAMBDA_PROPERTY)); TimeSeriesWeightedVolatilityOperator weightedVol = new TimeSeriesWeightedVolatilityOperator(lambda); LocalDateDoubleTimeSeries weightedVolSeries = (LocalDateDoubleTimeSeries) weightedVol.evaluate(ts); int n = weightedVolSeries.size(); double endDateWeightedVol = weightedVolSeries.getLatestValueFast(); double[] volWeightedDifferences = new double[n]; for (int i = 0; i < n; i++) { System.out.println( differenceSeries.getTimeAtIndex(i) + "," + differenceSeries.getValueAtIndexFast(i) + "," + weightedVolSeries.getValueAtIndexFast(i)); volWeightedDifferences[i] = differenceSeries.getValueAtIndexFast(i) * endDateWeightedVol / weightedVolSeries.getValueAtIndexFast(i); } LocalDateDoubleTimeSeries volWeightedDifferenceSeries = ImmutableLocalDateDoubleTimeSeries.of( weightedVolSeries.timesArrayFast(), volWeightedDifferences); return volWeightedDifferenceSeries; }
private static MarketDataEnvironment createSuppliedData() { LocalDateDoubleTimeSeries optionPrice = ImmutableLocalDateDoubleTimeSeries.of(VALUATION_TIME.toLocalDate(), 0.975); RawId<Double> optionRawId = RawId.of(ExternalSchemes.syntheticSecurityId("Test future option").toBundle()); MarketDataEnvironmentBuilder builder = new MarketDataEnvironmentBuilder(); builder.add(optionRawId, optionPrice); builder.add(VolatilitySurfaceId.of("TestExchange"), new VolatilitySurface(TEST_SURFACE)); builder.valuationTime(VALUATION_TIME); return builder.build(); }
/** * Adds or updates a time-series in the master. Will not "erase" any existing point, just used to * add a new point. * * @param description a description of the time-series for display purposes, not null * @param dataSource the data source, not null * @param dataProvider the data provider, not null * @param dataField the data field, not null * @param observationTime the descriptive observation time key, e.g. LONDON_CLOSE, not null * @param externalIdBundle the external identifiers with which the time-series is associated, not * null * @param date the date, not null * @param value the value, not null * @return the unique identifier of the time-series */ public UniqueId writeTimeSeriesPoint( String description, String dataSource, String dataProvider, String dataField, String observationTime, ExternalIdBundle externalIdBundle, LocalDate date, double value) { LocalDateDoubleTimeSeries ts = ImmutableLocalDateDoubleTimeSeries.of(date, value); return writeTimeSeries( description, dataSource, dataProvider, dataField, observationTime, externalIdBundle, ts); }
public void testEmptyCycle4() { ImmutableLocalDateDoubleTimeSeries cycleObject = cycleObject(ImmutableLocalDateDoubleTimeSeries.class, empty); assertEquals(ImmutableLocalDateDoubleTimeSeries.class, cycleObject.getClass()); assertEquals(empty, cycleObject); }