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
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! # Causal Attribution
//!
//! This module enables an agent to answer *why* an outcome occurred by running
//! counterfactual interventions on a [`lmm::causal::CausalGraph`] and
//! measuring how much each parent variable is responsible.
//!
//! ## Design
//!
//! Rather than correlating features with outcomes, [`CausalAttributor`] performs
//! the Pearl *do*-calculus intervention `do(parent = 0)` for each parent of the
//! query variable and computes the resulting change in the query variable's value.
//! The absolute intervention effect is normalised so all attribution weights sum
//! to `1.0`, making the result directly interpretable as "fraction of causation."
//!
//! ## See Also
//!
//! - [Pearl, J. (2009). Causality. Cambridge University Press.](https://doi.org/10.1017/CBO9780511803161)
//! - [Shapley value - Wikipedia](https://en.wikipedia.org/wiki/Shapley_value)
use CausalGraph;
use Result;
/// The fraction of the outcome attributable to each parent variable.
///
/// Weights are non-negative and normalised to sum to `1.0`.
/// The list is sorted by descending weight (most responsible variable first).
///
/// # Examples
///
/// ```
/// use lmm::causal::CausalGraph;
/// use lmm_agent::cognition::attribution::{CausalAttributor, AttributionReport};
///
/// let mut g = CausalGraph::new();
/// g.add_node("cause_a", Some(1.0));
/// g.add_node("cause_b", Some(0.0));
/// g.add_node("effect", None);
/// g.add_edge("cause_a", "effect", Some(3.0)).unwrap();
/// g.add_edge("cause_b", "effect", Some(1.0)).unwrap();
/// g.forward_pass().unwrap();
///
/// let report = CausalAttributor::attribute(&g, "effect").unwrap();
/// assert!(!report.weights.is_empty());
/// ```
/// Attributes the outcome of a query variable to its causal parents via counterfactuals.
///
/// For each parent `p` of `query_var`, the attributor performs `do(p = 0)` and
/// measures the resulting shift in `query_var`. The magnitude of each shift is
/// then normalised to produce fractional attribution weights.
;
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.