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