datasynth-generators 2.2.0

50+ data generators covering GL, P2P, O2C, S2C, HR, manufacturing, audit, tax, treasury, and ESG
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Anomaly type definitions and configurations for injection.

use datasynth_core::utils::weighted_select;
use rand::Rng;
use rust_decimal::Decimal;

use datasynth_core::models::{
    AnomalyType, ErrorType, FraudType, ProcessIssueType, RelationalAnomalyType,
    StatisticalAnomalyType,
};

/// Configuration for fraud type injection.
#[derive(Debug, Clone)]
pub struct FraudTypeConfig {
    /// Type of fraud.
    pub fraud_type: FraudType,
    /// Relative weight for selection.
    pub weight: f64,
    /// Minimum amount for this fraud type.
    pub min_amount: Option<Decimal>,
    /// Maximum amount for this fraud type.
    pub max_amount: Option<Decimal>,
    /// Whether this requires specific conditions.
    pub requires_conditions: bool,
    /// Description template.
    pub description_template: String,
}

impl FraudTypeConfig {
    /// Creates default configurations for all fraud types.
    pub fn all_defaults() -> Vec<Self> {
        vec![
            Self {
                fraud_type: FraudType::FictitiousEntry,
                weight: 1.0,
                min_amount: Some(Decimal::new(10000, 0)),
                max_amount: Some(Decimal::new(500000, 0)),
                requires_conditions: false,
                description_template: "Fictitious journal entry with no supporting documentation"
                    .to_string(),
            },
            Self {
                fraud_type: FraudType::RoundDollarManipulation,
                weight: 2.0,
                min_amount: Some(Decimal::new(1000, 0)),
                max_amount: Some(Decimal::new(100000, 0)),
                requires_conditions: false,
                description_template:
                    "Suspicious round-dollar amount suggesting manual manipulation".to_string(),
            },
            Self {
                fraud_type: FraudType::JustBelowThreshold,
                weight: 2.5,
                min_amount: None,
                max_amount: None,
                requires_conditions: true,
                description_template: "Transaction amount just below approval threshold of {}"
                    .to_string(),
            },
            Self {
                fraud_type: FraudType::SelfApproval,
                weight: 1.5,
                min_amount: None,
                max_amount: None,
                requires_conditions: true,
                description_template: "User {} approved their own transaction".to_string(),
            },
            Self {
                fraud_type: FraudType::ExceededApprovalLimit,
                weight: 1.5,
                min_amount: None,
                max_amount: None,
                requires_conditions: true,
                description_template: "Approval exceeds user's limit of {}".to_string(),
            },
            Self {
                fraud_type: FraudType::SegregationOfDutiesViolation,
                weight: 1.0,
                min_amount: None,
                max_amount: None,
                requires_conditions: true,
                description_template: "User performed conflicting duties: {} and {}".to_string(),
            },
            Self {
                fraud_type: FraudType::DuplicatePayment,
                weight: 2.0,
                min_amount: Some(Decimal::new(5000, 0)),
                max_amount: Some(Decimal::new(200000, 0)),
                requires_conditions: false,
                description_template: "Duplicate payment to vendor {} for invoice {}".to_string(),
            },
            Self {
                fraud_type: FraudType::FictitiousVendor,
                weight: 0.5,
                min_amount: Some(Decimal::new(25000, 0)),
                max_amount: Some(Decimal::new(1000000, 0)),
                requires_conditions: false,
                description_template: "Payment to potentially fictitious vendor {}".to_string(),
            },
            Self {
                fraud_type: FraudType::RevenueManipulation,
                weight: 0.5,
                min_amount: Some(Decimal::new(100000, 0)),
                max_amount: Some(Decimal::new(5000000, 0)),
                requires_conditions: false,
                description_template: "Premature or fraudulent revenue recognition".to_string(),
            },
            Self {
                fraud_type: FraudType::ImproperCapitalization,
                weight: 1.0,
                min_amount: Some(Decimal::new(10000, 0)),
                max_amount: Some(Decimal::new(500000, 0)),
                requires_conditions: false,
                description_template: "Expense improperly capitalized as asset".to_string(),
            },
        ]
    }

