Search in sources :

Example 16 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper in project random-cut-forest-by-aws by aws.

the class StateMapperBenchmark method roundTripFromJson.

@Benchmark
@OperationsPerInvocation(NUM_TEST_SAMPLES)
public String roundTripFromJson(BenchmarkState state, Blackhole blackhole) throws JsonProcessingException {
    String json = state.json;
    double[][] testData = state.testData;
    for (int i = 0; i < NUM_TEST_SAMPLES; i++) {
        ObjectMapper jsonMapper = new ObjectMapper();
        RandomCutForestState forestState = jsonMapper.readValue(json, RandomCutForestState.class);
        RandomCutForestMapper mapper = new RandomCutForestMapper();
        mapper.setSaveExecutorContextEnabled(true);
        mapper.setSaveTreeStateEnabled(state.saveTreeState);
        forest = mapper.toModel(forestState);
        double score = forest.getAnomalyScore(testData[i]);
        blackhole.consume(score);
        forest.update(testData[i]);
        json = jsonMapper.writeValueAsString(mapper.toState(forest));
    }
    bytes = json.getBytes();
    return json;
}
Also used : RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) RandomCutForestState(com.amazon.randomcutforest.state.RandomCutForestState) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Example 17 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper in project random-cut-forest-by-aws by aws.

the class StateMapperBenchmark method roundTripFromProtostuff.

@Benchmark
@OperationsPerInvocation(NUM_TEST_SAMPLES)
public byte[] roundTripFromProtostuff(BenchmarkState state, Blackhole blackhole) {
    bytes = state.protostuff;
    double[][] testData = state.testData;
    for (int i = 0; i < NUM_TEST_SAMPLES; i++) {
        Schema<RandomCutForestState> schema = RuntimeSchema.getSchema(RandomCutForestState.class);
        RandomCutForestState forestState = schema.newMessage();
        ProtostuffIOUtil.mergeFrom(bytes, forestState, schema);
        RandomCutForestMapper mapper = new RandomCutForestMapper();
        mapper.setSaveExecutorContextEnabled(true);
        mapper.setSaveTreeStateEnabled(state.saveTreeState);
        forest = mapper.toModel(forestState);
        double score = forest.getAnomalyScore(testData[i]);
        blackhole.consume(score);
        forest.update(testData[i]);
        forestState = mapper.toState(forest);
        LinkedBuffer buffer = LinkedBuffer.allocate(512);
        try {
            bytes = ProtostuffIOUtil.toByteArray(forestState, schema, buffer);
        } finally {
            buffer.clear();
        }
    }
    return bytes;
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) RandomCutForestState(com.amazon.randomcutforest.state.RandomCutForestState) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Example 18 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper in project random-cut-forest-by-aws by aws.

the class StateMapperBenchmark method roundTripFromState.

@Benchmark
@OperationsPerInvocation(NUM_TEST_SAMPLES)
public RandomCutForestState roundTripFromState(BenchmarkState state, Blackhole blackhole) {
    RandomCutForestState forestState = state.forestState;
    double[][] testData = state.testData;
    for (int i = 0; i < NUM_TEST_SAMPLES; i++) {
        RandomCutForestMapper mapper = new RandomCutForestMapper();
        mapper.setSaveExecutorContextEnabled(true);
        mapper.setSaveTreeStateEnabled(state.saveTreeState);
        forest = mapper.toModel(forestState);
        double score = forest.getAnomalyScore(testData[i]);
        blackhole.consume(score);
        forest.update(testData[i]);
        forestState = mapper.toState(forest);
    }
    return forestState;
}
Also used : RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) RandomCutForestState(com.amazon.randomcutforest.state.RandomCutForestState) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Example 19 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper 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 20 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper 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)

Aggregations

RandomCutForestMapper (com.amazon.randomcutforest.state.RandomCutForestMapper)21 RandomCutForestState (com.amazon.randomcutforest.state.RandomCutForestState)15 RandomCutForest (com.amazon.randomcutforest.RandomCutForest)10 Precision (com.amazon.randomcutforest.config.Precision)6 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 OperationsPerInvocation (org.openjdk.jmh.annotations.OperationsPerInvocation)6 NormalMixtureTestData (com.amazon.randomcutforest.testutils.NormalMixtureTestData)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 LinkedBuffer (io.protostuff.LinkedBuffer)5 Random (java.util.Random)5 Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 IRCFComputeDescriptor (com.amazon.randomcutforest.parkservices.IRCFComputeDescriptor)2 ThresholdedRandomCutForest (com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)2 Preprocessor (com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor)2 PreprocessorMapper (com.amazon.randomcutforest.parkservices.state.preprocessor.PreprocessorMapper)2 BasicThresholderMapper (com.amazon.randomcutforest.parkservices.state.threshold.BasicThresholderMapper)2 DiVectorMapper (com.amazon.randomcutforest.state.returntypes.DiVectorMapper)2 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2