datasynth-eval 2.2.0

Evaluation framework for synthetic financial data quality and coherence
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
//! Project accounting coherence evaluator.
//!
//! Validates percentage-of-completion revenue recognition, earned value
//! management (EVM) metrics, and retainage balance calculations.

use crate::error::EvalResult;
use serde::{Deserialize, Serialize};

/// Thresholds for project accounting evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectAccountingThresholds {
    /// Minimum accuracy for EVM metric calculations.
    pub min_evm_accuracy: f64,
    /// Minimum accuracy for PoC completion percentage.
    pub min_poc_accuracy: f64,
    /// Tolerance for EVM comparisons.
    pub evm_tolerance: f64,
}

impl Default for ProjectAccountingThresholds {
    fn default() -> Self {
        Self {
            min_evm_accuracy: 0.999,
            min_poc_accuracy: 0.99,
            evm_tolerance: 0.01,
        }
    }
}

/// Project revenue data for PoC validation.
#[derive(Debug, Clone)]
pub struct ProjectRevenueData {
    /// Project identifier.
    pub project_id: String,
    /// Costs incurred to date.
    pub costs_to_date: f64,
    /// Estimated total cost at completion.
    pub estimated_total_cost: f64,
    /// Reported completion percentage.
    pub completion_pct: f64,
    /// Total contract value.
    pub contract_value: f64,
    /// Cumulative revenue recognized.
    pub cumulative_revenue: f64,
    /// Amount billed to date.
    pub billed_to_date: f64,
    /// Unbilled revenue balance.
    pub unbilled_revenue: f64,
}

/// Earned value management data for EVM validation.
#[derive(Debug, Clone)]
pub struct EarnedValueData {
    /// Project identifier.
    pub project_id: String,
    /// Planned value (BCWS).
    pub planned_value: f64,
    /// Earned value (BCWP).
    pub earned_value: f64,
    /// Actual cost (ACWP).
    pub actual_cost: f64,
    /// Budget at completion.
    pub bac: f64,
    /// Schedule variance (SV = EV - PV).
    pub schedule_variance: f64,
    /// Cost variance (CV = EV - AC).
    pub cost_variance: f64,
    /// Schedule performance index (SPI = EV / PV).
    pub spi: f64,
    /// Cost performance index (CPI = EV / AC).
    pub cpi: f64,
}

/// Retainage data for balance validation.
#[derive(Debug, Clone)]
pub struct RetainageData {
    /// Retainage identifier.
    pub retainage_id: String,
    /// Total amount held.
    pub total_held: f64,
    /// Amount released.
    pub released_amount: f64,
    /// Current balance held.
    pub balance_held: f64,
}

/// Results of project accounting coherence evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectAccountingEvaluation {
    /// Fraction of projects with correct completion_pct.
    pub poc_accuracy: f64,
    /// Fraction of projects with correct cumulative_revenue.
    pub revenue_accuracy: f64,
    /// Fraction of projects with correct unbilled_revenue.
    pub unbilled_accuracy: f64,
    /// Fraction of EVM records with correct SV, CV, SPI, CPI.
    pub evm_accuracy: f64,
    /// Fraction of retainage records with correct balance.
    pub retainage_accuracy: f64,
    /// Total projects evaluated.
    pub total_projects: usize,
    /// Total EVM records evaluated.
    pub total_evm_records: usize,
    /// Total retainage records evaluated.
    pub total_retainage: usize,
    /// Overall pass/fail.
    pub passes: bool,
    /// Issues found.
    pub issues: Vec<String>,
}

/// Evaluator for project accounting coherence.
pub struct ProjectAccountingEvaluator {
    thresholds: ProjectAccountingThresholds,
}

impl ProjectAccountingEvaluator {
    /// Create a new evaluator with default thresholds.
    pub fn new() -> Self {
        Self {
            thresholds: ProjectAccountingThresholds::default(),
        }
    }

    /// Create with custom thresholds.
    pub fn with_thresholds(thresholds: ProjectAccountingThresholds) -> Self {
        Self { thresholds }
    }

