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
//! Financial reporting evaluator.
//!
//! Validates financial statement coherence including balance sheet equation,
//! statement-to-trial-balance tie-back, cash flow reconciliation,
//! KPI derivation accuracy, and budget variance realism.

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

/// Thresholds for financial reporting evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FinancialReportingThresholds {
    /// Minimum rate at which statement line items tie back to trial balance.
    pub min_statement_tb_tie_rate: f64,
    /// Minimum KPI derivation accuracy.
    pub min_kpi_accuracy: f64,
    /// Maximum budget variance standard deviation (as fraction of budget).
    pub max_budget_variance_std: f64,
    /// Tolerance for balance equation checks.
    pub balance_tolerance: f64,
}

impl Default for FinancialReportingThresholds {
    fn default() -> Self {
        Self {
            min_statement_tb_tie_rate: 0.99,
            min_kpi_accuracy: 0.95,
            max_budget_variance_std: 0.50,
            balance_tolerance: 0.01,
        }
    }
}

/// Input data for a financial statement period.
#[derive(Debug, Clone)]
pub struct FinancialStatementData {
    /// Period identifier (e.g., "2024-Q1").
    pub period: String,
    /// Total assets from balance sheet.
    pub total_assets: f64,
    /// Total liabilities from balance sheet.
    pub total_liabilities: f64,
    /// Total equity from balance sheet.
    pub total_equity: f64,
    /// Statement line item totals by GL account.
    pub line_item_totals: Vec<(String, f64)>,
    /// Trial balance totals by GL account for the same period.
    pub trial_balance_totals: Vec<(String, f64)>,
    /// Operating cash flow.
    pub cash_flow_operating: f64,
    /// Investing cash flow.
    pub cash_flow_investing: f64,
    /// Financing cash flow.
    pub cash_flow_financing: f64,
    /// Beginning cash balance.
    pub cash_beginning: f64,
    /// Ending cash balance.
    pub cash_ending: f64,
}

/// Input data for KPI validation.
#[derive(Debug, Clone)]
pub struct KpiData {
    /// KPI name.
    pub name: String,
    /// Reported KPI value.
    pub reported_value: f64,
    /// Computed KPI value from underlying GL data.
    pub computed_value: f64,
}

/// Input data for budget variance.
#[derive(Debug, Clone)]
pub struct BudgetVarianceData {
    /// Budget line item name.
    pub line_item: String,
    /// Budgeted amount.
    pub budget_amount: f64,
    /// Actual amount.
    pub actual_amount: f64,
}

/// Per-period balance sheet result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeriodBsResult {
    /// Period identifier.
    pub period: String,
    /// Whether A = L + E within tolerance.
    pub balanced: bool,
    /// Imbalance amount.
    pub imbalance: f64,
}

/// Per-period cash flow result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CashFlowResult {
    /// Period identifier.
    pub period: String,
    /// Whether cash flow reconciles.
    pub reconciled: bool,
    /// Discrepancy amount.
    pub discrepancy: f64,
}

/// Results of financial reporting evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FinancialReportingEvaluation {
    /// Whether all balance sheets are balanced (A = L + E).
    pub bs_equation_balanced: bool,
    /// Per-period balance sheet results.
    pub period_bs_results: Vec<PeriodBsResult>,
    /// Rate at which statement line items tie to trial balance.
    pub statement_tb_tie_rate: f64,
    /// Number of tie-back mismatches.
    pub tie_back_mismatches: usize,
    /// Whether all cash flow statements reconcile.
    pub cash_flow_reconciled: bool,
    /// Per-period cash flow results.
    pub period_cf_results: Vec<CashFlowResult>,
    /// KPI derivation accuracy (fraction of KPIs within tolerance).
    pub kpi_derivation_accuracy: f64,
    /// Number of KPI derivation mismatches.
    pub kpi_mismatches: usize,
    /// Budget variance standard deviation (normalized).
    pub budget_variance_std: f64,
    /// Whether budget variance is within bounds.
    pub budget_variance_within_bounds: bool,
    /// Overall pass/fail.
    pub passes: bool,
    /// Issues found.
    pub issues: Vec<String>,
}

