Search in sources :

Example 1 with BoundingBox

use of com.amazon.randomcutforest.tree.BoundingBox in project random-cut-forest-by-aws by aws.

the class SimpleInterpolationVisitorTest method testAccept.

@Test
public void testAccept() {
    float[] pointToScore = { 0.0f, 0.0f };
    int sampleSize = 50;
    SimpleInterpolationVisitor visitor = new SimpleInterpolationVisitor(pointToScore, sampleSize, 1, false);
    INodeView leafNode = mock(NodeView.class);
    float[] point = new float[] { 1.0f, -2.0f };
    when(leafNode.getLeafPoint()).thenReturn(point);
    when(leafNode.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    int leafMass = 3;
    when(leafNode.getMass()).thenReturn(leafMass);
    int depth = 4;
    visitor.acceptLeaf(leafNode, depth);
    InterpolationMeasure result = visitor.getResult();
    double expectedSumOfNewRange = 1.0 + 2.0;
    double[] expectedDifferenceInRangeVector = { 0.0, 1.0, 2.0, 0.0 };
    double[] expectedProbVector = Arrays.stream(expectedDifferenceInRangeVector).map(x -> x / expectedSumOfNewRange).toArray();
    double[] expectedNumPts = Arrays.stream(expectedProbVector).toArray();
    double[] expectedDistances = new double[2 * pointToScore.length];
    for (int i = 0; i < 2 * pointToScore.length; i++) {
        expectedDistances[i] = expectedProbVector[i] * expectedDifferenceInRangeVector[i];
    }
    for (int i = 0; i < 2 * pointToScore.length; i++) {
        expectedNumPts[i] = expectedNumPts[i] * 4;
    }
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(expectedProbVector[2 * i], result.probMass.high[i]);
        assertEquals(expectedProbVector[2 * i + 1], result.probMass.low[i]);
        assertEquals(expectedNumPts[2 * i], result.measure.high[i]);
        assertEquals(expectedNumPts[2 * i + 1], result.measure.low[i]);
        assertEquals(expectedDistances[2 * i], result.distances.high[i]);
        assertEquals(expectedDistances[2 * i + 1], result.distances.low[i]);
    }
    // parent does not contain pointToScore
    depth--;
    INodeView sibling = mock(NodeView.class);
    int siblingMass = 2;
    when(sibling.getMass()).thenReturn(siblingMass);
    INodeView parent = mock(NodeView.class);
    int parentMass = leafMass + siblingMass;
    when(parent.getMass()).thenReturn(parentMass);
    when(parent.getBoundingBox()).thenReturn(new BoundingBox(point, new float[] { 2.0f, -0.5f }));
    visitor.accept(parent, depth);
    result = visitor.getResult();
    double expectedSumOfNewRange2 = 2.0 + 2.0;
    double expectedProbOfCut2 = (1.0 + 0.5) / expectedSumOfNewRange2;
    double[] expectedDifferenceInRangeVector2 = { 0.0, 1.0, 0.5, 0.0 };
    double[] expectedDirectionalDistanceVector2 = { 0.0, 2.0, 2.0, 0.0 };
    for (int i = 0; i < 2 * pointToScore.length; i++) {
        double prob = expectedDifferenceInRangeVector2[i] / expectedSumOfNewRange2;
        expectedProbVector[i] = prob + (1 - expectedProbOfCut2) * expectedProbVector[i];
        expectedNumPts[i] = prob * (1 + parent.getMass()) + (1 - expectedProbOfCut2) * expectedNumPts[i];
        expectedDistances[i] = prob * expectedDirectionalDistanceVector2[i] + (1 - expectedProbOfCut2) * expectedDistances[i];
    }
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(expectedProbVector[2 * i], result.probMass.high[i]);
        assertEquals(expectedProbVector[2 * i + 1], result.probMass.low[i]);
        assertEquals(expectedNumPts[2 * i], result.measure.high[i]);
        assertEquals(expectedNumPts[2 * i + 1], result.measure.low[i]);
        assertEquals(expectedDistances[2 * i], result.distances.high[i]);
        assertEquals(expectedDistances[2 * i + 1], result.distances.low[i]);
    }
    // grandparent contains pointToScore
    assertFalse(visitor.pointInsideBox);
    depth--;
}
Also used : Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Test(org.junit.jupiter.api.Test) BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) Arrays(java.util.Arrays) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) INodeView(com.amazon.randomcutforest.tree.INodeView) InterpolationMeasure(com.amazon.randomcutforest.returntypes.InterpolationMeasure) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.when(org.mockito.Mockito.when) NodeView(com.amazon.randomcutforest.tree.NodeView) Mockito.mock(org.mockito.Mockito.mock) BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) InterpolationMeasure(com.amazon.randomcutforest.returntypes.InterpolationMeasure) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 2 with BoundingBox

