deep_causality_algorithms 0.4.5

Computational causality algorithms and utils used in the DeepCausality project.
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
 * SPDX-License-Identifier: MIT
 * Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
 */

//! Paper-grade evaluation harness: MAP-config pruning vs full enumeration.
//!
//! Two sweeps produce the headline evidence for claim (1) — *MAP-config pruning
//! matches full-enumeration accuracy at a fraction of the configuration work, and
//! scales where full enumeration cannot*:
//!
//! * **Sweep A — controlled degree (planted cliques, `du = c-1`).** For each
//!   clique size the full path's configuration space is exactly `2^{du}` while the
//!   MAP finder evaluates `du+1` configs (the empty start plus one per incident
//!   undirected edge along the hill-climb path). Full is enumerated up to the
//!   `MAX_CONFIG_EDGES = 16` cap (`c ≤ 17`); beyond it Full refuses and only
//!   MapPrune runs.
//! * **Sweep B — scaled `n` (random linear-Gaussian CPDAGs).** Across growing
//!   variable counts, accuracy is ≈ equal and MapPrune wall-clock ≤ Full.
//!
//! The **config-eval counts are exact and deterministic** (`2^{du}` vs `du+1`) —
//! that is the robust headline. Wall-clock is machine-dependent and only
//! indicative. Top-3 accuracy is reported alongside top-1 in full honesty: on
//! cliques the ranked tail can differ between strategies even when top-1 agrees.
//!
//! Run (release):
//!   cargo run --release -p deep_causality_algorithms \
//!     --example brcd_eval_accuracy_compute

use deep_causality_algorithms::brcd::brcd_augment::{augmented_graph, get_configurations_multi};
use deep_causality_algorithms::brcd::brcd_boss_cpdag::dag_to_cpdag;
use deep_causality_algorithms::brcd::brcd_config::{BrcdConfig, ConfigStrategy};
use deep_causality_algorithms::brcd::brcd_mapconfig::find_map_configs;
use deep_causality_algorithms::brcd::brcd_run;
use deep_causality_algorithms::dag_sampling::mec_size as poly_mec_size;
use deep_causality_rand::{Distribution, Normal, Rng, Xoshiro256};
use deep_causality_tensor::CausalTensor;
use deep_causality_topology::MixedGraph;
use std::time::Instant;

/// `MAX_CONFIG_EDGES` from the augment module — Full's hard `2^{du}` cap.
const MAX_CONFIG_EDGES: usize = 16;

/// A linear-Gaussian frame: normal / anomalous `n × num_vars` tensors, the planted
/// root cause, and the CPDAG it was sampled from.
struct Case {
    normal: CausalTensor<f64>,
    anomalous: CausalTensor<f64>,
    cpdag: MixedGraph<()>,
    rc: usize,
}

fn main() {
    // Optional sweep selection: `cargo run ... -- c` runs only Sweep C, etc.
    // With no args every sweep runs (the documented default).
    let args: Vec<String> = std::env::args().skip(1).collect();
    let run = |name: &str| args.is_empty() || args.iter().any(|a| a == name);

    println!();
    println!("BRCD evaluation harness — MapPrune vs Full (accuracy / compute)");
    println!();
    if run("a") {
        sweep_a();
    }
    if run("b") {
        sweep_b();
    }
    if run("c") {
        sweep_c();
    }
}

// ---------------------------------------------------------------------------
// Sweep A — controlled degree (planted cliques, du = c-1, perturb = 4.0)
// ---------------------------------------------------------------------------

fn sweep_a() {
    println!("============================================================================");
    println!("SWEEP A — controlled degree (planted cliques, du = c-1, perturb = 4.0)");
    println!("Config-eval counts are EXACT/DETERMINISTIC: Full = 2^du, MapPrune = du+1.");
    println!("Wall-clock is the median of >=5 seeds (machine-dependent, indicative).");
    println!("============================================================================");
    println!(
        "{:>3} {:>3} | {:>10} {:>10} | {:>9} {:>9} | {:>9} {:>9} | {:>10} {:>10}",
        "c",
        "du",
        "Full cfg",
        "MAP eval",
        "Full t1",
        "MAP t1",
        "Full t3",
        "MAP t3",
        "Full ms",
        "MAP ms"
    );
    println!("{}", "-".repeat(108));

    let perturb = 4.0;
    let n_each = 150usize;
    let seeds = 5usize;

    // c in 4..=13 (Full feasible) plus the MapPrune-only tier {18, 22, 26}.
    let clique_sizes: Vec<usize> = (4..=13).chain([18usize, 22, 26]).collect();

    for &c in &clique_sizes {
        let du = c - 1;
        let full_feasible = du <= MAX_CONFIG_EDGES;

        // Exact, deterministic config counts (structure only; one representative seed).
        let probe = make_clique_case(c, 8, perturb, 0xC0FFEE + c as u64);
        let full_cfg = full_valid_configs(&probe.cpdag, probe.rc);
        let map_eval = mapprune_evals(&probe.cpdag, probe.rc);

        let mut full_t1 = 0usize;
        let mut full_t3 = 0usize;
        let mut map_t1 = 0usize;
        let mut map_t3 = 0usize;
        let mut full_ms: Vec<f64> = Vec::new();
        let mut map_ms: Vec<f64> = Vec::new();

        for s in 0..seeds {
            let seed = 0x000C_116E + (c as u64) * 7919 + s as u64;
            let case = make_clique_case(c, n_each, perturb, seed);

            // MapPrune always runs.
            let t = Instant::now();
            let prune = brcd_run(
                &case.normal,
                &case.anomalous,
                Some(&case.cpdag),
                &cfg(ConfigStrategy::MapPrune, seed),
            )
            .expect("MapPrune run");
            map_ms.push(t.elapsed().as_secs_f64() * 1e3);
            if top1_hits(prune.top(), case.rc) {
                map_t1 += 1;
            }
            if top3_hits(prune.ranks(), case.rc) {
                map_t3 += 1;
            }

            // Full only where du <= MAX_CONFIG_EDGES.
            if full_feasible {
                let t = Instant::now();
                let full = brcd_run(
                    &case.normal,
                    &case.anomalous,
                    Some(&case.cpdag),
                    &cfg(ConfigStrategy::Full, seed),
                )
                .expect("Full run");
                full_ms.push(t.elapsed().as_secs_f64() * 1e3);
                if top1_hits(full.top(), case.rc) {
                    full_t1 += 1;
                }
                if top3_hits(full.ranks(), case.rc) {
                    full_t3 += 1;
                }
            }
        }

        let pct = |hits: usize| -> String { format!("{:.0}%", 100.0 * hits as f64 / seeds as f64) };
        let full_cfg_s = full_cfg.map_or_else(|| "".to_string(), |v| v.to_string());
        let (full_t1_s, full_t3_s, full_ms_s) = if full_feasible {
            (
                pct(full_t1),
                pct(full_t3),
                format!("{:.2}", median_ms(&mut full_ms)),
            )
        } else {
            ("".to_string(), "".to_string(), "".to_string())
        };

        println!(
            "{:>3} {:>3} | {:>10} {:>10} | {:>9} {:>9} | {:>9} {:>9} | {:>10} {:>10}",
            c,
            du,
            full_cfg_s,
            map_eval,
            full_t1_s,
            pct(map_t1),
            full_t3_s,
            pct(map_t3),
            full_ms_s,
            format!("{:.2}", median_ms(&mut map_ms)),
        );
    }
    println!("{}", "-".repeat(108));
    println!(
        "Reading: top-1 is identical (both 100%) where Full is feasible; Full's exact config\n\
         count is 2^du and its time explodes, while MapPrune stays at du+1 evals and flat time.\n\
         Past du = {MAX_CONFIG_EDGES} (c >= 18) Full refuses (\"\") and only MapPrune completes."
    );
    println!();
}

// ---------------------------------------------------------------------------
// Sweep B — scaled n (random linear-Gaussian CPDAGs, detectable anomaly)
// ---------------------------------------------------------------------------

fn sweep_b() {
    println!("============================================================================");
    println!("SWEEP B — scaled n (random linear-Gaussian CPDAGs, perturb = 3.0)");
    println!(">=10 graphs per n; accuracy ~ equal. On these low-du graphs Full's tiny");
    println!("enumeration is already cheap, so MapPrune's finder overhead makes it slower.");
    println!("============================================================================");
    println!(
        "{:>4} | {:>9} {:>9} | {:>9} {:>9} | {:>10} {:>10} | {:>9}",
        "n", "Full t1", "MAP t1", "Full t3", "MAP t3", "Full ms", "MAP ms", "agree-t1"
    );
    println!("{}", "-".repeat(86));

    let perturb = 3.0;
    let n_each = 150usize;
    let n_graphs = 12usize; // >= 10 graphs per n
    let p_edge = 0.30; // tuned so undirected structure exists in most graphs

    for &n in &[10usize, 25, 50, 75, 100] {
        let mut trials = 0usize;
        let mut full_t1 = 0usize;
        let mut full_t3 = 0usize;
        let mut map_t1 = 0usize;
        let mut map_t3 = 0usize;
        let mut agree_t1 = 0usize;
        let mut full_ms: Vec<f64> = Vec::new();
        let mut map_ms: Vec<f64> = Vec::new();

        let mut gi = 0u64;
        while trials < n_graphs {
            let seed = 0x5CA1ED + (n as u64) * 1_000_003 + gi;
            gi += 1;
            if gi > n_graphs as u64 * 40 {
                break; // safety: give up if a regime keeps producing degenerate graphs
            }
            let Some(case) = make_case(n, p_edge, n_each, perturb, seed) else {
                continue;
            };
            // Require some undirected structure (du > 0 somewhere) so the strategies differ.
            let has_undirected = (0..n).any(|v| !case.cpdag.undirected_neighbors(v).is_empty());
            if !has_undirected {
                continue;
            }
            // Skip graphs where Full would refuse (local du > cap) so the cell compares
            // both strategies on the same graphs.
            if full_valid_configs(&case.cpdag, case.rc).is_none() {
                continue;
            }

            let t = Instant::now();
            let full = brcd_run(
                &case.normal,
                &case.anomalous,
                Some(&case.cpdag),
                &cfg(ConfigStrategy::Full, seed),
            )
            .expect("Full run");
            full_ms.push(t.elapsed().as_secs_f64() * 1e3);

            let t = Instant::now();
            let prune = brcd_run(
                &case.normal,
                &case.anomalous,
                Some(&case.cpdag),
                &cfg(ConfigStrategy::MapPrune, seed),
            )
            .expect("MapPrune run");
            map_ms.push(t.elapsed().as_secs_f64() * 1e3);

            trials += 1;
            if top1_hits(full.top(), case.rc) {
                full_t1 += 1;
            }
            if top3_hits(full.ranks(), case.rc) {
                full_t3 += 1;
            }
            if top1_hits(prune.top(), case.rc) {
                map_t1 += 1;
            }
            if top3_hits(prune.ranks(), case.rc) {
                map_t3 += 1;
            }
            if full.top() == prune.top() {
                agree_t1 += 1;
            }
        }

        let pct = |hits: usize| -> String {
            if trials == 0 {
                "".to_string()
            } else {
                format!("{:.0}%", 100.0 * hits as f64 / trials as f64)
            }
        };
        println!(
            "{:>4} | {:>9} {:>9} | {:>9} {:>9} | {:>10} {:>10} | {:>9}",
            format!("{n}({trials})"),
            pct(full_t1),
            pct(map_t1),
            pct(full_t3),
            pct(map_t3),
            format!("{:.2}", median_ms(&mut full_ms)),
            format!("{:.2}", median_ms(&mut map_ms)),
            pct(agree_t1),
        );
    }
    println!("{}", "-".repeat(86));
    println!(
        "Reading: Full and MapPrune top-1/top-3 accuracy track each other and agree on nearly\n\
         every graph (agree-t1). Here MapPrune is SLOWER in wall-clock: on low-du random CPDAGs\n\
         Full enumerates only a handful of configs, so the finder's hill-climb bookkeeping costs\n\
         more than direct enumeration. The compute win is the high-local-degree regime (Sweep A),\n\
         where 2^du is the wall; on low-du graphs full enumeration is already cheap."
    );
    println!("n label shows n(trials).");
    println!();
}

// ---------------------------------------------------------------------------
// Sweep C — large n to 1000 (bounded-degree CPDAGs): near-linear scaling
// ---------------------------------------------------------------------------
//
// The axis of the original paper's Fig-2b: BRCD runtime vs the number of variables,
// out to n = 1000. To make the n-axis meaningful (rather than a dense blow-up) the
// expected in-degree is held ~constant via `p_edge = AVG_DEG / n`, so the local
// undirected degree stays bounded as n grows. Both strategies run on the same graphs
// (a graph where Full would refuse is skipped). The point: the production ranker
// completes at n = 1000 in pure Rust with near-linear wall-clock growth — no
// exponential in n. This is a Rust-native generator, NOT the paper's discrete
// pyAgrum protocol, so it reproduces the *scaling shape*, not a head-to-head number.
fn sweep_c() {
    println!("============================================================================");
    println!("SWEEP C — large n (bounded-degree random CPDAGs, avg in-degree ~ 2)");
    println!("Fig-2b axis: runtime vs n out to 1000. p_edge = 2/n keeps degree bounded so");
    println!("the curve reflects n-scaling, not a dense blow-up. Same graphs for both.");
    println!("============================================================================");
    println!(
        "{:>7} | {:>9} {:>9} | {:>9} {:>9} | {:>11} {:>11} | {:>9}",
        "n", "Full t1", "MAP t1", "Full t3", "MAP t3", "Full ms", "MAP ms", "agree-t1"
    );
    println!("{}", "-".repeat(92));

    let perturb = 3.0;
    let n_rows = 150usize;
    let avg_deg = 2.0;

    for &(n, n_graphs) in &[(50usize, 4usize), (100, 4), (250, 3), (500, 2), (1000, 2)] {
        let p_edge = (avg_deg / n as f64).min(0.5);
        let mut trials = 0usize;
        let mut full_t1 = 0usize;
        let mut full_t3 = 0usize;
        let mut map_t1 = 0usize;
        let mut map_t3 = 0usize;
        let mut agree_t1 = 0usize;
        let mut full_ms: Vec<f64> = Vec::new();
        let mut map_ms: Vec<f64> = Vec::new();

        let mut gi = 0u64;
        while trials < n_graphs {
            let seed = 0xF16B + (n as u64) * 2_750_159 + gi;
            gi += 1;
            if gi > n_graphs as u64 * 60 {
                break; // safety: give up if the regime keeps producing degenerate graphs
            }
            let Some(case) = make_case(n, p_edge, n_rows, perturb, seed) else {
                continue;
            };
            let has_undirected = (0..n).any(|v| !case.cpdag.undirected_neighbors(v).is_empty());
            if !has_undirected {
                continue;
            }
            if full_valid_configs(&case.cpdag, case.rc).is_none() {
                continue; // skip graphs where Full would refuse, so both run the same graphs
            }

            let t = Instant::now();
            let full = brcd_run(
                &case.normal,
                &case.anomalous,
                Some(&case.cpdag),
                &cfg(ConfigStrategy::Full, seed),
            )
            .expect("Full run");
            full_ms.push(t.elapsed().as_secs_f64() * 1e3);

            let t = Instant::now();
            let prune = brcd_run(
                &case.normal,
                &case.anomalous,
                Some(&case.cpdag),
                &cfg(ConfigStrategy::MapPrune, seed),
            )
            .expect("MapPrune run");
            map_ms.push(t.elapsed().as_secs_f64() * 1e3);

            trials += 1;
            if top1_hits(full.top(), case.rc) {
                full_t1 += 1;
            }
            if top3_hits(full.ranks(), case.rc) {
                full_t3 += 1;
            }
            if top1_hits(prune.top(), case.rc) {
                map_t1 += 1;
            }
            if top3_hits(prune.ranks(), case.rc) {
                map_t3 += 1;
            }
            if full.top() == prune.top() {
                agree_t1 += 1;
            }
            eprintln!("  [sweep C] n={n}: graph {trials}/{n_graphs} done");
        }

        let pct = |hits: usize| -> String {
            if trials == 0 {
                "".to_string()
            } else {
                format!("{:.0}%", 100.0 * hits as f64 / trials as f64)
            }
        };
        println!(
            "{:>7} | {:>9} {:>9} | {:>9} {:>9} | {:>11} {:>11} | {:>9}",
            format!("{n}({trials})"),
            pct(full_t1),
            pct(map_t1),
            pct(full_t3),
            pct(map_t3),
            format!("{:.1}", median_ms(&mut full_ms)),
            format!("{:.1}", median_ms(&mut map_ms)),
            pct(agree_t1),
        );
    }
    println!("{}", "-".repeat(92));
    println!(
        "Reading: both strategies COMPLETE at n = 1000 in pure Rust (Full ~11 s, MapPrune ~29 s)\n\
         with NO exponential in n — the bounded-degree regime removes the 2^du wall entirely.\n\
         But wall-clock here is super-linear, empirically ~n^2.5-3 (roughly cubic; dominated by\n\
         per-candidate graph augmentation/scoring over all n nodes), NOT near-linear. MapPrune is\n\
         ~2.6x slower than Full at bounded du (finder overhead, as in Sweep B). Accuracy is\n\
         identical (top-1/top-3 100%, 100% agree). This reproduces the Fig-2b *axis* on a\n\
         Rust-native continuous generator — NOT the paper's discrete pyAgrum protocol — so read it\n\
         as scaling shape + practical latency (well under the original's reported ~150 s at\n\
         n = 1000), not a controlled head-to-head."
    );
    println!("n label shows n(trials).");
    println!();
}

/// Random DAG (parents from lower indices), its CPDAG, and a linear-Gaussian
/// dataset with root cause `rc` (the first node with an incident undirected edge)
/// perturbed between regimes. Copied from `mapconfig_tests::make_case`.
fn make_case(n: usize, p_edge: f64, n_each: usize, perturb: f64, seed: u64) -> Option<Case> {
    let mut rng = Xoshiro256::from_seed(seed);
    let eps = Normal::new(0.0_f64, 1.0).unwrap();

    let mut parents: Vec<Vec<usize>> = vec![Vec::new(); n];
    let mut weight: Vec<Vec<f64>> = vec![Vec::new(); n];
    for i in 0..n {
        for j in 0..i {
            if rng.random_range(0.0..1.0) < p_edge {
                parents[i].push(j);
                let sign = if rng.random_range(0.0..1.0) < 0.5 {
                    -1.0
                } else {
                    1.0
                };
                weight[i].push(sign * (0.5 + rng.random_range(0.0..1.0)));
            }
        }
    }
    let cpdag = dag_to_cpdag(&parents).ok()?;
    let rc = (0..n)
        .find(|&v| !cpdag.undirected_neighbors(v).is_empty())
        .unwrap_or(0);

    let mut normal = Vec::with_capacity(n_each * n);
    let mut anomalous = Vec::with_capacity(n_each * n);
    for regime in 0..2 {
        for _ in 0..n_each {
            let mut x = vec![0.0_f64; n];
            for i in 0..n {
                let mut mean = if regime == 1 && i == rc { perturb } else { 0.0 };
                for (k, &j) in parents[i].iter().enumerate() {
                    mean += weight[i][k] * x[j];
                }
                x[i] = mean + eps.sample(&mut rng);
            }
            let dst = if regime == 0 {
                &mut normal
            } else {
                &mut anomalous
            };
            dst.extend_from_slice(&x);
        }
    }
    Some(Case {
        normal: CausalTensor::new(normal, vec![n_each, n]).unwrap(),
        anomalous: CausalTensor::new(anomalous, vec![n_each, n]).unwrap(),
        cpdag,
        rc,
    })
}

/// Planted clique: a transitive tournament on `c` nodes whose CPDAG is the
/// undirected `c`-clique (`du = c-1` for every node). Node 0 is the root cause.
/// Copied from `mapconfig_tests::make_clique_case`.
fn make_clique_case(c: usize, n_each: usize, perturb: f64, seed: u64) -> Case {
    let mut rng = Xoshiro256::from_seed(seed);
    let eps = Normal::new(0.0_f64, 1.0).unwrap();
    let mut parents: Vec<Vec<usize>> = vec![Vec::new(); c];
    let mut weight: Vec<Vec<f64>> = vec![Vec::new(); c];
    for i in 0..c {
        for j in 0..i {
            parents[i].push(j);
            let s = if rng.random_range(0.0..1.0) < 0.5 {
                -1.0
            } else {
                1.0
            };
            weight[i].push(s * (0.5 + rng.random_range(0.0..1.0)));
        }
    }
    let cpdag = dag_to_cpdag(&parents).expect("clique cpdag");
    let mut normal = Vec::with_capacity(n_each * c);
    let mut anomalous = Vec::with_capacity(n_each * c);
    for regime in 0..2 {
        for _ in 0..n_each {
            let mut x = vec![0.0_f64; c];
            for i in 0..c {
                let mut mean = if regime == 1 && i == 0 { perturb } else { 0.0 };
                for (k, &j) in parents[i].iter().enumerate() {
                    mean += weight[i][k] * x[j];
                }
                x[i] = mean + eps.sample(&mut rng);
            }
            let dst = if regime == 0 {
                &mut normal
            } else {
                &mut anomalous
            };
            dst.extend_from_slice(&x);
        }
    }
    Case {
        normal: CausalTensor::new(normal, vec![n_each, c]).unwrap(),
        anomalous: CausalTensor::new(anomalous, vec![n_each, c]).unwrap(),
        cpdag,
        rc: 0,
    }
}

/// `BrcdConfig::continuous(seed)` with the requested config strategy.
fn cfg(strategy: ConfigStrategy, seed: u64) -> BrcdConfig<f64> {
    let mut c = BrcdConfig::<f64>::continuous(seed);
    c.config_strategy = strategy;
    c
}

/// Median of a slice of millisecond timings (sorted copy, midpoint).
fn median_ms(samples: &mut [f64]) -> f64 {
    samples.sort_by(|a, b| a.partial_cmp(b).unwrap());
    let m = samples.len() / 2;
    if samples.len() % 2 == 1 {
        samples[m]
    } else {
        0.5 * (samples[m - 1] + samples[m])
    }
}

/// Exact Full valid-config count for the candidate `rc` on `cpdag`, or `None` when
/// the full path refuses (`du > MAX_CONFIG_EDGES`).
fn full_valid_configs(cpdag: &MixedGraph<()>, rc: usize) -> Option<usize> {
    get_configurations_multi(cpdag, &[rc]).ok().map(|v| v.len())
}

/// Exact MapPrune evaluation budget for the candidate `rc` on `cpdag`. The weight
/// is the structure-only augmented MEC size (uncapped polynomial counter), exactly
/// as in the finder-budget test, so the walk is exercised for any `du`.
fn mapprune_evals(cpdag: &MixedGraph<()>, rc: usize) -> usize {
    find_map_configs::<f64, (), _>(cpdag, &[rc], |g| {
        let aug = augmented_graph(g, &[rc])?;
        poly_mec_size::<f64, ()>(&aug)
    })
    .expect("finder")
    .evals
}

/// `true` when the strategy's top-1 set is exactly `{rc}`.
fn top1_hits(top: Option<&[usize]>, rc: usize) -> bool {
    top == Some([rc].as_slice())
}

/// `true` when `rc` appears among the first three ranked candidate sets, each a
/// singleton in these sweeps.
fn top3_hits(ranks: &[Vec<usize>], rc: usize) -> bool {
    ranks.iter().take(3).any(|c| c.as_slice() == [rc])
}