antecedent-core 0.3.0

Identifiers, schemas, assumptions, provenance, and execution policy shared across the Antecedent causal inference engine; start with the `antecedent` crate
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Query submodule.
//!
//! SPDX-License-Identifier: MIT OR Apache-2.0

use std::sync::Arc;

use crate::ids::VariableId;

use super::error::QueryError;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// Anomaly attribution query for observed units.
pub struct AnomalyAttributionQuery {
    /// Variables whose anomaly scores are requested.
    pub targets: Arc<[VariableId]>,
    /// Optional row indices into the factual table (`None` = all complete rows).
    pub unit_rows: Option<Arc<[usize]>>,
    /// Maximum number of units to score (hard size limit).
    pub max_units: usize,
}

impl AnomalyAttributionQuery {
    /// Score all complete rows for `targets`, capped at `max_units`.
    #[must_use]
    pub fn new(targets: impl Into<Arc<[VariableId]>>, max_units: usize) -> Self {
        Self { targets: targets.into(), unit_rows: None, max_units }
    }

    /// Restrict to explicit row indices.
    #[must_use]
    pub fn with_unit_rows(mut self, rows: impl Into<Arc<[usize]>>) -> Self {
        self.unit_rows = Some(rows.into());
        self
    }

    /// Validate targets and limits.
    ///
    /// # Errors
    ///
    /// Empty targets or zero `max_units`.
    pub fn validate(&self) -> Result<(), QueryError> {
        if self.targets.is_empty() {
            return Err(QueryError::EmptyAnomalyTargets);
        }
        if self.max_units == 0 {
            return Err(QueryError::NonPositiveAnomalyLimit);
        }
        Ok(())
    }
}

/// Population / period selector for change attribution.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PopulationSelector {
    /// All rows of the bound table.
    All,
    /// Explicit row indices into a tabular view.
    Rows(Arc<[usize]>),
    /// Multi-environment index (resolved against [`EnvironmentId`] maps at call sites).
    Environment {
        /// Dense environment index into a multi-env container.
        env_index: usize,
    },
    /// Inclusive-exclusive time/row range `[start, end)`.
    TimeRange {
        /// Start index (inclusive).
        start: usize,
        /// End index (exclusive).
        end: usize,
    },
}

impl PopulationSelector {
    /// Validate selector geometry.
    ///
    /// # Errors
    ///
    /// Empty row sets or inverted time ranges.
    pub fn validate(&self) -> Result<(), QueryError> {
        match self {
            Self::All | Self::Environment { .. } => Ok(()),
            Self::Rows(rows) => {
                if rows.is_empty() {
                    Err(QueryError::EmptyPopulationRows)
                } else {
                    Ok(())
                }
            }
            Self::TimeRange { start, end } => {
                if *end <= *start {
                    Err(QueryError::InvalidPopulationTimeRange { start: *start, end: *end })
                } else {
                    Ok(())
                }
            }
        }
    }
}

/// Which structural pieces participate in change decomposition.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum AttributionComponents {
    /// Input / exogenous / parent value changes only.
    Inputs,
    /// Mechanism (conditional) changes only.
    Mechanisms,
    /// Graph-structure changes only.
    Structure,
    /// Inputs and mechanisms jointly.
    InputsAndMechanisms,
    /// Full component set.
    All,
}

/// Shapley estimation mode.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum ShapleyMode {
    /// Exact enumeration of all coalitions (`2^n`).
    Exact,
    /// Monte Carlo coalition sampling.
    MonteCarlo {
        /// Number of coalition / permutation samples.
        n_samples: usize,
    },
    /// Random permutation sampling (classic Shapley estimator).
    Permutation {
        /// Number of random permutations.
        n_permutations: usize,
    },
}

/// Configuration for Shapley allocation.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct ShapleyConfig {
    /// Estimation mode.
    pub mode: ShapleyMode,
    /// Hard limit on exact combinatorial size (default 12).
    pub max_exact_components: usize,
    /// When true, Exact may exceed `max_exact_components` (explicit override).
    pub allow_exact_override: bool,
    /// RNG seed for approximate modes.
    pub seed: u64,
}

impl ShapleyConfig {
    /// Default exact config with size limit 12.
    #[must_use]
    pub const fn exact() -> Self {
        Self {
            mode: ShapleyMode::Exact,
            max_exact_components: 12,
            allow_exact_override: false,
            seed: 0,
        }
    }