use of com.amazon.randomcutforest.tree.BoundingBox in project random-cut-forest-by-aws by aws.

the class AnomalyAttributionVisitorTest method testAccept.

@Test
public void testAccept() {
    float[] pointToScore = { 0.0f, 0.0f };
    int treeMass = 50;
    AnomalyAttributionVisitor visitor = new AnomalyAttributionVisitor(pointToScore, treeMass, 0);
    INodeView leafNode = mock(NodeView.class);
    float[] point = new float[] { 1.0f, -2.0f };
    when(leafNode.getLeafPoint()).thenReturn(point);
    when(leafNode.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    int leafMass = 3;
    when(leafNode.getMass()).thenReturn(leafMass);
    int depth = 4;
    visitor.acceptLeaf(leafNode, depth);
    DiVector result = visitor.getResult();
    double expectedScoreSum = defaultScoreUnseenFunction(depth, leafNode.getMass());
    double sumOfNewRange = 1.0 + 2.0;
    double[] expectedUnnormalizedLow = new double[] { expectedScoreSum * 1.0 / sumOfNewRange, 0.0 };
    double[] expectedUnnormalizedHigh = new double[] { 0.0, expectedScoreSum * 2.0 / sumOfNewRange };
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(defaultScalarNormalizerFunction(expectedUnnormalizedLow[i], treeMass), result.low[i], EPSILON);
        assertEquals(defaultScalarNormalizerFunction(expectedUnnormalizedHigh[i], treeMass), result.high[i], EPSILON);
    }
    // parent does not contain pointToScore
    depth--;
    INodeView sibling = mock(NodeView.class);
    int siblingMass = 2;
    when(sibling.getMass()).thenReturn(siblingMass);
    INodeView parent = mock(NodeView.class);
    int parentMass = leafMass + siblingMass;
    when(parent.getMass()).thenReturn(parentMass);
    BoundingBox boundingBox = new BoundingBox(point, new float[] { 2.0f, -0.5f });
    when(parent.getBoundingBox()).thenReturn(boundingBox);
    visitor.accept(parent, depth);
    result = visitor.getResult();
    double expectedSumOfNewRange2 = 2.0 + 2.0;
    double expectedProbOfCut2 = (1.0 + 0.5) / expectedSumOfNewRange2;
    double[] expectedDifferenceInRangeVector2 = { 0.0, 1.0, 0.5, 0.0 };
    double expectedScore2 = defaultScoreUnseenFunction(depth, parent.getMass());
    double[] expectedUnnormalizedLow2 = new double[pointToScore.length];
    double[] expectedUnnormalizedHigh2 = new double[pointToScore.length];
    for (int i = 0; i < pointToScore.length; i++) {
        double prob = expectedDifferenceInRangeVector2[2 * i] / expectedSumOfNewRange2;
        expectedUnnormalizedHigh2[i] = prob * expectedScore2 + (1 - expectedProbOfCut2) * expectedUnnormalizedHigh[i];
        prob = expectedDifferenceInRangeVector2[2 * i + 1] / expectedSumOfNewRange2;
        expectedUnnormalizedLow2[i] = prob * expectedScore2 + (1 - expectedProbOfCut2) * expectedUnnormalizedLow[i];
    }
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(defaultScalarNormalizerFunction(expectedUnnormalizedLow2[i], treeMass), result.low[i], EPSILON);
        assertEquals(defaultScalarNormalizerFunction(expectedUnnormalizedHigh2[i], treeMass), result.high[i], EPSILON);
    }
    // grandparent contains pointToScore
    assertFalse(visitor.pointInsideBox);
    depth--;
    INodeView grandParent = mock(NodeView.class);
    when(grandParent.getMass()).thenReturn(parentMass + 2);
    when(grandParent.getBoundingBox()).thenReturn(boundingBox.getMergedBox(new BoundingBox(new float[] { -1.0f, 1.0f }).getMergedBox(new float[] { -0.5f, -1.5f })));
    visitor.accept(grandParent, depth);
    result = visitor.getResult();
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(defaultScalarNormalizerFunction(expectedUnnormalizedLow2[i], treeMass), result.low[i], EPSILON);
        assertEquals(defaultScalarNormalizerFunction(expectedUnnormalizedHigh2[i], treeMass), result.high[i], EPSILON);
    }
}
Also used : DiVector(com.amazon.randomcutforest.returntypes.DiVector) BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 3 with BoundingBox

