Skip to main content

datasynth_core/distributions/
line_item.rs

1//! Line item count distribution sampler.
2//!
3//! Implements the empirical distribution of journal entry line items
4//! as observed in the accounting network generation research.
5//!
6//! Key findings from the paper:
7//! - 60.68% of journal entries have exactly 2 line items
8//! - 16.63% have 4 line items
9//! - 88% have an even number of line items
10//! - 82% have equal debit and credit line counts
11
12use rand::prelude::*;
13use rand_chacha::ChaCha8Rng;
14use serde::{Deserialize, Serialize};
15
16/// Configuration for line item count distribution.
17///
18/// Based on empirical findings from Table III of the accounting network paper.
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct LineItemDistributionConfig {
21    /// Probability of 2 line items (60.68%)
22    pub two_items: f64,
23    /// Probability of 3 line items (5.77%)
24    pub three_items: f64,
25    /// Probability of 4 line items (16.63%)
26    pub four_items: f64,
27    /// Probability of 5 line items (3.06%)
28    pub five_items: f64,
29    /// Probability of 6 line items (3.32%)
30    pub six_items: f64,
31    /// Probability of 7 line items (1.13%)
32    pub seven_items: f64,
33    /// Probability of 8 line items (1.88%)
34    pub eight_items: f64,
35    /// Probability of 9 line items (0.42%)
36    pub nine_items: f64,
37    /// Probability of 10-99 line items (6.33%)
38    pub ten_to_ninety_nine: f64,
39    /// Probability of 100-999 line items (0.76%)
40    pub hundred_to_nine_ninety_nine: f64,
41    /// Probability of 1000+ line items (0.02%)
42    pub thousand_plus: f64,
43}
44
45impl Default for LineItemDistributionConfig {
46    fn default() -> Self {
47        // Values from Table III of the paper
48        Self {
49            two_items: 0.6068,
50            three_items: 0.0577,
51            four_items: 0.1663,
52            five_items: 0.0306,
53            six_items: 0.0332,
54            seven_items: 0.0113,
55            eight_items: 0.0188,
56            nine_items: 0.0042,
57            ten_to_ninety_nine: 0.0633,
58            hundred_to_nine_ninety_nine: 0.0076,
59            thousand_plus: 0.0002,
60        }
61    }
62}
63
64impl LineItemDistributionConfig {
65    /// Validate that probabilities sum to approximately 1.0.
66    pub fn validate(&self) -> Result<(), String> {
67        let sum = self.two_items
68            + self.three_items
69            + self.four_items
70            + self.five_items
71            + self.six_items
72            + self.seven_items
73            + self.eight_items
74            + self.nine_items
75            + self.ten_to_ninety_nine
76            + self.hundred_to_nine_ninety_nine
77            + self.thousand_plus;
78
79        if (sum - 1.0).abs() > 0.01 {
80            return Err(format!(
81                "Line item distribution probabilities sum to {}, expected ~1.0",
82                sum
83            ));
84        }
85        Ok(())
86    }
87
88    /// Get cumulative distribution values.
89    fn cumulative(&self) -> [f64; 11] {
90        let mut cum = [0.0; 11];
91        cum[0] = self.two_items;
92        cum[1] = cum[0] + self.three_items;
93        cum[2] = cum[1] + self.four_items;
94        cum[3] = cum[2] + self.five_items;
95        cum[4] = cum[3] + self.six_items;
96        cum[5] = cum[4] + self.seven_items;
97        cum[6] = cum[5] + self.eight_items;
98        cum[7] = cum[6] + self.nine_items;
99        cum[8] = cum[7] + self.ten_to_ninety_nine;
100        cum[9] = cum[8] + self.hundred_to_nine_ninety_nine;
101        cum[10] = cum[9] + self.thousand_plus;
102        cum
103    }
104}
105
106/// Configuration for even/odd line count distribution.
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct EvenOddDistributionConfig {
109    /// Probability of even line count (88%)
110    pub even: f64,
111    /// Probability of odd line count (12%)
112    pub odd: f64,
113}
114
115impl Default for EvenOddDistributionConfig {
116    fn default() -> Self {
117        // From the paper: 88% even, 12% odd
118        Self {
119            even: 0.88,
120            odd: 0.12,
121        }
122    }
123}
124
125/// Configuration for debit/credit balance distribution.
126#[derive(Debug, Clone, Serialize, Deserialize)]
127pub struct DebitCreditDistributionConfig {
128    /// Probability of equal debit and credit counts (82%)
129    pub equal: f64,
130    /// Probability of more debit lines than credit (7%)
131    pub more_debit: f64,
132    /// Probability of more credit lines than debit (11%)
133    pub more_credit: f64,
134}
135
136impl Default for DebitCreditDistributionConfig {
137    fn default() -> Self {
138        // From the paper: 82% equal, 11% more credit, 7% more debit
139        Self {
140            equal: 0.82,
141            more_debit: 0.07,
142            more_credit: 0.11,
143        }
144    }
145}
146
147/// Sampler for journal entry line item counts.
148///
149/// Produces realistic line item counts based on empirical distributions
150/// from real-world general ledger data.
151pub struct LineItemSampler {
152    /// RNG for sampling
153    rng: ChaCha8Rng,
154    /// Even/odd distribution config
155    even_odd_config: EvenOddDistributionConfig,
156    /// Debit/credit distribution config
157    debit_credit_config: DebitCreditDistributionConfig,
158    /// Cumulative distribution for line counts
159    cumulative: [f64; 11],
160}
161
162impl LineItemSampler {
163    /// Create a new sampler with default configuration.
164    pub fn new(seed: u64) -> Self {
165        let line_config = LineItemDistributionConfig::default();
166        let cumulative = line_config.cumulative();
167
168        Self {
169            rng: ChaCha8Rng::seed_from_u64(seed),
170            even_odd_config: EvenOddDistributionConfig::default(),
171            debit_credit_config: DebitCreditDistributionConfig::default(),
172            cumulative,
173        }
174    }
175
176    /// Create a sampler with custom configuration.
177    pub fn with_config(
178        seed: u64,
179        line_config: LineItemDistributionConfig,
180        even_odd_config: EvenOddDistributionConfig,
181        debit_credit_config: DebitCreditDistributionConfig,
182    ) -> Self {
183        let cumulative = line_config.cumulative();
184
185        Self {
186            rng: ChaCha8Rng::seed_from_u64(seed),
187            even_odd_config,
188            debit_credit_config,
189            cumulative,
190        }
191    }
192
193    /// Sample a line item count.
194    pub fn sample_count(&mut self) -> usize {
195        let p: f64 = self.rng.random();
196
197        // Find the bin using cumulative distribution
198        if p < self.cumulative[0] {
199            2
200        } else if p < self.cumulative[1] {
201            3
202        } else if p < self.cumulative[2] {
203            4
204        } else if p < self.cumulative[3] {
205            5
206        } else if p < self.cumulative[4] {
207            6
208        } else if p < self.cumulative[5] {
209            7
210        } else if p < self.cumulative[6] {
211            8
212        } else if p < self.cumulative[7] {
213            9
214        } else if p < self.cumulative[8] {
215            // 10-99 range - use uniform distribution within range
216            self.rng.random_range(10..100)
217        } else if p < self.cumulative[9] {
218            // 100-999 range
219            self.rng.random_range(100..1000)
220        } else {
221            // 1000+ range (cap at 10000 for practicality)
222            self.rng.random_range(1000..10000)
223        }
224    }
225
226    /// Sample whether the count should be even.
227    pub fn sample_even(&mut self) -> bool {
228        self.rng.random::<f64>() < self.even_odd_config.even
229    }
230
231    /// Sample a line item count with even/odd constraint.
232    ///
233    /// When adjustment is needed, randomly chooses to increment or decrement
234    /// to avoid biasing toward lower counts.
235    pub fn sample_count_with_parity(&mut self) -> usize {
236        let base_count = self.sample_count();
237        let should_be_even = self.sample_even();
238
239        // Adjust to match parity requirement
240        let is_even = base_count.is_multiple_of(2);
241        if should_be_even != is_even {
242            // Use symmetric adjustment: randomly increment or decrement
243            if base_count <= 2 {
244                // Can only increment for small counts
245                base_count + 1
246            } else if self.rng.random::<bool>() {
247                // Randomly choose to increment
248                base_count + 1
249            } else {
250                // Randomly choose to decrement
251                base_count - 1
252            }
253        } else {
254            base_count
255        }
256    }
257
258    /// Sample the debit/credit split type.
259    pub fn sample_debit_credit_type(&mut self) -> DebitCreditSplit {
260        let p: f64 = self.rng.random();
261
262        if p < self.debit_credit_config.equal {
263            DebitCreditSplit::Equal
264        } else if p < self.debit_credit_config.equal + self.debit_credit_config.more_debit {
265            DebitCreditSplit::MoreDebit
266        } else {
267            DebitCreditSplit::MoreCredit
268        }
269    }
270
271    /// Sample a complete line item specification.
272    pub fn sample(&mut self) -> LineItemSpec {
273        let total_count = self.sample_count_with_parity();
274        let split_type = self.sample_debit_credit_type();
275
276        let (debit_count, credit_count) = match split_type {
277            DebitCreditSplit::Equal => {
278                let half = total_count / 2;
279                (half, total_count - half)
280            }
281            DebitCreditSplit::MoreDebit => {
282                // More debit lines - 60% debit, 40% credit
283                let debit = (total_count as f64 * 0.6).round() as usize;
284                let debit = debit.max(1).min(total_count - 1);
285                (debit, total_count - debit)
286            }
287            DebitCreditSplit::MoreCredit => {
288                // More credit lines - 40% debit, 60% credit
289                let credit = (total_count as f64 * 0.6).round() as usize;
290                let credit = credit.max(1).min(total_count - 1);
291                (total_count - credit, credit)
292            }
293        };
294
295        LineItemSpec {
296            total_count,
297            debit_count,
298            credit_count,
299            split_type,
300        }
301    }
302
303    /// Reset the sampler with the same seed.
304    pub fn reset(&mut self, seed: u64) {
305        self.rng = ChaCha8Rng::seed_from_u64(seed);
306    }
307}
308
309/// Type of debit/credit split.
310#[derive(Debug, Clone, Copy, PartialEq, Eq)]
311pub enum DebitCreditSplit {
312    /// Equal number of debit and credit lines
313    Equal,
314    /// More debit lines than credit
315    MoreDebit,
316    /// More credit lines than debit
317    MoreCredit,
318}
319
320/// Specification for line items in a journal entry.
321#[derive(Debug, Clone)]
322pub struct LineItemSpec {
323    /// Total number of line items
324    pub total_count: usize,
325    /// Number of debit lines
326    pub debit_count: usize,
327    /// Number of credit lines
328    pub credit_count: usize,
329    /// Type of debit/credit split
330    pub split_type: DebitCreditSplit,
331}
332
333impl LineItemSpec {
334    /// Check if the spec is valid.
335    pub fn is_valid(&self) -> bool {
336        self.total_count >= 2
337            && self.debit_count >= 1
338            && self.credit_count >= 1
339            && self.debit_count + self.credit_count == self.total_count
340    }
341}
342
343#[cfg(test)]
344#[allow(clippy::unwrap_used)]
345mod tests {
346    use super::*;
347
348    #[test]
349    fn test_default_config_valid() {
350        let config = LineItemDistributionConfig::default();
351        assert!(config.validate().is_ok());
352    }
353
354    #[test]
355    fn test_sampler_determinism() {
356        let mut sampler1 = LineItemSampler::new(42);
357        let mut sampler2 = LineItemSampler::new(42);
358
359        for _ in 0..100 {
360            assert_eq!(sampler1.sample_count(), sampler2.sample_count());
361        }
362    }
363
364    #[test]
365    fn test_sampler_distribution() {
366        let mut sampler = LineItemSampler::new(42);
367        let sample_size = 100_000;
368
369        let mut counts = std::collections::HashMap::new();
370        for _ in 0..sample_size {
371            let count = sampler.sample_count();
372            *counts.entry(count).or_insert(0) += 1;
373        }
374
375        // Check that 2-line items are most common
376        let two_count = *counts.get(&2).unwrap_or(&0) as f64 / sample_size as f64;
377        assert!(
378            two_count > 0.55 && two_count < 0.65,
379            "Expected ~60% 2-item entries, got {}%",
380            two_count * 100.0
381        );
382
383        // Check that 4-line items are second most common
384        let four_count = *counts.get(&4).unwrap_or(&0) as f64 / sample_size as f64;
385        assert!(
386            four_count > 0.13 && four_count < 0.20,
387            "Expected ~16% 4-item entries, got {}%",
388            four_count * 100.0
389        );
390    }
391
392    #[test]
393    fn test_line_item_spec_valid() {
394        let mut sampler = LineItemSampler::new(42);
395
396        for _ in 0..1000 {
397            let spec = sampler.sample();
398            assert!(spec.is_valid(), "Invalid spec: {:?}", spec);
399        }
400    }
401}