Search in sources :

Example 6 with INodeView

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

the class ImputeVisitorTest method testAcceptLeafEqualsZeroDepth.

@Test
public void testAcceptLeafEqualsZeroDepth() {
    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 = 0;
    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(0.0, visitor.getAnomalyRank());
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 7 with INodeView

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

the class ImputeVisitorTest method testAccept.

@Test
public void testAccept() {
    float[] point = { queryPoint[0], 2.0f, -111.11f };
    INodeView node = mock(NodeView.class);
    when(node.getLeafPoint()).thenReturn(point);
    when(node.getLiftedLeafPoint()).thenReturn(point);
    when(node.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    int depth = 100;
    int leafMass = 10;
    when(node.getMass()).thenReturn(leafMass);
    visitor.acceptLeaf(node, depth);
    float[] expected = new float[] { -1.0f, 2.0f, 3.0f };
    assertArrayEquals(expected, visitor.getResult().leafPoint);
    assertEquals(defaultScoreUnseenFunction(depth, leafMass), visitor.getAnomalyRank());
    depth--;
    IBoundingBoxView boundingBox = node.getBoundingBox().getMergedBox(new float[] { 99.0f, 4.0f, -19.0f });
    when(node.getBoundingBox()).thenReturn(boundingBox);
    when(node.probailityOfSeparation(any())).thenReturn(CommonUtils.getProbabilityOfSeparation(boundingBox, expected));
    when(node.getMass()).thenReturn(leafMass + 2);
    double oldRank = visitor.getAnomalyRank();
    visitor.accept(node, depth);
    assertArrayEquals(expected, visitor.getResult().leafPoint);
    double p = CommonUtils.getProbabilityOfSeparation(boundingBox, expected);
    double expectedRank = p * defaultScoreUnseenFunction(depth, node.getMass()) + (1 - p) * oldRank;
    assertEquals(expectedRank, visitor.getAnomalyRank(), EPSILON);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) IBoundingBoxView(com.amazon.randomcutforest.tree.IBoundingBoxView) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 8 with INodeView

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

the class ImputeVisitorTest method testAcceptLeafNotEquals.

@Test
public void testAcceptLeafNotEquals() {
    float[] point = { queryPoint[0], 2.0f, -111.11f };
    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(defaultScoreUnseenFunction(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 9 with INodeView

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

the class AnomalyScoreVisitorTest method testAcceptLeafNotEquals.

@Test
public void testAcceptLeafNotEquals() {
    float[] point = new float[] { 1.0f, 2.0f, 3.0f };
    float[] anotherPoint = new float[] { 4.0f, 5.0f, 6.0f };
    INodeView leafNode = mock(NodeView.class);
    when(leafNode.getLeafPoint()).thenReturn(anotherPoint);
    when(leafNode.getBoundingBox()).thenReturn(new BoundingBox(anotherPoint, anotherPoint));
    int leafDepth = 100;
    AnomalyScoreVisitor visitor = new AnomalyScoreVisitor(point, 2);
    visitor.acceptLeaf(leafNode, leafDepth);
    double expectedScore = 1.0 / (leafDepth + 1);
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, 2), EPSILON));
    assertFalse(visitor.pointInsideBox);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 10 with INodeView

use of com.amazon.randomcutforest.tree.INodeView 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)

Aggregations

INodeView (com.amazon.randomcutforest.tree.INodeView)17 Test (org.junit.jupiter.api.Test)17 BoundingBox (com.amazon.randomcutforest.tree.BoundingBox)14 InterpolationMeasure (com.amazon.randomcutforest.returntypes.InterpolationMeasure)4 NodeView (com.amazon.randomcutforest.tree.NodeView)4 DiVector (com.amazon.randomcutforest.returntypes.DiVector)3 Neighbor (com.amazon.randomcutforest.returntypes.Neighbor)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 IBoundingBoxView (com.amazon.randomcutforest.tree.IBoundingBoxView)2 HashMap (java.util.HashMap)2