/// Evaluator for financial reporting coherence.
pub struct FinancialReportingEvaluator {
    thresholds: FinancialReportingThresholds,
}

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

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

    /// Evaluate financial reporting data.
    pub fn evaluate(
        &self,
        statements: &[FinancialStatementData],
        kpis: &[KpiData],
        budget_variances: &[BudgetVarianceData],
    ) -> EvalResult<FinancialReportingEvaluation> {
        let mut issues = Vec::new();

        // 1. Balance sheet equation: A = L + E
        let mut period_bs_results = Vec::new();
        let mut all_balanced = true;
        for stmt in statements {
            let imbalance = stmt.total_assets - (stmt.total_liabilities + stmt.total_equity);
            let balanced = imbalance.abs() <= self.thresholds.balance_tolerance;
            if !balanced {
                all_balanced = false;
                issues.push(format!(
                    "BS imbalance in {}: {:.2} (A={:.2}, L={:.2}, E={:.2})",
                    stmt.period,
                    imbalance,
                    stmt.total_assets,
                    stmt.total_liabilities,
                    stmt.total_equity
                ));
            }
            period_bs_results.push(PeriodBsResult {
                period: stmt.period.clone(),
                balanced,
                imbalance,
            });
        }

        // 2. Statement-to-trial-balance tie-back
        let mut total_line_items = 0usize;
        let mut matched_line_items = 0usize;
        for stmt in statements {
            let tb_map: std::collections::HashMap<&str, f64> = stmt
                .trial_balance_totals
                .iter()
                .map(|(k, v)| (k.as_str(), *v))
                .collect();
            for (account, amount) in &stmt.line_item_totals {
                total_line_items += 1;
                if let Some(&tb_amount) = tb_map.get(account.as_str()) {
                    if (amount - tb_amount).abs() <= self.thresholds.balance_tolerance {
                        matched_line_items += 1;
                    }
                }
            }
        }
        let statement_tb_tie_rate = if total_line_items > 0 {
            matched_line_items as f64 / total_line_items as f64
        } else {
            1.0
        };
        let tie_back_mismatches = total_line_items - matched_line_items;
        if statement_tb_tie_rate < self.thresholds.min_statement_tb_tie_rate {
            issues.push(format!(
                "Statement-TB tie rate {:.3} < {:.3} threshold ({} mismatches)",
                statement_tb_tie_rate,
                self.thresholds.min_statement_tb_tie_rate,
                tie_back_mismatches
            ));
        }

        // 3. Cash flow reconciliation
        let mut period_cf_results = Vec::new();
        let mut all_reconciled = true;
        for stmt in statements {
            let computed_ending = stmt.cash_beginning
                + stmt.cash_flow_operating
                + stmt.cash_flow_investing
                + stmt.cash_flow_financing;
            let discrepancy = (stmt.cash_ending - computed_ending).abs();
            let reconciled = discrepancy <= self.thresholds.balance_tolerance;
            if !reconciled {
                all_reconciled = false;
                issues.push(format!(
                    "Cash flow not reconciled in {}: discrepancy {:.2}",
                    stmt.period, discrepancy
                ));
            }
            period_cf_results.push(CashFlowResult {
                period: stmt.period.clone(),
                reconciled,
                discrepancy,
            });
        }

        // 4. KPI derivation accuracy
        let mut kpi_matches = 0usize;
        for kpi in kpis {
            let denominator = if kpi.computed_value.abs() > f64::EPSILON {
                kpi.computed_value.abs()
            } else {
                1.0
            };
            let error = (kpi.reported_value - kpi.computed_value).abs() / denominator;
            if error <= 0.05 {
                kpi_matches += 1;
            }
        }
        let kpi_derivation_accuracy = if kpis.is_empty() {
            1.0
        } else {
            kpi_matches as f64 / kpis.len() as f64
        };
        let kpi_mismatches = kpis.len() - kpi_matches;
        if kpi_derivation_accuracy < self.thresholds.min_kpi_accuracy {
            issues.push(format!(
                "KPI derivation accuracy {:.3} < {:.3} threshold ({} mismatches)",
                kpi_derivation_accuracy, self.thresholds.min_kpi_accuracy, kpi_mismatches
            ));
        }

        // 5. Budget variance realism
        let variance_ratios: Vec<f64> = budget_variances
            .iter()
            .filter(|bv| bv.budget_amount.abs() > f64::EPSILON)
            .map(|bv| (bv.actual_amount - bv.budget_amount) / bv.budget_amount)
            .collect();

        let budget_variance_std = if variance_ratios.len() >= 2 {
            let mean = variance_ratios.iter().sum::<f64>() / variance_ratios.len() as f64;
            let variance = variance_ratios
                .iter()
                .map(|v| (v - mean).powi(2))
                .sum::<f64>()
                / (variance_ratios.len() - 1) as f64;
            variance.sqrt()
        } else {
            0.0
        };

        let budget_variance_within_bounds =
            budget_variance_std <= self.thresholds.max_budget_variance_std;
        if !budget_variance_within_bounds && !variance_ratios.is_empty() {
            issues.push(format!(
                "Budget variance std {:.3} > {:.3} threshold",
                budget_variance_std, self.thresholds.max_budget_variance_std
            ));
        }

        let passes = issues.is_empty();

        Ok(FinancialReportingEvaluation {
            bs_equation_balanced: all_balanced,
            period_bs_results,
            statement_tb_tie_rate,
            tie_back_mismatches,
            cash_flow_reconciled: all_reconciled,
            period_cf_results,
            kpi_derivation_accuracy,
            kpi_mismatches,
            budget_variance_std,
            budget_variance_within_bounds,
            passes,
            issues,
        })
    }
}

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

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

    fn valid_statement() -> FinancialStatementData {
        FinancialStatementData {
            period: "2024-Q1".to_string(),
            total_assets: 1_000_000.0,
            total_liabilities: 600_000.0,
            total_equity: 400_000.0,
            line_item_totals: vec![
                ("1100".to_string(), 500_000.0),
                ("2000".to_string(), 300_000.0),
            ],
            trial_balance_totals: vec![
                ("1100".to_string(), 500_000.0),
                ("2000".to_string(), 300_000.0),
            ],
            cash_flow_operating: 50_000.0,
            cash_flow_investing: -20_000.0,
            cash_flow_financing: -10_000.0,
            cash_beginning: 100_000.0,
            cash_ending: 120_000.0,
        }
    }

    #[test]
    fn test_valid_financial_reporting() {
        let evaluator = FinancialReportingEvaluator::new();
        let stmts = vec![valid_statement()];
        let kpis = vec![KpiData {
            name: "ROA".to_string(),
            reported_value: 0.05,
            computed_value: 0.05,
        }];
        let budgets = vec![
            BudgetVarianceData {
                line_item: "Revenue".to_string(),
                budget_amount: 100_000.0,
                actual_amount: 105_000.0,
            },
            BudgetVarianceData {
                line_item: "COGS".to_string(),
                budget_amount: 60_000.0,
                actual_amount: 58_000.0,
            },
        ];

        let result = evaluator.evaluate(&stmts, &kpis, &budgets).unwrap();
        assert!(result.passes);
        assert!(result.bs_equation_balanced);
        assert!(result.cash_flow_reconciled);
        assert_eq!(result.statement_tb_tie_rate, 1.0);
        assert_eq!(result.kpi_derivation_accuracy, 1.0);
    }

    #[test]
    fn test_imbalanced_balance_sheet() {
        let evaluator = FinancialReportingEvaluator::new();
        let mut stmt = valid_statement();
        stmt.total_assets = 1_000_000.0;
        stmt.total_liabilities = 500_000.0;
        stmt.total_equity = 400_000.0; // 100k gap

        let result = evaluator.evaluate(&[stmt], &[], &[]).unwrap();
        assert!(!result.bs_equation_balanced);
        assert!(!result.passes);
    }

    #[test]
    fn test_cash_flow_mismatch() {
        let evaluator = FinancialReportingEvaluator::new();
        let mut stmt = valid_statement();
        stmt.cash_ending = 200_000.0; // Wrong

        let result = evaluator.evaluate(&[stmt], &[], &[]).unwrap();
        assert!(!result.cash_flow_reconciled);
        assert!(!result.passes);
    }

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

    #[test]
    fn test_kpi_mismatch() {
        let evaluator = FinancialReportingEvaluator::new();
        let kpis = vec![
            KpiData {
                name: "ROA".to_string(),
                reported_value: 0.10,
                computed_value: 0.05, // 100% error
            },
            KpiData {
                name: "ROE".to_string(),
                reported_value: 0.15,
                computed_value: 0.15, // exact match
            },
        ];

        let result = evaluator.evaluate(&[], &kpis, &[]).unwrap();
        assert_eq!(result.kpi_derivation_accuracy, 0.5);
        assert_eq!(result.kpi_mismatches, 1);
    }
}