use of com.amazon.randomcutforest.RandomCutForest in project random-cut-forest-by-aws by aws.
the class ThresholdedRandomCutForestMapper method toModel.
@Override
public ThresholdedRandomCutForest toModel(ThresholdedRandomCutForestState state, long seed) {
RandomCutForestMapper randomCutForestMapper = new RandomCutForestMapper();
BasicThresholderMapper thresholderMapper = new BasicThresholderMapper();
PreprocessorMapper preprocessorMapper = new PreprocessorMapper();
RandomCutForest forest = randomCutForestMapper.toModel(state.getForestState());
BasicThresholder thresholder = thresholderMapper.toModel(state.getThresholderState());
Preprocessor preprocessor = preprocessorMapper.toModel(state.getPreprocessorStates()[0]);
ForestMode forestMode = ForestMode.valueOf(state.getForestMode());
TransformMethod transformMethod = TransformMethod.valueOf(state.getTransformMethod());
RCFComputeDescriptor descriptor = new RCFComputeDescriptor(null, 0L);
descriptor.setRCFScore(state.getLastAnomalyScore());
descriptor.setInternalTimeStamp(state.getLastAnomalyTimeStamp());
descriptor.setAttribution(new DiVectorMapper().toModel(state.getLastAnomalyAttribution()));
descriptor.setRCFPoint(state.getLastAnomalyPoint());
descriptor.setExpectedRCFPoint(state.getLastExpectedPoint());
descriptor.setRelativeIndex(state.getLastRelativeIndex());
descriptor.setForestMode(forestMode);
descriptor.setTransformMethod(transformMethod);
descriptor.setImputationMethod(ImputationMethod.valueOf(state.getPreprocessorStates()[0].getImputationMethod()));
PredictorCorrector predictorCorrector = new PredictorCorrector(thresholder);
predictorCorrector.setIgnoreSimilar(state.isIgnoreSimilar());
predictorCorrector.setIgnoreSimilarFactor(state.getIgnoreSimilarFactor());
predictorCorrector.setTriggerFactor(state.getTriggerFactor());
predictorCorrector.setNumberOfAttributors(state.getNumberOfAttributors());
return new ThresholdedRandomCutForest(forestMode, transformMethod, forest, predictorCorrector, preprocessor, descriptor);
}
use of com.amazon.randomcutforest.RandomCutForest in project random-cut-forest-by-aws by aws.
the class HyperTreeTest method runRCF.
public static void runRCF(TestScores testScore, Function<IBoundingBoxView, double[]> gVec) {
Random prg = new Random(randomSeed);
for (int trials = 0; trials < numTrials; trials++) {
double[][] data = generator.generateTestData(dataSize + numTest, dimensions, 100 + trials);
RandomCutForest newForest = RandomCutForest.builder().dimensions(dimensions).numberOfTrees(numberOfTrees).sampleSize(sampleSize).randomSeed(prg.nextInt()).build();
for (int i = 0; i < dataSize; i++) {
// shrink, shift at random
for (int j = 0; j < dimensions; j++) data[i][j] *= 0.01;
if (prg.nextDouble() < 0.5)
data[i][0] += 5.0;
else
data[i][0] -= 5.0;
newForest.update(data[i]);
// the points are streamed
}
for (int i = dataSize; i < dataSize + numTest; i++) {
for (int j = 0; j < dimensions; j++) data[i][j] *= 0.01;
testScore.sumCenterScore += getSimulatedAnomalyScore(newForest, toFloatArray(data[i]), gVec);
testScore.sumCenterHeight += getSimulatedHeightScore(newForest, toFloatArray(data[i]), gVec);
testScore.sumCenterDisp += getSimulatedDisplacementScore(newForest, toFloatArray(data[i]), gVec);
// move to right cluster
data[i][0] += 5;
testScore.sumRightScore += getSimulatedAnomalyScore(newForest, toFloatArray(data[i]), gVec);
testScore.sumRightHeight += getSimulatedHeightScore(newForest, toFloatArray(data[i]), gVec);
testScore.sumRightDisp += getSimulatedDisplacementScore(newForest, toFloatArray(data[i]), gVec);
// move to left cluster
data[i][0] -= 10;
testScore.sumLeftScore += getSimulatedAnomalyScore(newForest, toFloatArray(data[i]), gVec);
testScore.sumLeftHeight += getSimulatedHeightScore(newForest, toFloatArray(data[i]), gVec);
testScore.sumLeftDisp += getSimulatedDisplacementScore(newForest, toFloatArray(data[i]), gVec);
}
}
assert (testScore.sumCenterScore > 2 * testScore.sumLeftScore);
assert (testScore.sumCenterScore > 2 * testScore.sumRightScore);
assert (testScore.sumCenterDisp > 10 * testScore.sumLeftDisp);
assert (testScore.sumCenterDisp > 10 * testScore.sumRightDisp);
assert (2 * testScore.sumCenterHeight < testScore.sumLeftHeight);
assert (2 * testScore.sumCenterHeight < testScore.sumRightHeight);
}
use of com.amazon.randomcutforest.RandomCutForest in project random-cut-forest-by-aws by aws.
the class DynamicNearNeighbor method run.
@Override
public void run() throws Exception {
int newDimensions = 2;
long randomSeed = 123;
RandomCutForest newForest = RandomCutForest.builder().numberOfTrees(100).sampleSize(256).dimensions(newDimensions).randomSeed(randomSeed).timeDecay(1.0 / 800).centerOfMassEnabled(true).storeSequenceIndexesEnabled(true).build();
String name = "dynamic_near_neighbor_example";
BufferedWriter file = new BufferedWriter(new FileWriter(name));
double[][] data = generate(1000);
double[] queryPoint = new double[] { 0.5, 0.6 };
for (int degree = 0; degree < 360; degree += 2) {
for (double[] datum : data) {
double[] transformed = rotateClockWise(datum, -2 * PI * degree / 360);
file.append(transformed[0] + " " + transformed[1] + "\n");
newForest.update(transformed);
}
file.append("\n");
file.append("\n");
double[] movingQuery = rotateClockWise(queryPoint, -3 * PI * degree / 360);
double[] neighbor = newForest.getNearNeighborsInSample(movingQuery, 1).get(0).point;
file.append(movingQuery[0] + " " + movingQuery[1] + " " + (neighbor[0] - movingQuery[0]) + " " + (neighbor[1] - movingQuery[1]) + "\n");
file.append("\n");
file.append("\n");
}
}
use of com.amazon.randomcutforest.RandomCutForest in project random-cut-forest-by-aws by aws.
the class DynamicSampling method run.
@Override
public void run() throws Exception {
// Create and populate a random cut forest
int dimensions = 4;
int numberOfTrees = 50;
int sampleSize = 256;
Precision precision = Precision.FLOAT_64;
int dataSize = 4 * sampleSize;
NormalMixtureTestData testData = new NormalMixtureTestData();
RandomCutForest forest = RandomCutForest.builder().compact(true).dimensions(dimensions).randomSeed(0).numberOfTrees(numberOfTrees).sampleSize(sampleSize).precision(precision).build();
RandomCutForest forest2 = RandomCutForest.builder().compact(true).dimensions(dimensions).randomSeed(0).numberOfTrees(numberOfTrees).sampleSize(sampleSize).precision(precision).build();
int first_anomalies = 0;
int second_anomalies = 0;
forest2.setTimeDecay(10 * forest2.getTimeDecay());
for (double[] point : testData.generateTestData(dataSize, dimensions)) {
if (forest.getAnomalyScore(point) > 1.0) {
first_anomalies++;
}
if (forest2.getAnomalyScore(point) > 1.0) {
second_anomalies++;
}
forest.update(point);
forest2.update(point);
}
System.out.println("Unusual scores: forest one " + first_anomalies + ", second one " + second_anomalies);
// should be roughly equal
first_anomalies = second_anomalies = 0;
testData = new NormalMixtureTestData(-3, 40);
for (double[] point : testData.generateTestData(dataSize, dimensions)) {
if (forest.getAnomalyScore(point) > 1.0) {
first_anomalies++;
}
if (forest2.getAnomalyScore(point) > 1.0) {
second_anomalies++;
}
forest.update(point);
forest2.update(point);
}
System.out.println("Unusual scores: forest one " + first_anomalies + ", second one " + second_anomalies);
// forest2 should adapt faster
first_anomalies = second_anomalies = 0;
RandomCutForestMapper mapper = new RandomCutForestMapper();
mapper.setSaveExecutorContextEnabled(true);
RandomCutForest copyForest = mapper.toModel(mapper.toState(forest));
copyForest.setTimeDecay(50 * forest.getTimeDecay());
// force an adjustment to catch up
testData = new NormalMixtureTestData(-10, -40);
int forced_change_anomalies = 0;
for (double[] point : testData.generateTestData(dataSize, dimensions)) {
if (forest.getAnomalyScore(point) > 1.0) {
first_anomalies++;
}
if (forest2.getAnomalyScore(point) > 1.0) {
second_anomalies++;
}
if (copyForest.getAnomalyScore(point) > 1.0) {
forced_change_anomalies++;
}
copyForest.update(point);
forest.update(point);
forest2.update(point);
}
// both should show the similar rate of adjustment
System.out.println("Unusual scores: forest one " + first_anomalies + ", second one " + second_anomalies + ", forced (first) " + forced_change_anomalies);
}
use of com.amazon.randomcutforest.RandomCutForest in project random-cut-forest-by-aws by aws.
the class AnomalyScoreRunnerTest method testAnomalyScoreTransformer.
@Test
public void testAnomalyScoreTransformer() {
RandomCutForest forest = mock(RandomCutForest.class);
AnomalyScoreRunner.AnomalyScoreTransformer transformer = new AnomalyScoreRunner.AnomalyScoreTransformer(forest);
when(forest.getAnomalyScore(new double[] { 1.0, 2.0, 3.0 })).thenReturn(11.0);
assertEquals(Collections.singletonList("11.0"), transformer.getResultValues(1.0, 2.0, 3.0));
assertEquals(Collections.singletonList("anomaly_score"), transformer.getResultColumnNames());
assertEquals(Collections.singletonList("NA"), transformer.getEmptyResultValue());
}
Aggregations