antecedent-model 0.5.1

Structural causal models, mechanisms, and interventional sampling for the Antecedent 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
//! Intervention overlays on an immutable compiled plan.
//!
//! SPDX-License-Identifier: MIT OR Apache-2.0

use std::sync::Arc;

use antecedent_core::{
    Intervention, InterventionSequence, MechanismOverride, StochasticPolicy, TemporalPolicy,
    VariableId,
};
use antecedent_graph::DenseNodeId;

use crate::compile::CompiledCausalModel;
use crate::error::ModelError;

/// Compact overlay describing how sampling differs from the observational plan.
///
/// The underlying [`CompiledCausalModel`] is never cloned; overlays are applied
/// during ancestral sampling.
#[derive(Clone, Debug, Default)]
pub struct InterventionOverlay {
    /// Per-node hard sets (dense index → value), `None` = not hard-set.
    pub hard_set: Vec<Option<f64>>,
    /// Per-node additive shifts.
    pub shifts: Vec<f64>,
    /// Per-node stochastic policies.
    pub stochastic: Vec<Option<StochasticPolicy>>,
    /// Per-node soft mechanism overrides.
    pub soft: Vec<Option<MechanismOverride>>,
    /// Optional temporal activation mask per node (`true` = active at current step).
    pub active: Vec<bool>,
}

impl InterventionOverlay {
    /// Empty overlay (observational) for `n_nodes`.
    #[must_use]
    pub fn observational(n_nodes: usize) -> Self {
        Self {
            hard_set: vec![None; n_nodes],
            shifts: vec![0.0; n_nodes],
            stochastic: vec![None; n_nodes],
            soft: vec![None; n_nodes],
            active: vec![true; n_nodes],
        }
    }

    /// Reject a node carrying both a hard set and an additive shift.
    ///
    /// A hard set replaces a node's assignment outright, so an additive shift on the
    /// same node has nothing well-defined to add to: the mechanism it would shift is
    /// exactly the one the set discarded. Rather than pick a winner silently, both
    /// overlay construction and the overlay-accepting samplers refuse the pair and
    /// make the caller say which they meant.
    ///
    /// Called for you by [`Self::from_interventions`] and [`Self::from_sequence_at`];
    /// call it yourself if you populate the public fields directly.
    ///
    /// # Errors
    ///
    /// [`ModelError::Unsupported`] naming the first offending dense node index.
    pub fn validate(&self) -> Result<(), ModelError> {
        for (idx, set) in self.hard_set.iter().enumerate() {
            if set.is_some() && self.shifts.get(idx).is_some_and(|s| *s != 0.0) {
                return Err(ModelError::Unsupported {
                    message: format!(
                        "dense node {idx} carries both a hard set and an additive shift; \
                         a variable cannot be simultaneously pinned (do(X := v)) and shifted \
                         (do(X := X + delta))"
                    ),
                });
            }
        }
        Ok(())
    }

    /// Whether any node is intervened.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.hard_set.iter().all(Option::is_none)
            && self.shifts.iter().all(|s| *s == 0.0)
            && self.stochastic.iter().all(Option::is_none)
            && self.soft.iter().all(Option::is_none)
            && self.active.iter().all(|a| *a)
    }

    /// Compile interventions against a model (simultaneous / single-step).
    ///
    /// # Errors
    ///
    /// Unknown variables, invalid interventions, or a node both set and shifted
    /// (see [`Self::validate`]).
    pub fn from_interventions(
        model: &CompiledCausalModel,
        interventions: &[Intervention],
    ) -> Result<Self, ModelError> {
        let mut overlay = Self::observational(model.n_nodes());
        for iv in interventions {
            apply_intervention(model, &mut overlay, iv, true)?;
        }
        // Checked once here rather than per-variant inside `apply_intervention`: the
        // conflict is order-independent, and any future shift-producing variant is
        // covered without having to remember to re-add the check.
        overlay.validate()?;
        Ok(overlay)
    }

    /// Overlay for a temporal sequence at discrete step `t`.
    ///
    /// # Errors
    ///
    /// Invalid sequence, unknown variables, or a node both set and shifted at `t`
    /// (see [`Self::validate`]).
    pub fn from_sequence_at(
        model: &CompiledCausalModel,
        seq: &InterventionSequence,
        t: i32,
    ) -> Result<Self, ModelError> {
        let mut overlay = Self::observational(model.n_nodes());
        for step in seq.steps.iter() {
            if temporal_active(&step.temporal, t)? {
                apply_intervention(model, &mut overlay, &step.intervention, true)?;
            }
        }
        // Only steps active at `t` compose, so a set and a shift that never overlap in
        // time remain legal — the conflict is evaluated per step, not across the sequence.
        overlay.validate()?;
        Ok(overlay)
    }
}

