arXiv:2609.02873cs.PLSep 2026

Manuscript Offprint

arXiv:2609.02873

Causal Probabilistic Programming via Magmadic Do-Notation

Mario Román

1. INTRODUCTION

1.1. Example — Simpson's paradox in observational clinical data. Imagine we have two treatments, named A and B, for the same disease, X. From the observational data, A seems slightly less successful than B: they achieve remission in 81.3% and 83.8% of the cases, respectively.

Treatment ATreatment B
Illness X81.3% (61 : 14)83.8% (88 : 17)
Variant X193.3% (28 : 02)86.6% (78 : 12)
Variant X273.3% (33 : 12)66.6% (10 : 05)

FIGURE 1. Remission odds, by treatment and variant.

However, we observe that clinical practice is to subdivide the illness in two variants: X1X_1 and X2X_2. After segregating patients (Figure 1), we find that treatment A is separately more successful for X1X_1—with a success rate of 93.3% versus 86.6%—and for X2X_2—with a success rate of 73.3% versus 66.6%. The explanation for this apparent paradox is that the choice of treatment was being influenced by the variant—while the X1X_1 variant was mostly treated with B, the more difficult and rare X2X_2 variant was being systematically sent for treatment A.

This is Simpson's paradox, a failure to account for a confounding variable in the observational data. Randomised trials avoid this issue: e.g., if we had truly randomized the application of A or B, we would have been able to compare both under equal conditions.¹

But, do we always need randomised trials to estimate treatment effect?

Date: September 3, 2026.

¹This is not an entirely fictional scenario. In 1986, an observational study by Charig et al. [CWPW86] reported the relative success of different modalities of kidney stone surgery. Open surgery seemed slightly less successful than a modality of closed surgery, but this was a real-world instance of Simpson's paradox, as noted in 1994 by Julious and Mullee [JM94], who concluded that "randomised trials are therefore necessary to demonstrate any treatment effect." Similar real-world examples exist for many causal inference problems [Pea09].

MARIO ROMÁN

1.2. Example — causal reasoning with observational data. In particular, in this example, we can estimate a randomised control trial from the given data.

(1) We can estimate the prevalence of the X1X_1 and X2X_2 variants. We have 120 patients with the X1X_1 variant and 60 patients with the X2X_2 variant (Figure 1).

(2) We can weight the success of the treatment AA on each variant by the actual probability of each, 28/30⋅120/180+33/45⋅60/180=13/1528/30 \cdot 120/180 + 33/45 \cdot 60/180 = 13/15.

(3) Analogously, we can weight the success of treatment BB by the actual probabilities, 78/90⋅120/180+10/15⋅60/180=12/1578/90 \cdot 120/180 + 10/15 \cdot 60/180 = 12/15.

(4) As a consequence, a 1:1 randomised control trial of AA and BB with 30 patients would expect 13/15 to achieve remission with AA and 12/15 to achieve remission with BB.

Thus, we have a mathematically valid estimator for a randomized control trial; and no randomized controlled trial was necessary for this estimate. Although the problem of finding estimators under causal assumptions is decidable and solved [SP08, TS10], it remains unintuitive to reason about causality [Pea09].

This manuscript introduces a syntax and a denotational semantics for a simple form of causal probabilistic programming: a programming paradigm for causal inference built over probabilistic programming.

1.3. Probabilistic programming. Probabilistic programming is a programming paradigm where statistical models are first-class citizens of the language; this feature facilitates a syntax for probabilistic inference: the problem of estimating a conditional probability from observational data and a statistical model.

While mathematically solved, probabilistic inference is conceptually difficult to apply correctly: probabilistic programming addresses this problem by allowing the declarative specification of probabilistic problems—it uses an observe operator that imposes a conditional constraint on a distribution (see, for instance, Figure 2).

(do (variant treatment outcome) <- data
    () <- (observe treatment 'A)
        return (outcome))

>>> '((success) 61/75)
>>> ((failure) 14/75))

(do (variant treatment outcome) <- data
    () <- (observe treatment 'B)
        return (outcome))

>>> '((success) 88/105)
>>> ((failure) 17/105))

FIGURE 2. Naive conditional estimation of treatment outcome.

However, a naive application of probabilistic inference would fall prey to the confounding variable problem in Section 1.1 (see the results in Figure 2). The query in Figure 2 asks “after observing that I have been assigned to treatment AA (or BB), what are the odds of remission?” Note that this is different from asking “what are the odds of remission if I undergo treatment AA (or BB)?”

Naively conditioning on observations does not answer this second question, for what we need is a causal model that explains out the bias introduced by deciding treatment based on variant. Endowed with a causal model, causal inference is solvable [SP08], but still conceptually difficult. This conceptual difficulty is eased by the syntax of causal probabilistic programming.

1.4. Causal probabilistic programming. Causal probabilistic programming allows us to declaratively specify problems of causal inference. Causal inference is the problem of estimating effects from observational data and a causal model [Pea09]. As an example, consider that we want to estimate the effect of a 1:1 randomized control trial for the data in Section 1.1. Figure 3 shows the query code: it assigns a treatment arm uniformly and estimates the causal effect of the treatment.

(do (arm) <- (uniform '(A) '(B)) (outcome) <- (intervene data withModel (do variant <- () treatment <- (variant) outcome <- (variant treatment) return (variant treatment outcome)) setting (treatment) to (arm) in (outcome)) return (arm outcome))

'((open success) 634983/1525400) ((open failure) 127717/1525400) ((closed success) 6231/16000) ((closed failure) 1769/16000))

FIGURE 3. Causal estimation of a randomized control trial.

Causal inference is achieved via the intervene operator: it takes a distribution and a causal model and yields the intervened distribution—or an exception when impossible. Intervene is not a primitive: it is internally rewritten into non-associative sequencing and observe. For instance, Figure 3 becomes Figure 4.

(do (arm) <- (uniform '(A) '(B)) (outcome) <- (do (variant) <- (do (variant1 treatment1 outcome1) <- data return (variant1)) (outcome) <- (do (variant2 treatment2 outcome2) <- data () <- (observe arm treatment2) () <- (observe variant variant2) return (outcome2)) return (outcome)) return (arm outcome))

FIGURE 4. Probabilistic rewriting of the causal estimation.

From this point of view, the primitives of our syntax are the (non-associative) do and the observe primitive from normalized probabilistic programming. Next section discusses the non-associative do primitive [DRS26] (Section 2); last section recasts an identifiability algorithm [SP08] in terms of do and observe (Section 3).

MARIO ROMÁN

Remark 1.4.1. The causal model can be itself declared using a form of single-output anonymous do-notation, as we do in Figure 3. It specifies a semimarkovian model.