change probabilities and parameters

This commit is contained in:
kacperlo 2025-06-12 16:30:52 +02:00
parent ff642d68f0
commit 458850e355
2 changed files with 7 additions and 7 deletions

View File

@ -147,7 +147,7 @@ public class AnomalyDetector {
// Check for anomalies (transactions that are more than 1.7 standard deviations from mean) // Check for anomalies (transactions that are more than 1.7 standard deviations from mean)
for (Transaction transaction : transactionList) { 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( out.collect(new TransactionAlert(
"AMOUNT_ANOMALY", "AMOUNT_ANOMALY",
transaction.getCardId(), transaction.getCardId(),
@ -177,8 +177,8 @@ public class AnomalyDetector {
private transient MapState<String, Set<LocationPoint>> knownLocations; private transient MapState<String, Set<LocationPoint>> knownLocations;
private static final int MAX_KNOWN_LOCATIONS = 5; // Limit known locations to avoid memory issues 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 double ANOMALY_DISTANCE_THRESHOLD = 10.0; // Threshold in km
private static final int MIN_LOCATIONS_FOR_DETECTION = 3; // Minimum known locations before detecting anomalies private static final int MIN_LOCATIONS_FOR_DETECTION = 2; // Minimum known locations before detecting anomalies
@Override @Override
public void open(Configuration parameters) throws Exception { public void open(Configuration parameters) throws Exception {
@ -359,7 +359,7 @@ public class AnomalyDetector {
long windowSizeMinutes = (windowEnd - windowStart) / (1000 * 60); long windowSizeMinutes = (windowEnd - windowStart) / (1000 * 60);
// If there are more than 5 transactions in 5 minutes for the same card, flag it // 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() Transaction latestTransaction = transactionList.stream()
.max(Comparator.comparing(Transaction::getTimestamp)) .max(Comparator.comparing(Transaction::getTimestamp))
.orElse(transactionList.get(0)); .orElse(transactionList.get(0));

View File

@ -21,7 +21,7 @@ public class TransactionGenerator {
private static final int ANOMALY_FREQUENCY = 3; private static final int ANOMALY_FREQUENCY = 3;
// Probability of generating an anomaly (1%) // 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 // Initialize card and user data
static { static {
@ -58,9 +58,9 @@ public class TransactionGenerator {
anomalyType : ThreadLocalRandom.current().nextInt(1, 4); anomalyType : ThreadLocalRandom.current().nextInt(1, 4);
} else if (ThreadLocalRandom.current().nextDouble() < ANOMALY_PROBABILITY) { } else if (ThreadLocalRandom.current().nextDouble() < ANOMALY_PROBABILITY) {
double roll = ThreadLocalRandom.current().nextDouble(); double roll = ThreadLocalRandom.current().nextDouble();
if (roll < 0.3) { if (roll < 0.4) {
actualAnomalyType = ANOMALY_LOCATION; actualAnomalyType = ANOMALY_LOCATION;
} else if (roll < 0.8) { } else if (roll < 0.7) {
actualAnomalyType = ANOMALY_AMOUNT; actualAnomalyType = ANOMALY_AMOUNT;
} else { } else {
actualAnomalyType = ANOMALY_FREQUENCY; actualAnomalyType = ANOMALY_FREQUENCY;