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