    /// Monte Carlo Shapley with `n_samples` coalition evaluations.
    #[must_use]
    pub const fn monte_carlo(n_samples: usize) -> Self {
        Self {
            mode: ShapleyMode::MonteCarlo { n_samples },
            max_exact_components: 12,
            allow_exact_override: false,
            seed: 0,
        }
    }

    /// Permutation sampling with `n_permutations` random orders.
    #[must_use]
    pub const fn permutation(n_permutations: usize) -> Self {
        Self {
            mode: ShapleyMode::Permutation { n_permutations },
            max_exact_components: 12,
            allow_exact_override: false,
            seed: 0,
        }
    }

    /// Override the exact size limit.
    #[must_use]
    pub const fn with_max_exact_components(mut self, max: usize) -> Self {
        self.max_exact_components = max;
        self
    }

    /// Allow Exact above the configured limit (explicit opt-in).
    #[must_use]
    pub const fn with_exact_override(mut self, allow: bool) -> Self {
        self.allow_exact_override = allow;
        self
    }

    /// Set RNG seed for approximate modes.
    #[must_use]
    pub const fn with_seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }

    /// Validate configuration.
    ///
    /// # Errors
    ///
    /// Zero sample budgets or zero exact limit.
    pub fn validate(&self) -> Result<(), QueryError> {
        if self.max_exact_components == 0 {
            return Err(QueryError::NonPositiveShapleyLimit);
        }
        match self.mode {
            ShapleyMode::Exact => Ok(()),
            ShapleyMode::MonteCarlo { n_samples } => {
                if n_samples == 0 {
                    Err(QueryError::NonPositiveShapleySamples)
                } else {
                    Ok(())
                }
            }
            ShapleyMode::Permutation { n_permutations } => {
                if n_permutations == 0 {
                    Err(QueryError::NonPositiveShapleySamples)
                } else {
                    Ok(())
                }
            }
        }
    }
}

/// How to allocate total change across components.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AllocationMethod {
    /// Fixed sequential order (path-dependent; interactions explicit).
    Sequential {
        /// Component evaluation order.
        order: Arc<[crate::ids::ComponentId]>,
    },
    /// Shapley symmetrization (exact or approximate).
    Shapley {
        /// Approximation / size-limit config.
        approximation: ShapleyConfig,
    },
    /// Path-based dynamic-programming decomposition.
    PathBased,
}

impl AllocationMethod {
    /// Validate allocation settings.
    ///
    /// # Errors
    ///
    /// Empty sequential order or invalid Shapley config.
    pub fn validate(&self) -> Result<(), QueryError> {
        match self {
            Self::Sequential { order } if order.is_empty() => Err(QueryError::EmptyAllocationOrder),
            Self::Sequential { order } => {
                for (i, component) in order.iter().enumerate() {
                    if order[..i].contains(component) {
                        return Err(QueryError::DuplicateAllocationComponent);
                    }
                }
                Ok(())
            }
            Self::PathBased => Ok(()),
            Self::Shapley { approximation } => approximation.validate(),
        }
    }
}

/// Distribution / population change attribution query.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ChangeAttributionQuery {
    /// Outcome whose marginal (or summary) change is attributed.
    pub outcome: VariableId,
    /// Baseline population / period.
    pub baseline: PopulationSelector,
    /// Comparison population / period.
    pub comparison: PopulationSelector,
    /// Which structural pieces participate.
    pub components: AttributionComponents,
    /// Allocation rule.
    pub allocation: AllocationMethod,
    /// Maximum number of attribution components (hard size guard for Exact).
    pub max_components: usize,
}

impl ChangeAttributionQuery {
    /// Construct with Shapley Monte Carlo allocation (common pinned baseline-GCM path).
    #[must_use]
    pub fn new(
        outcome: VariableId,
        baseline: PopulationSelector,
        comparison: PopulationSelector,
    ) -> Self {
        Self {
            outcome,
            baseline,
            comparison,
            components: AttributionComponents::Mechanisms,
            allocation: AllocationMethod::Shapley {
                approximation: ShapleyConfig::monte_carlo(2_000),
            },
            max_components: 64,
        }
    }

    /// Set component family.
    #[must_use]
    pub const fn with_components(mut self, components: AttributionComponents) -> Self {
        self.components = components;
        self
    }

    /// Set allocation method.
    #[must_use]
    pub fn with_allocation(mut self, allocation: AllocationMethod) -> Self {
        self.allocation = allocation;
        self
    }

    /// Cap the number of components considered.
    #[must_use]
    pub const fn with_max_components(mut self, max_components: usize) -> Self {
        self.max_components = max_components;
        self
    }