    /// Selects a fraud type based on weights.
    pub fn select_weighted<'a, R: Rng>(configs: &'a [Self], rng: &mut R) -> &'a Self {
        let options: Vec<(usize, f64)> = configs
            .iter()
            .enumerate()
            .map(|(i, c)| (i, c.weight))
            .collect();

        let &idx = weighted_select(rng, &options);
        &configs[idx]
    }
}

/// Configuration for error type injection.
#[derive(Debug, Clone)]
pub struct ErrorTypeConfig {
    /// Type of error.
    pub error_type: ErrorType,
    /// Relative weight for selection.
    pub weight: f64,
    /// Whether this error can be auto-detected.
    pub auto_detectable: bool,
    /// Description template.
    pub description_template: String,
}

impl ErrorTypeConfig {
    /// Creates default configurations for all error types.
    pub fn all_defaults() -> Vec<Self> {
        vec![
            Self {
                error_type: ErrorType::DuplicateEntry,
                weight: 2.0,
                auto_detectable: true,
                description_template: "Duplicate entry of document {}".to_string(),
            },
            Self {
                error_type: ErrorType::ReversedAmount,
                weight: 1.5,
                auto_detectable: false,
                description_template: "Debit and credit amounts appear reversed".to_string(),
            },
            Self {
                error_type: ErrorType::TransposedDigits,
                weight: 2.5,
                auto_detectable: false,
                description_template: "Digits transposed in amount: {} vs expected {}".to_string(),
            },
            Self {
                error_type: ErrorType::DecimalError,
                weight: 1.5,
                auto_detectable: false,
                description_template: "Decimal place error: {} should be {}".to_string(),
            },
            Self {
                error_type: ErrorType::MissingField,
                weight: 3.0,
                auto_detectable: true,
                description_template: "Missing required field: {}".to_string(),
            },
            Self {
                error_type: ErrorType::InvalidAccount,
                weight: 1.0,
                auto_detectable: true,
                description_template: "Invalid account code: {}".to_string(),
            },
            Self {
                error_type: ErrorType::WrongPeriod,
                weight: 2.0,
                auto_detectable: false,
                description_template: "Entry posted to wrong period: {} vs {}".to_string(),
            },
            Self {
                error_type: ErrorType::BackdatedEntry,
                weight: 1.5,
                auto_detectable: true,
                description_template: "Entry backdated by {} days".to_string(),
            },
            Self {
                error_type: ErrorType::FutureDatedEntry,
                weight: 0.5,
                auto_detectable: true,
                description_template: "Entry future-dated by {} days".to_string(),
            },
            Self {
                error_type: ErrorType::MisclassifiedAccount,
                weight: 2.0,
                auto_detectable: false,
                description_template: "Account {} misclassified, should be {}".to_string(),
            },
            Self {
                error_type: ErrorType::WrongCostCenter,
                weight: 2.5,
                auto_detectable: false,
                description_template: "Wrong cost center: {} vs {}".to_string(),
            },
            Self {
                error_type: ErrorType::UnbalancedEntry,
                weight: 0.5,
                auto_detectable: true,
                description_template: "Journal entry out of balance by {}".to_string(),
            },
            Self {
                error_type: ErrorType::RoundingError,
                weight: 3.0,
                auto_detectable: false,
                description_template: "Rounding discrepancy of {}".to_string(),
            },
            Self {
                error_type: ErrorType::CurrencyError,
                weight: 1.0,
                auto_detectable: false,
                description_template: "Currency conversion error: rate {} vs {}".to_string(),
            },
        ]
    }

    /// Selects an error type based on weights.
    pub fn select_weighted<'a, R: Rng>(configs: &'a [Self], rng: &mut R) -> &'a Self {
        let options: Vec<(usize, f64)> = configs
            .iter()
            .enumerate()
            .map(|(i, c)| (i, c.weight))
            .collect();

        let &idx = weighted_select(rng, &options);
        &configs[idx]
    }
}

/// Configuration for process issue injection.
#[derive(Debug, Clone)]
pub struct ProcessIssueConfig {
    /// Type of process issue.
    pub issue_type: ProcessIssueType,
    /// Relative weight for selection.
    pub weight: f64,
    /// Description template.
    pub description_template: String,
}