fn temporal_active(policy: &TemporalPolicy, t: i32) -> Result<bool, ModelError> {
    policy.validate().map_err(|e| ModelError::Unsupported { message: e.to_string() })?;
    Ok(policy.is_active_at(t))
}

fn apply_intervention(
    model: &CompiledCausalModel,
    overlay: &mut InterventionOverlay,
    iv: &Intervention,
    allow_nested_sequence: bool,
) -> Result<(), ModelError> {
    match iv {
        Intervention::Set { variable, value } => {
            let dense = require_dense(model, *variable)?;
            let v = value.as_f64().ok_or_else(|| ModelError::Unsupported {
                message: "hard set requires numeric value".into(),
            })?;
            overlay.hard_set[dense.as_usize()] = Some(v);
            Ok(())
        }
        Intervention::Shift { variable, delta } => {
            let dense = require_dense(model, *variable)?;
            let d = delta.as_f64().ok_or_else(|| ModelError::Unsupported {
                message: "shift requires numeric delta".into(),
            })?;
            overlay.shifts[dense.as_usize()] += d;
            Ok(())
        }
        Intervention::Stochastic { variable, policy } => {
            policy.validate().map_err(|e| ModelError::Unsupported { message: e.to_string() })?;
            let dense = require_dense(model, *variable)?;
            overlay.stochastic[dense.as_usize()] = Some(policy.clone());
            Ok(())
        }
        Intervention::Soft { variable, mechanism } => {
            let dense = require_dense(model, *variable)?;
            // Unify with `Intervention::Shift`: additive soft overrides are shifts, so
            // ancestral and structural sampling share the same noise semantics.
            if mechanism.family_id.as_ref() == "additive_shift" {
                let d = mechanism.parameters.first().copied().unwrap_or(0.0);
                overlay.shifts[dense.as_usize()] += d;
                return Ok(());
            }
            overlay.soft[dense.as_usize()] = Some(mechanism.clone());
            Ok(())
        }
        Intervention::Sequence(seq) => {
            if !allow_nested_sequence {
                return Err(ModelError::Unsupported {
                    message: "nested intervention sequences are not supported here".into(),
                });
            }
            // Simultaneous interpretation at t=0 for static models.
            for step in seq.steps.iter() {
                if temporal_active(&step.temporal, 0)? {
                    apply_intervention(model, overlay, &step.intervention, false)?;
                }
            }
            Ok(())
        }
        _ => Err(ModelError::Unsupported { message: "unknown intervention variant".into() }),
    }
}

fn require_dense(model: &CompiledCausalModel, var: VariableId) -> Result<DenseNodeId, ModelError> {
    model.dense_of(var).ok_or_else(|| ModelError::Shape {
        message: format!("variable {var} not in compiled model"),
    })
}

/// Shared immutable model plus overlay (no model clone).
#[derive(Clone, Debug)]
pub struct ModelView<'a> {
    /// Borrowed compiled plan.
    pub model: &'a CompiledCausalModel,
    /// Intervention overlay.
    pub overlay: Arc<InterventionOverlay>,
}

impl<'a> ModelView<'a> {
    /// Observational view.
    #[must_use]
    pub fn observational(model: &'a CompiledCausalModel) -> Self {
        Self { model, overlay: Arc::new(InterventionOverlay::observational(model.n_nodes())) }
    }