    /// Validate query.
    ///
    /// # Errors
    ///
    /// Invalid populations, allocation, or zero `max_components`.
    pub fn validate(&self) -> Result<(), QueryError> {
        if self.max_components == 0 {
            return Err(QueryError::NonPositiveComponentLimit);
        }
        self.baseline.validate()?;
        self.comparison.validate()?;
        self.allocation.validate()?;
        Ok(())
    }
}

/// Mechanism-change *detection* query — not attribution.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct MechanismChangeQuery {
    /// Nodes whose mechanisms are tested for change.
    pub targets: Arc<[VariableId]>,
    /// Baseline population.
    pub baseline: PopulationSelector,
    /// Comparison population.
    pub comparison: PopulationSelector,
    /// Significance level for change tests.
    pub significance_level: OrderedFloatBits,
    /// Maximum targets to test.
    pub max_targets: usize,
}

/// Bit-pattern wrapper so [`MechanismChangeQuery`] stays `Eq`/`Hash` with an f64 level.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct OrderedFloatBits(u64);

impl OrderedFloatBits {
    /// From f64 (NaN → 0 payload).
    #[must_use]
    pub fn from_f64(v: f64) -> Self {
        Self(if v.is_nan() { 0 } else { v.to_bits() })
    }

    /// To f64.
    #[must_use]
    pub const fn to_f64(self) -> f64 {
        f64::from_bits(self.0)
    }
}

impl MechanismChangeQuery {
    /// Test all `targets` at the given significance level.
    #[must_use]
    pub fn new(
        targets: impl Into<Arc<[VariableId]>>,
        baseline: PopulationSelector,
        comparison: PopulationSelector,
        significance_level: f64,
        max_targets: usize,
    ) -> Self {
        Self {
            targets: targets.into(),
            baseline,
            comparison,
            significance_level: OrderedFloatBits::from_f64(significance_level),
            max_targets,
        }
    }

    /// Validate.
    ///
    /// # Errors
    ///
    /// Empty targets, invalid α, or bad populations.
    pub fn validate(&self) -> Result<(), QueryError> {
        if self.targets.is_empty() {
            return Err(QueryError::EmptyMechanismChangeTargets);
        }
        if self.max_targets == 0 {
            return Err(QueryError::NonPositiveComponentLimit);
        }
        let alpha = self.significance_level.to_f64();
        if !(alpha > 0.0 && alpha < 1.0) {
            return Err(QueryError::InvalidSignificanceLevel);
        }
        self.baseline.validate()?;
        self.comparison.validate()?;
        Ok(())
    }
}

/// Per-unit change attribution query.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct UnitChangeQuery {
    /// Outcome variable.
    pub outcome: VariableId,
    /// Optional factual row indices (`None` = all units up to `max_units`).
    pub unit_rows: Option<Arc<[usize]>>,
    /// Components to attribute (inputs / mechanisms / both).
    pub components: AttributionComponents,
    /// Allocation method.
    pub allocation: AllocationMethod,
    /// Hard unit count limit.
    pub max_units: usize,
}

impl UnitChangeQuery {
    /// Attribute change for `outcome` across units.
    #[must_use]
    pub fn new(outcome: VariableId, max_units: usize) -> Self {
        Self {
            outcome,
            unit_rows: None,
            components: AttributionComponents::Inputs,
            allocation: AllocationMethod::Shapley {
                approximation: ShapleyConfig::monte_carlo(500),
            },
            max_units,
        }
    }

    /// Restrict to explicit rows.
    #[must_use]
    pub fn with_unit_rows(mut self, rows: impl Into<Arc<[usize]>>) -> Self {
        self.unit_rows = Some(rows.into());
        self
    }

    /// Set components.
    #[must_use]
    pub const fn with_components(mut self, components: AttributionComponents) -> Self {
        self.components = components;
        self
    }

    /// Set allocation.
    #[must_use]
    pub fn with_allocation(mut self, allocation: AllocationMethod) -> Self {
        self.allocation = allocation;
        self
    }

    /// Validate.
    ///
    /// # Errors
    ///
    /// Zero `max_units` or invalid allocation.
    pub fn validate(&self) -> Result<(), QueryError> {
        if self.max_units == 0 {
            return Err(QueryError::NonPositiveAnomalyLimit);
        }
        self.allocation.validate()?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ComponentId;

    #[test]
    fn sequential_allocation_rejects_duplicate_components() {
        let component = ComponentId::from_raw(7);
        let allocation = AllocationMethod::Sequential { order: Arc::from([component, component]) };
        assert_eq!(allocation.validate(), Err(QueryError::DuplicateAllocationComponent));
    }
}