sim-lib-numbers-stats 0.6.0

Statistical and probability helpers for the SIM number stack.
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
//! Versioned deterministic sampling and bounded space-filling designs.

use core::fmt;

/// Stable identity of a deterministic sampler algorithm.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SamplerAlgorithm {
    /// SplitMix64 with the published Steele-Vigna mixing constants, version 1.
    SplitMix64V1,
}

/// Canonical, architecture-independent sampler state.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SamplerState {
    /// Algorithm and stream-version identity.
    pub algorithm: SamplerAlgorithm,
    /// Current 64-bit state word.
    pub state: u64,
    /// Exact number of generated 64-bit words.
    pub words_generated: u64,
    /// Root seed from which this stream was constructed.
    pub seed: u64,
    /// Substream label, zero for the root stream.
    pub stream: u64,
}

/// Replay receipt captured at an observable sampling boundary.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SamplerReceipt {
    /// State sufficient to resume the stream exactly.
    pub state: SamplerState,
    /// Optional hard word allowance selected by the caller.
    pub max_words: Option<u64>,
}

/// A public deterministic sampler with explicit version, work, state and forks.
///
/// `fork(label)` is domain-separated from both the root stream and other labels;
/// it never consumes its parent. Existing consumers use the root stream and thus
/// retain their historical SplitMix64-v1 output exactly.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SeededSampler {
    state: SamplerState,
    max_words: Option<u64>,
}

impl SeededSampler {
    /// Constructs an unbounded root stream. Algorithms should preflight their
    /// own exact work or use [`Self::with_max_words`] for refusal.
    pub fn new(seed: u64) -> Self {
        Self::from_parts(seed, 0, seed, None)
    }

    /// Constructs a root stream that refuses generation beyond `max_words`.
    pub fn with_max_words(seed: u64, max_words: u64) -> Self {
        Self::from_parts(seed, 0, seed, Some(max_words))
    }

    fn from_parts(seed: u64, stream: u64, state: u64, max_words: Option<u64>) -> Self {
        Self {
            state: SamplerState {
                algorithm: SamplerAlgorithm::SplitMix64V1,
                state,
                words_generated: 0,
                seed,
                stream,
            },
            max_words,
        }
    }

    /// Restores a canonical state and word allowance.
    pub fn replay(receipt: SamplerReceipt) -> Self {
        Self {
            state: receipt.state,
            max_words: receipt.max_words,
        }
    }

    /// Captures a replay receipt.
    pub fn receipt(&self) -> SamplerReceipt {
        SamplerReceipt {
            state: self.state,
            max_words: self.max_words,
        }
    }

    /// Returns an independent, deterministic labeled substream without
    /// advancing this stream.
    pub fn fork(&self, label: u64) -> Self {
        let state = mix(self.state.seed
            ^ label.wrapping_mul(0xd2b7_4407_b1ce_6e93)
            ^ 0xa076_1d64_78bd_642f);
        Self::from_parts(self.state.seed, label, state, self.max_words)
    }

    /// Produces the next word, refusing when the declared allowance is spent.
    pub fn try_next_u64(&mut self) -> Result<u64, DesignError> {
        if self
            .max_words
            .is_some_and(|limit| self.state.words_generated >= limit)
        {
            return Err(DesignError::WorkLimit {
                required: self.state.words_generated.saturating_add(1),
                limit: self.max_words.unwrap_or(0),
            });
        }
        self.state.state = self.state.state.wrapping_add(0x9e37_79b9_7f4a_7c15);
        self.state.words_generated += 1;
        Ok(mix(self.state.state))
    }

    /// Produces the next word on a stream whose work was preflighted.
    pub(crate) fn next_u64(&mut self) -> u64 {
        self.try_next_u64()
            .expect("unbounded or preflighted sampler")
    }

    /// Maps one word to `[0, 1)` using the stable 53-bit convention.
    pub fn unit_interval(&mut self) -> f64 {
        (self.next_u64() >> 11) as f64 * (1.0 / ((1_u64 << 53) as f64))
    }

    /// Historical modulo mapping used by clustering and HMM fixtures.
    pub(crate) fn index_modulo(&mut self, length: usize) -> usize {
        (self.next_u64() % length as u64) as usize
    }

    /// Historical multiply-high mapping used by bootstrap fixtures.
    pub(crate) fn index_multiply_high(&mut self, length: usize) -> usize {
        ((u128::from(self.next_u64()) * length as u128) >> 64) as usize
    }
}

