Search in sources :

Example 1 with NodeView

use of com.amazon.randomcutforest.tree.NodeView 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 2 with NodeView

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

Example 3 with NodeView

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

the class ImputeVisitorTest method testMerge.

@Test
public void testMerge() {
    float[] otherPoint = new float[] { 99, 100, 101 };
    ImputeVisitor other = new ImputeVisitor(otherPoint, 0, new int[0]);
    // set other.rank to a small value
    NodeView node = mock(NodeView.class);
    when(node.getLeafPoint()).thenReturn(new float[] { 0, 0, 0 });
    when(node.getLiftedLeafPoint()).thenReturn(new float[] { 0, 0, 0 });
    when(node.getBoundingBox()).thenReturn(new BoundingBox(new float[] { 0, 0, 0 }));
    other.acceptLeaf(node, 99);
    assertTrue(other.getAnomalyRank() < visitor.getAnomalyRank());
    other.combine(visitor);
    assertArrayEquals(otherPoint, other.getResult().leafPoint);
    visitor.combine(other);
    assertArrayEquals(otherPoint, visitor.getResult().leafPoint);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) 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)3 INodeView (com.amazon.randomcutforest.tree.INodeView)3 NodeView (com.amazon.randomcutforest.tree.NodeView)3 Test (org.junit.jupiter.api.Test)3 IBoundingBoxView (com.amazon.randomcutforest.tree.IBoundingBoxView)2