|
| 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.examples.parkservices; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Random; |
| 20 | + |
| 21 | +import com.amazon.randomcutforest.config.ForestMode; |
| 22 | +import com.amazon.randomcutforest.config.ImputationMethod; |
| 23 | +import com.amazon.randomcutforest.config.Precision; |
| 24 | +import com.amazon.randomcutforest.config.TransformMethod; |
| 25 | +import com.amazon.randomcutforest.examples.Example; |
| 26 | +import com.amazon.randomcutforest.parkservices.AnomalyDescriptor; |
| 27 | +import com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest; |
| 28 | +import com.amazon.randomcutforest.testutils.MultiDimDataWithKey; |
| 29 | +import com.amazon.randomcutforest.testutils.ShingledMultiDimDataWithKeys; |
| 30 | + |
| 31 | +public class ThresholdedImpute implements Example { |
| 32 | + |
| 33 | + public static void main(String[] args) throws Exception { |
| 34 | + new ThresholdedImpute().run(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String command() { |
| 39 | + return "Thresholded_Imputation_example"; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String description() { |
| 44 | + return "Thresholded Imputation Example"; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void run() throws Exception { |
| 49 | + // Create and populate a random cut forest |
| 50 | + |
| 51 | + int shingleSize = 4; |
| 52 | + int numberOfTrees = 50; |
| 53 | + int sampleSize = 256; |
| 54 | + Precision precision = Precision.FLOAT_32; |
| 55 | + int dataSize = 4 * sampleSize; |
| 56 | + int baseDimensions = 1; |
| 57 | + |
| 58 | + long count = 0; |
| 59 | + |
| 60 | + int dropped = 0; |
| 61 | + |
| 62 | + int dimensions = baseDimensions * shingleSize; |
| 63 | + ThresholdedRandomCutForest forest = new ThresholdedRandomCutForest.Builder<>().compact(true) |
| 64 | + .dimensions(dimensions).randomSeed(0).numberOfTrees(numberOfTrees).shingleSize(shingleSize) |
| 65 | + .sampleSize(sampleSize).precision(precision).anomalyRate(0.01).imputationMethod(ImputationMethod.RCF) |
| 66 | + .forestMode(ForestMode.STREAMING_IMPUTE).transformMethod(TransformMethod.NORMALIZE_DIFFERENCE) |
| 67 | + .adjustThreshold(true).build(); |
| 68 | + |
| 69 | + long seed = new Random().nextLong(); |
| 70 | + Random noisePRG = new Random(0); |
| 71 | + |
| 72 | + System.out.println("seed = " + seed); |
| 73 | + MultiDimDataWithKey dataWithKeys = ShingledMultiDimDataWithKeys.getMultiDimData(dataSize + shingleSize - 1, 50, |
| 74 | + 100, 5, seed, baseDimensions); |
| 75 | + |
| 76 | + // as we loop over the data we will be dropping observations with probability |
| 77 | + // 0.2 |
| 78 | + // note that as a result the predictor correct method would like be more |
| 79 | + // error-prone |
| 80 | + // note that estimation of the number of entries to be imputed is also another |
| 81 | + // estimation |
| 82 | + // therefore the overall method may have runaway effects if more values are |
| 83 | + // dropped. |
| 84 | + |
| 85 | + int keyCounter = 0; |
| 86 | + for (double[] point : dataWithKeys.data) { |
| 87 | + |
| 88 | + if (noisePRG.nextDouble() < 0.2 && !((keyCounter < dataWithKeys.changeIndices.length |
| 89 | + && count == dataWithKeys.changeIndices[keyCounter]))) { |
| 90 | + dropped++; |
| 91 | + if (keyCounter < dataWithKeys.changeIndices.length && count == dataWithKeys.changeIndices[keyCounter]) { |
| 92 | + System.out.println(" dropped sequence " + (count) + " INPUT " + Arrays.toString(point) + " CHANGE " |
| 93 | + + Arrays.toString(dataWithKeys.changes[keyCounter])); |
| 94 | + } |
| 95 | + } else { |
| 96 | + long newStamp = 100 * count + 2 * noisePRG.nextInt(10) - 5; |
| 97 | + AnomalyDescriptor result = forest.process(point, newStamp); |
| 98 | + |
| 99 | + if (keyCounter < dataWithKeys.changeIndices.length && count == dataWithKeys.changeIndices[keyCounter]) { |
| 100 | + System.out.println("sequence " + (count) + " INPUT " + Arrays.toString(point) + " CHANGE " |
| 101 | + + Arrays.toString(dataWithKeys.changes[keyCounter])); |
| 102 | + ++keyCounter; |
| 103 | + } |
| 104 | + |
| 105 | + if (result.getAnomalyGrade() != 0) { |
| 106 | + System.out.print("sequence " + (count) + " RESULT value "); |
| 107 | + for (int i = 0; i < baseDimensions; i++) { |
| 108 | + System.out.print(result.getCurrentInput()[i] + ", "); |
| 109 | + } |
| 110 | + System.out.print("score " + result.getRCFScore() + ", grade " + result.getAnomalyGrade() + ", "); |
| 111 | + |
| 112 | + if (result.isExpectedValuesPresent()) { |
| 113 | + if (result.getRelativeIndex() != 0 && result.isStartOfAnomaly()) { |
| 114 | + System.out.print(-result.getRelativeIndex() + " steps ago, instead of "); |
| 115 | + for (int i = 0; i < baseDimensions; i++) { |
| 116 | + System.out.print(result.getPastValues()[i] + ", "); |
| 117 | + } |
| 118 | + System.out.print("expected "); |
| 119 | + for (int i = 0; i < baseDimensions; i++) { |
| 120 | + System.out.print(result.getExpectedValuesList()[0][i] + ", "); |
| 121 | + if (result.getPastValues()[i] != result.getExpectedValuesList()[0][i]) { |
| 122 | + System.out.print( |
| 123 | + "( " + (result.getPastValues()[i] - result.getExpectedValuesList()[0][i]) |
| 124 | + + " ) "); |
| 125 | + } |
| 126 | + } |
| 127 | + } else { |
| 128 | + System.out.print("expected "); |
| 129 | + for (int i = 0; i < baseDimensions; i++) { |
| 130 | + System.out.print(result.getExpectedValuesList()[0][i] + ", "); |
| 131 | + if (result.getCurrentInput()[i] != result.getExpectedValuesList()[0][i]) { |
| 132 | + System.out.print( |
| 133 | + "( " + (result.getCurrentInput()[i] - result.getExpectedValuesList()[0][i]) |
| 134 | + + " ) "); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + System.out.println(); |
| 140 | + } |
| 141 | + } |
| 142 | + ++count; |
| 143 | + } |
| 144 | + System.out.println("Dropped " + dropped + " out of " + count); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments