antecedent 0.1.0

Antecedent — explicit causal inference facade (identify → estimate → refute)
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
//! Coarse discovery stage APIs for the facade.
//!
//! Bindings and analysis call these instead of constructing PCMCI-family
//! algorithms directly when the facade covers the stage.
//!
//! SPDX-License-Identifier: MIT OR Apache-2.0

#![allow(clippy::cast_possible_truncation)]

use std::sync::Arc;

use antecedent_core::{ExecutionContext, VariableId};
use antecedent_data::{MultiEnvironmentData, TabularData, TimeSeriesData};
pub use antecedent_discovery::{
    CiScreenedPosterior, CiSoftWeight, ContextKind, CpdagDiscoveryResult, DagDiscoveryResult,
    DbnPosterior, DirectLingam, DiscoveryPerformanceRecord, EXACT_ENUM_MAX_NODES,
    ExactDagPosterior, Fci, Ges, GraphPosterior, GraphPosteriorEngine, GraphPrior, JpcmciNodeRole,
    JpcmciPlus, Lpcmci, MultiDatasetConstraints, Notears, NotearsDiscoveryResult, OrderMcmc,
    PagDiscoveryResult, Pc, RegimeAssignment, RegimeGraphCollection, Rfci, Rpcmci,
    RpcmciDiscoveryResult, ScoredLink, SpaceDummyCiMode, StaticCpdagDiscoveryResult,
    StaticDagDiscoveryResult, StaticPagDiscoveryResult, StructureMcmc, TimeDummyCiMode,
    two_regime_half_split,
};
use antecedent_discovery::{DiscoveryWorkspace, Pcmci, PcmciPlus};
use antecedent_graph::{DenseNodeId, Endpoint, TemporalPag};
use antecedent_state::GraphScoreFamily;
use antecedent_stats::{ConditionalIndependence, FdrAdjustment};

use crate::discovery_defaults::{
    DEFAULT_RPCMCI_MIN_REGIME_LEN, contemporaneous_constraints, jpcmci_constraints,
    pcmci_constraints, static_pc_constraints,
};
use crate::error::CausalError;

/// Prior + score family for Bayesian graph-posterior discovery.
#[derive(Clone, Debug)]
pub struct BayesianDiscoverParams {
    /// Structural prior (default: uniform over constraint-valid DAGs).
    pub prior: GraphPrior,
    /// Local score family (currently Gaussian BIC only).
    pub score_family: GraphScoreFamily,
}

impl Default for BayesianDiscoverParams {
    fn default() -> Self {
        Self { prior: GraphPrior::uniform(), score_family: GraphScoreFamily::GaussianBic }
    }
}

/// MCMC schedule for order / structure / CI-screened / DBN posterior search.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GraphMcmcSchedule {
    /// Number of chains (≥ 2 for R-hat).
    pub n_chains: u32,
    /// Warmup draws discarded per chain.
    pub n_warmup: u32,
    /// Post-warmup draws retained per chain (before thinning).
    pub n_draws: u32,
    /// Keep every `thin`-th post-warmup draw.
    pub thin: u32,
}

impl Default for GraphMcmcSchedule {
    fn default() -> Self {
        Self { n_chains: 4, n_warmup: 500, n_draws: 1000, thin: 1 }
    }
}

/// Parameters shared by PCMCI-family discovery stage calls.
#[derive(Clone)]
pub struct DiscoverParams {
    /// Maximum lag.
    pub max_lag: u32,
    /// Significance level.
    pub alpha: f64,
    /// Multiple-testing adjustment (`None` = off).
    pub fdr: Option<antecedent_stats::FdrAdjustment>,
    /// Conditional-independence test (resolved via [`crate::discovery_defaults::resolve_ci`]).
    pub ci: Arc<dyn ConditionalIndependence + Send + Sync>,
    /// Multi-dataset / context settings (J-PCMCI+); ignored by single-series algorithms.
    pub multi_dataset: MultiDatasetConstraints,
}

impl std::fmt::Debug for DiscoverParams {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DiscoverParams")
            .field("max_lag", &self.max_lag)
            .field("alpha", &self.alpha)
            .field("fdr", &self.fdr)
            .field("ci", &"<dyn ConditionalIndependence>")
            .field("multi_dataset", &self.multi_dataset)
            .finish()
    }
}

/// Parameters for static (non-temporal) discovery stage calls.
#[derive(Clone)]
pub struct StaticDiscoverParams {
    /// Significance level.
    pub alpha: f64,
    /// Max conditioning-set size.
    pub max_cond_size: usize,
    /// Multiple-testing adjustment (`None` = off).
    pub fdr: Option<FdrAdjustment>,
    /// Conditional-independence test.
    pub ci: Arc<dyn ConditionalIndependence + Send + Sync>,
    /// GES only: soft PC-skeleton screening for Insert candidates.
    pub screen_pc: bool,
    /// GES only: T/H subset enumeration cap (`None` → default 12).
    pub max_subset: Option<usize>,
}

impl std::fmt::Debug for StaticDiscoverParams {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StaticDiscoverParams")
            .field("alpha", &self.alpha)
            .field("max_cond_size", &self.max_cond_size)
            .field("fdr", &self.fdr)
            .field("ci", &"<dyn ConditionalIndependence>")
            .field("screen_pc", &self.screen_pc)
            .field("max_subset", &self.max_subset)
            .finish()
    }
}

/// Run lagged PCMCI.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_pcmci(
    data: &TimeSeriesData,
    variables: &[VariableId],
    params: &DiscoverParams,
    ctx: &ExecutionContext,
) -> Result<DagDiscoveryResult, CausalError> {
    let pcmci = Pcmci::new()
        .with_fdr_adjustment(params.fdr)
        .with_constraints(pcmci_constraints(params.max_lag, params.alpha))
        .with_ci(Arc::clone(&params.ci));
    let mut ws = DiscoveryWorkspace::default();
    pcmci.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Run PCMCI+.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_pcmci_plus(
    data: &TimeSeriesData,
    variables: &[VariableId],
    params: &DiscoverParams,
    ctx: &ExecutionContext,
) -> Result<CpdagDiscoveryResult, CausalError> {
    let plus = PcmciPlus::new()
        .with_fdr_adjustment(params.fdr)
        .with_constraints(contemporaneous_constraints(params.max_lag, params.alpha))
        .with_ci(Arc::clone(&params.ci));
    let mut ws = DiscoveryWorkspace::default();
    plus.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Run LPCMCI.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_lpcmci(
    data: &TimeSeriesData,
    variables: &[VariableId],
    params: &DiscoverParams,
    ctx: &ExecutionContext,
) -> Result<PagDiscoveryResult, CausalError> {
    let alg = Lpcmci::new()
        .with_fdr_adjustment(params.fdr)
        .with_constraints(contemporaneous_constraints(params.max_lag, params.alpha))
        .with_ci(Arc::clone(&params.ci));
    let mut ws = DiscoveryWorkspace::default();
    alg.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Run J-PCMCI+ over multi-environment series.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_jpcmci_plus(
    data: &MultiEnvironmentData,
    variables: &[VariableId],
    params: &DiscoverParams,
    ctx: &ExecutionContext,
) -> Result<CpdagDiscoveryResult, CausalError> {
    let alg = JpcmciPlus::new()
        .with_fdr_adjustment(params.fdr)
        .with_constraints(jpcmci_constraints(
            params.max_lag,
            params.alpha,
            params.multi_dataset.clone(),
        ))
        .with_ci(Arc::clone(&params.ci));
    let mut ws = DiscoveryWorkspace::default();
    alg.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Run RPCMCI with a supplied regime assignment.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_rpcmci(
    data: &TimeSeriesData,
    variables: &[VariableId],
    assignment: &RegimeAssignment,
    params: &DiscoverParams,
    min_regime_len: Option<usize>,
    ctx: &ExecutionContext,
) -> Result<RpcmciDiscoveryResult, CausalError> {
    let plus = PcmciPlus::new()
        .with_fdr_adjustment(params.fdr)
        .with_constraints(contemporaneous_constraints(params.max_lag, params.alpha))
        .with_ci(Arc::clone(&params.ci));
    let alg = Rpcmci::new()
        .with_min_regime_len(min_regime_len.unwrap_or(DEFAULT_RPCMCI_MIN_REGIME_LEN))
        .with_pcmci_plus(plus);
    let mut ws = DiscoveryWorkspace::default();
    alg.run(data, variables, assignment, &mut ws, ctx).map_err(CausalError::from)
}

/// Run static PC over tabular data.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_pc(
    data: &TabularData,
    variables: &[VariableId],
    params: &StaticDiscoverParams,
    ctx: &ExecutionContext,
) -> Result<StaticCpdagDiscoveryResult, CausalError> {
    let fdr = params.fdr.map(|f| f.with_exclude_contemporaneous(false));
    let alg = Pc::new()
        .with_fdr_adjustment(fdr)
        .with_constraints(static_pc_constraints(params.alpha, params.max_cond_size))
        .with_ci(Arc::clone(&params.ci));
    let mut ws = DiscoveryWorkspace::default();
    alg.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Run classic static FCI over tabular data → PAG.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_fci(
    data: &TabularData,
    variables: &[VariableId],
    params: &StaticDiscoverParams,
    ctx: &ExecutionContext,
) -> Result<StaticPagDiscoveryResult, CausalError> {
    let fdr = params.fdr.map(|f| f.with_exclude_contemporaneous(false));
    let alg = Fci::new()
        .with_fdr_adjustment(fdr)
        .with_constraints(static_pc_constraints(params.alpha, params.max_cond_size))
        .with_ci(Arc::clone(&params.ci));
    let mut ws = DiscoveryWorkspace::default();
    alg.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Run classic static RFCI over tabular data → PAG.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_rfci(
    data: &TabularData,
    variables: &[VariableId],
    params: &StaticDiscoverParams,
    ctx: &ExecutionContext,
) -> Result<StaticPagDiscoveryResult, CausalError> {
    let fdr = params.fdr.map(|f| f.with_exclude_contemporaneous(false));
    let alg = Rfci::new()
        .with_fdr_adjustment(fdr)
        .with_constraints(static_pc_constraints(params.alpha, params.max_cond_size))
        .with_ci(Arc::clone(&params.ci));
    let mut ws = DiscoveryWorkspace::default();
    alg.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Run GES (Gaussian BIC) over tabular data → CPDAG.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_ges(
    data: &TabularData,
    variables: &[VariableId],
    params: &StaticDiscoverParams,
    ctx: &ExecutionContext,
) -> Result<StaticCpdagDiscoveryResult, CausalError> {
    let fdr = params.fdr.map(|f| f.with_exclude_contemporaneous(false));
    let alg = Ges::new()
        .with_fdr_adjustment(fdr)
        .with_constraints(static_pc_constraints(params.alpha, params.max_cond_size))
        .with_ci(Arc::clone(&params.ci))
        .with_pc_screening(params.screen_pc)
        .with_max_subset(params.max_subset);
    let mut ws = DiscoveryWorkspace::default();
    alg.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Run `DirectLiNGAM` over tabular data → DAG.
///
/// # Errors
///
/// Discovery failures.
pub fn discover_lingam(
    data: &TabularData,
    variables: &[VariableId],
    params: &StaticDiscoverParams,
    prune_threshold: f64,
    ctx: &ExecutionContext,
) -> Result<StaticDagDiscoveryResult, CausalError> {
    let alg = DirectLingam::new()
        .with_constraints(static_pc_constraints(params.alpha, params.max_cond_size))
        .with_prune_threshold(prune_threshold);
    let mut ws = DiscoveryWorkspace::default();
    alg.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Discover a static DAG with NOTEARS (continuous SEM; ).
///
/// Returns the hard DAG review plus the soft weight matrix for mechanism seeding.
///
/// # Errors
///
/// Discovery / solver failures.
pub fn discover_notears(
    data: &TabularData,
    variables: &[VariableId],
    params: &StaticDiscoverParams,
    lambda: f64,
    threshold: f64,
    standardize: bool,
    ctx: &ExecutionContext,
) -> Result<NotearsDiscoveryResult, CausalError> {
    let alg = Notears::new()
        .with_constraints(static_pc_constraints(params.alpha, params.max_cond_size))
        .with_lambda(lambda)
        .with_threshold(threshold)
        .with_standardize(standardize);
    let mut ws = DiscoveryWorkspace::default();
    alg.run(data, variables, &mut ws, ctx).map_err(CausalError::from)
}

/// Exact DAG posterior enumeration (`n ≤ 6`, Gaussian BIC).
///
/// # Errors
///
/// Discovery failures (oversized graph, empty support, score/data errors).
pub fn discover_exact_dag_posterior(
    data: &TabularData,
    variables: &[VariableId],
    params: &BayesianDiscoverParams,
    ctx: &ExecutionContext,
) -> Result<GraphPosterior, CausalError> {
    let eng = ExactDagPosterior::new();
    let mut ws = DiscoveryWorkspace::default();
    eng.run(data, variables, &params.prior, params.score_family, &mut ws, ctx)
        .map_err(CausalError::from)
}

/// Order MCMC DAG posterior (Gaussian BIC).
///
/// # Errors
///
/// Discovery / diagnostics-gate failures.
pub fn discover_order_mcmc(
    data: &TabularData,
    variables: &[VariableId],
    params: &BayesianDiscoverParams,
    schedule: &GraphMcmcSchedule,
    require_diagnostics_gate: bool,
    ctx: &ExecutionContext,
) -> Result<GraphPosterior, CausalError> {
    let eng = OrderMcmc::new()
        .with_schedule(schedule.n_chains, schedule.n_warmup, schedule.n_draws, schedule.thin)
        .with_diagnostics_gate(require_diagnostics_gate);
    let mut ws = DiscoveryWorkspace::default();
    eng.run(data, variables, &params.prior, params.score_family, &mut ws, ctx)
        .map_err(CausalError::from)
}

/// Structure MCMC DAG posterior (Gaussian BIC).
///
/// # Errors
///
/// Discovery / diagnostics-gate failures.
pub fn discover_structure_mcmc(
    data: &TabularData,
    variables: &[VariableId],
    params: &BayesianDiscoverParams,
    schedule: &GraphMcmcSchedule,
    ctx: &ExecutionContext,
) -> Result<GraphPosterior, CausalError> {
    let eng = StructureMcmc::new().with_schedule(
        schedule.n_chains,
        schedule.n_warmup,
        schedule.n_draws,
        schedule.thin,
    );
    let mut ws = DiscoveryWorkspace::default();
    eng.run(data, variables, &params.prior, params.score_family, &mut ws, ctx)
        .map_err(CausalError::from)
}

/// CI-screened candidate-edge posterior (PC skeleton → structure MCMC).
///
/// # Errors
///
/// Screening, empty skeleton, or MCMC failures.
pub fn discover_ci_screened_posterior(
    data: &TabularData,
    variables: &[VariableId],
    params: &BayesianDiscoverParams,
    screen: &StaticDiscoverParams,
    schedule: &GraphMcmcSchedule,
    soft_weight: CiSoftWeight,
    ctx: &ExecutionContext,
) -> Result<GraphPosterior, CausalError> {
    let fdr = screen.fdr.map(|f| f.with_exclude_contemporaneous(false));
    let mcmc = StructureMcmc::new().with_schedule(
        schedule.n_chains,
        schedule.n_warmup,
        schedule.n_draws,
        schedule.thin,
    );
    let eng = CiScreenedPosterior::new()
        .with_constraints(static_pc_constraints(screen.alpha, screen.max_cond_size))
        .with_ci(Arc::clone(&screen.ci))
        .with_soft_weight(soft_weight)
        .with_mcmc(mcmc);
    // Preserve FDR from screen (CiScreenedPosterior::new sets a default).
    let mut eng = eng;
    eng.fdr = fdr;
    let mut ws = DiscoveryWorkspace::default();
    eng.run(data, variables, &params.prior, params.score_family, &mut ws, ctx)
        .map_err(CausalError::from)
}

/// Bounded-lag DBN template posterior from a time series (Gaussian BIC).
///
/// # Errors
///
/// Short series, unsupported size, score, or empty support.
pub fn discover_dbn_posterior(
    data: &TimeSeriesData,
    variables: &[VariableId],
    params: &BayesianDiscoverParams,
    max_lag: u32,
    force_mcmc: bool,
    schedule: &GraphMcmcSchedule,
    ctx: &ExecutionContext,
) -> Result<GraphPosterior, CausalError> {
    let eng = DbnPosterior::new(max_lag).with_force_mcmc(force_mcmc).with_mcmc_schedule(
        schedule.n_chains,
        schedule.n_warmup,
        schedule.n_draws,
    );
    eng.run(data, variables, &params.prior, params.score_family, ctx).map_err(CausalError::from)
}

/// Count definite directed edges in a temporal PAG (Tail–Arrow or Arrow–Tail).
#[must_use]
pub fn pag_definite_directed_edge_count(pag: &TemporalPag) -> u64 {
    let mut directed = 0u64;
    for i in 0..pag.node_count() {
        let a = DenseNodeId::from_raw(i as u32);
        for (b, at_a, at_b) in pag.neighbors(a) {
            if b.raw() < a.raw() {
                continue;
            }
            if matches!(
                (at_a, at_b),
                (Endpoint::Tail, Endpoint::Arrow) | (Endpoint::Arrow, Endpoint::Tail)
            ) {
                directed += 1;
            }
        }
    }
    directed
}