@@ -0,0 +1,1735 @@
+package de.lmu.ifi.dbs.elki.math;%0A%0Aimport static org.junit.Assert.assertEquals;%0A%0Aimport java.util.Random;%0A%0Aimport org.junit.Test;%0A%0Aimport de.lmu.ifi.dbs.elki.JUnit4Test;%0A%0A/**%0A * Unit test if negative weights in %7B@link MeanVariance%7D work as desired.%0A * %0A * @author Erich Schubert%0A */%0Apublic class TestSlidingVariance implements JUnit4Test %7B%0A  /**%0A   * Size of test data set.%0A   */%0A  private static final int SIZE = 100000;%0A%0A  /**%0A   * Sliding window size.%0A   */%0A  private static final int WINDOWSIZE = 100;%0A%0A  @Test%0A  public void testSlidingWindowVariance() %7B%0A    MeanVariance mv = new MeanVariance();%0A    MeanVariance mc = new MeanVariance();%0A%0A    Random r = new Random();%0A    double%5B%5D data = new double%5BSIZE%5D;%0A    for(int i = 0; i %3C data.length; i++) %7B%0A      data%5Bi%5D = r.nextDouble();%0A    %7D%0A%0A    // Pre-roll:%0A    for(int i = 0; i %3C WINDOWSIZE; i++) %7B%0A      mv.put(data%5Bi%5D);%0A    %7D%0A    // Compare to window approach%0A    for(int i = WINDOWSIZE; i %3C data.length; i++) %7B%0A      mv.put(data%5Bi - WINDOWSIZE%5D, -1.); // Remove%0A      mv.put(data%5Bi%5D);%0A%0A      mc.reset(); // Reset statistics%0A      for(int j = i + 1 - WINDOWSIZE; j %3C= i; j++) %7B%0A        mc.put(data%5Bj%5D);%0A      %7D%0A      // Fully manual statistics, exact two-pass algorithm:%0A      double mean = 0.0;%0A      for(int j = i + 1 - WINDOWSIZE; j %3C= i; j++) %7B%0A        mean += data%5Bj%5D;%0A      %7D%0A      mean /= WINDOWSIZE;%0A      double var = 0.0;%0A      for(int j = i + 1 - WINDOWSIZE; j %3C= i; j++) %7B%0A        double v = data%5Bj%5D - mean;%0A        var += v * v;%0A      %7D%0A      var /= (WINDOWSIZE - 1);%0A      assertEquals(%22Variance does not agree.%22, mv.getSampleVariance(), mc.getSampleVariance(), 1e-14);%0A      assertEquals(%22Variance does not agree.%22, mv.getSampleVariance(), var, 1e-14);%0A    %7D%0A  %7D%0A%7D%0A

Test sliding window variance support.