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
use std::collections::{BTreeMap, HashMap};
use crate::approx::{
approximate_asv, approximate_asv_adaptive, approximate_asv_adaptive_batched,
approximate_asv_batched, approximate_asv_uniform, approximate_asv_uniform_sparse,
approximate_asv_uniform_sparse_adaptive,
};
use crate::cache::value_cached;
use crate::dag_dp::{compute_dp_ind, dag_exact_asv};
use crate::dag_dp_sparse::{ExactDagConfig, dag_exact_asv_sparse, estimate_sparse_feasible};
use crate::error::CausasvError;
use crate::graph::{Dag, NodeId};
use crate::sampler::{AdaptiveSamplingConfig, SamplingConfig};
use crate::topo::enumerate_topos;
use crate::tree::tree_exact_asv;
/// Result of an ASV computation.
#[derive(Debug)]
pub struct AsvResult {
/// Per-node ASV values in ascending NodeId order.
pub values: BTreeMap<NodeId, f64>,
/// Number of topological orderings (exact) or samples used (approximate).
pub n_samples: usize,
/// RNG seed used; None for exact computation or unseeded approximate.
pub seed: Option<u64>,
/// True if exact (brute-force or tree-exact), false if approximate.
pub is_exact: bool,
/// Effective Sample Size for approximate methods: ESS = (Σw)² / Σw².
/// ESS ≈ n_samples → uniform IS weights (reliable). ESS ≪ n_samples → high variance.
/// None for exact methods.
pub effective_sample_size: Option<f64>,
/// True if adaptive sampling converged before max_samples. None for non-adaptive methods.
pub converged: Option<bool>,
/// Per-node IS standard error estimates. None for exact and non-adaptive approx.
pub stderr: Option<BTreeMap<NodeId, f64>>,
/// Number of order ideals visited by exact_dag_sparse. None for other methods.
pub n_order_ideals: Option<usize>,
/// Fraction of 2^n states visited: n_order_ideals / 2^n. None for other methods.
pub state_ratio: Option<f64>,
/// Estimated memory used by sparse DP in MB. None for other methods.
pub memory_mb: Option<f64>,
/// Method that was attempted before falling back (e.g. "exact_dag_sparse"). None normally.
pub fallback_from: Option<String>,
/// Reason for the fallback (error message). None normally.
pub fallback_reason: Option<String>,
/// Actual method dispatched by `auto()`; None when method was caller-specified.
pub method_used: Option<&'static str>,
}
/// Entry point for ASV computation over a causal DAG.
pub struct AsvExplainer {
dag: Dag,
// Bitmask of all DAG parents per node. Safe for n ≤ 63 (1u64 << p.0 is always in-range).
// Empty for n > 63; exact_dag / exact_dag_sparse both error before using it.
parents_mask: Vec<u64>,
// Cached once at construction; DAG is owned and immutable after new().
is_rooted_tree: bool,
}
impl AsvExplainer {
pub fn new(dag: Dag) -> Self {
let n = dag.node_count();
let parents_mask = if n <= 63 {
(0..n)
.map(|i| {
dag.parents_raw(NodeId(i as u32))
.iter()
.fold(0u64, |m, &p| m | (1u64 << p.0))
})
.collect()
} else {
vec![]
};
let is_rooted_tree = crate::tree::find_rooted_tree_root(&dag).is_ok();
Self {
dag,
parents_mask,
is_rooted_tree,
}
}
/// Brute-force exact ASV: enumerates all topological orderings.
/// Correct for any DAG; only practical for n ≤ ~8.
pub fn exact<F>(&self, value_fn: F) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
self.dag.validate()?;
let n = self.dag.node_count();
if n > 64 {
return Err(CausasvError::InvalidConfig(format!(
"bitmask coalitions require n ≤ 64, got {n}"
)));
}
let orderings = enumerate_topos(&self.dag)?;
let n_orderings = orderings.len();
let mut phi = vec![0.0f64; n];
let mut cache = HashMap::<u64, f64>::new();
for ordering in &orderings {
let mut prefix_mask: u64 = 0;
for &node in ordering {
let without = prefix_mask;
let with_node = prefix_mask | (1u64 << node.0);
phi[node.0 as usize] += value_cached(&mut cache, &value_fn, with_node)?
- value_cached(&mut cache, &value_fn, without)?;
prefix_mask = with_node;
}
}
let scale = 1.0 / n_orderings as f64;
let values = (0..n).map(|i| (NodeId(i as u32), phi[i] * scale)).collect();
Ok(AsvResult {
values,
n_samples: n_orderings,
seed: None,
is_exact: true,
effective_sample_size: None,
converged: None,
stderr: None,
n_order_ideals: None,
state_ratio: None,
memory_mb: None,
fallback_from: None,
fallback_reason: None,
method_used: None,
})
}
/// Approximate ASV via IS-weighted topological order sampling.
/// Works for any DAG size; use a large n_samples for accuracy.
pub fn approximate<F>(
&self,
value_fn: F,
config: SamplingConfig,
) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError> + Send + Sync,
{
self.dag.validate()?;
approximate_asv(&self.dag, value_fn, config)
}
/// Approximate ASV via uniform topological order sampling.
///
/// Unlike [`approximate`](Self::approximate) which uses IS-weighted frontier sampling,
/// this method samples each linear extension with probability exactly 1/L(G) by
/// consulting the `dp_ind` table at every step. Every sample contributes equally —
/// there is no importance-weight variance, so ESS = n_samples exactly.
///
/// A 2^n `dp_ind` table is precomputed in O(2^n × n) before sampling begins.
/// For n_samples much smaller than 2^n this pays off handsomely; for n_samples ≈ 2^n
/// use [`exact_dag`](Self::exact_dag) instead.
///
/// **Limit**: n ≤ 20 (same as `exact_dag`). For larger DAGs use `approximate()`.
pub fn approximate_uniform<F>(
&self,
value_fn: F,
config: SamplingConfig,
) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
let n = self.dag.node_count();
if n > 20 {
return Err(CausasvError::InvalidConfig(format!(
"approximate_uniform requires n ≤ 20 (2^n dp_ind table), got {n}; \
use approximate() for larger DAGs"
)));
}
if config.n_samples == 0 {
return Err(CausasvError::InvalidConfig(
"n_samples must be > 0".to_string(),
));
}
self.dag.validate()?;
let dp_ind = compute_dp_ind(n, &self.parents_mask)?;
approximate_asv_uniform(value_fn, config, &dp_ind, &self.parents_mask)
}
/// Uniform topological ordering sampler for sparse DAGs with n > 20.
///
/// Uses lazily memoized dp_ind (HashMap) instead of a precomputed 2^n table,
/// enabling uniform sampling for n up to 63 on DAGs with manageable state spaces.
/// Every linear extension is sampled with equal probability — ESS = n_samples exactly,
/// with no IS weight variance. Returns `Err(Overflow)` if the dp_ind cache exceeds
/// 2 GiB (same memory limit as `exact_dag_sparse`).
pub fn approximate_uniform_sparse<F>(
&self,
value_fn: F,
config: SamplingConfig,
) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
let n = self.dag.node_count();
if n > 63 {
return Err(CausasvError::InvalidConfig(format!(
"approximate_uniform_sparse requires n ≤ 63 (u64 bitmask), got {n}; \
use approximate() for larger DAGs"
)));
}
if config.n_samples == 0 {
return Err(CausasvError::InvalidConfig(
"n_samples must be > 0".to_string(),
));
}
self.dag.validate()?;
approximate_asv_uniform_sparse(
&self.dag,
value_fn,
config,
&self.parents_mask,
2 * 1024 * 1024 * 1024, // 2 GiB — same as ExactDagConfig default
)
}
/// Adaptive uniform topological ordering sampler for sparse DAGs.
///
/// Like `approximate_uniform_sparse` but with adaptive stopping: runs in batches until
/// per-node estimates stabilize (`rel_tol`) or `max_samples` is reached. Returns per-node
/// stderr and convergence flag. ESS = n_samples exactly (no IS weight variance).
pub fn approximate_uniform_sparse_adaptive<F>(
&self,
value_fn: F,
config: AdaptiveSamplingConfig,
) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
let n = self.dag.node_count();
if n > 63 {
return Err(CausasvError::InvalidConfig(format!(
"approximate_uniform_sparse_adaptive requires n ≤ 63, got {n}"
)));
}
self.dag.validate()?;
approximate_asv_uniform_sparse_adaptive(
&self.dag,
value_fn,
config,
&self.parents_mask,
2 * 1024 * 1024 * 1024,
)
}
/// Exact ASV for rooted directed trees. Returns Err(NotRootedTree) if the graph is not one.
/// Validates tree structure before computing; otherwise identical to `exact`.
pub fn exact_tree<F>(&self, value_fn: F) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
self.dag.validate()?;
tree_exact_asv(&self.dag, value_fn)
}
/// Exact ASV for any DAG via order-ideal DP. Practical for n ≤ 20.
///
/// Computes `dp[mask]` = number of linear extensions with prefix `mask`,
/// then accumulates weighted marginal contributions. O(2^n × n) time.
pub fn exact_dag<F>(&self, value_fn: F) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
self.dag.validate()?;
dag_exact_asv(&self.dag, value_fn, &self.parents_mask)
}
/// Exact ASV for general DAGs using sparse order-ideal DP.
///
/// BFS-enumerates only valid order ideals — sets S where all parents of every
/// i ∈ S are also in S. For sparse DAGs the number of valid order ideals can be
/// far less than 2^n, enabling exact computation for n > 20.
///
/// Uses the default `ExactDagConfig` (max_nodes=28, memory_limit_bytes=2GiB).
/// Returns `Err` if n > max_nodes or if the memory limit is exceeded mid-BFS.
pub fn exact_dag_sparse<F>(&self, value_fn: F) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
self.exact_dag_sparse_with_config(value_fn, &ExactDagConfig::default())
}
/// Like `exact_dag_sparse` but with a custom `ExactDagConfig`.
pub fn exact_dag_sparse_with_config<F>(
&self,
value_fn: F,
config: &ExactDagConfig,
) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
self.dag.validate()?;
let (result, _, _, _) =
dag_exact_asv_sparse(&self.dag, value_fn, config, &self.parents_mask)?;
Ok(result)
}
/// Automatic method selection based on graph size and structure.
///
/// Dispatch rules:
/// - n ≤ 8: `exact` — brute-force, lowest overhead for small n
/// - n > 8, rooted directed tree: `exact_tree` — order-ideal DP
/// - 8 < n ≤ 20: `exact_dag_sparse` if edge_count ≤ 2n (sparse heuristic), else `exact_dag`
/// - 20 < n ≤ 28: `exact_dag_sparse` — sparse BFS over order ideals;
/// falls back to `approximate` on memory-limit or overflow errors
/// - 28 < n ≤ 63: `exact_dag_sparse` if order ideal count ≤ 250k (sparse preflight),
/// else `approximate`
/// - n > 63: `approximate` — u64 bitmask limit
///
/// `config` is used only when the approximate path is taken.
pub fn auto<F>(&self, value_fn: F, config: SamplingConfig) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError> + Send + Sync,
{
self.dag.validate()?;
let n = self.dag.node_count();
if n <= 8 {
let mut r = self.exact(value_fn)?;
r.method_used = Some("exact");
Ok(r)
} else if self.is_rooted_tree {
let mut r = self.exact_tree(value_fn)?;
r.method_used = Some("exact_tree");
Ok(r)
} else if n <= 20 {
let m = self.dag.edge_count();
if m <= 2 * n {
// Sparse DAG: try sparse DP first; fall back to dense on the rare Overflow edge case.
// Use a closure to borrow value_fn without consuming it (same pattern as n<=28 branch).
match self.exact_dag_sparse_with_config(|c| value_fn(c), &ExactDagConfig::default())
{
Ok(mut r) => {
r.method_used = Some("exact_dag_sparse");
Ok(r)
}
Err(CausasvError::InvalidConfig(_)) | Err(CausasvError::Overflow(_)) => {
let mut r = self.exact_dag(value_fn)?;
r.method_used = Some("exact_dag");
Ok(r)
}
Err(e) => Err(e),
}
} else {
let mut r = self.exact_dag(value_fn)?;
r.method_used = Some("exact_dag");
Ok(r)
}
} else if n <= 28 {
// Use a closure to borrow value_fn without consuming it, so we can
// fall back to approximate if sparse DP hits the memory or overflow limit.
match self.exact_dag_sparse_with_config(|c| value_fn(c), &ExactDagConfig::default()) {
Ok(mut r) => {
r.method_used = Some("exact_dag_sparse");
Ok(r)
}
Err(CausasvError::InvalidConfig(ref msg))
| Err(CausasvError::Overflow(ref msg)) => {
let mut r = self.approximate(value_fn, config)?;
r.fallback_from = Some("exact_dag_sparse".to_string());
r.fallback_reason = Some(msg.clone());
r.method_used = Some("approx");
Ok(r)
}
Err(e) => Err(e),
}
} else if n <= 63 {
// For n > 28 and n ≤ 63: run a cheap BFS preflight to count order ideals.
// If the state count is manageable, use exact_dag_sparse with an elevated
// max_nodes limit; otherwise fall back to approximate.
if estimate_sparse_feasible(&self.dag, &self.parents_mask, 250_000) {
let sparse_cfg = ExactDagConfig {
max_nodes: n,
..ExactDagConfig::default()
};
match self.exact_dag_sparse_with_config(|c| value_fn(c), &sparse_cfg) {
Ok(mut r) => {
r.method_used = Some("exact_dag_sparse");
Ok(r)
}
Err(CausasvError::InvalidConfig(ref msg))
| Err(CausasvError::Overflow(ref msg)) => {
let mut r = self.approximate(value_fn, config)?;
r.fallback_from = Some("exact_dag_sparse".to_string());
r.fallback_reason = Some(msg.clone());
r.method_used = Some("approx");
Ok(r)
}
Err(e) => Err(e),
}
} else {
let mut r = self.approximate(value_fn, config)?;
r.method_used = Some("approx");
Ok(r)
}
} else {
let mut r = self.approximate(value_fn, config)?;
r.method_used = Some("approx");
Ok(r)
}
}
/// Quality-first automatic dispatch: like `auto()` but the IS approximate fallback is
/// replaced by `approximate_uniform_sparse_adaptive` (zero IS variance, ESS = n_samples,
/// stderr + CI always available). For n > 63 falls back to `approximate_adaptive`.
///
/// Every code path returns `stderr` and `converged` — use `explain_quality()` in Python
/// for the friendliest interface.
///
/// Dispatch rules:
/// - n ≤ 8: `exact`
/// - rooted tree: `exact_tree`
/// - 8 < n ≤ 20: `exact_dag_sparse` (sparse-first) or `exact_dag`
/// - 20 < n ≤ 63: sparse preflight (≤ 250k order ideals) → `exact_dag_sparse`;
/// preflight fails or BFS overflows → `approximate_uniform_sparse_adaptive`
/// - n > 63: `approximate_adaptive`
pub fn auto_quality<F>(
&self,
value_fn: F,
config: AdaptiveSamplingConfig,
) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
self.dag.validate()?;
let n = self.dag.node_count();
if n <= 8 {
let mut r = self.exact(value_fn)?;
r.method_used = Some("exact");
Ok(r)
} else if self.is_rooted_tree {
let mut r = self.exact_tree(value_fn)?;
r.method_used = Some("exact_tree");
Ok(r)
} else if n <= 20 {
let m = self.dag.edge_count();
if m <= 2 * n {
match self.exact_dag_sparse_with_config(|c| value_fn(c), &ExactDagConfig::default())
{
Ok(mut r) => {
r.method_used = Some("exact_dag_sparse");
Ok(r)
}
Err(CausasvError::InvalidConfig(_)) | Err(CausasvError::Overflow(_)) => {
let mut r = self.exact_dag(value_fn)?;
r.method_used = Some("exact_dag");
Ok(r)
}
Err(e) => Err(e),
}
} else {
let mut r = self.exact_dag(value_fn)?;
r.method_used = Some("exact_dag");
Ok(r)
}
} else if n <= 63 {
// Cheap preflight: count order ideals up to 250k before committing to full BFS.
if estimate_sparse_feasible(&self.dag, &self.parents_mask, 250_000) {
let sparse_cfg = ExactDagConfig {
max_nodes: n.min(63),
..ExactDagConfig::default()
};
match self.exact_dag_sparse_with_config(|c| value_fn(c), &sparse_cfg) {
Ok(mut r) => {
r.method_used = Some("exact_dag_sparse");
Ok(r)
}
Err(CausasvError::InvalidConfig(ref msg))
| Err(CausasvError::Overflow(ref msg)) => {
let mut r = self.approximate_uniform_sparse_adaptive(value_fn, config)?;
r.fallback_from = Some("exact_dag_sparse".to_string());
r.fallback_reason = Some(msg.clone());
r.method_used = Some("uniform_sparse_adaptive");
Ok(r)
}
Err(e) => Err(e),
}
} else {
// Preflight exceeded 250k budget — skip exact, go to uniform sparse adaptive.
let mut r = self.approximate_uniform_sparse_adaptive(value_fn, config)?;
r.method_used = Some("uniform_sparse_adaptive");
Ok(r)
}
} else {
// n > 63: uniform_sparse_adaptive requires n ≤ 63; use IS adaptive.
let mut r = self.approximate_adaptive(value_fn, config)?;
r.method_used = Some("approx_adaptive");
Ok(r)
}
}
/// Adaptive approximate ASV: runs sampling in batches and stops when estimates
/// converge (relative change < `config.rel_tol` and ESS ratio ≥ `config.ess_ratio_min`),
/// or when `config.max_samples` is reached.
///
/// Always single-threaded for deterministic convergence behavior.
/// Returns per-node standard error estimates alongside ASV values.
pub fn approximate_adaptive<F>(
&self,
value_fn: F,
config: AdaptiveSamplingConfig,
) -> Result<AsvResult, CausasvError>
where
F: Fn(&[NodeId]) -> Result<f64, CausasvError>,
{
self.dag.validate()?;
approximate_asv_adaptive(&self.dag, value_fn, config)
}
/// Batched approximate ASV: like `approximate`, but evaluates coalitions in batches
/// via `value_fn_batch(coalitions) -> values` to reduce per-call overhead (e.g. Python GIL).
///
/// Set `config.batch_size` to control how many samples to collect per batch call.
/// With `batch_size=1`, results are identical to `approximate` for the same seed.
pub fn approximate_batched<F>(
&self,
value_fn_batch: F,
config: SamplingConfig,
) -> Result<AsvResult, CausasvError>
where
F: Fn(&[Vec<NodeId>]) -> Result<Vec<f64>, CausasvError>,
{
self.dag.validate()?;
approximate_asv_batched(&self.dag, value_fn_batch, config)
}
/// Adaptive batched approximate ASV: like `approximate_adaptive`, but evaluates
/// coalitions in batches via `value_fn_batch`.
///
/// Each sampling batch (of `config.batch_size` samples) becomes one `value_fn_batch` call.
pub fn approximate_adaptive_batched<F>(
&self,
value_fn_batch: F,
config: AdaptiveSamplingConfig,
) -> Result<AsvResult, CausasvError>
where
F: Fn(&[Vec<NodeId>]) -> Result<Vec<f64>, CausasvError>,
{
self.dag.validate()?;
approximate_asv_adaptive_batched(&self.dag, value_fn_batch, config)
}
}