TimeSeriesWeightedVolatilityOperator extends Function1D<DateDoubleTimeSeries<?>, DateDoubleTimeSeries<?>> { @Override public DateDoubleTimeSeries<?> evaluate(DateDoubleTimeSeries<?> ts) { ArgumentChecker.notNull(ts, "time series"); ArgumentChecker.isTrue(ts.size() > _seedLength, "time series length must be > ", _seedLength); DateDoubleTimeSeries<?> returnSeries = _changeOperator.evaluate(ts); int n = returnSeries.size(); int[] returnTimes = returnSeries.timesArrayFast(); int seedLengthAdjusted = Math.max(_seedLength, 1); double seedVariance = 0.0; for (int i = 0; i < seedLengthAdjusted; i++) { double returnTs = returnSeries.getValueAtIndexFast(i); seedVariance += returnTs * returnTs; } seedVariance /= seedLengthAdjusted; int outputLength = n - seedLengthAdjusted + 1; double[] weightedVolatilities = new double[outputLength]; int[] volatilityTimes = new int[outputLength]; weightedVolatilities[0] = Math.sqrt(seedVariance); volatilityTimes[0] = returnTimes[seedLengthAdjusted - 1]; double ewmaVariance = seedVariance; for (int i = 0; i < outputLength - 1; i++) { double returnTs = returnSeries.getValueAtIndexFast(i + seedLengthAdjusted); ewmaVariance = _lambda * ewmaVariance + (1 - _lambda) * returnTs * returnTs; weightedVolatilities[i + 1] = Math.sqrt(ewmaVariance); volatilityTimes[i + 1] = returnTimes[i + seedLengthAdjusted]; } return ImmutableLocalDateDoubleTimeSeries.of(volatilityTimes, weightedVolatilities); } TimeSeriesWeightedVolatilityOperator(
      Function1D<DateDoubleTimeSeries<?>, DateDoubleTimeSeries<?>> returnOperator, 
      double lambda,
      int seedLength); @Override DateDoubleTimeSeries<?> evaluate(DateDoubleTimeSeries<?> ts); static TimeSeriesWeightedVolatilityOperator relative(double lambda); static TimeSeriesWeightedVolatilityOperator absolute(double lambda);  }
