datasynth-banking 2.5.0

KYC/AML banking transaction generator for synthetic data - compliance testing and fraud analytics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Main AML typology injector.

use datasynth_core::models::banking::{AmlTypology, LaunderingStage, Sophistication};
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;

use crate::config::BankingConfig;
use crate::models::{
    AmlScenario, BankAccount, BankTransaction, BankingCustomer, CaseNarrative, CaseRecommendation,
};

use super::{FunnelInjector, LayeringInjector, MuleInjector, SpoofingEngine, StructuringInjector};
use crate::seed_offsets::TYPOLOGY_INJECTOR_SEED_OFFSET;

/// Main AML typology injector.
pub struct TypologyInjector {
    config: BankingConfig,
    rng: ChaCha8Rng,
    structuring_injector: StructuringInjector,
    funnel_injector: FunnelInjector,
    layering_injector: LayeringInjector,
    mule_injector: MuleInjector,
    spoofing_engine: SpoofingEngine,
    scenarios: Vec<AmlScenario>,
    scenario_counter: u32,
}

impl TypologyInjector {
    /// Create a new typology injector.
    pub fn new(config: BankingConfig, seed: u64) -> Self {
        Self {
            config: config.clone(),
            rng: ChaCha8Rng::seed_from_u64(seed.wrapping_add(TYPOLOGY_INJECTOR_SEED_OFFSET)),
            structuring_injector: StructuringInjector::new(seed),
            funnel_injector: FunnelInjector::new(seed),
            layering_injector: LayeringInjector::new(seed),
            mule_injector: MuleInjector::new(seed),
            spoofing_engine: SpoofingEngine::new(config.spoofing.clone(), seed),
            scenarios: Vec::new(),
            scenario_counter: 0,
        }
    }

    /// Inject AML patterns into transactions.
    pub fn inject(
        &mut self,
        customers: &mut [BankingCustomer],
        accounts: &mut [BankAccount],
        transactions: &mut Vec<BankTransaction>,
    ) {
        if !self.config.typologies.suspicious_rate.is_finite()
            || self.config.typologies.suspicious_rate <= 0.0
        {
            return;
        }

        let total_customers = customers.len();

        // Calculate number of suspicious customers
        let suspicious_count =
            (total_customers as f64 * self.config.typologies.suspicious_rate) as usize;

        // Select random customers for suspicious activity
        let mut customer_indices: Vec<usize> = (0..total_customers).collect();
        customer_indices.shuffle(&mut self.rng);
        let suspicious_indices: Vec<usize> = customer_indices
            .into_iter()
            .take(suspicious_count)
            .collect();

        // Inject different typologies
        for &idx in &suspicious_indices {
            let typology = self.select_typology();
            let sophistication = self.select_sophistication();

            match typology {
                AmlTypology::Structuring | AmlTypology::Smurfing => {
                    self.inject_structuring(idx, customers, accounts, transactions, sophistication);
                }
                AmlTypology::FunnelAccount => {
                    self.inject_funnel(idx, customers, accounts, transactions, sophistication);
                }
                AmlTypology::Layering => {
                    self.inject_layering(idx, customers, accounts, transactions, sophistication);
                }
                AmlTypology::MoneyMule => {
                    self.inject_mule(idx, customers, accounts, transactions, sophistication);
                }
                _ => {
                    // Generic suspicious marking
                    self.inject_generic(idx, customers, accounts, transactions, typology);
                }
            }
        }

        // Apply spoofing to sophisticated typologies
        if self.config.spoofing.enabled {
            self.apply_spoofing(customers, transactions);
        }
    }

    /// Select a typology based on configured rates.
    fn select_typology(&mut self) -> AmlTypology {
        let rates = [
            (
                AmlTypology::Structuring,
                self.config.typologies.structuring_rate,
            ),
            (
                AmlTypology::FunnelAccount,
                self.config.typologies.funnel_rate,
            ),
            (AmlTypology::Layering, self.config.typologies.layering_rate),
            (AmlTypology::MoneyMule, self.config.typologies.mule_rate),
            (
                AmlTypology::AccountTakeover,
                self.config.typologies.fraud_rate * 0.3,
            ),
            (
                AmlTypology::FirstPartyFraud,
                self.config.typologies.fraud_rate * 0.3,
            ),
            (
                AmlTypology::AuthorizedPushPayment,
                self.config.typologies.fraud_rate * 0.4,
            ),
        ];

        let total: f64 = rates.iter().map(|(_, r)| r).sum();
        if total <= 0.0 {
            return AmlTypology::Structuring;
        }

        let roll: f64 = self.rng.random::<f64>() * total;
        let mut cumulative = 0.0;

        for (typology, rate) in rates {
            cumulative += rate;
            if roll < cumulative {
                return typology;
            }
        }

        AmlTypology::Structuring
    }