use of com.amazon.randomcutforest.tree.BoundingBox in project random-cut-forest-by-aws by aws.

the class AnomalyAttributionVisitorTest method testAcceptLeafEquals.

@Test
public void testAcceptLeafEquals() {
    float[] point = { 1.1f, -2.2f, 3.3f };
    INodeView leafNode = mock(NodeView.class);
    when(leafNode.getLeafPoint()).thenReturn(point);
    when(leafNode.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    int leafDepth = 100;
    int leafMass = 10;
    when(leafNode.getMass()).thenReturn(leafMass);
    int treeMass = 21;
    AnomalyAttributionVisitor visitor = new AnomalyAttributionVisitor(point, treeMass, 0);
    visitor.acceptLeaf(leafNode, leafDepth);
    assertTrue(visitor.hitDuplicates);
    double expectedScoreSum = CommonUtils.defaultDampFunction(leafMass, treeMass) / (leafDepth + Math.log(leafMass + 1) / Math.log(2));
    double expectedScore = expectedScoreSum / (2 * point.length);
    DiVector result = visitor.getResult();
    for (int i = 0; i < point.length; i++) {
        assertEquals(defaultScalarNormalizerFunction(expectedScore, treeMass), result.low[i], EPSILON);
        assertEquals(defaultScalarNormalizerFunction(expectedScore, treeMass), result.high[i], EPSILON);
    }
}
Also used : DiVector(com.amazon.randomcutforest.returntypes.DiVector) BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 4 with BoundingBox

use of com.amazon.randomcutforest.tree.BoundingBox in project random-cut-forest-by-aws by aws.

the class AnomalyScoreVisitorTest method testAcceptEqualsLeafPoint.

@Test
public void testAcceptEqualsLeafPoint() {
    float[] pointToScore = { 0.0f, 0.0f };
    int sampleSize = 50;
    AnomalyScoreVisitor visitor = new AnomalyScoreVisitor(pointToScore, sampleSize);
    float[] point = Arrays.copyOf(pointToScore, pointToScore.length);
    INodeView node = mock(NodeView.class);
    when(node.getLeafPoint()).thenReturn(point);
    when(node.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    int depth = 2;
    visitor.acceptLeaf(node, depth);
    double expectedScore = CommonUtils.defaultDampFunction(node.getMass(), sampleSize) / (depth + Math.log(node.getMass() + 1) / Math.log(2));
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, sampleSize), EPSILON));
    depth--;
    IBoundingBoxView boundingBox = node.getBoundingBox().getMergedBox(new float[] { 1.0f, 1.0f });
    node = new NodeView(null, null, Null);
    visitor.accept(node, depth);
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, sampleSize), EPSILON));
    depth--;
    boundingBox = boundingBox.getMergedBox(new float[] { -1.0f, -1.0f });
    node = new NodeView(null, null, Null);
    visitor.accept(node, depth);
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, sampleSize), EPSILON));
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) IBoundingBoxView(com.amazon.randomcutforest.tree.IBoundingBoxView) INodeView(com.amazon.randomcutforest.tree.INodeView) NodeView(com.amazon.randomcutforest.tree.NodeView) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 5 with BoundingBox

