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