Upload 2 files
Browse files- data_simulator.py +98 -0
- test_transactions_v2.json +0 -0
data_simulator.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import json
|
| 3 |
+
import uuid
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
|
| 6 |
+
# Define laundering patterns
|
| 7 |
+
PATTERNS = ["Fan-Out", "Fan-In", "Cycle", "Bipartite", "Stack", "Scatter Gather", "Gather Scatter", "Random"]
|
| 8 |
+
TRANSACTION_TYPES = ["Credit", "Debit", "Wire", "Card", "Cash"]
|
| 9 |
+
TIME_PERIODS = ["Morning", "Afternoon", "Night"]
|
| 10 |
+
COUNTRIES = ["USA", "UK", "Canada", "Germany", "India", "China", "UAE", "Australia"]
|
| 11 |
+
|
| 12 |
+
# Store transaction history
|
| 13 |
+
transaction_history = {}
|
| 14 |
+
|
| 15 |
+
def create_transaction(aml_flag=None):
|
| 16 |
+
"""Generate a simulated transaction"""
|
| 17 |
+
sender = f"A{random.randint(1, 1000)}"
|
| 18 |
+
receiver = f"A{random.randint(1, 1000)}"
|
| 19 |
+
amount = random.randint(50, 10000)
|
| 20 |
+
transaction_id = str(uuid.uuid4())
|
| 21 |
+
|
| 22 |
+
# Random timestamp (past 6 months)
|
| 23 |
+
timestamp = (datetime.now() - timedelta(days=random.randint(0, 180))).isoformat()
|
| 24 |
+
|
| 25 |
+
currency = "USD"
|
| 26 |
+
transaction_type = random.choice(TRANSACTION_TYPES)
|
| 27 |
+
sender_ip = random.choice(COUNTRIES)
|
| 28 |
+
receiver_ip = random.choice(COUNTRIES)
|
| 29 |
+
|
| 30 |
+
# Risk Score & AML Flag (Ensuring 50% AML and 50% Normal Transactions)
|
| 31 |
+
if aml_flag is None:
|
| 32 |
+
risk_score = round(random.uniform(0, 1), 2)
|
| 33 |
+
aml_flag = 1 if risk_score > 0.7 else 0
|
| 34 |
+
else:
|
| 35 |
+
risk_score = round(random.uniform(0.8, 1), 2) if aml_flag == 1 else round(random.uniform(0, 0.3), 2)
|
| 36 |
+
|
| 37 |
+
# Track transaction history
|
| 38 |
+
sender_history = transaction_history.get(sender, [])
|
| 39 |
+
sender_history.append((amount, timestamp))
|
| 40 |
+
transaction_history[sender] = sender_history[-30:] # Store last 30 transactions
|
| 41 |
+
|
| 42 |
+
# Compute Behavioral & Network Features
|
| 43 |
+
last_24h_tx = [tx for tx in sender_history if datetime.fromisoformat(tx[1]) > datetime.now() - timedelta(days=1)]
|
| 44 |
+
last_7d_tx = [tx for tx in sender_history if datetime.fromisoformat(tx[1]) > datetime.now() - timedelta(days=7)]
|
| 45 |
+
last_30d_tx = sender_history
|
| 46 |
+
|
| 47 |
+
daily_tx_count = len(last_24h_tx)
|
| 48 |
+
weekly_tx_count = len(last_7d_tx)
|
| 49 |
+
monthly_tx_count = len(last_30d_tx)
|
| 50 |
+
total_tx_volume = sum(tx[0] for tx in last_30d_tx)
|
| 51 |
+
|
| 52 |
+
avg_tx_amount = total_tx_volume / len(last_30d_tx) if last_30d_tx else amount
|
| 53 |
+
tx_amount_deviation = abs(amount - avg_tx_amount)
|
| 54 |
+
|
| 55 |
+
new_account_flag = 1 if len(sender_history) < 3 else 0
|
| 56 |
+
dormant_to_active_flag = 1 if random.random() < 0.05 else 0 # 5% chance
|
| 57 |
+
|
| 58 |
+
# Transaction Metadata
|
| 59 |
+
weekend_flag = 1 if datetime.fromisoformat(timestamp).weekday() >= 5 else 0
|
| 60 |
+
repeated_amount_flag = 1 if sum(1 for tx in sender_history if tx[0] == amount) > 1 else 0
|
| 61 |
+
time_of_day = random.choice(TIME_PERIODS)
|
| 62 |
+
|
| 63 |
+
return {
|
| 64 |
+
"TransactionID": transaction_id,
|
| 65 |
+
"Timestamp": timestamp,
|
| 66 |
+
"SenderAccount": sender,
|
| 67 |
+
"ReceiverAccount": receiver,
|
| 68 |
+
"Amount": amount,
|
| 69 |
+
"Currency": currency,
|
| 70 |
+
"TransactionType": transaction_type,
|
| 71 |
+
"AML_Flag": aml_flag,
|
| 72 |
+
"RiskScore": risk_score,
|
| 73 |
+
"DailyTransactionCount": daily_tx_count,
|
| 74 |
+
"WeeklyTransactionCount": weekly_tx_count,
|
| 75 |
+
"MonthlyTransactionCount": monthly_tx_count,
|
| 76 |
+
"TotalTransactionVolume": total_tx_volume,
|
| 77 |
+
"AverageTransactionAmount": avg_tx_amount,
|
| 78 |
+
"TransactionAmountDeviation": tx_amount_deviation,
|
| 79 |
+
"NewAccountFlag": new_account_flag,
|
| 80 |
+
"DormantToActiveFlag": dormant_to_active_flag,
|
| 81 |
+
"SenderIPLocation": sender_ip,
|
| 82 |
+
"ReceiverIPLocation": receiver_ip,
|
| 83 |
+
"WeekendFlag": weekend_flag,
|
| 84 |
+
"RepeatedAmountFlag": repeated_amount_flag,
|
| 85 |
+
"TimeOfDay": time_of_day
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
# Generate 50% AML and 50% Normal Transactions
|
| 89 |
+
aml_transactions = [create_transaction(aml_flag=1) for _ in range(5000)] # 50% AML
|
| 90 |
+
normal_transactions = [create_transaction(aml_flag=0) for _ in range(5000)] # 50% Normal
|
| 91 |
+
transactions = aml_transactions + normal_transactions
|
| 92 |
+
random.shuffle(transactions)
|
| 93 |
+
|
| 94 |
+
# Save to JSON file
|
| 95 |
+
with open("simulated_transactions.json", "w") as f:
|
| 96 |
+
json.dump(transactions, f, indent=4)
|
| 97 |
+
|
| 98 |
+
print("✅ Balanced simulated dataset created: simulated_transactions.json")
|
test_transactions_v2.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|