Search in sources :

Example 11 with BoundingBox

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

the class AnomalyScoreVisitorTest method test_getProbabilityOfSeparation_leafNode.

@Test
public void test_getProbabilityOfSeparation_leafNode() {
    float[] point = new float[] { 1.0f, 2.0f, 3.0f };
    float[] leafPoint = Arrays.copyOf(point, point.length);
    BoundingBox boundingBox = new BoundingBox(leafPoint);
    AnomalyScoreVisitor visitor = new AnomalyScoreVisitor(point, 2);
    assertThrows(IllegalStateException.class, () -> visitor.getProbabilityOfSeparation(boundingBox));
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) Test(org.junit.jupiter.api.Test)

Example 12 with BoundingBox

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

the class AnomalyScoreVisitorTest method testGetProbabilityOfSeparation.

@Test
public void testGetProbabilityOfSeparation() {
    float[] minPoint = { 0.0f, 0.0f, 0.0f };
    float[] maxPoint = { 1.0f, 2.0f, 3.0f };
    IBoundingBoxView boundingBox = new BoundingBox(minPoint);
    boundingBox = boundingBox.getMergedBox(maxPoint);
    float[] point = { 0.5f, 0.5f, 0.5f };
    int sampleSize = 2;
    AnomalyScoreVisitor visitor = new AnomalyScoreVisitor(point, sampleSize);
    double p = visitor.getProbabilityOfSeparation(boundingBox);
    assertThat(p, closeTo(0.0, EPSILON));
    assertTrue(visitor.coordInsideBox[0]);
    assertTrue(visitor.coordInsideBox[1]);
    assertTrue(visitor.coordInsideBox[2]);
    visitor = new AnomalyScoreVisitor(point, sampleSize);
    visitor.coordInsideBox[1] = visitor.coordInsideBox[2] = true;
    p = visitor.getProbabilityOfSeparation(boundingBox);
    assertThat(p, closeTo(0.0, EPSILON));
    assertTrue(visitor.coordInsideBox[0]);
    assertTrue(visitor.coordInsideBox[1]);
    assertTrue(visitor.coordInsideBox[2]);
    point = new float[] { 2.0f, 0.5f, 0.5f };
    visitor = new AnomalyScoreVisitor(point, sampleSize);
    p = visitor.getProbabilityOfSeparation(boundingBox);
    assertThat(p, closeTo(1.0 / (2.0 + 2.0 + 3.0), EPSILON));
    assertFalse(visitor.coordInsideBox[0]);
    assertTrue(visitor.coordInsideBox[1]);
    assertTrue(visitor.coordInsideBox[2]);
    visitor = new AnomalyScoreVisitor(point, sampleSize);
    visitor.coordInsideBox[1] = visitor.coordInsideBox[2] = true;
    p = visitor.getProbabilityOfSeparation(boundingBox);
    assertThat(p, closeTo(1.0 / (2.0 + 2.0 + 3.0), EPSILON));
    assertFalse(visitor.coordInsideBox[0]);
    assertTrue(visitor.coordInsideBox[1]);
    assertTrue(visitor.coordInsideBox[2]);
    point = new float[] { 0.5f, -3.0f, 4.0f };
    visitor = new AnomalyScoreVisitor(point, sampleSize);
    p = visitor.getProbabilityOfSeparation(boundingBox);
    assertThat(p, closeTo((3.0 + 1.0) / (1.0 + 5.0 + 4.0), EPSILON));
    assertTrue(visitor.coordInsideBox[0]);
    assertFalse(visitor.coordInsideBox[1]);
    assertFalse(visitor.coordInsideBox[2]);
    visitor = new AnomalyScoreVisitor(point, sampleSize);
    visitor.coordInsideBox[0] = true;
    p = visitor.getProbabilityOfSeparation(boundingBox);
    assertThat(p, closeTo((3.0 + 1.0) / (1.0 + 5.0 + 4.0), EPSILON));
    assertTrue(visitor.coordInsideBox[0]);
    assertFalse(visitor.coordInsideBox[1]);
    assertFalse(visitor.coordInsideBox[2]);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) IBoundingBoxView(com.amazon.randomcutforest.tree.IBoundingBoxView) Test(org.junit.jupiter.api.Test)

Example 13 with BoundingBox

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

the class AnomalyScoreVisitorTest method testAcceptLeafEquals.

