diff --git a/anomaly-detector/src/main/java/com/anomaly/detector/AnomalyDetector.java b/anomaly-detector/src/main/java/com/anomaly/detector/AnomalyDetector.java index c7128331..be2df2ce 100644 --- a/anomaly-detector/src/main/java/com/anomaly/detector/AnomalyDetector.java +++ b/anomaly-detector/src/main/java/com/anomaly/detector/AnomalyDetector.java @@ -147,7 +147,7 @@ public class AnomalyDetector { // Check for anomalies (transactions that are more than 1.7 standard deviations from mean) for (Transaction transaction : transactionList) { - if (stdDeviation > 0 && Math.abs(transaction.getAmount() - averageAmount) > 2 * stdDeviation && transaction.getAmount() > averageAmount && transaction.getAmount() > 1000) { + if (stdDeviation > 0 && Math.abs(transaction.getAmount() - averageAmount) > 1.5 * stdDeviation && transaction.getAmount() > averageAmount && transaction.getAmount() > 1000) { out.collect(new TransactionAlert( "AMOUNT_ANOMALY", transaction.getCardId(), @@ -177,8 +177,8 @@ public class AnomalyDetector { private transient MapState> knownLocations; private static final int MAX_KNOWN_LOCATIONS = 5; // Limit known locations to avoid memory issues - private static final double ANOMALY_DISTANCE_THRESHOLD = 50.0; // Threshold in km - private static final int MIN_LOCATIONS_FOR_DETECTION = 3; // Minimum known locations before detecting anomalies + private static final double ANOMALY_DISTANCE_THRESHOLD = 10.0; // Threshold in km + private static final int MIN_LOCATIONS_FOR_DETECTION = 2; // Minimum known locations before detecting anomalies @Override public void open(Configuration parameters) throws Exception { @@ -359,7 +359,7 @@ public class AnomalyDetector { long windowSizeMinutes = (windowEnd - windowStart) / (1000 * 60); // If there are more than 5 transactions in 5 minutes for the same card, flag it - if (transactionList.size() > 5) { + if (transactionList.size() > 7) { Transaction latestTransaction = transactionList.stream() .max(Comparator.comparing(Transaction::getTimestamp)) .orElse(transactionList.get(0)); diff --git a/transaction-simulator/src/main/java/com/anomaly/generator/TransactionGenerator.java b/transaction-simulator/src/main/java/com/anomaly/generator/TransactionGenerator.java index 5cdab5d7..fa2950bd 100644 --- a/transaction-simulator/src/main/java/com/anomaly/generator/TransactionGenerator.java +++ b/transaction-simulator/src/main/java/com/anomaly/generator/TransactionGenerator.java @@ -21,7 +21,7 @@ public class TransactionGenerator { private static final int ANOMALY_FREQUENCY = 3; // Probability of generating an anomaly (1%) - private static final double ANOMALY_PROBABILITY = 0.5; + private static final double ANOMALY_PROBABILITY = 0.05; // Initialize card and user data static { @@ -58,9 +58,9 @@ public class TransactionGenerator { anomalyType : ThreadLocalRandom.current().nextInt(1, 4); } else if (ThreadLocalRandom.current().nextDouble() < ANOMALY_PROBABILITY) { double roll = ThreadLocalRandom.current().nextDouble(); - if (roll < 0.3) { + if (roll < 0.4) { actualAnomalyType = ANOMALY_LOCATION; - } else if (roll < 0.8) { + } else if (roll < 0.7) { actualAnomalyType = ANOMALY_AMOUNT; } else { actualAnomalyType = ANOMALY_FREQUENCY;