datasynth_core/distributions/
line_item.rs1use rand::prelude::*;
13use rand_chacha::ChaCha8Rng;
14use serde::{Deserialize, Serialize};
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct LineItemDistributionConfig {
21 pub two_items: f64,
23 pub three_items: f64,
25 pub four_items: f64,
27 pub five_items: f64,
29 pub six_items: f64,
31 pub seven_items: f64,
33 pub eight_items: f64,
35 pub nine_items: f64,
37 pub ten_to_ninety_nine: f64,
39 pub hundred_to_nine_ninety_nine: f64,
41 pub thousand_plus: f64,
43}
44
45impl Default for LineItemDistributionConfig {
46 fn default() -> Self {
47 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 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 {sum}, expected ~1.0"
82 ));
83 }
84 Ok(())
85 }
86
87 fn cumulative(&self) -> [f64; 11] {
89 let mut cum = [0.0; 11];
90 cum[0] = self.two_items;
91 cum[1] = cum[0] + self.three_items;
92 cum[2] = cum[1] + self.four_items;
93 cum[3] = cum[2] + self.five_items;
94 cum[4] = cum[3] + self.six_items;
95 cum[5] = cum[4] + self.seven_items;
96 cum[6] = cum[5] + self.eight_items;
97 cum[7] = cum[6] + self.nine_items;
98 cum[8] = cum[7] + self.ten_to_ninety_nine;
99 cum[9] = cum[8] + self.hundred_to_nine_ninety_nine;
100 cum[10] = cum[9] + self.thousand_plus;
101 cum
102 }
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct EvenOddDistributionConfig {
108 pub even: f64,
110 pub odd: f64,
112}
113
114impl Default for EvenOddDistributionConfig {
115 fn default() -> Self {
116 Self {
118 even: 0.88,
119 odd: 0.12,
120 }
121 }
122}
123
124#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct DebitCreditDistributionConfig {
127 pub equal: f64,
129 pub more_debit: f64,
131 pub more_credit: f64,
133}
134
135impl Default for DebitCreditDistributionConfig {
136 fn default() -> Self {
137 Self {
139 equal: 0.82,
140 more_debit: 0.07,
141 more_credit: 0.11,
142 }
143 }
144}
145
146pub struct LineItemSampler {
151 rng: ChaCha8Rng,
153 even_odd_config: EvenOddDistributionConfig,
155 debit_credit_config: DebitCreditDistributionConfig,
157 cumulative: [f64; 11],
159}
160
161impl LineItemSampler {
162 pub fn new(seed: u64) -> Self {
164 let line_config = LineItemDistributionConfig::default();
165 let cumulative = line_config.cumulative();
166
167 Self {
168 rng: ChaCha8Rng::seed_from_u64(seed),
169 even_odd_config: EvenOddDistributionConfig::default(),
170 debit_credit_config: DebitCreditDistributionConfig::default(),
171 cumulative,
172 }
173 }
174
175 pub fn with_config(
177 seed: u64,
178 line_config: LineItemDistributionConfig,
179 even_odd_config: EvenOddDistributionConfig,
180 debit_credit_config: DebitCreditDistributionConfig,
181 ) -> Self {
182 let cumulative = line_config.cumulative();
183
184 Self {
185 rng: ChaCha8Rng::seed_from_u64(seed),
186 even_odd_config,
187 debit_credit_config,
188 cumulative,
189 }
190 }
191
192 pub fn sample_count(&mut self) -> usize {
194 let p: f64 = self.rng.random();
195
196 if p < self.cumulative[0] {
198 2
199 } else if p < self.cumulative[1] {
200 3
201 } else if p < self.cumulative[2] {
202 4
203 } else if p < self.cumulative[3] {
204 5
205 } else if p < self.cumulative[4] {
206 6
207 } else if p < self.cumulative[5] {
208 7
209 } else if p < self.cumulative[6] {
210 8
211 } else if p < self.cumulative[7] {
212 9
213 } else if p < self.cumulative[8] {
214 self.rng.random_range(10..100)
216 } else if p < self.cumulative[9] {
217 self.rng.random_range(100..1000)
219 } else {
220 self.rng.random_range(1000..10000)
222 }
223 }
224
225 pub fn sample_even(&mut self) -> bool {
227 self.rng.random::<f64>() < self.even_odd_config.even
228 }
229
230 pub fn sample_count_with_parity(&mut self) -> usize {
235 let base_count = self.sample_count();
236 let should_be_even = self.sample_even();
237
238 let is_even = base_count.is_multiple_of(2);
240 if should_be_even != is_even {
241 if base_count <= 2 {
243 base_count + 1
245 } else if self.rng.random::<bool>() {
246 base_count + 1
248 } else {
249 base_count - 1
251 }
252 } else {
253 base_count
254 }
255 }
256
257 pub fn sample_debit_credit_type(&mut self) -> DebitCreditSplit {
259 let p: f64 = self.rng.random();
260
261 if p < self.debit_credit_config.equal {
262 DebitCreditSplit::Equal
263 } else if p < self.debit_credit_config.equal + self.debit_credit_config.more_debit {
264 DebitCreditSplit::MoreDebit
265 } else {
266 DebitCreditSplit::MoreCredit
267 }
268 }
269
270 pub fn sample(&mut self) -> LineItemSpec {
272 let total_count = self.sample_count_with_parity();
273 let split_type = self.sample_debit_credit_type();
274
275 let (debit_count, credit_count) = match split_type {
276 DebitCreditSplit::Equal => {
277 let half = total_count / 2;
278 (half, total_count - half)
279 }
280 DebitCreditSplit::MoreDebit => {
281 let debit = (total_count as f64 * 0.6).round() as usize;
283 let debit = debit.max(1).min(total_count - 1);
284 (debit, total_count - debit)
285 }
286 DebitCreditSplit::MoreCredit => {
287 let credit = (total_count as f64 * 0.6).round() as usize;
289 let credit = credit.max(1).min(total_count - 1);
290 (total_count - credit, credit)
291 }
292 };
293
294 LineItemSpec {
295 total_count,
296 debit_count,
297 credit_count,
298 split_type,
299 }
300 }
301
302 pub fn reset(&mut self, seed: u64) {
304 self.rng = ChaCha8Rng::seed_from_u64(seed);
305 }
306}
307
308#[derive(Debug, Clone, Copy, PartialEq, Eq)]
310pub enum DebitCreditSplit {
311 Equal,
313 MoreDebit,
315 MoreCredit,
317}
318
319#[derive(Debug, Clone)]
321pub struct LineItemSpec {
322 pub total_count: usize,
324 pub debit_count: usize,
326 pub credit_count: usize,
328 pub split_type: DebitCreditSplit,
330}
331
332impl LineItemSpec {
333 pub fn is_valid(&self) -> bool {
335 self.total_count >= 2
336 && self.debit_count >= 1
337 && self.credit_count >= 1
338 && self.debit_count + self.credit_count == self.total_count
339 }
340}
341
342#[cfg(test)]
343#[allow(clippy::unwrap_used)]
344mod tests {
345 use super::*;
346
347 #[test]
348 fn test_default_config_valid() {
349 let config = LineItemDistributionConfig::default();
350 assert!(config.validate().is_ok());
351 }
352
353 #[test]
354 fn test_sampler_determinism() {
355 let mut sampler1 = LineItemSampler::new(42);
356 let mut sampler2 = LineItemSampler::new(42);
357
358 for _ in 0..100 {
359 assert_eq!(sampler1.sample_count(), sampler2.sample_count());
360 }
361 }
362
363 #[test]
364 fn test_sampler_distribution() {
365 let mut sampler = LineItemSampler::new(42);
366 let sample_size = 100_000;
367
368 let mut counts = std::collections::HashMap::new();
369 for _ in 0..sample_size {
370 let count = sampler.sample_count();
371 *counts.entry(count).or_insert(0) += 1;
372 }
373
374 let two_count = *counts.get(&2).unwrap_or(&0) as f64 / sample_size as f64;
376 assert!(
377 two_count > 0.55 && two_count < 0.65,
378 "Expected ~60% 2-item entries, got {}%",
379 two_count * 100.0
380 );
381
382 let four_count = *counts.get(&4).unwrap_or(&0) as f64 / sample_size as f64;
384 assert!(
385 four_count > 0.13 && four_count < 0.20,
386 "Expected ~16% 4-item entries, got {}%",
387 four_count * 100.0
388 );
389 }
390
391 #[test]
392 fn test_line_item_spec_valid() {
393 let mut sampler = LineItemSampler::new(42);
394
395 for _ in 0..1000 {
396 let spec = sampler.sample();
397 assert!(spec.is_valid(), "Invalid spec: {:?}", spec);
398 }
399 }
400}