Code
using DataFrames
using Distributions
using GLM
using Statistics
using Random
using LinearAlgebra
using Printf
using CairoMakie
CairoMakie.activate!(type = "png")using DataFrames
using Distributions
using GLM
using Statistics
using Random
using LinearAlgebra
using Printf
using CairoMakie
CairoMakie.activate!(type = "png")Bayesian methods return a posterior distribution, not only a point estimate and a standard error. For causal inference this is useful when we want uncertainty about conditional average treatment effects, policy values, or other derived quantities.
The two common models in this area are:
Both have mature R packages (BART, bcf). Julia does not currently have a comparable BART/BCF workflow. So this chapter shows the basic Bayesian g-computation idea with a conjugate linear model, and points to R for the tree-based versions.
Related reading: For a hands-on Bayesian model with
Numpyro, see the Using Numpyro chapter in Topics on Econometrics and Causal Inference. The R companion gives the BART and BCF version.
Random.seed!(42)
n = 1500
p = 3
X = randn(n, p)
ps = @. 1 / (1 + exp(-(-0.3 + 0.5 * X[:, 1] + 0.3 * X[:, 2])))
D = Float64.(rand(n) .< ps)
tau = @. 1 + 2 * X[:, 1] # true CATE
Y0 = @. 0.5 * X[:, 2] + 0.3 * X[:, 3] + randn()
Y1 = Y0 .+ tau
Y = ifelse.(D .== 1, Y1, Y0)
df = DataFrame(Y = Y, D = D, X1 = X[:, 1], X2 = X[:, 2], X3 = X[:, 3])
@printf("True ATE = %.3f\n", mean(tau))True ATE = 0.901
The simplest Bayesian causal inference recipe is Bayesian g-computation:
Both potential outcomes in the simulation share the same residual, so every unit’s individual effect equals \(\tau(X_i)\). That equality is a feature of the simulation. In general the difference of predicted means at \(x\) is the conditional average effect \(E[Y(1) - Y(0) \mid X = x]\); the data show one potential outcome per person and do not identify the individual effect without a model for how the two potential outcomes are related.
A Bayesian linear regression with the usual improper prior \(p(\beta, \sigma^2) \propto 1/\sigma^2\) is enough to show the idea:
"""
bayes_linreg(X, y; n_samples=2000)
Bayesian linear regression under the improper prior p(β, σ²) ∝ 1/σ²: σ² is
drawn from its Inverse-Gamma marginal posterior and β | σ² from a normal centred
at the OLS estimates, so the spread is close to the OLS standard errors. Returns `n_samples` posterior
draws of (β, σ²).
"""
function bayes_linreg(X::Matrix, y::Vector; n_samples::Int = 2000)
n, p = size(X)
XtX_inv = inv(X' * X)
beta_hat = XtX_inv * X' * y
sse = sum(abs2.(y .- X * beta_hat))
# σ² ~ InverseGamma((n-p)/2, sse/2)
σ²_post = [rand(InverseGamma((n - p) / 2, sse / 2)) for _ in 1:n_samples]
# β | σ² ~ MultivariateNormal(beta_hat, σ² * XtX_inv)
betas = zeros(n_samples, p)
for i in 1:n_samples
cov_mat = σ²_post[i] .* XtX_inv
betas[i, :] = rand(MvNormal(beta_hat, Symmetric(cov_mat)))
end
return betas, σ²_post
end
# Fit on (D, X1, X2, X3, D*X1, D*X2, D*X3) — allow treatment effect heterogeneity
DesignMat = hcat(ones(n), df.D, df.X1, df.X2, df.X3,
df.D .* df.X1, df.D .* df.X2, df.D .* df.X3)
betas, σ²_draws = bayes_linreg(DesignMat, df.Y; n_samples = 2000)
# Counterfactual prediction matrices
X_treat = hcat(ones(n), ones(n), df.X1, df.X2, df.X3,
df.X1, df.X2, df.X3) # D = 1, so D*X = X
X_control = hcat(ones(n), zeros(n), df.X1, df.X2, df.X3,
zeros(n), zeros(n), zeros(n)) # D = 0
# Posterior ITEs: n_samples x n
ite_post = (X_treat * betas')' .- (X_control * betas')'
# ATE posterior: average over n at each draw
ate_post = vec(mean(ite_post, dims = 2))
@printf("Bayesian g-computation:\n")
@printf(" Posterior mean ATE: %.3f\n", mean(ate_post))
@printf(" Posterior SD: %.3f\n", std(ate_post))
@printf(" 95%% CrI: [%.3f, %.3f]\n",
quantile(ate_post, 0.025), quantile(ate_post, 0.975))
@printf(" True ATE: %.3f\n", mean(tau))Bayesian g-computation:
Posterior mean ATE: 0.904
Posterior SD: 0.056
95% CrI: [0.798, 1.016]
True ATE: 0.901
With this prior, the credible interval is close to the usual frequentist interval. With informative priors, the posterior reflects both the prior and the data.
ite_mean = vec(mean(ite_post, dims = 1))
ite_lo = [quantile(ite_post[:, i], 0.025) for i in 1:n]
ite_hi = [quantile(ite_post[:, i], 0.975) for i in 1:n]
# Sort by X1 for plotting
ord = sortperm(df.X1)
fig = Figure(size = (700, 400))
ax = Axis(fig[1, 1], xlabel = "X1", ylabel = "CATE",
title = "Posterior CATEs vs truth")
band!(ax, df.X1[ord], ite_lo[ord], ite_hi[ord],
color = (:steelblue, 0.2), label = "95% CrI")
scatter!(ax, df.X1, ite_mean, color = (:steelblue, 0.4), markersize = 3,
label = "Posterior mean CATE")
lines!(ax, [-3, 3], 1 .+ 2 .* [-3, 3], color = :firebrick,
linestyle = :dash, linewidth = 2, label = "True τ(x)")
axislegend(ax, position = :rb, framevisible = false)
figPosterior mean of the conditional average effect at each unit’s covariates vs true τ(x) = 1 + 2 X₁. Blue band shows 95% pointwise credible interval across posterior draws.
The blue ribbon is the pointwise 95% credible interval for the conditional average effect, given the linear model and the prior. The red dashed line is the true treatment-effect function.
The linear g-computation above is useful when:
For flexible Bayesian causal inference, R’s BART and bcf are better tools. They use sums of regression trees and provide:
Compare the rough sketch:
| Capability | Julia (this chapter) | R (BART/BCF) |
|---|---|---|
| Bayesian point estimate of ATE | ✓ | ✓ |
| Posterior credible intervals | ✓ | ✓ |
| Flexible nonparametric outcome model | (limited to linear + interactions) | ✓ |
| Native CATE posterior with regularisation | (linear-model heterogeneity only) | ✓ |
| Separation of prognostic and treatment effects | – | ✓ (BCF) |
For now, Julia users who need BART/BCF should call out to R. Reimplementing these models in Julia would be a separate package project.
| Reason to use Bayesian | Reason to use Frequentist |
|---|---|
| Prior information matters | Asymptotic theory is well-understood |
| Need full posterior for downstream decisions | Computational cost matters at scale |
| Small sample, weak identification | Effects are well-identified |
| Want posterior intervals for conditional effects | Existing frequentist toolkit suffices |
Bayesian methods are most useful when the posterior itself will be used: for treatment recommendations, policy values, or hierarchical models. If the target is just a well-identified ATE in a large sample, the frequentist tools are usually simpler.