Causal Bayesian networks, local interventions, causal influence diagrams and policy oracles
DefinitionRobustAgentsCausal_BasicObjects of Sections 2.1-2.3 and Appendices A.1-A.3 of Richens and Everitt (2024).
- Chance variables. Finitely many chance variables , ; takes the categorical values . A joint state is . Two joint states agree on if their coordinates in coincide, and a function of depends only on if it is equal at any two states that agree on .
- Graphs (Definition 1). A graph on is given by parent sets (edges for ). is a proper ancestor of if there is a directed path , and the graph is a DAG if no variable is its own proper ancestor.
- Causal Bayesian networks (Definition 1). A family of conditional distributions for the graph assigns to each and joint state a probability vector on the values of that depends on only through the parents. The joint law is the Markov factorisation .
- Interventions (Definitions 2, 3). A local intervention is a family of maps , one per variable ( means no intervention, a constant is a hard intervention). It replaces each conditional by , and . A mixture is a probability vector on the (finitely many) local interventions, with . A soft intervention on a set replaces , , by conditional distributions for new parent sets , provided the modified graph is still acyclic; then .
- Decision task (Definition 4, Assumption 1). The decision takes values in a finite nonempty set ; its parents are the chance variables , and its only child is the utility , whose parents are and the chance variables . The utility is a real function of and the configuration of . The chance variables of are those of together with their proper ancestors.
- Parameters (Appendix A.2). consists of one real number for each variable, configuration of its parents and state ; it is valid if these numbers are nonnegative and sum to at most , and the last state receives . consists of one real number for each decision and configuration of . "Almost all CIDs" refers to Lebesgue measure on the finite-dimensional space of , the graph being fixed.
- Policies and regret (Section 2.2). A policy with input is a stochastic kernel depending on only through . Its expected utility under a joint law is . It has regret at most if for every policy with input , and is optimal if .
- Policy oracles (Definition 5). A policy oracle maps a masking and a mixture of local interventions to a policy. It is -optimal if for every and every mixture it returns a policy with input and regret at most under .
- Domain dependence (Assumption 2). There are joint laws that factorise according to the graph such that no policy (with input ) optimal under is optimal under .
- Context quantities (Appendix C). For a context on , and , with .
Formalization Note The decision has no chance-variable children: the paper's proofs use (Appendix C, proof of Lemma 4), and under Assumption 1 any chance descendant of is outside and does not affect any expected utility. Assumption 1 then holds automatically. A local intervention acts on all variables at once, as in the proofs of Lemma 3 and Theorem 1. Conditional distributions are written as functions of the whole joint state that depend only on the parents.
import Mathlib
/-!
Richens and Everitt, *Robust agents learn causal world models*, ICLR 2024, arXiv:2402.10877v7:
causal Bayesian networks over finitely many categorical chance variables, local, mixed and soft
interventions, single-decision single-utility causal influence diagrams with Assumptions 1 and 2,
policies, regret, policy oracles, and the parameterisation of CIDs used for "almost all CIDs"
(Sections 2.1-2.3 and Appendices A.1-A.3).
-/
open MeasureTheory
noncomputable section
namespace RobustAgentsCausal
variable {ι : Type*} [Fintype ι] [DecidableEq ι] {k : ι → ℕ} {A : Type*} [Fintype A]
/-- Section 2.1 and Appendix A.2: a joint state `c` of the chance variables `C = {C_i}`, the
variable `C_i` taking the categorical values `0, …, k i - 1`. -/
abbrev Cfg (k : ι → ℕ) := (i : ι) → Fin (k i)
/-- The joint states `c` and `c'` agree on the variables in `S`. -/
def AgreeOn (S : Finset ι) (c c' : Cfg k) : Prop := ∀ i ∈ S, c i = c' i
/-- A function of the joint state depends only on the variables in `S`. -/
def DependsOnlyOn {β : Type*} (S : Finset ι) (f : Cfg k → β) : Prop :=
∀ c c' : Cfg k, AgreeOn S c c' → f c = f c'
/-- Appendix A.1: `C_j` is a (proper) ancestor of `C_i` in the graph whose parent sets are `pa`,
i.e. there is a directed path `C_j → ⋯ → C_i` with edges `C_a → C_b` for `a ∈ pa b`. -/
def IsProperAncestor (pa : ι → Finset ι) (j i : ι) : Prop :=
Relation.TransGen (fun a b => a ∈ pa b) j i
/-- Definition 1: the graph with parent sets `pa` is a directed acyclic graph. -/
def IsAcyclic (pa : ι → Finset ι) : Prop := ∀ i, ¬ IsProperAncestor pa i i
/-- Appendix A.1, Figure 5: the chance variables in `Anc_U`. The utility `U` has the decision `D`
and the chance variables `paU` as parents and the decision has the chance variables `paD` as
parents, so these are the chance variables in `paU ∪ paD` and their ancestors. -/
def ancU (pa : ι → Finset ι) (paD paU : Finset ι) : Set ι :=
{j | ∃ i ∈ paU ∪ paD, j = i ∨ IsProperAncestor pa j i}
/-- Definition 1: `P i c` is a conditional probability distribution `P(C_i = · ∣ pa_i)` over the
values of `C_i` that depends on the joint state `c` only through the parents `pa i`. -/
def IsCPDAt (pa : ι → Finset ι) (P : (i : ι) → Cfg k → Fin (k i) → ℝ) (i : ι) : Prop :=
(∀ c v, 0 ≤ P i c v) ∧ (∀ c, ∑ v, P i c v = 1) ∧ DependsOnlyOn (pa i) (P i)
/-- Definition 1: a family of conditional probability distributions for the graph `pa`. -/
def IsCPD (pa : ι → Finset ι) (P : (i : ι) → Cfg k → Fin (k i) → ℝ) : Prop :=
∀ i, IsCPDAt pa P i
/-- Definition 1: the Markov factorisation `P(c) = ∏_i P(c_i ∣ pa_i)`. -/
def jointProb (P : (i : ι) → Cfg k → Fin (k i) → ℝ) (c : Cfg k) : ℝ := ∏ i, P i c (c i)
/-- Definition 2: a local intervention `do(C_i = f_i(c_i))` on every chance variable at once, as used
in the proofs of Lemma 3 and Theorem 1 (`f_i = id` leaves `C_i` untouched, a constant `f_i` is a
hard intervention). -/
abbrev LocalInt (k : ι → ℕ) := (i : ι) → Fin (k i) → Fin (k i)
/-- Definition 2, eq. (1): the conditional distribution after the local intervention `f`,
`P(c_i ∣ pa_i; σ) = ∑_{c_i' : f_i(c_i') = c_i} P(c_i' ∣ pa_i)`. -/
def localCPD (P : (i : ι) → Cfg k → Fin (k i) → ℝ) (f : LocalInt k) (i : ι) (c : Cfg k)
(v : Fin (k i)) : ℝ :=
∑ v' ∈ Finset.univ.filter (fun v' => f i v' = v), P i c v'
/-- Section 2.1: the joint distribution of the chance variables after the local intervention `f`,
`P(c; σ) = ∏_i P(c_i ∣ pa_i; σ)`. -/
def localJoint (P : (i : ι) → Cfg k → Fin (k i) → ℝ) (f : LocalInt k) (c : Cfg k) : ℝ :=
∏ i, localCPD P f i c (c i)
/-- Definition 3: `w` is a mixed intervention `σ* = ∑_f w_f σ_f` over the local interventions
(`w_f ≥ 0`, `∑_f w_f = 1`). -/
def IsMixture (w : LocalInt k → ℝ) : Prop := (∀ f, 0 ≤ w f) ∧ ∑ f, w f = 1
/-- Definition 3: `P(c ∣ σ*) = ∑_f w_f P(c ∣ σ_f)`. -/
def mixJoint (P : (i : ι) → Cfg k → Fin (k i) → ℝ) (w : LocalInt k → ℝ) (c : Cfg k) : ℝ :=
∑ f, w f * localJoint P f c
/-- Section 2.1: a soft intervention on the chance variables in `T`, replacing the conditional
distribution of each `C_i`, `i ∈ T`, by `newCPD i`, which depends on a new parent set `newPa i`. -/
structure SoftInt (k : ι → ℕ) where
/-- The intervened variables. -/
T : Finset ι
/-- The new parent sets `Pa*_i`. -/
newPa : ι → Finset ι
/-- The new conditional distributions `P'(C_i ∣ Pa*_i)`. -/
newCPD : (i : ι) → Cfg k → Fin (k i) → ℝ
/-- Section 2.1: the parent sets of the graph after the soft intervention `σ`. -/
def SoftInt.modPa (σ : SoftInt k) (pa : ι → Finset ι) : ι → Finset ι :=
fun i => if i ∈ σ.T then σ.newPa i else pa i
/-- Section 2.1: `σ` is a soft intervention for the graph `pa`: its new conditional distributions
are conditional distributions for the new parent sets, and no cycle is introduced. -/
def SoftInt.IsCompatible (σ : SoftInt k) (pa : ι → Finset ι) : Prop :=
(∀ i ∈ σ.T, IsCPDAt σ.newPa σ.newCPD i) ∧ IsAcyclic (σ.modPa pa)
/-- Section 2.1: `P(c; σ) = ∏_{i ∈ T} P'(c_i ∣ pa*_i) ∏_{i ∉ T} P(c_i ∣ pa_i)`. -/
def softJoint (P : (i : ι) → Cfg k → Fin (k i) → ℝ) (σ : SoftInt k) (c : Cfg k) : ℝ :=
∏ i, if i ∈ σ.T then σ.newCPD i c (c i) else P i c (c i)
/-- Appendix A.2: a configuration `pa_i` of the parents of `C_i`. -/
abbrev PaCfg (k : ι → ℕ) (pa : ι → Finset ι) (i : ι) := (j : ↥(pa i)) → Fin (k j)
/-- Appendix A.2: the parameters `θ_P = {P(c_i ∣ pa_i) : c_i ∈ {0, …, k_i - 2}}` of the joint
distribution, one real number per variable, parent configuration and non-final state. -/
abbrev CPDParam (k : ι → ℕ) (pa : ι → Finset ι) :=
((i : ι) × (PaCfg k pa i × Fin (k i - 1))) → ℝ
/-- Appendix A.2: the parameters `θ_P` define conditional distributions: they are nonnegative and
sum to at most `1` for each variable and parent configuration. -/
def ValidParam (pa : ι → Finset ι) (θ : CPDParam k pa) : Prop :=
∀ i (p : PaCfg k pa i),
(∀ v, 0 ≤ θ ⟨i, (p, v)⟩) ∧ ∑ v : Fin (k i - 1), θ ⟨i, (p, v)⟩ ≤ 1
/-- Appendix A.2: the conditional distributions defined by `θ_P`; the last state receives
`P(C_i = k_i - 1 ∣ pa_i) = 1 - ∑_{c_i ≤ k_i - 2} P(c_i ∣ pa_i)`. -/
def cpdOf (pa : ι → Finset ι) (θ : CPDParam k pa) (i : ι) (c : Cfg k) (v : Fin (k i)) : ℝ :=
if h : (v : ℕ) + 1 < k i then θ ⟨i, (fun j => c j.1, ⟨v, by omega⟩)⟩
else 1 - ∑ v' : Fin (k i - 1), θ ⟨i, (fun j => c j.1, v')⟩
/-- Appendix A.2: the parameters `θ_U = {U(d, x)}` of the utility, one real number per decision `d`
and configuration `x` of the chance parents `paU` of `U`. -/
abbrev UParam (k : ι → ℕ) (A : Type*) (paU : Finset ι) := A → ((j : ↥paU) → Fin (k j)) → ℝ
/-- Definition 4: the utility `U(d, x)` as a function of the decision and the joint state. -/
def utilOf (paU : Finset ι) (uθ : UParam k A paU) (d : A) (c : Cfg k) : ℝ :=
uθ d (fun j => c j.1)
/-- Section 2.2: a (stochastic) policy `π(d ∣ pa_D)` whose input is the chance variables in `S`. -/
def IsPolicy (S : Finset ι) (π : Cfg k → A → ℝ) : Prop :=
(∀ c d, 0 ≤ π c d) ∧ (∀ c, ∑ d, π c d = 1) ∧ DependsOnlyOn S π
/-- Section 2.2: the expected utility `E^π[U] = ∑_c P(c) ∑_d π(d ∣ pa_D) U(d, c)` of a policy when the
chance variables have joint distribution `μ` (the decision has no chance descendants). -/
def expUtil (μ : Cfg k → ℝ) (u : A → Cfg k → ℝ) (π : Cfg k → A → ℝ) : ℝ :=
∑ c, μ c * ∑ d, π c d * u d c
/-- Section 2.2 and Definition 5: `π` has regret at most `δ`, `E^π[U] ≥ E^{π*}[U] - δ`, among the
policies with input `S`. -/
def IsDeltaOptimal (δ : ℝ) (S : Finset ι) (μ : Cfg k → ℝ) (u : A → Cfg k → ℝ)
(π : Cfg k → A → ℝ) : Prop :=
IsPolicy S π ∧ ∀ π', IsPolicy S π' → expUtil μ u π' ≤ expUtil μ u π + δ
/-- Section 2.2: `π` is an optimal policy among the policies with input `S`. -/
def IsOptimal (S : Finset ι) (μ : Cfg k → ℝ) (u : A → Cfg k → ℝ) (π : Cfg k → A → ℝ) : Prop :=
IsDeltaOptimal 0 S μ u π
/-- Definition 5: a policy oracle on the domains `Σ` (mixtures of local interventions together with
a masking `Pa_D' ⊆ Pa_D` of the policy's inputs), returning the policy `π_σ(d ∣ pa_D')`. -/
abbrev Oracle (k : ι → ℕ) (A : Type*) := Finset ι → (LocalInt k → ℝ) → Cfg k → A → ℝ
/-- Definition 5: the policy oracle is `δ`-optimal for the CID with conditional distributions `P`,
utility `u` and decision parents `paD`: for every masking `S ⊆ paD` and mixture `w` of local
interventions it returns a policy with input `S` and regret at most `δ` in `M(σ)`. -/
def IsDeltaOptimalOracle (δ : ℝ) (paD : Finset ι) (P : (i : ι) → Cfg k → Fin (k i) → ℝ)
(u : A → Cfg k → ℝ) (O : Oracle k A) : Prop :=
∀ S ⊆ paD, ∀ w : LocalInt k → ℝ, IsMixture w → IsDeltaOptimal δ S (mixJoint P w) u (O S w)
/-- Definition 5: an optimal policy oracle (`δ = 0`). -/
def IsOptimalOracle (paD : Finset ι) (P : (i : ι) → Cfg k → Fin (k i) → ℝ)
(u : A → Cfg k → ℝ) (O : Oracle k A) : Prop :=
IsDeltaOptimalOracle 0 paD P u O
/-- Assumption 2 (domain dependence): there are joint distributions `P`, `P'` compatible with the
graph `pa` such that no policy optimal under `P` is optimal under `P'`. -/
def DomainDependent (pa : ι → Finset ι) (paD : Finset ι) (u : A → Cfg k → ℝ) : Prop :=
∃ P P' : (i : ι) → Cfg k → Fin (k i) → ℝ, IsCPD pa P ∧ IsCPD pa P' ∧
∀ π, IsOptimal paD (jointProb P) u π → ¬ IsOptimal paD (jointProb P') u π
open Classical in
/-- Appendix C: the probability `P(Pa_D' = pa_D'; σ) = ∑_z P(C = c; σ)` of the context `c₀` on the
variables `S`, `Z = C ∖ S`, under the joint distribution `μ`. -/
def ctxProb (μ : Cfg k → ℝ) (S : Finset ι) (c₀ : Cfg k) : ℝ :=
∑ c ∈ Finset.univ.filter (fun c => AgreeOn S c₀ c), μ c
open Classical in
/-- Appendix C, eqs. (8)-(9): the unnormalised conditional expected utility
`∑_z P(C = c; σ) U(d, c) = P(pa_D'; σ) E[u ∣ pa_D', do(D = d); σ]` of decision `d` in the context
`c₀` on the variables `S`, `Z = C ∖ S`. -/
def ctxUtil (μ : Cfg k → ℝ) (u : A → Cfg k → ℝ) (S : Finset ι) (c₀ : Cfg k) (d : A) : ℝ :=
∑ c ∈ Finset.univ.filter (fun c => AgreeOn S c₀ c), μ c * u d c
end RobustAgentsCausal
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Common setting. Throughout, is a finite index set with decidable equality, whose elements index the chance variables. assigns to each a number of states . is a finite set of decisions. Neither nor is required to be nonempty, and any may be . For write , so .
Joint state (Cfg). A joint state is a function assigning to each a value . Write
for the set of all joint states. If some , then is empty. In that case every statement below of the form "for all joint states " holds vacuously, and every sum over equals .
Agreement (AgreeOn). For a finite set and : and agree on when for every . If , every pair agrees.
Dependence (DependsOnlyOn). A function , into an arbitrary type , depends only on when for all that agree on . For this means is constant.
Proper ancestor (IsProperAncestor). Let be an arbitrary assignment of parent sets. Then is a proper ancestor of when there is a chain
with for every . In other words, it is the transitive (not reflexive) closure of "is a parent of". If , then is a proper ancestor of itself.
Acyclicity (IsAcyclic). is acyclic when no is a proper ancestor of itself. This excludes self-loops as well as longer cycles.
Ancestor set (ancU). Given and finite sets ,
Only indices in appear. No decision or utility node exists as an element of .
Conditional distribution at one variable (IsCPDAt). A family assigns to each , each and each a real number . It is a conditional distribution at for when all three of the following hold:
- for all and ;
- for every ;
- the map depends only on .
Acyclicity of is not required. If , the distribution of may depend on itself.
Conditional distributions for a graph (IsCPD). is a family of conditional distributions for when the previous condition holds at every . Again need not be acyclic.
Markov product (jointProb). For any (no conditions imposed) and ,
This is just the product. Nothing asserts that it is nonnegative or sums to . If , it equals .
Local intervention (LocalInt). A local intervention is a choice, for every simultaneously, of an arbitrary function . The set of all of them is finite.
Intervened conditional (localCPD). For any , any local intervention , any , , and :
An empty sum is .
Intervened joint (localJoint).
Mixture weights (IsMixture). A function from local interventions to is a mixture when for every local intervention and , the sum running over all local interventions. This is satisfiable exactly when at least one local intervention exists, which is always the case: if some , the empty function still counts.
Mixed joint (mixJoint). For any and any (not required to be a mixture),
Soft intervention (SoftInt). A soft intervention consists of three unconstrained pieces of data:
- a finite set ;
- new parent sets for every ;
- real numbers for every , , .
The values of and at are carried along but never used below.
Modified parents (SoftInt.modPa). The parent sets after are if , and otherwise.
Compatibility (SoftInt.IsCompatible). is compatible with when two conditions hold. First, for each , is a conditional distribution at for , in the sense of IsCPDAt. Second, the modified parent assignment is acyclic. Acyclicity of the original is not required.
Soft joint (softJoint). For any and ,
Parent configuration (PaCfg). A parent configuration of is a function assigning to each a value .
CPD parameters (CPDParam). A parameter assigns a real number to each triple consisting of:
- ;
- a parent configuration of ;
- , where is truncated subtraction in .
So ranges over , and over the empty set when .
Valid parameters (ValidParam). is valid when, for every and every parent configuration of , both of the following hold:
- for all ;
- .
Distributions from parameters (cpdOf). For any (not required valid), , , and :
Here is the restriction of to .
Utility parameters (UParam). For a finite , a utility parameter assigns an arbitrary real number to each decision and each assignment of values to the .
Utility of a joint state (utilOf). .
Policy (IsPolicy). A function , written , is a policy with input when all three of the following hold:
- for all and ;
- for every ;
- depends only on .
If and , no policy exists. If , every function is a policy.
Expected utility (expUtil). For arbitrary real functions on and on , and any :
Nothing requires to be a probability distribution.
-optimality (IsDeltaOptimal). For , is -optimal for when both of the following hold:
- is a policy with input ;
- every policy with input satisfies .
Taking shows that this is unsatisfiable when .
Optimality (IsOptimal). is optimal when it is -optimal.
Oracle (Oracle). An oracle is an arbitrary function that takes a finite set and a weight function on local interventions, and returns a function .
-optimal oracle (IsDeltaOptimalOracle). Let be real, finite, and , arbitrary. The oracle is -optimal when, for every and every mixture , is -optimal for input , joint function (the mixed joint) and utility .
Several things are not required:
- need not be a family of conditional distributions;
- is not tied to any graph;
- the interventions range over all local interventions on all of , with no restriction to .
Optimal oracle (IsOptimalOracle). The oracle is optimal when it is -optimal with .
Domain dependence (DomainDependent). Given , a finite , and , the setting is domain dependent when there exist and , both families of conditional distributions for (with not required acyclic), such that every policy that is optimal for input under the Markov product of is not optimal for input under the Markov product of .
Two edge cases follow:
- If and , no optimal policy exists. The condition then holds vacuously, provided suitable and exist.
- If , every function is an optimal policy under both. The condition then fails.
Context probability (ctxProb). For any , finite , and :
Context utility (ctxUtil). For any , , finite , , and :
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.