    /// Select sophistication level.
    fn select_sophistication(&mut self) -> Sophistication {
        let dist = &self.config.typologies.sophistication;
        let total = dist.basic + dist.standard + dist.professional + dist.advanced;
        let roll: f64 = self.rng.random::<f64>() * total;

        let mut cumulative = 0.0;
        cumulative += dist.basic;
        if roll < cumulative {
            return Sophistication::Basic;
        }
        cumulative += dist.standard;
        if roll < cumulative {
            return Sophistication::Standard;
        }
        cumulative += dist.professional;
        if roll < cumulative {
            return Sophistication::Professional;
        }
        Sophistication::Advanced
    }

    /// Inject structuring pattern.
    fn inject_structuring(
        &mut self,
        customer_idx: usize,
        customers: &mut [BankingCustomer],
        accounts: &mut [BankAccount],
        transactions: &mut Vec<BankTransaction>,
        sophistication: Sophistication,
    ) {
        let customer = &mut customers[customer_idx];
        if customer.account_ids.is_empty() {
            return;
        }

        let account_id = customer.account_ids[0];
        let account = accounts.iter_mut().find(|a| a.account_id == account_id);

        if let Some(account) = account {
            let scenario_id = self.next_scenario_id();
            let start_date = crate::parse_start_date(&self.config.population.start_date);
            let end_date = start_date + chrono::Months::new(1);

            let mut scenario =
                AmlScenario::new(&scenario_id, AmlTypology::Structuring, start_date, end_date)
                    .with_sophistication(sophistication);

            // Generate structuring transactions
            let structuring_txns = self.structuring_injector.generate(
                customer,
                account,
                start_date,
                end_date,
                sophistication,
            );

            for txn in structuring_txns {
                scenario.add_transaction(txn.transaction_id, txn.amount);
                transactions.push(txn);
            }

            scenario.add_customer(customer.customer_id);
            scenario.add_account(account.account_id);
            scenario.add_stage(LaunderingStage::Placement);

            // Generate narrative
            scenario.narrative = CaseNarrative::new(
                &format!(
                    "Customer {} conducted multiple cash deposits just below reporting threshold over a short period.",
                    customer.name.display_name()
                ),
            )
            .with_recommendation(CaseRecommendation::FileSar);

            // Mark customer
            customer.is_mule = false;
            account.case_id = Some(scenario_id.clone());

            self.scenarios.push(scenario);
        }
    }

    /// Inject funnel account pattern.
    fn inject_funnel(
        &mut self,
        customer_idx: usize,
        customers: &mut [BankingCustomer],
        accounts: &mut [BankAccount],
        transactions: &mut Vec<BankTransaction>,
        sophistication: Sophistication,
    ) {
        let customer = &mut customers[customer_idx];
        if customer.account_ids.is_empty() {
            return;
        }

        let account_id = customer.account_ids[0];
        if let Some(account) = accounts.iter_mut().find(|a| a.account_id == account_id) {
            let scenario_id = self.next_scenario_id();
            let start_date = crate::parse_start_date(&self.config.population.start_date);
            let end_date = start_date + chrono::Months::new(2);

            let mut scenario = AmlScenario::new(
                &scenario_id,
                AmlTypology::FunnelAccount,
                start_date,
                end_date,
            )
            .with_sophistication(sophistication);

            // Generate funnel transactions
            let funnel_txns = self.funnel_injector.generate(
                customer,
                account,
                start_date,
                end_date,
                sophistication,
            );

            for txn in funnel_txns {
                scenario.add_transaction(txn.transaction_id, txn.amount);
                transactions.push(txn);
            }

            scenario.add_customer(customer.customer_id);
            scenario.add_account(account.account_id);
            scenario.add_stage(LaunderingStage::Layering);
            account.is_funnel_account = true;
            account.case_id = Some(scenario_id.clone());

            scenario.narrative = CaseNarrative::new(
                "Account shows funnel pattern with many inbound transfers rapidly consolidated and moved out.",
            )
            .with_recommendation(CaseRecommendation::FileSar);

            self.scenarios.push(scenario);
        }
    }

    /// Inject layering pattern.
    fn inject_layering(
        &mut self,
        customer_idx: usize,
        customers: &mut [BankingCustomer],
        accounts: &mut [BankAccount],
        transactions: &mut Vec<BankTransaction>,
        sophistication: Sophistication,
    ) {
        let customer = &mut customers[customer_idx];
        if customer.account_ids.is_empty() {
            return;
        }

        let account_id = customer.account_ids[0];
        if let Some(account) = accounts.iter_mut().find(|a| a.account_id == account_id) {
            let scenario_id = self.next_scenario_id();
            let start_date = crate::parse_start_date(&self.config.population.start_date);
            let end_date = start_date + chrono::Months::new(1);

            let mut scenario =
                AmlScenario::new(&scenario_id, AmlTypology::Layering, start_date, end_date)
                    .with_sophistication(sophistication);

            let layering_txns = self.layering_injector.generate(
                customer,
                account,
                start_date,
                end_date,
                sophistication,
            );

            for txn in layering_txns {
                scenario.add_transaction(txn.transaction_id, txn.amount);
                transactions.push(txn);
            }

            scenario.add_customer(customer.customer_id);
            scenario.add_account(account.account_id);
            scenario.add_stage(LaunderingStage::Layering);
            account.case_id = Some(scenario_id.clone());

            scenario.narrative = CaseNarrative::new(
                "Complex layering pattern with rapid multi-hop transfers designed to obscure fund trail.",
            )
            .with_recommendation(CaseRecommendation::FileSar);

            self.scenarios.push(scenario);
        }
    }