@Test public void ewmaRelative1NoSeed() { DateDoubleTimeSeries<?> tsEwma = OP_EWMA_1.evaluate(TS_1); DateDoubleTimeSeries<?> tsRet = OP_REL_1.evaluate(TS_1); assertEquals(NB_DATA_1 - 1, tsEwma.size()); double variancePrevious = tsRet.getValueAtIndex(0) * tsRet.getValueAtIndex(0); double volExpected0 = Math.sqrt(variancePrevious); double volComputed0 = tsEwma.getValueAtIndex(0); assertEquals("TimeSeriesWeightedVolatilityOperator: volatility - " + 0, volExpected0, volComputed0, TOLERANCE_DIFF); for (int i = 1; i < tsEwma.size(); i++) { assertEquals(tsEwma.getTimeAtIndex(i), tsRet.getTimeAtIndex(i)); double varianceExpected = LAMBDA * variancePrevious + (1.0d - LAMBDA) * tsRet.getValueAtIndex(i) * tsRet.getValueAtIndex(i); double volExpected = Math.sqrt(varianceExpected); double volComputed = tsEwma.getValueAtIndex(i); assertEquals("TimeSeriesWeightedVolatilityOperator: volatility - " + i, volExpected, volComputed, TOLERANCE_DIFF); variancePrevious = varianceExpected; } }
@Test public void ewmaRelative2NoSeed() { DateDoubleTimeSeries<?> tsEwma = OP_EWMA_2.evaluate(TS_1); DateDoubleTimeSeries<?> tsRet = OP_REL_2.evaluate(TS_1); assertEquals(NB_DATA_1 - 2, tsEwma.size()); double variancePrevious = tsRet.getValueAtIndex(0) * tsRet.getValueAtIndex(0); double volExpected0 = Math.sqrt(variancePrevious); double volComputed0 = tsEwma.getValueAtIndex(0); assertEquals("TimeSeriesWeightedVolatilityOperator: volatility - " + 0, volExpected0, volComputed0, TOLERANCE_DIFF); for (int i = 1; i < tsEwma.size(); i++) { assertEquals(tsEwma.getTimeAtIndex(i), tsRet.getTimeAtIndex(i)); double varianceExpected = LAMBDA * variancePrevious + (1.0d - LAMBDA) * tsRet.getValueAtIndex(i) * tsRet.getValueAtIndex(i); double volExpected = Math.sqrt(varianceExpected); double volComputed = tsEwma.getValueAtIndex(i); assertEquals("TimeSeriesWeightedVolatilityOperator: volatility - " + i, volExpected, volComputed, TOLERANCE_DIFF); variancePrevious = varianceExpected; } }
@Test public void ewmaRelative1Seed1() { DateDoubleTimeSeries<?> tsEwmaNS = OP_EWMA_1.evaluate(TS_1); TimeSeriesWeightedVolatilityOperator opEwmaS1 = new TimeSeriesWeightedVolatilityOperator(OP_REL_1, LAMBDA, 1); DateDoubleTimeSeries<?> tsEwmaS1 = opEwmaS1.evaluate(TS_1); assertEquals(tsEwmaNS.size(), tsEwmaS1.size()); for (int i = 0; i < tsEwmaNS.size(); i++) { assertEquals(tsEwmaNS.getTimeAtIndex(i), tsEwmaS1.getTimeAtIndex(i)); assertEquals("TimeSeriesWeightedVolatilityOperator: volatility - " + i, tsEwmaNS.getValueAtIndexFast(i), tsEwmaS1.getValueAtIndexFast(i), TOLERANCE_DIFF); } }
@Test public void ewmaRelative1Seed10() { int seedLength = 10; DateDoubleTimeSeries<?> tsRet = OP_REL_1.evaluate(TS_1); TimeSeriesWeightedVolatilityOperator opEwmaS10 = new TimeSeriesWeightedVolatilityOperator(OP_REL_1, LAMBDA, seedLength); DateDoubleTimeSeries<?> tsEwmaS1 = opEwmaS10.evaluate(TS_1); assertEquals(tsRet.size() - seedLength + 1, tsEwmaS1.size()); int outputLength = tsEwmaS1.size(); double seedVariance = 0.0; for (int i = 0; i < seedLength; i++) { double returnTs = tsRet.getValueAtIndexFast(i); seedVariance += returnTs * returnTs; } seedVariance /= seedLength; assertEquals(tsRet.getTimeAtIndexFast(seedLength - 1), tsEwmaS1.getTimeAtIndexFast(0)); assertEquals(Math.sqrt(seedVariance), tsEwmaS1.getValueAtIndexFast(0), TOLERANCE_DIFF); double varianceEwma = seedVariance; for (int i = 1; i < outputLength; i++) { varianceEwma = LAMBDA * varianceEwma + (1.0d - LAMBDA) * tsRet.getValueAtIndexFast(i + seedLength - 1) * tsRet.getValueAtIndexFast(i + seedLength - 1); assertEquals(tsRet.getTimeAtIndexFast(seedLength - 1 + i), tsEwmaS1.getTimeAtIndexFast(i)); assertEquals("TimeSeriesWeightedVolatilityOperator: volatility - " + i, Math.sqrt(varianceEwma), tsEwmaS1.getValueAtIndexFast(i), TOLERANCE_DIFF); } }
@Test public void ewmaRelative2Seed10() { int seedLength = 10; DateDoubleTimeSeries<?> tsRet = OP_REL_2.evaluate(TS_1); TimeSeriesWeightedVolatilityOperator opEwmaS10 = new TimeSeriesWeightedVolatilityOperator(OP_REL_2, LAMBDA, seedLength); DateDoubleTimeSeries<?> tsEwmaS1 = opEwmaS10.evaluate(TS_1); assertEquals(tsRet.size() - seedLength + 1, tsEwmaS1.size()); int outputLength = tsEwmaS1.size(); double seedVariance = 0.0; for (int i = 0; i < seedLength; i++) { double returnTs = tsRet.getValueAtIndexFast(i); seedVariance += returnTs * returnTs; } seedVariance /= seedLength; assertEquals(tsRet.getTimeAtIndexFast(seedLength - 1), tsEwmaS1.getTimeAtIndexFast(0)); assertEquals(Math.sqrt(seedVariance), tsEwmaS1.getValueAtIndexFast(0), TOLERANCE_DIFF); double varianceEwma = seedVariance; for (int i = 1; i < outputLength; i++) { varianceEwma = LAMBDA * varianceEwma + (1.0d - LAMBDA) * tsRet.getValueAtIndexFast(i + seedLength - 1) * tsRet.getValueAtIndexFast(i + seedLength - 1); assertEquals(tsRet.getTimeAtIndexFast(seedLength - 1 + i), tsEwmaS1.getTimeAtIndexFast(i)); assertEquals("TimeSeriesWeightedVolatilityOperator: volatility - " + i, Math.sqrt(varianceEwma), tsEwmaS1.getValueAtIndexFast(i), TOLERANCE_DIFF); } }