package macrobase.analysis.stats;

import macrobase.conf.MacroBaseConf;
import macrobase.datamodel.Datum;

import org.apache.commons.math3.linear.ArrayRealVector;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

public class MADTest {
    private static final Logger log = LoggerFactory.getLogger(MADTest.class);

    @Test
    public void simpleTest() {
        MAD m = new MAD(new MacroBaseConf());

        List<Datum> data = new ArrayList<>();
        for (int i = 0; i < 100; ++i) {
            double[] sample = new double[1];
            sample[0] = i;
            data.add(new Datum(new ArrayList<>(), new ArrayRealVector(sample)));
        }

        m.train(data);
        assertEquals(1.98, m.score(data.get(0)), 1e-5);
        assertEquals(1.98, m.score(data.get(data.size() - 1)), 1e-5);
        assertEquals(0.02, m.score(data.get(50)), 1e-5);
    }

    @Test
    public void zScoreTest() {
        MAD m = new MAD(new MacroBaseConf());

        List<Datum> data = new ArrayList<>();
        for (int i = 0; i < 10; ++i) {
            double[] sample = new double[1];
            sample[0] = i;
            data.add(new Datum(new ArrayList<>(), new ArrayRealVector(sample)));
        }

        double[] sample = new double[1];
        sample[0] = 20;
        data.add(new Datum(new ArrayList<>(), new ArrayRealVector(sample)));


        m.train(data);
        log.debug("score is {}", m.score(data.get(data.size() - 1)));

        assertEquals(5.0, m.score(data.get(data.size() - 1)), 1e-5);
        assertEquals(5.0 / 1.4826,
                     m.getZScoreEquivalent(m.score(data.get(data.size() - 1))),
                     1e-1);
    }
}

@@ -973,24 +973,614 @@
 -5);%0A    %7D%0A%0A
+    @Test%0A    public void zeroMADTest() %7B%0A        MAD m = new MAD(new MacroBaseConf());%0A%0A        List%3CDatum%3E data = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C 30; ++i) %7B%0A            double%5B%5D sample = new double%5B1%5D;%0A            if (i == 0 %7C%7C i  %3E= 28) %7B%0A                sample%5B0%5D = 5;%0A            %7D%0A            else %7B%0A                sample%5B0%5D = 10;%0A            %7D%0A            data.add(new Datum(new ArrayList%3C%3E(), new ArrayRealVector(sample)));%0A        %7D%0A%0A        m.train(data);%0A        assertEquals(27, m.score(data.get(0)), 0);%0A        assertEquals(0, m.score(data.get(2)), 0);%0A    %7D%0A%0A
     @Test%0A  

Test for zero MAD fallback to trimmed means.