    /// Interventional view.
    #[must_use]
    pub fn with_overlay(model: &'a CompiledCausalModel, overlay: InterventionOverlay) -> Self {
        Self { model, overlay: Arc::new(overlay) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use antecedent_core::{
        DynamicRuleId, Intervention, InterventionSequence, SequencedIntervention, TemporalPolicy,
        Value, VariableId,
    };
    use antecedent_graph::Dag;

    #[test]
    fn hard_set_overlay() {
        let g = Dag::with_variables(2);
        let model = CompiledCausalModel::compile(g).unwrap();
        let t = VariableId::from_raw(0);
        let overlay = InterventionOverlay::from_interventions(
            &model,
            &[Intervention::set(t, Value::f64(1.0))],
        )
        .unwrap();
        assert_eq!(overlay.hard_set[0], Some(1.0));
        assert!(overlay.hard_set[1].is_none());
    }

    #[test]
    fn shift_overlay_accumulates_and_is_independent_of_hard_set() {
        let g = Dag::with_variables(2);
        let model = CompiledCausalModel::compile(g).unwrap();
        let x = VariableId::from_raw(0);
        let y = VariableId::from_raw(1);
        let overlay = InterventionOverlay::from_interventions(
            &model,
            &[Intervention::shift(x, Value::f64(1.5)), Intervention::shift(x, Value::f64(0.5))],
        )
        .unwrap();
        // Multiple shifts on the same variable accumulate additively.
        assert!((overlay.shifts[0] - 2.0).abs() < 1e-12);
        assert!(overlay.hard_set[0].is_none());
        // An unrelated variable is untouched.
        assert!(overlay.shifts[1].abs() < 1e-12);
        assert!(overlay.hard_set[y.as_usize()].is_none());
    }

    /// Order-independence matters: `apply_intervention` writes `hard_set` and `shifts`
    /// into separate arrays, so neither one "sees" the other as it lands.
    #[test]
    fn set_and_shift_on_same_variable_rejected_in_either_order() {
        let g = Dag::with_variables(2);
        let model = CompiledCausalModel::compile(g).unwrap();
        let x = VariableId::from_raw(0);

        for ivs in [
            vec![Intervention::set(x, Value::f64(1.0)), Intervention::shift(x, Value::f64(0.5))],
            vec![Intervention::shift(x, Value::f64(0.5)), Intervention::set(x, Value::f64(1.0))],
        ] {
            let err = InterventionOverlay::from_interventions(&model, &ivs).unwrap_err();
            assert!(
                matches!(&err, ModelError::Unsupported { message }
                    if message.contains("hard set") && message.contains("additive shift")),
                "unexpected error: {err}"
            );
        }
    }

    /// `Intervention::Soft` with the `additive_shift` family folds into `shifts`, so it
    /// collides with a hard set exactly like `Intervention::Shift` does.
    #[test]
    fn additive_shift_soft_override_collides_with_hard_set() {
        let g = Dag::with_variables(1);
        let model = CompiledCausalModel::compile(g).unwrap();
        let x = VariableId::from_raw(0);
        let err = InterventionOverlay::from_interventions(
            &model,
            &[
                Intervention::set(x, Value::f64(2.0)),
                Intervention::soft(x, MechanismOverride::additive_shift(0.25)),
            ],
        )
        .unwrap_err();
        assert!(matches!(err, ModelError::Unsupported { .. }), "unexpected error: {err}");
    }

    /// A zero shift is not a shift: it leaves the assignment untouched, so pairing it
    /// with a set discards nothing and stays legal.
    #[test]
    fn zero_shift_alongside_hard_set_is_allowed() {
        let g = Dag::with_variables(1);
        let model = CompiledCausalModel::compile(g).unwrap();
        let x = VariableId::from_raw(0);
        let overlay = InterventionOverlay::from_interventions(
            &model,
            &[Intervention::shift(x, Value::f64(0.0)), Intervention::set(x, Value::f64(3.0))],
        )
        .unwrap();
        assert_eq!(overlay.hard_set[0], Some(3.0));
    }

    /// Shifts on a *different* variable never conflict — the check is per node, not global.
    #[test]
    fn set_and_shift_on_different_variables_compose() {
        let g = Dag::with_variables(2);
        let model = CompiledCausalModel::compile(g).unwrap();
        let x = VariableId::from_raw(0);
        let y = VariableId::from_raw(1);
        let overlay = InterventionOverlay::from_interventions(
            &model,
            &[Intervention::set(x, Value::f64(1.0)), Intervention::shift(y, Value::f64(0.5))],
        )
        .unwrap();
        assert_eq!(overlay.hard_set[0], Some(1.0));
        assert!((overlay.shifts[1] - 0.5).abs() < 1e-12);
    }

    /// Sequences compose only the steps active at `t`, so a set and a shift scheduled on
    /// disjoint steps are legal at every `t` even though they name the same variable.
    #[test]
    fn sequence_set_and_shift_conflict_only_when_steps_overlap() {
        let g = Dag::with_variables(1);
        let model = CompiledCausalModel::compile(g).unwrap();
        let x = VariableId::from_raw(0);

        let disjoint = InterventionSequence::new(vec![
            SequencedIntervention::new(
                Intervention::set(x, Value::f64(1.0)),
                TemporalPolicy::dynamic(DynamicRuleId::from_raw(0), [0]),
            ),
            SequencedIntervention::new(
                Intervention::shift(x, Value::f64(0.5)),
                TemporalPolicy::dynamic(DynamicRuleId::from_raw(1), [1]),
            ),
        ]);
        assert_eq!(
            InterventionOverlay::from_sequence_at(&model, &disjoint, 0).unwrap().hard_set[0],
            Some(1.0)
        );
        let at_one = InterventionOverlay::from_sequence_at(&model, &disjoint, 1).unwrap();
        assert!(at_one.hard_set[0].is_none());
        assert!((at_one.shifts[0] - 0.5).abs() < 1e-12);

        let overlapping = InterventionSequence::new(vec![
            SequencedIntervention::new(
                Intervention::set(x, Value::f64(1.0)),
                TemporalPolicy::dynamic(DynamicRuleId::from_raw(0), [0]),
            ),
            SequencedIntervention::new(
                Intervention::shift(x, Value::f64(0.5)),
                TemporalPolicy::dynamic(DynamicRuleId::from_raw(1), [0]),
            ),
        ]);
        assert!(InterventionOverlay::from_sequence_at(&model, &overlapping, 0).is_err());
    }

    /// The fields are public, so an overlay can be built without going through
    /// `from_interventions`. `validate` is what such a caller has to reach for.
    #[test]
    fn hand_built_overlay_validates() {
        let mut overlay = InterventionOverlay::observational(2);
        assert!(overlay.validate().is_ok());
        overlay.hard_set[0] = Some(1.0);
        assert!(overlay.validate().is_ok());
        overlay.shifts[0] = 0.5;
        let err = overlay.validate().unwrap_err();
        assert!(
            matches!(&err, ModelError::Unsupported { message } if message.contains("dense node 0")),
            "error should name the offending node: {err}"
        );
    }

    #[test]
    fn dynamic_policy_sequence_activates_on_schedule() {
        let g = Dag::with_variables(1);
        let model = CompiledCausalModel::compile(g).unwrap();
        let t = VariableId::from_raw(0);
        let seq = InterventionSequence::new(vec![SequencedIntervention::new(
            Intervention::set(t, Value::f64(1.0)),
            TemporalPolicy::dynamic(DynamicRuleId::from_raw(0), [0, 2]),
        )]);
        let overlay = InterventionOverlay::from_sequence_at(&model, &seq, 0).unwrap();
        assert_eq!(overlay.hard_set[0], Some(1.0));
        let idle = InterventionOverlay::from_sequence_at(&model, &seq, 1).unwrap();
        assert!(idle.hard_set[0].is_none());
        let again = InterventionOverlay::from_sequence_at(&model, &seq, 2).unwrap();
        assert_eq!(again.hard_set[0], Some(1.0));
    }
}