impl ProcessIssueConfig {
    /// Creates default configurations for all process issue types.
    pub fn all_defaults() -> Vec<Self> {
        vec![
            Self {
                issue_type: ProcessIssueType::SkippedApproval,
                weight: 1.5,
                description_template: "Required approval level skipped".to_string(),
            },
            Self {
                issue_type: ProcessIssueType::LateApproval,
                weight: 2.5,
                description_template: "Approval received {} days after posting".to_string(),
            },
            Self {
                issue_type: ProcessIssueType::MissingDocumentation,
                weight: 3.0,
                description_template: "Missing supporting documentation for {}".to_string(),
            },
            Self {
                issue_type: ProcessIssueType::LatePosting,
                weight: 3.5,
                description_template: "Posted {} days after transaction date".to_string(),
            },
            Self {
                issue_type: ProcessIssueType::AfterHoursPosting,
                weight: 2.0,
                description_template: "Posted at {} outside business hours".to_string(),
            },
            Self {
                issue_type: ProcessIssueType::WeekendPosting,
                weight: 1.5,
                description_template: "Posted on weekend: {}".to_string(),
            },
            Self {
                issue_type: ProcessIssueType::RushedPeriodEnd,
                weight: 2.0,
                description_template: "Rushed posting in final {} hours of period".to_string(),
            },
            Self {
                issue_type: ProcessIssueType::ManualOverride,
                weight: 1.0,
                description_template: "Manual override of control: {}".to_string(),
            },
            Self {
                issue_type: ProcessIssueType::VagueDescription,
                weight: 4.0,
                description_template: "Vague or non-descriptive text: '{}'".to_string(),
            },
            Self {
                issue_type: ProcessIssueType::IncompleteApprovalChain,
                weight: 1.5,
                description_template: "Approval chain incomplete: missing {}".to_string(),
            },
        ]
    }

    /// Selects a process issue type based on weights.
    pub fn select_weighted<'a, R: Rng>(configs: &'a [Self], rng: &mut R) -> &'a Self {
        let options: Vec<(usize, f64)> = configs
            .iter()
            .enumerate()
            .map(|(i, c)| (i, c.weight))
            .collect();

        let &idx = weighted_select(rng, &options);
        &configs[idx]
    }
}

/// Configuration for statistical anomaly injection.
#[derive(Debug, Clone)]
pub struct StatisticalAnomalyConfig {
    /// Type of statistical anomaly.
    pub anomaly_type: StatisticalAnomalyType,
    /// Relative weight for selection.
    pub weight: f64,
    /// Multiplier for amount anomalies.
    pub amount_multiplier: Option<f64>,
    /// Description template.
    pub description_template: String,
}

impl StatisticalAnomalyConfig {
    /// Creates default configurations for all statistical anomaly types.
    pub fn all_defaults() -> Vec<Self> {
        vec![
            Self {
                anomaly_type: StatisticalAnomalyType::UnusuallyHighAmount,
                weight: 2.0,
                amount_multiplier: Some(5.0),
                description_template: "Amount {} is {} standard deviations above mean".to_string(),
            },
            Self {
                anomaly_type: StatisticalAnomalyType::UnusuallyLowAmount,
                weight: 1.5,
                amount_multiplier: Some(0.1),
                description_template: "Amount {} is unusually low for this account".to_string(),
            },
            Self {
                anomaly_type: StatisticalAnomalyType::BenfordViolation,
                weight: 2.5,
                amount_multiplier: None,
                description_template:
                    "First digit {} violates Benford's Law (expected probability: {})".to_string(),
            },
            Self {
                anomaly_type: StatisticalAnomalyType::ExactDuplicateAmount,
                weight: 2.0,
                amount_multiplier: None,
                description_template: "Exact duplicate amount {} found in {} transactions"
                    .to_string(),
            },
            Self {
                anomaly_type: StatisticalAnomalyType::RepeatingAmount,
                weight: 1.5,
                amount_multiplier: None,
                description_template: "Repeating amount pattern: {} appears {} times".to_string(),
            },
            Self {
                anomaly_type: StatisticalAnomalyType::UnusualFrequency,
                weight: 2.0,
                amount_multiplier: None,
                description_template: "Unusual transaction frequency: {} vs expected {}"
                    .to_string(),
            },
            Self {
                anomaly_type: StatisticalAnomalyType::TransactionBurst,
                weight: 1.5,
                amount_multiplier: None,
                description_template: "Burst of {} transactions in {} minute window".to_string(),
            },
            Self {
                anomaly_type: StatisticalAnomalyType::UnusualTiming,
                weight: 3.0,
                amount_multiplier: None,
                description_template: "Transaction at unusual time: {}".to_string(),
            },
            Self {
                anomaly_type: StatisticalAnomalyType::TrendBreak,
                weight: 1.0,
                amount_multiplier: None,
                description_template: "Break in historical trend for account {}".to_string(),
            },
            Self {
                anomaly_type: StatisticalAnomalyType::StatisticalOutlier,
                weight: 2.0,
                amount_multiplier: Some(3.0),
                description_template: "Statistical outlier: z-score of {}".to_string(),
            },
        ]
    }