    /// Inject mule pattern.
    fn inject_mule(
        &mut self,
        customer_idx: usize,
        customers: &mut [BankingCustomer],
        accounts: &mut [BankAccount],
        transactions: &mut Vec<BankTransaction>,
        sophistication: Sophistication,
    ) {
        let customer = &mut customers[customer_idx];
        if customer.account_ids.is_empty() {
            return;
        }

        let account_id = customer.account_ids[0];
        if let Some(account) = accounts.iter_mut().find(|a| a.account_id == account_id) {
            let scenario_id = self.next_scenario_id();
            let start_date = crate::parse_start_date(&self.config.population.start_date);
            let end_date = start_date + chrono::Months::new(1);

            let mut scenario =
                AmlScenario::new(&scenario_id, AmlTypology::MoneyMule, start_date, end_date)
                    .with_sophistication(sophistication);

            let mule_txns = self.mule_injector.generate(
                customer,
                account,
                start_date,
                end_date,
                sophistication,
            );

            for txn in mule_txns {
                scenario.add_transaction(txn.transaction_id, txn.amount);
                transactions.push(txn);
            }

            scenario.add_customer(customer.customer_id);
            scenario.add_account(account.account_id);
            customer.is_mule = true;
            account.is_mule_account = true;
            account.case_id = Some(scenario_id.clone());

            scenario.narrative = CaseNarrative::new(
                "Account shows classic money mule pattern: inbound transfers followed by rapid cash withdrawals or wire transfers.",
            )
            .with_recommendation(CaseRecommendation::CloseAccount);

            self.scenarios.push(scenario);
        }
    }

    /// Inject generic suspicious pattern.
    fn inject_generic(
        &mut self,
        customer_idx: usize,
        customers: &mut [BankingCustomer],
        accounts: &mut [BankAccount],
        transactions: &mut [BankTransaction],
        typology: AmlTypology,
    ) {
        let customer = &mut customers[customer_idx];
        if customer.account_ids.is_empty() {
            return;
        }

        // Mark random transactions as suspicious
        let account_id = customer.account_ids[0];
        let scenario_id = self.next_scenario_id();

        for txn in transactions.iter_mut() {
            if txn.account_id == account_id && self.rng.random::<f64>() < 0.1 {
                txn.is_suspicious = true;
                txn.suspicion_reason = Some(typology);
                txn.case_id = Some(scenario_id.clone());
            }
        }

        if let Some(account) = accounts.iter_mut().find(|a| a.account_id == account_id) {
            account.case_id = Some(scenario_id);
        }
    }

    /// Apply spoofing to sophisticated transactions.
    fn apply_spoofing(
        &mut self,
        customers: &[BankingCustomer],
        transactions: &mut [BankTransaction],
    ) {
        for txn in transactions.iter_mut() {
            if txn.is_suspicious {
                // Find the customer
                let customer = customers
                    .iter()
                    .find(|c| c.account_ids.contains(&txn.account_id));

                if let Some(customer) = customer {
                    self.spoofing_engine.apply(txn, customer);
                }
            }
        }
    }

    /// Generate next scenario ID.
    fn next_scenario_id(&mut self) -> String {
        self.scenario_counter += 1;
        format!("SC-{:06}", self.scenario_counter)
    }

    /// Get all generated scenarios.
    pub fn get_scenarios(&self) -> &[AmlScenario] {
        &self.scenarios
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn test_typology_injection() {
        let config = BankingConfig::small();
        let mut injector = TypologyInjector::new(config.clone(), 12345);

        // Generate test data
        let mut customer_gen = crate::generators::CustomerGenerator::new(config.clone(), 12345);
        let mut customers = customer_gen.generate_all();

        let mut account_gen = crate::generators::AccountGenerator::new(config.clone(), 12345);
        let mut accounts = account_gen.generate_for_customers(&mut customers);

        let mut txn_gen = crate::generators::TransactionGenerator::new(config, 12345);
        let mut transactions = txn_gen.generate_all(&customers, &mut accounts);

        let initial_count = transactions.len();

        injector.inject(&mut customers, &mut accounts, &mut transactions);

        // Should have added some suspicious transactions
        let suspicious_count = transactions.iter().filter(|t| t.is_suspicious).count();
        assert!(suspicious_count > 0 || transactions.len() > initial_count);
    }
}