Search in sources :

Example 21 with RandomCutForest

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);
}
Also used : ForestMode(com.amazon.randomcutforest.config.ForestMode) BasicThresholderMapper(com.amazon.randomcutforest.parkservices.state.threshold.BasicThresholderMapper) PredictorCorrector(com.amazon.randomcutforest.parkservices.PredictorCorrector) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) ThresholdedRandomCutForest(com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest) DiVectorMapper(com.amazon.randomcutforest.state.returntypes.DiVectorMapper) Preprocessor(com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor) PreprocessorMapper(com.amazon.randomcutforest.parkservices.state.preprocessor.PreprocessorMapper) TransformMethod(com.amazon.randomcutforest.config.TransformMethod) IRCFComputeDescriptor(com.amazon.randomcutforest.parkservices.IRCFComputeDescriptor) RCFComputeDescriptor(com.amazon.randomcutforest.parkservices.RCFComputeDescriptor) BasicThresholder(com.amazon.randomcutforest.parkservices.threshold.BasicThresholder) ThresholdedRandomCutForest(com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)

Example 22 with RandomCutForest

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);
}
Also used : Random(java.util.Random) RandomCutForest(com.amazon.randomcutforest.RandomCutForest)

Example 23 with RandomCutForest

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");
    }
}
Also used : RandomCutForest(com.amazon.randomcutforest.RandomCutForest) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Example 24 with RandomCutForest

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);
}
Also used : Precision(com.amazon.randomcutforest.config.Precision) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) NormalMixtureTestData(com.amazon.randomcutforest.testutils.NormalMixtureTestData)

Example 25 with RandomCutForest

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());
}
Also used : RandomCutForest(com.amazon.randomcutforest.RandomCutForest) Test(org.junit.jupiter.api.Test)

Aggregations

RandomCutForest (com.amazon.randomcutforest.RandomCutForest)33 Random (java.util.Random)14 RandomCutForestMapper (com.amazon.randomcutforest.state.RandomCutForestMapper)11 Precision (com.amazon.randomcutforest.config.Precision)10 RandomCutForestState (com.amazon.randomcutforest.state.RandomCutForestState)10 Test (org.junit.jupiter.api.Test)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 NormalMixtureTestData (com.amazon.randomcutforest.testutils.NormalMixtureTestData)7 ThresholdedRandomCutForest (com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)5 AnomalyDescriptor (com.amazon.randomcutforest.parkservices.AnomalyDescriptor)4 CompactSampler (com.amazon.randomcutforest.sampler.CompactSampler)4 MultiDimDataWithKey (com.amazon.randomcutforest.testutils.MultiDimDataWithKey)4 ArrayList (java.util.ArrayList)4 ComponentList (com.amazon.randomcutforest.ComponentList)3 PointStoreCoordinator (com.amazon.randomcutforest.executor.PointStoreCoordinator)3 CompactSamplerMapper (com.amazon.randomcutforest.state.sampler.CompactSamplerMapper)3 CompactSamplerState (com.amazon.randomcutforest.state.sampler.CompactSamplerState)3 PointStoreMapper (com.amazon.randomcutforest.state.store.PointStoreMapper)3 CompactRandomCutTreeContext (com.amazon.randomcutforest.state.tree.CompactRandomCutTreeContext)3 IPointStore (com.amazon.randomcutforest.store.IPointStore)3