    /// Selects a statistical anomaly type based on weights.
    pub fn select_weighted<'a, R: Rng>(configs: &'a [Self], rng: &mut R) -> &'a Self {
        let options: Vec<(usize, f64)> = configs
            .iter()
            .enumerate()
            .map(|(i, c)| (i, c.weight))
            .collect();

        let &idx = weighted_select(rng, &options);
        &configs[idx]
    }
}

/// Configuration for relational anomaly injection.
#[derive(Debug, Clone)]
pub struct RelationalAnomalyConfig {
    /// Type of relational anomaly.
    pub anomaly_type: RelationalAnomalyType,
    /// Relative weight for selection.
    pub weight: f64,
    /// Description template.
    pub description_template: String,
}

impl RelationalAnomalyConfig {
    /// Creates default configurations for all relational anomaly types.
    pub fn all_defaults() -> Vec<Self> {
        vec![
            Self {
                anomaly_type: RelationalAnomalyType::CircularTransaction,
                weight: 1.0,
                description_template: "Circular transaction pattern: {} -> {} -> {}".to_string(),
            },
            Self {
                anomaly_type: RelationalAnomalyType::UnusualAccountPair,
                weight: 2.5,
                description_template: "Unusual account combination: {} with {}".to_string(),
            },
            Self {
                anomaly_type: RelationalAnomalyType::NewCounterparty,
                weight: 3.0,
                description_template: "First transaction with new counterparty {}".to_string(),
            },
            Self {
                anomaly_type: RelationalAnomalyType::DormantAccountActivity,
                weight: 2.0,
                description_template: "Activity on dormant account {} after {} days".to_string(),
            },
            Self {
                anomaly_type: RelationalAnomalyType::UnmatchedIntercompany,
                weight: 1.5,
                description_template: "Unmatched intercompany transaction: {} vs {}".to_string(),
            },
            Self {
                anomaly_type: RelationalAnomalyType::CircularIntercompany,
                weight: 0.5,
                description_template: "Circular intercompany flow detected".to_string(),
            },
            Self {
                anomaly_type: RelationalAnomalyType::TransferPricingAnomaly,
                weight: 1.0,
                description_template: "Transfer price deviation: {} vs arm's length {}".to_string(),
            },
            Self {
                anomaly_type: RelationalAnomalyType::MissingRelationship,
                weight: 2.0,
                description_template: "Expected relationship missing between {} and {}".to_string(),
            },
            Self {
                anomaly_type: RelationalAnomalyType::CentralityAnomaly,
                weight: 1.0,
                description_template: "Node {} has unusual centrality score: {}".to_string(),
            },
        ]
    }

    /// Selects a relational anomaly type based on weights.
    pub fn select_weighted<'a, R: Rng>(configs: &'a [Self], rng: &mut R) -> &'a Self {
        let options: Vec<(usize, f64)> = configs
            .iter()
            .enumerate()
            .map(|(i, c)| (i, c.weight))
            .collect();

        let &idx = weighted_select(rng, &options);
        &configs[idx]
    }
}