    /// Evaluate project accounting data coherence.
    pub fn evaluate(
        &self,
        projects: &[ProjectRevenueData],
        evm_records: &[EarnedValueData],
        retainage: &[RetainageData],
    ) -> EvalResult<ProjectAccountingEvaluation> {
        let mut issues = Vec::new();
        let tolerance = self.thresholds.evm_tolerance;

        // 1. PoC: completion_pct ≈ costs_to_date / estimated_total_cost
        let poc_ok = projects
            .iter()
            .filter(|p| {
                if p.estimated_total_cost <= 0.0 {
                    return true;
                }
                let expected = p.costs_to_date / p.estimated_total_cost;
                (p.completion_pct - expected).abs() <= tolerance
            })
            .count();
        let poc_accuracy = if projects.is_empty() {
            1.0
        } else {
            poc_ok as f64 / projects.len() as f64
        };

        // 2. Revenue: cumulative_revenue ≈ contract_value * completion_pct
        let rev_ok = projects
            .iter()
            .filter(|p| {
                let expected = p.contract_value * p.completion_pct;
                (p.cumulative_revenue - expected).abs()
                    <= tolerance * p.contract_value.abs().max(1.0)
            })
            .count();
        let revenue_accuracy = if projects.is_empty() {
            1.0
        } else {
            rev_ok as f64 / projects.len() as f64
        };

        // 3. Unbilled: unbilled_revenue ≈ cumulative_revenue - billed_to_date
        let unbilled_ok = projects
            .iter()
            .filter(|p| {
                let expected = p.cumulative_revenue - p.billed_to_date;
                (p.unbilled_revenue - expected).abs()
                    <= tolerance * p.cumulative_revenue.abs().max(1.0)
            })
            .count();
        let unbilled_accuracy = if projects.is_empty() {
            1.0
        } else {
            unbilled_ok as f64 / projects.len() as f64
        };

        // 4. EVM: SV = EV - PV, CV = EV - AC, SPI = EV/PV, CPI = EV/AC
        let evm_ok = evm_records
            .iter()
            .filter(|e| {
                let sv_expected = e.earned_value - e.planned_value;
                let cv_expected = e.earned_value - e.actual_cost;
                let sv_ok = (e.schedule_variance - sv_expected).abs()
                    <= tolerance * e.earned_value.abs().max(1.0);
                let cv_ok = (e.cost_variance - cv_expected).abs()
                    <= tolerance * e.earned_value.abs().max(1.0);

                let spi_ok = if e.planned_value > 0.0 {
                    let expected = e.earned_value / e.planned_value;
                    (e.spi - expected).abs() <= tolerance
                } else {
                    true
                };
                let cpi_ok = if e.actual_cost > 0.0 {
                    let expected = e.earned_value / e.actual_cost;
                    (e.cpi - expected).abs() <= tolerance
                } else {
                    true
                };

                sv_ok && cv_ok && spi_ok && cpi_ok
            })
            .count();
        let evm_accuracy = if evm_records.is_empty() {
            1.0
        } else {
            evm_ok as f64 / evm_records.len() as f64
        };

        // 5. Retainage: balance_held ≈ total_held - released_amount
        let ret_ok = retainage
            .iter()
            .filter(|r| {
                let expected = r.total_held - r.released_amount;
                (r.balance_held - expected).abs() <= tolerance * r.total_held.abs().max(1.0)
            })
            .count();
        let retainage_accuracy = if retainage.is_empty() {
            1.0
        } else {
            ret_ok as f64 / retainage.len() as f64
        };

        // Check thresholds
        if poc_accuracy < self.thresholds.min_poc_accuracy {
            issues.push(format!(
                "PoC completion accuracy {:.4} < {:.4}",
                poc_accuracy, self.thresholds.min_poc_accuracy
            ));
        }
        if revenue_accuracy < self.thresholds.min_poc_accuracy {
            issues.push(format!(
                "Revenue recognition accuracy {:.4} < {:.4}",
                revenue_accuracy, self.thresholds.min_poc_accuracy
            ));
        }
        if unbilled_accuracy < self.thresholds.min_poc_accuracy {
            issues.push(format!(
                "Unbilled revenue accuracy {:.4} < {:.4}",
                unbilled_accuracy, self.thresholds.min_poc_accuracy
            ));
        }
        if evm_accuracy < self.thresholds.min_evm_accuracy {
            issues.push(format!(
                "EVM metric accuracy {:.4} < {:.4}",
                evm_accuracy, self.thresholds.min_evm_accuracy
            ));
        }
        if retainage_accuracy < self.thresholds.min_evm_accuracy {
            issues.push(format!(
                "Retainage balance accuracy {:.4} < {:.4}",
                retainage_accuracy, self.thresholds.min_evm_accuracy
            ));
        }

        let passes = issues.is_empty();

        Ok(ProjectAccountingEvaluation {
            poc_accuracy,
            revenue_accuracy,
            unbilled_accuracy,
            evm_accuracy,
            retainage_accuracy,
            total_projects: projects.len(),
            total_evm_records: evm_records.len(),
            total_retainage: retainage.len(),
            passes,
            issues,
        })
    }
}

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

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

    #[test]
    fn test_valid_project_accounting() {
        let evaluator = ProjectAccountingEvaluator::new();
        let projects = vec![ProjectRevenueData {
            project_id: "PRJ001".to_string(),
            costs_to_date: 500_000.0,
            estimated_total_cost: 1_000_000.0,
            completion_pct: 0.50,
            contract_value: 1_200_000.0,
            cumulative_revenue: 600_000.0,
            billed_to_date: 550_000.0,
            unbilled_revenue: 50_000.0,
        }];
        let evm = vec![EarnedValueData {
            project_id: "PRJ001".to_string(),
            planned_value: 600_000.0,
            earned_value: 500_000.0,
            actual_cost: 520_000.0,
            bac: 1_000_000.0,
            schedule_variance: -100_000.0,
            cost_variance: -20_000.0,
            spi: 500_000.0 / 600_000.0,
            cpi: 500_000.0 / 520_000.0,
        }];
        let retainage = vec![RetainageData {
            retainage_id: "RET001".to_string(),
            total_held: 60_000.0,
            released_amount: 10_000.0,
            balance_held: 50_000.0,
        }];

        let result = evaluator.evaluate(&projects, &evm, &retainage).unwrap();
        assert!(result.passes);
        assert_eq!(result.total_projects, 1);
        assert_eq!(result.total_evm_records, 1);
    }

    #[test]
    fn test_wrong_completion_pct() {
        let evaluator = ProjectAccountingEvaluator::new();
        let projects = vec![ProjectRevenueData {
            project_id: "PRJ001".to_string(),
            costs_to_date: 500_000.0,
            estimated_total_cost: 1_000_000.0,
            completion_pct: 0.80, // Wrong: should be 0.50
            contract_value: 1_200_000.0,
            cumulative_revenue: 960_000.0,
            billed_to_date: 900_000.0,
            unbilled_revenue: 60_000.0,
        }];

        let result = evaluator.evaluate(&projects, &[], &[]).unwrap();
        assert!(!result.passes);
        assert!(result.issues.iter().any(|i| i.contains("PoC completion")));
    }

    #[test]
    fn test_wrong_evm_metrics() {
        let evaluator = ProjectAccountingEvaluator::new();
        let evm = vec![EarnedValueData {
            project_id: "PRJ001".to_string(),
            planned_value: 600_000.0,
            earned_value: 500_000.0,
            actual_cost: 520_000.0,
            bac: 1_000_000.0,
            schedule_variance: 0.0, // Wrong: should be -100,000
            cost_variance: -20_000.0,
            spi: 500_000.0 / 600_000.0,
            cpi: 500_000.0 / 520_000.0,
        }];

        let result = evaluator.evaluate(&[], &evm, &[]).unwrap();
        assert!(!result.passes);
        assert!(result.issues.iter().any(|i| i.contains("EVM metric")));
    }

    #[test]
    fn test_wrong_retainage_balance() {
        let evaluator = ProjectAccountingEvaluator::new();
        let retainage = vec![RetainageData {
            retainage_id: "RET001".to_string(),
            total_held: 60_000.0,
            released_amount: 10_000.0,
            balance_held: 60_000.0, // Wrong: should be 50,000
        }];

        let result = evaluator.evaluate(&[], &[], &retainage).unwrap();
        assert!(!result.passes);
        assert!(result.issues.iter().any(|i| i.contains("Retainage")));
    }

    #[test]
    fn test_wrong_cumulative_revenue() {
        let evaluator = ProjectAccountingEvaluator::new();
        let projects = vec![ProjectRevenueData {
            project_id: "PRJ001".to_string(),
            costs_to_date: 500_000.0,
            estimated_total_cost: 1_000_000.0,
            completion_pct: 0.50,
            contract_value: 1_200_000.0,
            cumulative_revenue: 900_000.0, // Wrong: should be 600,000
            billed_to_date: 550_000.0,
            unbilled_revenue: 350_000.0,
        }];

        let result = evaluator.evaluate(&projects, &[], &[]).unwrap();
        assert!(!result.passes);
        assert!(result
            .issues
            .iter()
            .any(|i| i.contains("Revenue recognition")));
    }

    #[test]
    fn test_wrong_unbilled_revenue() {
        let evaluator = ProjectAccountingEvaluator::new();
        let projects = vec![ProjectRevenueData {
            project_id: "PRJ001".to_string(),
            costs_to_date: 500_000.0,
            estimated_total_cost: 1_000_000.0,
            completion_pct: 0.50,
            contract_value: 1_200_000.0,
            cumulative_revenue: 600_000.0,
            billed_to_date: 550_000.0,
            unbilled_revenue: 200_000.0, // Wrong: should be 50,000
        }];

        let result = evaluator.evaluate(&projects, &[], &[]).unwrap();
        assert!(!result.passes);
        assert!(result.issues.iter().any(|i| i.contains("Unbilled revenue")));
    }

    #[test]
    fn test_empty_data() {
        let evaluator = ProjectAccountingEvaluator::new();
        let result = evaluator.evaluate(&[], &[], &[]).unwrap();
        assert!(result.passes);
    }
}