use of com.amazon.randomcutforest.tree.BoundingBox in project random-cut-forest-by-aws by aws.

the class AnomalyScoreVisitorTest method testAccept.

@Test
public void testAccept() {
    float[] pointToScore = new float[] { 0.0f, 0.0f };
    int sampleSize = 50;
    AnomalyScoreVisitor visitor = new AnomalyScoreVisitor(pointToScore, sampleSize);
    NodeView node = mock(NodeView.class);
    float[] otherPoint = new float[] { 1.0f, 1.0f };
    when(node.getLeafPoint()).thenReturn(otherPoint);
    when(node.getBoundingBox()).thenReturn(new BoundingBox(otherPoint, otherPoint));
    int depth = 4;
    visitor.acceptLeaf(node, depth);
    double expectedScore = 1.0 / (depth + 1);
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, sampleSize), EPSILON));
    depth--;
    IBoundingBoxView boundingBox = node.getBoundingBox().getMergedBox(new float[] { 2.0f, 0.0f });
    when(node.getBoundingBox()).thenReturn(boundingBox);
    when(node.probailityOfSeparation(any())).thenReturn(1.0 / 3);
    visitor.accept(node, depth);
    double p = visitor.getProbabilityOfSeparation(boundingBox);
    expectedScore = p * (1.0 / (depth + 1)) + (1 - p) * expectedScore;
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, sampleSize), EPSILON));
    depth--;
    boundingBox = boundingBox.getMergedBox(new float[] { -1.0f, 0.0f });
    when(node.getBoundingBox()).thenReturn(boundingBox);
    when(node.probailityOfSeparation(any())).thenReturn(0.0);
    visitor.accept(node, depth);
    p = visitor.getProbabilityOfSeparation(boundingBox);
    expectedScore = p * (1.0 / (depth + 1)) + (1 - p) * expectedScore;
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, sampleSize), EPSILON));
    depth--;
    boundingBox = boundingBox.getMergedBox(new float[] { -1.0f, -1.0f });
    when(node.probailityOfSeparation(any())).thenReturn(0.0);
    visitor.accept(node, depth);
    p = visitor.getProbabilityOfSeparation(boundingBox);
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, sampleSize), EPSILON));
    assertTrue(visitor.pointInsideBox);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) IBoundingBoxView(com.amazon.randomcutforest.tree.IBoundingBoxView) INodeView(com.amazon.randomcutforest.tree.INodeView) NodeView(com.amazon.randomcutforest.tree.NodeView) Test(org.junit.jupiter.api.Test)

Aggregations

BoundingBox (com.amazon.randomcutforest.tree.BoundingBox)18 Test (org.junit.jupiter.api.Test)18 INodeView (com.amazon.randomcutforest.tree.INodeView)16 NodeView (com.amazon.randomcutforest.tree.NodeView)6 InterpolationMeasure (com.amazon.randomcutforest.returntypes.InterpolationMeasure)4 IBoundingBoxView (com.amazon.randomcutforest.tree.IBoundingBoxView)4 DiVector (com.amazon.randomcutforest.returntypes.DiVector)3 Arrays (java.util.Arrays)3 Assertions.assertArrayEquals (org.junit.jupiter.api.Assertions.assertArrayEquals)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.when (org.mockito.Mockito.when)3