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());
}
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);
}
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());
}
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);
}
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);
}
Aggregations