fn mix(mut value: u64) -> u64 {
    value = (value ^ (value >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
    value = (value ^ (value >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
    value ^ (value >> 31)
}

/// Optional reviewed digital scrambling policy.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Scramble {
    /// Preserve the canonical digital net.
    None,
    /// Apply a deterministic digital XOR shift from the sampler seed.
    DigitalShift,
}

/// Caller-described region intentionally absent from a sweep.
#[derive(Clone, Debug, PartialEq)]
pub struct UntestedRegion {
    /// Stable caller-owned label.
    pub label: String,
    /// Human-reviewable reason for exclusion.
    pub reason: String,
}

/// Coverage and replay evidence returned with every design.
#[derive(Clone, Debug, PartialEq)]
pub struct CoverageEvidence {
    /// Sequence identity including algorithm version and policy.
    pub sequence_identity: String,
    /// Exact zero-based boundary point indices injected by policy.
    pub boundary_injections: Vec<usize>,
    /// Pairs `(later, earlier)` for exactly duplicated points.
    pub duplicates: Vec<(usize, usize)>,
    /// Per-dimension occupancy counts for every declared stratum.
    pub stratum_occupancy: Vec<Vec<usize>>,
    /// Final sampler state when a sampler participated.
    pub sampler: Option<SamplerReceipt>,
    /// Caller-supplied excluded-region metadata, preserved verbatim.
    pub untested_regions: Vec<UntestedRegion>,
    /// Exact admitted work units.
    pub work: u64,
}

/// A generated, reconstructable design and its coverage evidence.
#[derive(Clone, Debug, PartialEq)]
pub struct SampleDesign {
    /// Row-major points in the unit hypercube.
    pub points: Vec<Vec<f64>>,
    /// Auditable coverage and replay facts.
    pub coverage: CoverageEvidence,
}

/// Latin hypercube request.
#[derive(Clone, Debug, PartialEq)]
pub struct LatinHypercubePlan {
    /// Number of coordinates, bounded to 64.
    pub dimensions: usize,
    /// Number of points and strata.
    pub points: usize,
    /// Root seed.
    pub seed: u64,
    /// Maximum shuffle draws.
    pub max_work: u64,
    /// Caller-owned exclusions.
    pub untested_regions: Vec<UntestedRegion>,
}

impl LatinHypercubePlan {
    /// Generates a centered Latin hypercube with one point per stratum in every dimension.
    pub fn generate(&self) -> Result<SampleDesign, DesignError> {
        validate_shape(self.dimensions, self.points, 64)?;
        let required = (self.dimensions as u64)
            .checked_mul(self.points.saturating_sub(1) as u64)
            .ok_or(DesignError::Overflow)?;
        if required > self.max_work {
            return Err(DesignError::WorkLimit {
                required,
                limit: self.max_work,
            });
        }
        let mut sampler = SeededSampler::with_max_words(self.seed, self.max_work);
        let mut points = vec![vec![0.0; self.dimensions]; self.points];
        let occupancy = vec![vec![1; self.points]; self.dimensions];
        for (dimension, _) in occupancy.iter().enumerate() {
            let mut permutation = (0..self.points).collect::<Vec<_>>();
            for end in (1..self.points).rev() {
                let chosen = sampler.index_modulo(end + 1);
                permutation.swap(end, chosen);
            }
            for (row, stratum) in permutation.into_iter().enumerate() {
                points[row][dimension] = (stratum as f64 + 0.5) / self.points as f64;
            }
        }
        Ok(design(
            points,
            format!("latin-hypercube/centered-v1;seed={}", self.seed),
            vec![],
            occupancy,
            Some(sampler.receipt()),
            self.untested_regions.clone(),
            required,
        ))
    }
}

/// Sobol base-2 digital-net request. The reviewed direction table is bounded to four dimensions.
#[derive(Clone, Debug, PartialEq)]
pub struct SobolPlan {
    /// Dimensions in `1..=4`.
    pub dimensions: usize,
    /// Point count.
    pub points: usize,
    /// Number of canonical prefix points to skip.
    pub skip: u64,
    /// Scrambling policy.
    pub scramble: Scramble,
    /// Seed used only by reviewed scrambling.
    pub seed: u64,
    /// Maximum coordinate work.
    pub max_work: u64,
    /// Caller-owned exclusions.
    pub untested_regions: Vec<UntestedRegion>,
}

impl SobolPlan {
    /// Generates the requested bounded Sobol prefix.
    pub fn generate(&self) -> Result<SampleDesign, DesignError> {
        validate_shape(self.dimensions, self.points, 4)?;
        let end = self
            .skip
            .checked_add(self.points as u64)
            .ok_or(DesignError::Overflow)?;
        if end > u32::MAX as u64 {
            return Err(DesignError::UnsupportedPoint { point: end });
        }
        let required = (self.dimensions as u64)
            .checked_mul(self.points as u64)
            .ok_or(DesignError::Overflow)?;
        if required > self.max_work {
            return Err(DesignError::WorkLimit {
                required,
                limit: self.max_work,
            });
        }
        let mut sampler = SeededSampler::new(self.seed);
        let shifts = (0..self.dimensions)
            .map(|_| {
                if self.scramble == Scramble::DigitalShift {
                    sampler.next_u64()
                } else {
                    0
                }
            })
            .collect::<Vec<_>>();
        let mut points = Vec::with_capacity(self.points);
        for index in self.skip..end {
            let gray = index ^ (index >> 1);
            let mut row = Vec::with_capacity(self.dimensions);
            for (dimension, shift) in shifts.iter().copied().enumerate() {
                let mut bits = 0_u64;
                for bit in 0..32 {
                    if gray & (1_u64 << bit) != 0 {
                        bits ^= direction(dimension, bit);
                    }
                }
                row.push(((bits ^ shift) >> 11) as f64 * (1.0 / ((1_u64 << 53) as f64)));
            }
            points.push(row);
        }
        let receipt = (self.scramble == Scramble::DigitalShift).then(|| sampler.receipt());
        Ok(design(
            points,
            format!(
                "sobol/joe-kuo-reviewed-4d-v1;skip={};scramble={:?};seed={}",
                self.skip, self.scramble, self.seed
            ),
            vec![],
            vec![],
            receipt,
            self.untested_regions.clone(),
            required,
        ))
    }
}

// Bratley-Fox direction recurrence for dimensions 1..4: (s, a, m_i).
fn direction(dimension: usize, bit: usize) -> u64 {
    if dimension == 0 {
        return 1_u64 << (63 - bit);
    }
    let (s, a, initial): (usize, u32, &[u32]) = match dimension {
        1 => (1, 0, &[1]),
        2 => (2, 1, &[1, 3]),
        3 => (3, 1, &[1, 3, 1]),
        _ => unreachable!(),
    };
    let mut values = [0_u64; 32];
    for index in 0..s {
        values[index] = (initial[index] as u64) << (63 - index);
    }
    for index in s..=bit {
        let mut value = values[index - s] ^ (values[index - s] >> s);
        for k in 1..s {
            if ((a >> (s - 1 - k)) & 1) != 0 {
                value ^= values[index - k];
            }
        }
        values[index] = value;
    }
    values[bit]
}

/// Boundary-injection wrapper for any already generated unit-cube design.
#[derive(Clone, Debug, PartialEq)]
pub struct SweepPlan {
    /// Whether to prepend the all-zero corner.
    pub inject_lower_boundary: bool,
    /// Whether to append the all-one corner.
    pub inject_upper_boundary: bool,
    /// Caller-owned exclusions.
    pub untested_regions: Vec<UntestedRegion>,
}

impl SweepPlan {
    /// Injects requested exact boundaries and recomputes duplicate evidence.
    pub fn apply(&self, mut design: SampleDesign) -> SampleDesign {
        let dimensions = design.points.first().map_or(0, Vec::len);
        let mut boundaries = Vec::new();
        if self.inject_lower_boundary {
            design.points.insert(0, vec![0.0; dimensions]);
            boundaries.push(0);
        }
        if self.inject_upper_boundary {
            boundaries.push(design.points.len());
            design.points.push(vec![1.0; dimensions]);
        }
        design.coverage.boundary_injections = boundaries;
        design.coverage.duplicates = duplicates(&design.points);
        design
            .coverage
            .untested_regions
            .extend(self.untested_regions.clone());
        design
            .coverage
            .sequence_identity
            .push_str(";sweep-boundaries-v1");
        design
    }
}

fn validate_shape(
    dimensions: usize,
    points: usize,
    max_dimensions: usize,
) -> Result<(), DesignError> {
    if dimensions == 0 || dimensions > max_dimensions {
        return Err(DesignError::UnsupportedDimension {
            requested: dimensions,
            maximum: max_dimensions,
        });
    }
    if points == 0 {
        return Err(DesignError::InvalidPointCount);
    }
    Ok(())
}
fn design(
    points: Vec<Vec<f64>>,
    identity: String,
    boundaries: Vec<usize>,
    occupancy: Vec<Vec<usize>>,
    sampler: Option<SamplerReceipt>,
    untested_regions: Vec<UntestedRegion>,
    work: u64,
) -> SampleDesign {
    let duplicates = duplicates(&points);
    SampleDesign {
        points,
        coverage: CoverageEvidence {
            sequence_identity: identity,
            boundary_injections: boundaries,
            duplicates,
            stratum_occupancy: occupancy,
            sampler,
            untested_regions,
            work,
        },
    }
}
fn duplicates(points: &[Vec<f64>]) -> Vec<(usize, usize)> {
    let mut result = Vec::new();
    for later in 0..points.len() {
        if let Some(earlier) = (0..later).find(|&earlier| points[earlier] == points[later]) {
            result.push((later, earlier));
        }
    }
    result
}

/// Fail-closed design and sampler errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DesignError {
    /// Dimension exceeds the reviewed table or is zero.
    UnsupportedDimension {
        /// Requested dimension.
        requested: usize,
        /// Maximum reviewed dimension.
        maximum: usize,
    },
    /// Zero points were requested.
    InvalidPointCount,
    /// Skip plus point count exceeds the supported prefix.
    UnsupportedPoint {
        /// First unsupported point index.
        point: u64,
    },
    /// Exact work exceeds policy.
    WorkLimit {
        /// Required work.
        required: u64,
        /// Allowed work.
        limit: u64,
    },
    /// Size arithmetic overflowed.
    Overflow,
}
impl fmt::Display for DesignError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}
impl std::error::Error for DesignError {}