@Test
public void testAcceptLeafEquals() {
    float[] point = { 1.0f, 2.0f, 3.0f };
    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 subSampleSize = 21;
    AnomalyScoreVisitor visitor = new AnomalyScoreVisitor(point, subSampleSize);
    visitor.acceptLeaf(leafNode, leafDepth);
    double expectedScore = CommonUtils.defaultDampFunction(leafMass, subSampleSize) / (leafDepth + Math.log(leafMass + 1) / Math.log(2));
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, subSampleSize), EPSILON));
    assertTrue(visitor.pointInsideBox);
    visitor = new AnomalyScoreVisitor(point, subSampleSize);
    visitor.acceptLeaf(leafNode, 0);
    expectedScore = CommonUtils.defaultDampFunction(leafMass, subSampleSize) / (Math.log(leafMass + 1) / Math.log(2.0));
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, subSampleSize), EPSILON));
    assertTrue(visitor.pointInsideBox);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 14 with BoundingBox

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

the class ImputeVisitorTest method testAcceptLeafEquals.

@Test
public void testAcceptLeafEquals() {
    float[] point = { queryPoint[0], 2.0f, queryPoint[2] };
    INodeView leafNode = mock(NodeView.class);
    when(leafNode.getLeafPoint()).thenReturn(point);
    when(leafNode.getLiftedLeafPoint()).thenReturn(point);
    when(leafNode.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    int leafDepth = 100;
    int leafMass = 10;
    when(leafNode.getMass()).thenReturn(leafMass);
    visitor.acceptLeaf(leafNode, leafDepth);
    float[] expected = new float[] { -1.0f, 2.0f, 3.0f };
    assertArrayEquals(expected, visitor.getResult().leafPoint);
    assertEquals(defaultScoreSeenFunction(leafDepth, leafMass), visitor.getAnomalyRank());
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 15 with BoundingBox

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

the class SimpleInterpolationVisitorTest method testAcceptEqualsLeafPoint.

@Test
public void testAcceptEqualsLeafPoint() {
    float[] pointToScore = { 0.0f, 0.0f };
    int sampleSize = 50;
    SimpleInterpolationVisitor visitor = new SimpleInterpolationVisitor(pointToScore, sampleSize, 1, false);
    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));
    when(node.getMass()).thenReturn(1);
    int depth = 2;
    visitor.acceptLeaf(node, depth);
    InterpolationMeasure result = visitor.getResult();
    double[] expected = new double[point.length];
    Arrays.fill(expected, 0.5 * (1 + node.getMass()) / point.length);
    assertArrayEquals(expected, result.measure.high);
    assertArrayEquals(expected, result.measure.low);
    Arrays.fill(expected, 0.5 / point.length);
    assertArrayEquals(expected, result.probMass.high);
    assertArrayEquals(expected, result.probMass.low);
    Arrays.fill(expected, 0.0);
    assertArrayEquals(expected, result.distances.high);
    assertArrayEquals(expected, result.distances.low);
    depth--;
    float[] siblingPoint = { 1.0f, -2.0f };
    INodeView sibling = mock(NodeView.class);
    int siblingMass = 2;
    when(sibling.getMass()).thenReturn(siblingMass);
    INodeView parent = mock(NodeView.class);
    when(parent.getMass()).thenReturn(1 + siblingMass);
    BoundingBox boundingBox = new BoundingBox(point, siblingPoint);
    when(parent.getBoundingBox()).thenReturn(boundingBox);
    when(parent.getSiblingBoundingBox(any())).thenReturn(new BoundingBox(siblingPoint));
    visitor.accept(parent, depth);
    result = visitor.getResult();
    // compute using shadow box (sibling leaf node at {1.0, -2.0} and parent
    // bounding box
    double[] directionalDistance = { 0.0, 1.0, 2.0, 0.0 };
    double[] differenceInRange = { 0.0, 1.0, 2.0, 0.0 };
    double sumOfNewRange = 1.0 + 2.0;
    double[] probVector = Arrays.stream(differenceInRange).map(x -> x / sumOfNewRange).toArray();
    expected = new double[2 * pointToScore.length];
    for (int i = 0; i < expected.length; i++) {
        expected[i] = probVector[i] * (1 + node.getMass() + parent.getMass());
    }
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(expected[2 * i], result.measure.high[i]);
        assertEquals(expected[2 * i + 1], result.measure.low[i]);
    }
    for (int i = 0; i < expected.length; i++) {
        expected[i] = probVector[i];
    }
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(expected[2 * i], result.probMass.high[i]);
        assertEquals(expected[2 * i + 1], result.probMass.low[i]);
    }
    for (int i = 0; i < expected.length; i++) {
        expected[i] = probVector[i] * directionalDistance[i];
    }
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(expected[2 * i], result.distances.high[i]);
        assertEquals(expected[2 * i + 1], result.distances.low[i]);
    }
}
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)

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