### Question:
Autocorrelation { public static double[] calculate(double[] samples) { double[] acovResult = autocovariance(samples); double variance = acovResult[0]; double[] autocorr = Arrays.stream(acovResult).map(x -> x / variance).toArray(); return autocorr; }  static double[] calculate(double[] samples);  }### Answer:
@Test public void autocorrAtLagZeroIsOne() { double[] samples = generateUniformRandomArray(20); double[] autocorr = Autocorrelation.calculate(samples); assertEquals(autocorr[0], 1.0, 0.0); }
@Test public void randomlyGeneratedSamplesHaveCloseToZeroAutocorrelationAtLowLags() { double[] samples = generateUniformRandomArray(500); double[] autocorr = Autocorrelation.calculate(samples); for (int lag = 1; lag < 10; lag++) { assertThat(autocorr[lag], lessThan(0.1)); } }
@Test public void increasingSequenceReturnsCorrectAutocorr() { double[] samples = {1, 2, 3, 4, 5, 6, 7, 8}; double[] autocorr = Autocorrelation.calculate(samples); double[] expected = {1., 0.625, 0.27380952, -0.0297619, -0.26190476, -0.39880952, -0.41666667, -0.29166667}; assertArrayEquals(expected, autocorr, 0.0001); }