EVIE Preprint 1–18
DeepSWIP: Quotient-WMC Counterfactuals for Neural Probabilistic Logic Programs Saimun Habib
University of Edinburgh
Vaishak Belle
University of Edinburgh
arXiv:2606.20526v1 [cs.AI] 18 Jun 2026
Fengxiang He
University of Edinburgh
Abstract Neurosymbolic systems such as DeepProbLog combine neural perception with probabilistic logic, but standard inference is associational. Counterfactual reasoning additionally requires a causal semantics for interventions and evidence. We introduce DeepSWIP, a single-world counterfactual semantics for DeepProbLog programs. Using neural materialization, we reduce fixed-context neural predicates to ordinary ProbLog choices, apply Single World Intervention Programs (SWIPs), and compute counterfactuals by weighted model counting (WMC) over a single transformed program. Under finite grounding and unique-supportedmodel assumptions, DeepSWIP is exact relative to the learned materialized FCM. The standard quotient-WMC form of ProbLog conditionals identifies active neural probabilities and explains intervention cleaning, calibration sensitivity, and rare-evidence instability. Experiments on MPI3D confirm the transformation against a DeepTwin construction against 12,000 queries, as predicted and a 2.14× inference speedup from avoiding the Twin’s endogenous duplication. A SUMO HOV experiment shows that neural calibration degradation biases plug-in estimates, while a correctly scoped randomized-policy AIPW estimator removes most first-order bias for population mean and ATE estimands. Code is at https://github.com/saibib/deep_SWIP
1. Introduction In DeepProbLog, Scallop, and related probabilistic logic programming (PLP) systems, neural modules map raw inputs to distributions over symbolic atoms, and symbolic inference composes these uncertain atoms through structured logical rules (Manhaeve et al., 2018; Li et al., 2023). This architecture is effective for prediction under perceptual uncertainty, but prediction is not intervention. A model that answers P(Y | X = x) cannot answer what would have happened under do(X = x), nor what would have happened under do(X = x) given contradictory factual evidence. Counterfactual reasoning is central to explanation, robustness, fairness, and decision making (Pearl, 2009; Halpern, 2016). In PLPs, counterfactuals have been given causal semantics by translating programs into functional causal models (FCMs) and evaluating counterfactual queries through Twin Networks (Kiesel et al., 2023). The Twin construction is semantically convenient. It duplicates the endogenous structure into factual and counterfactual copies while sharing exogenous choices. However, duplication is expensive for relational programs © S. Habib.
Habib
and awkward for neural predicates Habib et al. (2026). In a visual DeepProbLog program, should the convolutional predicate be evaluated twice, once per world, or shared as an exogenous symbolic uncertainty? More importantly, duplication obscures the single-world semantics of interventions that motivates Single World Intervention Graphs (SWIGs) and creates unnecessary (and unverifiable) independence assumptions (Richardson and Robins, 2013, 2023; Habib et al., 2026). The SWIP methodology of Habib et al. (2026) translates SWIG-style intervention surgery into ProbLog program rewriting. The methodology defines causal semantics for removing clauses defining intervened atoms, redirecting downstream uses to fixed intervention atoms, and querying the resulting single-world program to produce programs equivalent to the interventional and counterfactual distribution specified by the FCM. DeepSWIP extends this idea to DeepProbLog. Once neural predicates are materialized into probabilistic choices, counterfactual inference becomes a quotient of weighted model counts, Q(p) = P(Yx = y | E = e) =
ΦxY,E (p) ΦxE (p)
,
(1)
where p are materialized neural probabilities and Φ are multilinear WMC polynomials. This algebraic representation is the paper’s organizing principle and explains why intervened mechanisms have zero derivative, why calibration error rather than classification accuracy is the relevant neural failure mode, and why rare evidence causes structural instability through the denominator ΦxE . Empirically, we evaluate DeepSWIP on two complementary settings. MPI3D tests the full neurosymbolic pipeline with neural predicates, symbolic rules, exact factor-level counterfactual ground truth, and a Twin-style comparison. The SUMO traffic experiments tests the statistical side of the framework with imperfect neural traffic-state estimates, rare-evidence amplification, and the distinction between individual plug-in counterfactual prediction and population-level AIPW/DML estimation. We make four contributions: (i) neural materialization for fixed-context DeepProbLog counterfactuals; (ii) a correctness result lifting SWIP to the learned materialized FCM; (iii) a quotient-WMC analysis of active neural predicates, intervention cleaning, calibration sensitivity, and rare-evidence instability; and (iv) MPI3D and SUMO experiments separating exact symbolic inference from statistical error in learned predicates.
2. Background and Related Work Probabilistic logic and counterfactuals. A ground ProbLog program consists of probabilistic facts and deterministic rules. Under distribution semantics, probabilistic facts define independent exogenous choices and the logic program maps each exogenous assignment to a unique symbolic world, provided the program has unique supported models (De Raedt et al., 2007). Kiesel et al. (2023) show how to interpret such programs as FCMs and answer counterfactual queries by a Twin Network transformation, proving consistency with Pearl-style counterfactuals and CP-logic (Vennekens et al., 2009). Habib et al. (2026) instead proposes single-world intervention programs, following the SWIG idea that an intervention splits a node into a random factual part and a fixed intervention part (Richardson and 2
DeepSWIP
Robins, 2013). Our work assumes these ordinary-ProbLog results and asks how they extend when some exogenous symbolic choices are produced by neural predicates. Neural probabilistic logic and statistical correction. DeepProbLog introduces neural predicates of the form nn(m, x, y), where a neural model m maps input x to a distribution over symbolic values y (Manhaeve et al., 2018). Inference is typically reduced to arithmetic circuits or WMC after evaluating neural outputs. Scallop and related systems similarly reduce neural-symbolic inference to provenance circuits or weighted logical inference (Li et al., 2023; Valentin et al., 2025). This circuit view is unified by algebraic model counting, which casts WMC, gradients, and related tasks as semiring evaluations over a shared circuit (Kimmig et al., 2017; Maene and De Raedt, 2025); we use this perspective in Section 5. DeepSWIP is compatible with such solvers but our contribution does not focus on them. Instead, we treat neural predicates as probability estimators inside a causal logic program and this separation lets us analyze how neural nuisance error propagates through symbolic counterfactuals. To create causal estimands with desirable properties, we implement Double/debiased machine learning (DML) (Chernozhukov et al., 2018; Robins et al., 1994). We do not claim universal DML for arbitrary logic queries but we prove a plug-in sensitivity result for general DeepSWIP queries and use AIPW only in experiments where the target is a randomized binary-treatment population estimand.
3. Preliminaries Let P = (F, R, N ) be a finite ground DeepProbLog program, where F contains ordinary probabilistic choices, R deterministic rules, and N neural predicates. Let B be the finite Herbrand base induced by the query, evidence, intervention, and neural contexts under consideration. We write E ⊆ B for the external atoms; ordinary probabilistic choices from F together with the categorical choices obtained by neural-materializing predicates in N . The internal atoms are I = B \ E and are defined by the rules in R. For an assignment u ∈ {0, 1}E of external atoms, write SMR (u) = {v ∈ {0, 1}I : u ∪ v is a supported model of R}. For an intervention X = x, let Rx denote the rule set obtained by deleting all rules whose head is in X and adding the corresponding constant assignments X := x. We assume for the query, evidence, intervention, and neural contexts under consideration, the grounding of P is finite. Moreover, each neural predicate k ∈ N has a finite output domain Yk = {yk,1 , . . . , yk,rk }, and for fixed parameters ηk and context ck returns a categorical distribution rk X pk,j (ηk , ck ) = Pηk (Yk = yk,j | ck ), pk,j (ηk , ck ) = 1. (2) j=1
We write p(η, c) for all ordinary and neural-materialized weights. We also require PMη,c (E = e) > 0 for every conditional query P(Yx = y | E = e), so that the WMC quotient is defined. The semantics of P define a probability distribution over the possible worlds of the assignments of probabilistic facts and neural-materialized choices as mutually independent 3
Habib
external choices. Thus, for fixed (η, c), every assignment u ∈ {0, 1}E has probability Y Y pa (η, c) 1 − pa (η, c) , Pη,c (u) = a∈u
a∈E\u
with annotated-disjunction choices interpreted as one-hot categorical choices. Finally, we assume unique supported models before and after intervention: |SMR (u)| = 1,
|SMRx (u)| = 1
for all u ∈ {0, 1}E .
Equivalently, the materialized program induces a well-defined FCM. Acyclic rule dependency is sufficient but not necessary.
4. The DeepSWIP Transformation The purpose of DeepSWIP is to separate neural perception from symbolic counterfactual inference. A DeepProbLog neural predicate is not intervened on as a neural network, rather, for fixed parameters and fixed input context, it induces a finite distribution over symbolic values. We therefore freeze the neural computation and expose only the induced symbolic uncertainty to the logic program. This turns a neural probabilistic logic program into an ordinary ProbLog program, after which standard causal program transformations can be applied. Definition 1 (Neural materialization) Fix neural parameters η and contexts c. The materialized program Pmat (η, c) is the ordinary ProbLog program obtained by replacing each neural predicate nn(mk , ck , Yk ) by the categorical probabilistic choice pk,1 :: ak (yk,1 ); · · · ; pk,rk :: ak (yk,rk ), where pk,j = pk,j (ηk , ck ). The deterministic rule set R is unchanged. Theorem 2 (Materialization equivalence) Fix η and c. For every ground formula φ over the symbolic atoms of P, PP (φ | η, c) = PPmat (η,c) (φ). Materialization gives the object on which intervention surgery is well-defined. The intervention itself is a program transformation that removes the mechanisms defining intervened atoms, redirects downstream rules to fixed intervention atoms, and leaves all nonintervened mechanisms unchanged. Thus, DeepSWIP inherits the single-world interpretation of SWIGs while retaining the ordinary distribution semantics of the materialized ProbLog program. Theorem 3 (DeepSWIP correctness against FCM) Assume Pmat (η, c) has unique supported models, and let Mη,c be its induced FCM. Let Sx (Pmat ) be the SWIP transformation under do(X = x). For any query Y = y and evidence E = e with PMη,c (E = e) > 0, PDeepSW IP (Yx = y | E = e; η, c) = PMη,c (Yx = y | E = e). 4
DeepSWIP
Algorithm 1 DeepSWIP transformation Input: Ground DeepProbLog program P = (F, R, N ); neural parameters η; contexts c; intervention do(X = x); evidence E = e; query Y = y Output: Counterfactual probability P (Yx = y | E = e; η, c) Pmat ← F ∪ R foreach nn(mk , ck , Yk ) ∈ N do Evaluate mk (ck ) and obtain pk,j = Pηk (Yk = yk,j | ck ) for j = 1, . . . , rk Replace nn(mk , ck , Yk ) in Pmat by pk,1 :: ak (yk,1 ); · · · ; pk,rk :: ak (yk,rk ) end Sx ← probabilistic choices of Pmat foreach clause C = (h ← b1 , . . . , bℓ ) in R do if h defines an intervened atom in X then discard C else replace each body occurrence of Xi by Xi,fix (xi ) add the rewritten clause to Sx end end foreach intervention assignment Xi = xi do add the deterministic fixed assertion Xi,fix (xi ) to Sx end Let ∆x be the Boolean theory induced by the transformed program Φx Y,E (p) ← WMC(∆x ∧ Y = y ∧ E = e; p) x x Φx E (p) ← WMC(∆x ∧ E = e; p) return ΦY,E (p)/ΦE (p)
The theorem is conditional on the learned/materialized FCM. It says that symbolic counterfactual inference is exact given the materialized neural probabilities. It does not say that η is the true data-generating parameter. Algorithm 1 details the implementation of the program transformation and counterfactual querying. We give a minimal example that illustrates the complete DeepSWIP pipeline. Consider a DeepProbLog traffic-monitoring fragment in which a neural predicate classifies an upstream sensor image into a latent traffic state, and symbolic rules derive congestion and delay: nn(state_net, img42, [free,queued])::traffic_state(S). 0.20::rain. 0.10::roadworks. incident :- rain, roadworks. congested :- traffic_state(queued). congested :- incident. delayed :- congested. reroute :- incident.
For fixed neural parameters and input image img42, neural materialization evaluates the network once and replaces the neural predicate with its softmax output as an annotated disjunction. For example, if the network returns probability 0.30 for free and 0.70 for queued, the materialized program is now an ordinary ProbLog program containing 0.30::traffic state(free); 0.70::traffic state(queued). Now, suppose the factual evidence is that congestion was observed while it was raining, but the counterfactual query asks what the delay would have been under do(congested = false). The counterfactual program is now: 0.30::traffic_state(free); 0.70::traffic_state(queued). 0.20::rain. 0.10::roadworks.
5
Habib
incident :- rain, roadworks. congested_obs :- traffic_state(queued). congested_obs :- incident. 0.0::congested_fix_true. delayed :- congested_fix_true. reroute :- incident. evidence(congested_obs, true). evidence(rain, true). query(delayed).
The causal pathways between traffic state(queued) → congested obs and incident → congested obs remain in the denominator of the WMC quotient. They perform the abduction step by reweighting the exogenous explanations of the factual congestion, including the neural-materialized traffic-state choice. Only the outgoing use of congested in the descendant rule for delayed is redirected to the fixed false intervention. The counterfactual probability is then computed as the corresponding WMC quotient with numerator additionally conjoining delayed and denominator conditioning on congested obs and rain. As desired, the query evaluates to zero because delayed cannot be derived through the intervened congestion mechanism, even though factual evidence says congestion occurred. By contrast, PDeepSWIP (reroute | congested, do(congested = false)) is still governed by reroute :- incident. Thus DeepSWIP does not erase factual causes of the intervened variable, instead it only severs the intervened atom’s defining equations and redirects downstream uses to the fixed value. In general, we can bound how much the materialized program changes. Proposition 4 (Program growth) Let Pmat have m probabilistic choices and |R| deterministic clauses. For an intervention on k ground atoms, Sx (Pmat ) contains at most m + k probabilistic choices if fixed atoms are encoded as degenerate facts, and m otherwise. Its number of clauses is at most |R| + k before pruning. Proposition 4 is a syntactic statement. WMC can still be exponential in circuit size or treewidth.
5. Quotient-WMC Semantics and Consequences The quotient (4) is an instance of algebraic model counting (AMC), which generalizes weighted model counting from the probability semiring to arbitrary commutative semirings, unifying a range of inference tasks as one circuit evaluation (Kimmig et al., 2017). Both ΦxY,E and ΦxE are AMC evaluations over the probability semiring on the single SWIP-transformed circuit ∆x , so the counterfactual conditional is a ratio of two AMC values on the same circuit. This view is more than terminological. The local sensitivities of Theorems 8 and 9 are exactly the quantities computed by AMC over the gradient semiring, the construction used to train neural predicates in DeepProbLog, and recently shown to unify gradient and model-counting computations in a single algebraic framework (Maene and De Raedt, 2025). 6
DeepSWIP
Consequently ∇Q is not only characterized analytically by (6) but is computable by one gradient-semiring pass over ∆x . Intervention cleaning (Theorem 6) is likewise algebraic with SWIP removing the intervened literals from the labeling, so their semiring weights never enter the count, and the active neural set is the support of the labeling on the pruned circuit. We read the results below as a quotient-AMC account of neurosymbolic counterfactuals and the causal SWIP transformation and the counterfactual reading of the quotient are our additions on top of the AMC machinery. Let ∆x be the Boolean theory induced by the SWIP-transformed materialized program. For a world ω over probabilistic choices p1 , . . . , pm , define Y Y X wp (ω) = pi (1 − pi ), WMC(Γ; p) = wp (ω). (3) i:ωi =1
i:ωi =0
ω|=Γ
The previous section establishes that DeepSWIP produces the correct transformed program. After materialization and intervention surgery, neural dependence enters only through the weights p, while the logic determines which assignments satisfy query and evidence. Thus a counterfactual conditional is a quotient of two weighted model counts. This quotient is the central algebraic object of the paper. Theorem 5 (Quotient-polynomial WMC semantics) For any query Y = y and evidence E = e with WMC(∆x ∧ E = e; p) > 0, Q(p) = P∆x (Y = y | E = e) =
ΦxY,E (p) ΦxE (p)
,
(4)
where ΦxY,E (p) = WMC(∆x ∧ Y = y ∧ E = e; p),
ΦxE (p) = WMC(∆x ∧ E = e; p).
Both ΦxY,E and ΦxE are multilinear polynomials in the materialized probabilities. The remaining results are consequences of this quotient representation. The transformation changes which weights appear in the numerator and denominator and this gives a clean lens to view intervention, pruning, calibration, and rare event stability through. Theorem 6 (Intervention cleaning) If a materialized probability pk appears only in clauses defining an intervened atom removed by SWIP, then Q does not depend on pk . Hence ∂Q/∂pk = 0 wherever the derivative is defined. This formalizes the intuition that intervened mechanisms are no longer active sources of uncertainty for the counterfactual query. Neural errors in a mechanism that has been surgically replaced cannot affect the resulting quotient. In effect, we should only consider the active neural set. Proposition 7 (Active neural set) Let Q(p) = ΦxY,E (p)/ΦxE (p) be the DeepSWIP quotient. If a materialized neural probability pk does not occur in either ΦxY,E or ΦxE after SWIP and relevance pruning, then ∂Q/∂pk = 0. 7
Habib
Thus each query induces an active set of neural probabilities and only probabilities that survive intervention surgery and appear in the numerator or denominator can influence the answer. It follows that the query is sensitive to the materialized neural probabilities and the logical rules and we can decompose this sensitivity accordingly Theorem 8 (Local calibration sensitivity) Let p⋆ be ideal materialized probabilities and assume ΦxE (p⋆ ) > 0. For p̂ = p⋆ + ε sufficiently close to p⋆ , Q(p̂) − Q(p⋆ ) = ∇Q(p⋆ )⊤ ε + O(∥ε∥2 ),
(5)
where for each active coordinate x ∂ΦY,E ∂ΦxE ∂Q 1 (p) = x (p) − Q(p) (p) . ∂pk ΦE (p) ∂pk ∂pk
(6)
The leading error term is a product of neural probability error and logical sensitivity. This is why calibration, rather than classification accuracy alone, is the relevant neural property for counterfactual reliability. A classifier may have high accuracy but poor probability calibration and such calibration error can still be amplified by the symbolic quotient. This also reveals a structural instability: when the evidence probability is small, small neural calibration errors may induce large counterfactual errors. Theorem 9 (Rare-evidence instability) Let δ = ΦxE (p) > 0. If |∂ΦxY,E /∂pk | ≤ C1 and |∂ΦxE /∂pk | ≤ C2 in a neighborhood of p, then ∂Q C1 + C 2 (p) ≤ . ∂pk δ Thus local sensitivity can grow as 1/P(E = e) and diverge. Rare-evidence instability is a property of the counterfactual functional itself. It is not caused by a particular neural architecture or optimizer. Better neural training can reduce ∥p̂ − p⋆ ∥, but it cannot remove the denominator ΦxE (p) from the counterfactual query. This motivates the use of double machine learning in the context of neurosymbolic counterfactuals. Proposition 10 (Cross-fitting and scoped DML) Let A be the active set after intervention cleaning and pruning. For any plug-in estimator p̄ of p⋆ , E[Q(p̄) − Q(p⋆ )] = ∇A Q(p⋆ )⊤ E[p̄A − p⋆A ] + O(E∥p̄A − p⋆A ∥2 ). Therefore calibration or cross-fitting can reduce first-order plug-in bias only to the extent that it reduces active probability bias. This is not a universal DML guarantee. AIPW/DML requires a target estimand, valid treatment/propensity structure, and a Neyman-orthogonal score. The proposition separates two claims that should not be conflated. Full proofs are provided in Appendix A. The main point for experiments is that DeepSWIP has two separable error sources: exact symbolic inference relative to p, and statistical/perceptual error in estimating p. 8
DeepSWIP
6. Experiments Our experiments are designed around the theory rather than breadth of applications. MPI3D tests visual neural materialization, the speedup from the single-world transformation, pruning, and intervention cleaning, using DeepSWIP–DeepTwin agreement as an implementation correctness check and factor labels as counterfactual ground truth. SUMO HOV tests calibration-sensitive plug-in error and scoped AIPW correction. 6.1. MPI3D visual counterfactuals Setup. MPI3D-toy is factorized by color, shape, size, camera, background, and pose (Gondal et al., 2019). We train neural predicates for shape, size, and color, materialize and use symbolic rules for can roll, can stack, stable, and risky on shelf. Counterfactuals intervene on shape or size and compare DeepSWIP with a DeepTwin construction over the same materialized program. Factor labels supply ground-truth potential outcomes. Results. With 8,368 training and 2,000 test images, neural predicate accuracies were 0.889 for shape, 0.987 for size, and 1.000 for color. Table 1 reports across 500 evaluation images, six shape interventions, and four queries (12,000 ProbLog calls), DeepSWIP and DeepTwin agreed on 100% of queries. DeepSWIP averaged 3.57 ms/query and DeepTwin 7.65 ms/query, a 2.14× speedup. Ground-truth RMSE was zero for intervention-determined queries and small for queries depending on non-intervened predicates: 0.058 for can stack and 0.082 for risky on shelf. Pruning under do(shape=sphere) reduced statement counts from 18 to 5, 8, 11, and 7 for the four queries, pruning color in all cases. DeepSWIP and DeepTwin are both exact relative to the materialized FCM (Theorem 3), so we use their agreement as a correctness check on the transformation rather than as evidence for the semantics. Across 500 evaluation images, six shape interventions, and four queries (12,000 ProbLog calls) they agreed on 100% of queries, exactly as the equivalence theorem predicts, and any disagreement would have indicated a transformation or implementation bug. The substantive results are the speedup and the propagation of neural error. Avoiding the Twin’s endogenous duplication (Theorem 4) reduced inference from 7.65 to 3.57 ms/query, a 2.14× speedup on this program, consistent with the duplication argument. Because
Table 1: MPI3D visual counterfactuals. Component
Metric
Value
Comparison
Value
Shape predicate Size predicate Colour predicate
Accuracy Accuracy Accuracy
0.889 0.987 1.000
Calibration gap Calibration gap Calibration gap
–0.046 –0.026 –0.001
Agreement DeepSWIP Twin/SWIP
100.0% 3.57 ms 2.14×
Queries DeepTwin
12,000 7.65 ms
RMSE RMSE RMSE RMSE
0.000 0.058 0.082 0.000
Bias Bias Bias Bias
0.000 0.001 –0.003 0.000
DeepSWIP vs DeepTwin Inference time Runtime ratio can roll can stack risky on shelf stable
9
Habib
DeepSWIP is exact, the only error against factor ground truth comes from neural calibration on the active predicates: RMSE was zero for intervention-determined queries (can roll, stable), empirically exhibiting intervention cleaning, and small for queries depending on non-intervened predicates (0.058 for can stack, 0.082 for risky on shelf). Pruning under do(shape=sphere) reduced statement counts from 18 to 5, 8, 11, and 7, removing color in all cases. Neural predicate accuracies were 0.889 (shape), 0.987 (size), and 1.000 (color). 6.2. HOV randomized-policy calibration and DML stress test Setup. The HOV experiment uses paired SUMO simulations of restricted and open-lane policies, yielding potential outcomes Yi (0) and Yi (1) for throughput (Lopez et al., 2018). We construct a randomized-policy observation process with Ti ∼ Bernoulli(0.5) and target ψ1 = E[Y (1)] and τ = E[Y (1) − Y (0)]. A neural traffic-state predicate is trained under increasing sensor noise. The plug-in DeepSWIP nuisance model maps state probabilities to state-conditional throughput means, while AIPW uses known propensity g = 0.5 and fold-specific observed-only nuisance estimates. AIPW is evaluated as a population estimator, not an individual counterfactual RMSE. The HOV experiment uses paired SUMO simulations of restricted and open-lane policies, yielding potential outcomes Yi (0) and Yi (1) for throughput (Lopez et al., 2018). To test valid AIPW/DML, we construct a randomized-policy observation process: Ti ∼ Bernoulli(0.5) and Yi = Ti Yi (1)+(1−Ti )Yi (0). The target estimands are ψ1 = E[Y (1)] and τ = E[Y (1)−Y (0)]. A neural traffic-state predicate is trained under increasing sensor noise. The plug-in DeepSWIP nuisance model maps state probabilities to state-conditional throughput means. AIPW is evaluated as a population estimator, not an individual counterfactual RMSE. Results. The dataset has N = 5000 episodes: 4247 freeflow and 753 congested. The true values are E[Y (1)] = 248.221, E[Y (0)] = 184.893, and τ = 63.329. Figure 1 shows, as sensor noise increases, congested calibration falls from 0.985 to 0.556. Naive plug-in bias worsens for both ψ1 (−4.75 to −9.55) and ATE (−1.24 to −2.57), while cross-fitted plug-in remains biased. AIPW/DML stays near zero bias across noise levels, as does difference-in-means under randomized treatment.
7. Discussion and Limitations DeepSWIP’s exactness is exactness relative to the learned materialized FCM. If a neural predicate is miscalibrated, the symbolic counterfactual can be exactly wrong with respect to the external data-generating process. This is not a defect of SWIP; it is the distinction between causal-symbolic inference and statistical estimation. The quotient derivative in Theorem 8 makes this distinction apparent as only active, non-intervened neural probabilities matter, and their errors are amplified by the logic and by 1/ΦE . The DML result is deliberately scoped. The HOV AIPW experiment is valid because we construct a randomized binary treatment with known propensity and evaluate population means/ATEs. This does not imply that arbitrary DeepSWIP counterfactual queries have an orthogonal score, nor that AIPW estimates individual counterfactual outcomes. For general path-specific, continuous, or relational counterfactuals, deriving the efficient influence function is a separate statistical problem. Similarly, approximate WMC methods may 10
DeepSWIP
Figure 1: HOV randomized-policy calibration and DML stress test. accelerate inference, but approximation error in a quotient requires denominator-aware control; we leave approximate counterfactual WMC to future work. Finally, our MPI3D rules are intentionally simple. The experiment is not a claim about state-of-the-art vision or physics. Its role is to breackdown the NeSy counterfactuals as neural predicates are materialized, symbolic interventions are performed in a single world, and WMC computes the resulting quotient exactly.
8. Conclusion We presented DeepSWIP, a framework for counterfactual reasoning in neural probabilistic logic programs. The method evaluates neural predicates once, materializes them as ordinary probabilistic choices, applies a single-world intervention transformation, and computes the result by WMC. This yields exact counterfactuals relative to the learned materialized FCM and exposes the counterfactual query as a quotient of WMC polynomials. The quotient view provides a common explanation for program growth, pruning, intervention cleaning, calibration sensitivity, rare-evidence instability, and the limited role of DML. Experiments on MPI3D and SUMO support these claims while keeping the statistical scope explicit. DeepSWIP is therefore best understood not as a universal causal discovery method, but as a precise counterfactual inference layer for NeSy programs with explicit causal structure and learned probabilistic mechanisms. 11
Habib
Acknowledgments Funding for this research was provided by NERC through an E4 DTP studentship (NE/S007407/1).
References Victor Chernozhukov, Denis Chetverikov, Mert Demirer, Esther Duflo, Christian Hansen, Whitney Newey, and James Robins. Double/debiased machine learning for treatment and structural parameters. The Econometrics Journal, 21(1):C1–C68, 2018. Luc De Raedt, Angelika Kimmig, and Hannu Toivonen. Problog: A probabilistic prolog and its application in link discovery. Proceedings of the International Joint Conference on Artificial Intelligence, 2007. Muhammad Waleed Gondal, Manuel Wuthrich, Djordje Miladinovic, et al. On the transfer of inductive bias from simulation to the real world: A new disentanglement dataset. In Advances in Neural Information Processing Systems, 2019. Saimun Habib, Vaishak Belle, and Fengxiang He. Efficient counterfactual reasoning in problog via single world intervention programs. arXiv preprint arXiv:2603.20505, 2026. Joseph Y. Halpern. Actual Causality. MIT Press, 2016. Rafael Kiesel, Kilian Ruckschloss, and Felix Weitkamper. “what if?” in probabilistic logic programming. arXiv preprint arXiv:2305.15318, 2023. Angelika Kimmig, Guy Van den Broeck, and Luc De Raedt. Algebraic model counting. Journal of Applied Logic, 22:46–62, 2017. Jiani Li, Ruitao Zhu, Yewen Cheng, et al. Scallop: A language for neurosymbolic programming. In Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation, 2023. Pablo Alvarez Lopez, Michael Behrisch, Laura Bieker-Walz, et al. Microscopic traffic simulation using sumo. In IEEE International Conference on Intelligent Transportation Systems, 2018. Jaron Maene and Luc De Raedt. The gradient of algebraic model counting. In Proceedings of the AAAI Conference on Artificial Intelligence, 2025. arXiv:2502.18406. Robin Manhaeve, Sebastijan Dumančić, Angelika Kimmig, Thomas Demeester, and Luc De Raedt. Deepproblog: Neural probabilistic logic programming. In Advances in Neural Information Processing Systems, 2018. Judea Pearl. Causality: Models, Reasoning, and Inference. Cambridge University Press, 2 edition, 2009. Thomas S. Richardson and James M. Robins. Single world intervention graphs: A unification of the counterfactual and graphical approaches to causality. Working paper, 2013. 12
DeepSWIP
Thomas S. Richardson and James M. Robins. Single world intervention graphs. In Probabilistic and Causal Inference: The Works of Judea Pearl. ACM Books, 2023. James M. Robins, Andrea Rotnitzky, and Lue Ping Zhao. Estimation of regression coefficients when some regressors are not always observed. Journal of the American Statistical Association, 89(427):846–866, 1994. Thomas Jean-Michel Valentin, Luisa Sophie Werner, Pierre Genevès, and Nabil Layaı̈da. On scaling neurosymbolic programming through guided logical inference. In arXiv preprint arXiv:2501.18202, 2025. Joost Vennekens, Marc Denecker, and Maurice Bruynooghe. Cp-logic: A language of causal probabilistic events and its relation to logic programming. Theory and Practice of Logic Programming, 9(3):245–308, 2009.
Appendix A. Proofs A.1. Proof of Theorem 2 Fix η and c. Each neural predicate k induces a categorical distribution over a finite symbolic choice Uk ∈ Yk with probabilities pk,j (ηk , ck ). Materialization replaces this random choice by an ordinary probabilistic choice with exactly the same probabilities. Ordinary probabilistic facts are unchanged. Therefore every complete assignment u to all ordinary and neural-materialized choices has the same probability before and after materialization: PP (U = u | η, c) = PPmat (η,c) (U = u). For a fixed u, the deterministic rules are identical in P and Pmat . By unique supported models, the closure of the rules under u is unique and the same in both programs. Let Ωφ be the set of assignments whose closure satisfies φ. Then PP (φ | η, c) =
X
PP (u | η, c) =
u∈Ωφ
X
PPmat (u) = PPmat (φ).
u∈Ωφ
A.2. Proof of Theorem 3 By Theorem 2, the fixed-parameter DeepProbLog program and Pmat induce the same distribution over symbolic worlds. Since Pmat has unique supported models, it induces an FCM Mη,c whose exogenous variables are the probabilistic choices and whose endogenous structural assignments are the deterministic closures of the rules. The SWIP transformation removes clauses defining X, redirects downstream body literals to fixed atoms, and asserts the intervention values. This is exactly the program-level implementation of replacing the structural assignments for X by constants while leaving other mechanisms unchanged. The ordinary-ProbLog SWIP correctness theorem of Habib et al. (2026), applied to Pmat , gives equality between the probability computed on Sx (Pmat ) and the FCM counterfactual in Mη,c . The condition P(E = e) > 0 ensures the conditional probability is defined. 13
Habib
A.3. Proof of Theorem 4 Materialization yields m probabilistic choices. SWIP does not duplicate any retained rule. For each intervened atom, all clauses defining that atom are removed; all remaining clauses are either unchanged or have body literals renamed to fixed atoms. This operation cannot increase the number of retained original clauses. SWIP then adds at most one fixed assertion per intervened atom. If these assertions are deterministic facts they add no probabilistic choices; if encoded as degenerate probabilistic facts they add at most k. Hence the bounds m+k and |R|+k. A Twin construction creates factual and counterfactual endogenous copies, giving two copies of each endogenous rule before intervention-specific deletions. Exogenous choices may be shared, but endogenous symbolic structure is duplicated. A.4. Proof of Theorem 5 Conditional probability gives P∆x (Y = y | E = e) =
P∆x (Y = y, E = e) . P∆x (E = e)
Under WMC, these probabilities are exactly WMC(∆x ∧ Y = y ∧ E = e; p) and WMC(∆x ∧ E = e; p). For any formula Γ, WMC(Γ; p) =
X Y ω|=Γ i:ωi =1
pi
Y
(1 − pi ).
i:ωi =0
Each summand contains each pi at most once, either as pi or 1 − pi . Expanding yields a multilinear polynomial. Thus both numerator and denominator are multilinear polynomials and the query is their quotient. A.5. Proof of Theorem 6 If pk appears only in clauses defining an intervened atom, those clauses are removed by SWIP. Therefore the transformed theory ∆x contains no dependence of Y or E on the Boolean choice weighted by pk . In the WMC sum, assignments to that variable factor as pk + (1 − pk ) = 1 in both numerator and denominator. Thus neither polynomial depends on pk , and the quotient derivative is zero. A.6. Proof of Theorem 8 By Theorem 5, Q(p) = N (p)/D(p) with N = ΦxY,E and D = ΦxE . Since N and D are polynomials and D(p⋆ ) > 0, Q is differentiable in a neighborhood of p⋆ . Taylor’s theorem gives (5). The quotient rule gives ∂Q D ∂N/∂pk − N ∂D/∂pk 1 ∂N ∂D = = −Q , ∂pk D2 D ∂pk ∂pk which is (6). 14
DeepSWIP
A.7. Proof of Theorem 9 Using (6) and 0 ≤ Q(p) ≤ 1, ∂Q 1 ≤ x ∂pk ΦE (p)
∂ΦxY,E ∂pk
+ |Q(p)|
∂ΦxE ∂pk
≤
C1 + C 2 . δ
Thus the derivative bound diverges as δ → 0. A.8. Proof of Theorem 10 Partition worlds as (ωA , ωĀ ), where A = Ax (Y, E). By definition, variables outside A have no directed path to Y or E in the transformed dependency graph. Under the stated constraint-separation assumption, changing ωĀ cannot affect satisfaction of Y , E, or the retained clauses. Hence X X ΦxY,E (p) = I{ωA |= ∆A ∧ Y ∧ E}w (ω ) wpĀ (ωĀ ). p A x A ωA
ωĀ
The inner sum is one because it sums the probability mass of independent removed choices. x,A x Therefore ΦxY,E (p) = Φx,A Y,E (pA ). The same argument gives ΦE (p) = ΦE (pA ), and taking ⋆ quotients proves the claim. Now, apply Theorem 8 to p̄ = p + ε. Coordinates outside A are absent or have zero derivative by Theorem 6 and the above. Hence Q(p̄) − Q(p⋆ ) = ∇A Q(p⋆ )⊤ (p̄A − p⋆A ) + O(∥p̄A − p⋆A ∥2 ). Taking expectations yields the stated expansion. The final statement follows from the definition of DML: cross-fitting controls nuisance overfitting, but first-order bias is removed only when the estimator is built from a Neyman-orthogonal score for a valid target estimand.
Appendix B. Experimental DeepProbLog Programs This appendix gives the DeepProbLog-style program templates used in the experiments. The actual experiments first evaluate the neural predicates at a fixed input context and then replace them by ordinary probabilistic choices before applying the DeepSWIP transformation. Thus the code below specifies the neural-symbolic model before materialization; each experimental query is then evaluated on the corresponding materialized and SWIP-transformed ProbLog program. B.1. MPI3D visual counterfactual program The MPI3D experiment uses neural predicates for visual factors and deterministic rules for physical predicates. For each image, the neural predicates return distributions over shape, size, and color. Counterfactual interventions are then applied to materialized shape or size atoms. Listing 1: DeepProbLog template for the MPI3D visual counterfactual experiment. % Neural visual predicates. % For a fixed image I, these are evaluated once and materialized.
15
Habib
nn(shape_net, I, Shape, [cone,cube,cylinder,hexagonal,pyramid,sphere]) :: shape(I, Shape). nn(size_net, I, Size, [small,large]) :: size(I, Size). nn(color_net, I, Color, [red,green,blue,white,brown,olive]) :: color(I, Color). % Shape-derived physical properties. rollable(I) :- shape(I, sphere). rollable(I) :- shape(I, cylinder). flat_base(I) :- shape(I, cube). flat_base(I) :- shape(I, cylinder). flat_base(I) :- shape(I, hexagonal). pointed(I) :- shape(I, cone). pointed(I) :- shape(I, pyramid). % Size-derived physical properties. heavy(I) :- size(I, large). light(I) :- size(I, small). % Symbolic task predicates. can_roll(I) :- rollable(I). can_stack(I) :- flat_base(I). can_stack(I) :- light(I), \+ pointed(I). stable(I) :- flat_base(I), heavy(I). stable(I) :- flat_base(I), light(I). risky_on_shelf(I) :- rollable(I). risky_on_shelf(I) :- pointed(I), heavy(I). % Example queries used after materialization and SWIP transformation. query(can_roll(I)). query(can_stack(I)). query(stable(I)). query(risky_on_shelf(I)).
For example, the intervention do(shape(I, sphere)) removes the materialized mechanism defining the shape of image I, redirects downstream uses of shape to the fixed intervention atom, and then evaluates one of the four queries above. The same materialized program is also used to construct the DeepTwin baseline, so that disagreement would indicate an error in the counterfactual transformation rather than in the neural visual classifiers. B.2. SUMO HOV traffic program The SUMO HOV experiment uses a neural traffic-state predicate learned from simulated sensor features. The symbolic program maps traffic state and lane policy to coarse throughput regimes. The randomized-policy AIPW analysis is not itself a ProbLog program: it is a population estimator applied around the DeepSWIP plug-in nuisance model. The program below is the symbolic nuisance model whose materialized probabilities are perturbed by sensor noise. 16
DeepSWIP
Listing 2: DeepProbLog template for the SUMO HOV traffic experiment. % Neural traffic-state predicate. % Sensor features Z are produced by SUMO loop-detector counts and speeds. nn(traffic_state_net, Z, State, [freeflow,congested]) :: traffic_state(Episode, State). % Policy atoms. In the randomized-policy experiment, treatment is assigned % outside the program and then materialized as one of these facts. hov_restricted(Episode) :- policy(Episode, restricted). hov_open(Episode) :- policy(Episode, open). % State indicators. freeflow(Episode) :- traffic_state(Episode, freeflow). congested(Episode) :- traffic_state(Episode, congested). % Coarse symbolic throughput regimes. high_throughput(Episode) :- freeflow(Episode), hov_open(Episode). high_throughput(Episode) :- freeflow(Episode), hov_restricted(Episode). medium_throughput(Episode) :- congested(Episode), hov_open(Episode). low_throughput(Episode) :- congested(Episode), hov_restricted(Episode). % Binary summaries used by the plug-in DeepSWIP nuisance model. good_outcome(Episode) :- high_throughput(Episode). good_outcome(Episode) :- medium_throughput(Episode). % Example evidence and query forms. % The experiments instantiate these per episode after materialization. query(good_outcome(Episode)). query(high_throughput(Episode)). query(medium_throughput(Episode)). query(low_throughput(Episode)).
For the HOV counterfactual query, the intervention do(policy = open) replaces the policy mechanism with the fixed open-lane policy. Conditioning evidence such as congested(i) is evaluated in the factual world, while the query is evaluated in the SWIP-transformed program. The AIPW/DML correction reported in the experiment is then applied only to the constructed randomized-policy population estimands, not to arbitrary individual counterfactual queries. B.3. Materialized form used by DeepSWIP After neural evaluation, each neural predicate is replaced by an ordinary probabilistic choice. For example, an MPI3D image with shape probabilities (0.01, 0.02, 0.05, 0.03, 0.04, 0.85) is represented as: Listing 3: Example materialized MPI3D neural predicate. 0.01::shape(i, cone); 0.02::shape(i, cube); 0.05::shape(i, cylinder); 0.03::shape(i, hexagonal); 0.04::shape(i, pyramid); 0.85::shape(i, sphere).
17
Habib
Similarly, a SUMO episode with predicted traffic-state probabilities (0.84, 0.16) is represented as: Listing 4: Example materialized SUMO neural predicate. 0.84::traffic_state(e, freeflow); 0.16::traffic_state(e, congested).
DeepSWIP is applied only after this materialization step. This is why the counterfactual solver operates on ordinary ProbLog programs even though the original experimental models contain neural predicates. The AIPW estimator uses known propensity g = 0.5 and fold-specific observed-only nuisance estimates: n Ti 1 X (−i) (−i) AIPW b µ̂1 (Wi ) + {Yi − µ̂1 (Wi )} , ψ1 = (7) n g i=1
with the analogous expression for ψ0 and τ̂ = ψ̂1 − ψ̂0 .
Appendix C. Additional Experimental Details Table 2: MPI3D visual counterfactuals. SWIP and Twin agree exactly over 12,000 materialized visual-symbolic counterfactual queries. RMSE is against factor-ground-truth potential outcomes. Query
SWIP=Twin
Bias
RMSE
Pruning
100% 100% 100% 100%
+0.0000 +0.0013 -0.0026 +0.0000
0.0000 0.0580 0.0821 0.0000
18 → 5 18 → 8 18 → 11 18 → 7
can roll can stack risky on shelf stable
Table 3: SUMO HOV corrected DML/AIPW stress test. σ
Cal(ff)
Cal(cg)
Naive ψ1
CV ψ1
AIPW ψ1
Naive ATE
AIPW ATE
0.0 1.0 2.0 3.0
0.920 0.890 0.845 0.767
0.985 0.900 0.706 0.556
-4.75 -5.66 -6.32 -9.55
-4.67 -5.36 -7.04 -8.92
+0.14 +0.08 +0.13 +0.03
-1.24 -1.50 -1.68 -2.57
+0.15 +0.06 +0.14 -0.03
18