|
| 1 | +/* |
| 2 | + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazon.randomcutforest.parkservices; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Random; |
| 24 | + |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.EnumSource; |
| 27 | + |
| 28 | +import com.amazon.randomcutforest.config.ForestMode; |
| 29 | +import com.amazon.randomcutforest.config.ImputationMethod; |
| 30 | +import com.amazon.randomcutforest.config.Precision; |
| 31 | +import com.amazon.randomcutforest.config.TransformMethod; |
| 32 | + |
| 33 | +public class MissingValueTest { |
| 34 | + @ParameterizedTest |
| 35 | + @EnumSource(ImputationMethod.class) |
| 36 | + public void testConfidence(ImputationMethod method) { |
| 37 | + // Create and populate a random cut forest |
| 38 | + |
| 39 | + int shingleSize = 4; |
| 40 | + int numberOfTrees = 50; |
| 41 | + int sampleSize = 256; |
| 42 | + Precision precision = Precision.FLOAT_32; |
| 43 | + int baseDimensions = 1; |
| 44 | + |
| 45 | + long count = 0; |
| 46 | + |
| 47 | + int dimensions = baseDimensions * shingleSize; |
| 48 | + ThresholdedRandomCutForest forest = new ThresholdedRandomCutForest.Builder<>().compact(true) |
| 49 | + .dimensions(dimensions).randomSeed(0).numberOfTrees(numberOfTrees).shingleSize(shingleSize) |
| 50 | + .sampleSize(sampleSize).precision(precision).anomalyRate(0.01).imputationMethod(method) |
| 51 | + .fillValues(new double[] { 3 }).forestMode(ForestMode.STREAMING_IMPUTE) |
| 52 | + .transformMethod(TransformMethod.NORMALIZE).autoAdjust(true).build(); |
| 53 | + |
| 54 | + // Define the size and range |
| 55 | + int size = 400; |
| 56 | + double min = 200.0; |
| 57 | + double max = 240.0; |
| 58 | + |
| 59 | + // Generate the list of doubles |
| 60 | + List<Double> randomDoubles = generateUniformRandomDoubles(size, min, max); |
| 61 | + |
| 62 | + double lastConfidence = 0; |
| 63 | + for (double val : randomDoubles) { |
| 64 | + double[] point = new double[] { val }; |
| 65 | + long newStamp = 100 * count; |
| 66 | + if (count >= 300 && count < 325) { |
| 67 | + // drop observations |
| 68 | + AnomalyDescriptor result = forest.process(new double[] { Double.NaN }, newStamp, |
| 69 | + generateIntArray(point.length)); |
| 70 | + if (count > 300) { |
| 71 | + // confidence start decreasing after 1 missing point |
| 72 | + assertTrue(result.getDataConfidence() < lastConfidence, "count " + count); |
| 73 | + } |
| 74 | + lastConfidence = result.getDataConfidence(); |
| 75 | + float[] rcfPoint = result.getRCFPoint(); |
| 76 | + double scale = result.getScale()[0]; |
| 77 | + double shift = result.getShift()[0]; |
| 78 | + double[] actual = new double[] { (rcfPoint[3] * scale) + shift }; |
| 79 | + if (method == ImputationMethod.ZERO) { |
| 80 | + assertEquals(0, actual[0], 0.001d); |
| 81 | + } else if (method == ImputationMethod.FIXED_VALUES) { |
| 82 | + assertEquals(3.0d, actual[0], 0.001d); |
| 83 | + } |
| 84 | + } else { |
| 85 | + AnomalyDescriptor result = forest.process(point, newStamp); |
| 86 | + if ((count > 100 && count < 300) || count >= 326) { |
| 87 | + // The first 65+ observations gives 0 confidence. |
| 88 | + // Confidence start increasing after 1 observed point |
| 89 | + assertTrue(result.getDataConfidence() > lastConfidence); |
| 90 | + } |
| 91 | + lastConfidence = result.getDataConfidence(); |
| 92 | + } |
| 93 | + ++count; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static int[] generateIntArray(int size) { |
| 98 | + int[] intArray = new int[size]; |
| 99 | + for (int i = 0; i < size; i++) { |
| 100 | + intArray[i] = i; |
| 101 | + } |
| 102 | + return intArray; |
| 103 | + } |
| 104 | + |
| 105 | + public static List<Double> generateUniformRandomDoubles(int size, double min, double max) { |
| 106 | + List<Double> randomDoubles = new ArrayList<>(size); |
| 107 | + Random random = new Random(0); |
| 108 | + |
| 109 | + for (int i = 0; i < size; i++) { |
| 110 | + double randomValue = min + (max - min) * random.nextDouble(); |
| 111 | + randomDoubles.add(randomValue); |
| 112 | + } |
| 113 | + |
| 114 | + return randomDoubles; |
| 115 | + } |
| 116 | +} |
0 commit comments