/// Combined anomaly type selector.
pub struct AnomalyTypeSelector {
    fraud_configs: Vec<FraudTypeConfig>,
    error_configs: Vec<ErrorTypeConfig>,
    process_configs: Vec<ProcessIssueConfig>,
    statistical_configs: Vec<StatisticalAnomalyConfig>,
    relational_configs: Vec<RelationalAnomalyConfig>,
}

impl Default for AnomalyTypeSelector {
    fn default() -> Self {
        Self::new()
    }
}

impl AnomalyTypeSelector {
    /// Creates a new selector with default configurations.
    pub fn new() -> Self {
        Self {
            fraud_configs: FraudTypeConfig::all_defaults(),
            error_configs: ErrorTypeConfig::all_defaults(),
            process_configs: ProcessIssueConfig::all_defaults(),
            statistical_configs: StatisticalAnomalyConfig::all_defaults(),
            relational_configs: RelationalAnomalyConfig::all_defaults(),
        }
    }

    /// Selects a fraud anomaly type.
    pub fn select_fraud<R: Rng>(&self, rng: &mut R) -> AnomalyType {
        let config = FraudTypeConfig::select_weighted(&self.fraud_configs, rng);
        AnomalyType::Fraud(config.fraud_type)
    }

    /// Selects an error anomaly type.
    pub fn select_error<R: Rng>(&self, rng: &mut R) -> AnomalyType {
        let config = ErrorTypeConfig::select_weighted(&self.error_configs, rng);
        AnomalyType::Error(config.error_type)
    }

    /// Selects a process issue anomaly type.
    pub fn select_process_issue<R: Rng>(&self, rng: &mut R) -> AnomalyType {
        let config = ProcessIssueConfig::select_weighted(&self.process_configs, rng);
        AnomalyType::ProcessIssue(config.issue_type)
    }

    /// Selects a statistical anomaly type.
    pub fn select_statistical<R: Rng>(&self, rng: &mut R) -> AnomalyType {
        let config = StatisticalAnomalyConfig::select_weighted(&self.statistical_configs, rng);
        AnomalyType::Statistical(config.anomaly_type)
    }

    /// Selects a relational anomaly type.
    pub fn select_relational<R: Rng>(&self, rng: &mut R) -> AnomalyType {
        let config = RelationalAnomalyConfig::select_weighted(&self.relational_configs, rng);
        AnomalyType::Relational(config.anomaly_type)
    }

    /// Gets the fraud config for a specific type.
    pub fn get_fraud_config(&self, fraud_type: FraudType) -> Option<&FraudTypeConfig> {
        self.fraud_configs
            .iter()
            .find(|c| c.fraud_type == fraud_type)
    }

    /// Gets the error config for a specific type.
    pub fn get_error_config(&self, error_type: ErrorType) -> Option<&ErrorTypeConfig> {
        self.error_configs
            .iter()
            .find(|c| c.error_type == error_type)
    }

    /// Gets the statistical config for a specific type.
    pub fn get_statistical_config(
        &self,
        anomaly_type: StatisticalAnomalyType,
    ) -> Option<&StatisticalAnomalyConfig> {
        self.statistical_configs
            .iter()
            .find(|c| c.anomaly_type == anomaly_type)
    }
}

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

    #[test]
    fn test_fraud_type_selection() {
        let configs = FraudTypeConfig::all_defaults();
        let mut rng = ChaCha8Rng::seed_from_u64(42);

        // Select multiple times to ensure variety
        let mut selected = std::collections::HashSet::new();
        for _ in 0..100 {
            let config = FraudTypeConfig::select_weighted(&configs, &mut rng);
            selected.insert(format!("{:?}", config.fraud_type));
        }

        assert!(selected.len() > 3); // Should select multiple types
    }

    #[test]
    fn test_anomaly_type_selector() {
        let selector = AnomalyTypeSelector::new();
        let mut rng = ChaCha8Rng::seed_from_u64(42);

        let fraud = selector.select_fraud(&mut rng);
        assert!(matches!(fraud, AnomalyType::Fraud(_)));

        let error = selector.select_error(&mut rng);
        assert!(matches!(error, AnomalyType::Error(_)));
    }
}