ConceptioArchivearXiv CS
arXiv CSopen access

Calibration, Not Compilation: Detecting and Repairing Misspecified Probabilistic Programs Written by Language Models

Unknown · 2026 · arxiv_cs
arXiv CS · Papers · License: Open Access · 2026
Open Source ↗Direct PDF ↓
machine learning, deep learning, neural networks

Calibration, Not Compilation: Detecting and Repairing Misspecified Probabilistic Programs Written by Language Models Jian Xu1,2 1

RIKEN iTHEMS

2

Delu Zeng3

RIKEN AIP

3

John Paisley4

Qibin Zhao2

South China University of Technology

4

Columbia University

arXiv:2606.31630v1 [cs.LG] 30 Jun 2026

[email protected] Abstract Language models increasingly write probabilistic programs (in NumPyro, Stan, or Pyro), but a program that compiles, runs, and passes every unit test can still be statistically wrong—a Gaussian likelihood for heavy-tailed data, a Poisson for over-dispersed counts, an invalid prior support, or a pathological parameterization. The right verifier is therefore not a test suite but the Bayesian workflow itself : posterior predictive checks, simulation-based calibration, sampler diagnostics (R̂, divergences, ESS), and held-out predictive density. We study this calibration oracle along three axes. Detection: on a benchmark of 14 misspecification types across 10 model families (200 instances), it flags the bug with AUC 0.97 (88% at 2% FPR when handed the correct reference program, an upper bound )—and a fully reference-free version that uses no correct program reaches 62–78% (the upper figure from a small automated model search), versus 0% for a unit-test oracle. Repair: used as feedback in an LLM repair loop across fifteen models, calibration significantly outperforms unit-test feedback—which is itself significantly worse than no feedback at all, a passing test inducing false confidence that suppresses repair—and improves over no feedback on strong-but-unsaturated models (GPT-5.1 33→92%, Claude 75→100%; paired McNemar, n=228). Reality: on programs LLMs write from scratch for neutral briefs, 15–47% of runnable ones are statistically misspecified (unit tests catch none), and calibration-guided repair significantly beats LLM-as-judge review, a Bayesian-workflow checklist, and data-summary self-debug. Across all three, the lesson is the same: for probabilistic programs, correctness is calibration, not compilation.

Introduction

invisible misspecification. Our central claim is that the right verifier for LLMwritten probabilistic programs is the Bayesian workflow (Gelman et al., 2020): simulation-based calibration (SBC) (Talts et al., 2018), posterior predictive checks (PPC) (Gelman et al., 1996), sampler diagnostics such as R̂ (Vehtari et al., 2021) and divergent transitions (Hoffman et al., 2014), and held-out predictive density. These quantities form a calibration oracle that, unlike a test suite, is sensitive precisely to statistical error. We study it as both a detector (does it flag misspecification a test misses?) and a repair signal (does feeding it back to the writer fix the program?), on a controlled benchmark and on programs models write from scratch. In one phrase: for probabilistic programs, correctness is calibration, not compilation. We make three contributions.

Probabilistic programming languages (PPLs) such as Stan (Carpenter et al., 2017), Pyro/NumPyro (Bingham et al., 2019; Phan et al., 2019), and PyMC let practitioners specify a generative model and obtain a posterior by general-purpose inference. Writing a correct probabilistic program, however, requires statistical expertise: one must choose an appropriate likelihood, give parameters valid support, capture the dispersion and tail behavior of the data, and pick a parameterization that the sampler can actually explore. Large language models are increasingly used to draft such programs, and a tempting deployment pattern is to wrap the LLM in an agentic loop that “tests” its output and iterates. The difficulty is that the usual notion of a test does not apply. A probabilistic program is not correct because it runs; it is correct because its posterior is well-calibrated and its predictions match held-out data. A model that uses a Gaussian likelihood for heavy-tailed data, a Poisson likelihood for over-dispersed counts, or a centered parameterization for a hierarchical funnel will compile, run under NUTS, return finite samples, and pass every conventional unit test—while being statistically wrong in a way that no amount of code inspection on a neutral task description can reveal. We call such errors code-

• A misspecification benchmark and the detection result. We build a benchmark of 14 misspecification types across 10 model families (200 instances), all of which compile and run. The calibration oracle flags the bug in 88% of cases (93% of code-invisible ones) at a 2% false-positive rate (detection AUC 0.97), versus 0% for a unit-test oracle. Each error type lights up the right diagnostic, and 1

the oracle further localizes the bug type with 75% top-2 accuracy.

and to draft or discover probabilistic models. These efforts largely assume the generated model is structurally acceptable; we instead target the detection and repair of statistical misspecification using calibration as the criterion.

• It does not need a correct program. A reference-free oracle (PPC + sampler + SBC) detects 62% of bugs; adding a classical GLM baseline reaches 68%, and an automated model search (LOO over a small standard library) reaches 78%— all without a hand-written correct program. The 88% “reference” figure is an upper bound that assumes the correct program is known. An ablation shows held-out density is the strongest single component, while SBC alone cannot detect model–data misspecification.

LLM code repair and self-improvement. Iterative self-correction (Madaan et al., 2023) and test-driven program repair use execution traces or failing unit tests as the feedback signal. For ordinary software this is appropriate; for probabilistic programs the analogous “it runs and returns numbers” signal is exactly the false all-clear we show to be harmful. Our contribution is to replace the test oracle with a calibration oracle.

• Calibration-as-oracle repair. Used as feedback in an LLM repair loop and compared against no feedback (self-refine) and unit-test feedback, calibration is the strongest signal at every capability level able to use it, lifting GPT-5.1 from 33% → 92% and Claude Sonnet 4.6 from 75% → 100% on codeinvisible bugs across fifteen models.

Method Figure 1 summarizes our approach: an LLM drafts a probabilistic program, a sampler fits it, a calibration oracle criticizes the fit, and—if the fit is not calibrated—a structured diagnostic is fed back for repair. We now formalize each component.

• Two robust phenomena. (a) Unit-test feedback is harmful : “all checks pass” suppresses repair, so test feedback is ≤ no feedback (Claude: 25% vs. 75%). (b) The benefit of calibration feedback is non-monotone in capability—negligible for models too weak to act on a diagnostic or strong enough to repair blindly, and largest for strong-butunsaturated models (GPT-5.1, Claude).

Problem Formalization A task is a tuple ( b, Dtr , Dte ) with a natural-language brief b and data drawn from an unknown generating process y ∼ p⋆ . A candidate program M defines a joint density pM (y, θ) = pM (θ) pM (y | θ) over parameters θ and observations y; fitting yields a posterior π̂(θ) ≈ pM R (θ | Dtr ) and a posterior predictive pM (ỹ | Dtr ) = pM (ỹ | θ) π̂(θ) dθ. We call M statistically correct if its predictive distribution is indistinguishable from p⋆ under a chosen family of discrepancies, and misspecified otherwise. Importantly, two programs can induce very different pM while being syntactically interchangeable: e.g. Poisson vs. NegativeBinomial differ in one token but encode different dispersion.

• Real, not just injected, failures. On programs LLMs write from scratch for neutral briefs, 15–47% of runnable ones are statistically misspecified (unit tests flag none), and calibration repair significantly beats LLM-as-judge review, a Bayesian checklist, and data-summary self-debug (p < 0.05, paired McNemar).

Related Work The Calibration Oracle

Bayesian workflow and model criticism. Posterior predictive checks (Gelman et al., 1996), simulationbased calibration (Talts et al., 2018), improved R̂ and tail diagnostics (Vehtari et al., 2021), and divergencebased geometry diagnostics for HMC/NUTS (Hoffman et al., 2014; Betancourt, 2017) are the standard tools for criticizing a fitted Bayesian model, codified as the Bayesian workflow (Gelman et al., 2020). We repurpose these tools as an automated oracle and repair signal rather than a human-facing diagnostic dashboard.

The oracle O(M, D) aggregates four families of Bayesian-workflow diagnostics. Posterior predictive checks. For a test statistic T , the two-sided posterior-predictive tail probability is  pT = min u, 1−u ,

u=

Pr

  T (ỹ) ≥ T (yobs ) ,

ỹ∼pM (·|Dtr )

(1) estimated from posterior-predictive replicates over T ∈ {mean, sd, max, #zeros}. Under correct specification u is approximately uniform, so pT concentrates near 21 ; a value near 0 indicates the model cannot reproduce that aspect of the data.

LLMs for probabilistic and statistical modeling. Recent work uses LLMs to elicit priors (Huang, 2025), to quantify uncertainty of LLM-based systems by treating prompts as Bayesian parameters (Ross et al., 2025), 2

Calibration Oracle

Brief + Data LLM writer

Program M

R, divergences, ESS posterior predictive checks simulation-based calibration held-out predictive density

NUTS fit

no

diagnostic feedback

calibrated?

yes

accept M

Figure 1: Calibration-as-oracle repair. The verifier is the Bayesian workflow, not a test suite. An LLM drafts a probabilistic program, NUTS fits it, and the calibration oracle aggregates sampler geometry, posterior predictive checks, simulation-based calibration, and held-out predictive density. If the fit is not calibrated, the oracle returns a structured diagnostic ϕ naming which aspect of the data the model fails to reproduce, and the writer repairs the program; correct programs are a fixpoint (Remark 1). Verdict. Writing 1[·] for the indicator, the calibration oracle returns

Simulation-based calibration. For a scalar g(θ), draw θ0 ∼ pM (θ), y ∼ pM (· | θ0 ), fit, and form the rank of the prior draw among S posterior draws,

r=

S X

1[g(θs ) < g(θ0 )] ,

O(M, D) = 1[ran] ∧ 1[R̂ < 1.05] ∧ 1[#div ≤ 15] ∧ 1[minT pT ≥ α] ∧ 1[lppd ≥ lppd⋆ − δ], (5) with α = 0.01, δ = 0.15 nats, and lppd⋆ the reference. The competing unit-test oracle Ou returns “pass” iff the program compiles, runs, and yields finite samples of the right shape.

r ∼ Uniform{0, 1, . . . , S}

s=1

(2) where the uniformity holds exactly when inference is exact and the model is well-specified (Talts et al., 2018); systematic deviation of the rank histogram flags inference or specification error.

Why Tests Fail and Calibration Succeeds The two statements below are elementary—they formalize intuition rather than contribute new theory; our contribution is the framing, benchmark, and empirical study, not the propositions themselves.

Sampler geometry. The split-R̂ (Vehtari et al., 2021), minimum bulk ESS, and the number of divergent transitions of NUTS (Hoffman et al., 2014; Betancourt, 2017) detect parameterizations whose posterior geometry the sampler cannot traverse (e.g. the hierarchical funnel). Held-out predictive density. dictive density

lppd(M ) =

Proposition 1 (Blindness of the test oracle) Ou (M, D) is a function only of the program’s executability and output type, not of pM or p⋆ . Hence for any two executable programs M1 , M2 returning finite samples of the same shape, Ou (M1 ) = Ou (M2 ) regardless of whether either is misspecified. On a family of misspecifications that all execute (our code-invisible class), Ou is therefore uninformative: its detection rate equals its base “pass” rate, independent of correctness.

The pointwise log pre-

S 1 X 1X log pM (yi | θs ) |Dte | S s=1

(3)

i∈Dte

This is immediate from the definition of Ou , but it is the crux: a passing test conveys zero information about statistical correctness for code-invisible bugs.

is a Monte-Carlo estimate of the log score, a strictly proper scoring rule (Gneiting and Raftery, 2007). By Gibbs’ inequality,     Ey∼p⋆ log pM (y) ≤ Ey∼p⋆ log p⋆ (y) ,

Proposition 2 (Detection by calibration) Let M be misspecified in a statistic T , i.e. T (yobs ) lies in the tails of pM (T (ỹ) | Dtr ). Then as the number of posterior-predictive replicates grows, the estimated pT → min(u, 1−u) with u → 0 or 1, so O flags M at any fixed threshold α > 0. Analogously, a parameterization

(4)

with equality iff pM = p⋆ ; hence a misspecified M has strictly lower expected lppd than the reference and the ranking is consistent as |Dte | → ∞. 3

Algorithm 1: Calibration-as-oracle repair

with unreachable geometry yields R̂ > 1 and divergences bounded away from 0, and a misspecified likelihood yields strictly lower expected lppd than the reference. Thus O is a consistent detector for misspecification expressed through its diagnostics.

Require: brief b, data (Dtr , Dte ), writer L, oracle O, feedback map ϕ, budget K 1: M ← L(b) {or a given buggy program} 2: for t = 0 to K do 3: fit M with NUTS; compute O(M, D) 4: if O reports calibrated then 5: return M {accept} 6: end if 7: if t < K then  8: M ← L b, M, ϕ(O(M, D)) {repair} 9: end if 10: end for 11: return M {best effort}

Bug Taxonomy: Visible vs. Invisible We distinguish code-visible misspecification, diagnosable from the source under a neutral brief (a hard-coded tiny noise scale, an absurd prior, a missing link function), from code-invisible misspecification, where the program is a faithful, standard implementation of the brief but wrong for this data: a Gaussian likelihood for outlierladen residuals (should be Student-t), a Poisson for overdispersed counts (negative-binomial), a plain Poisson for zero-inflated counts (zero-inflated), and a centered parameterization for a hierarchical funnel (non-centered). By Proposition 1 the invisible class is exactly where a test oracle is powerless, and by Proposition 2 exactly where calibration retains power; it is therefore the discriminating class in our experiments. The split is defined a priori by code-inspectability under a neutral brief—can a competent programmer flag the bug from the source alone, without seeing the data—and not by any outcome. The clamped/identity link error is classed visible because the mistake is literally readable in the source, even though (as Figure 2 shows) it is among the hardest for the calibration oracle to catch from data; including it therefore makes our detection numbers conservative, not favorable. We report detection on both classes (Figure 2) and focus the repair study on the invisible class only because visible bugs are repaired at ≈ 100% by every feedback regime and so do not discriminate.

that test feedback is no better than, and often worse than, no feedback (Sec. Results).

Experimental Setup

Given a buggy program M0 , at round t we fit Mt , query the oracle, and stop if calibrated; otherwise the writer receives the brief, Mt , and feedback ϕt and returns Mt+1 (Algorithm 1). We compare three feedback maps ϕ: none (“the model may be misspecified; produce a corrected version”), test (the unit-test verdict), and diag (the calibration diagnostics plus a plain-language reading of which statistic is misfit, e.g. “the spread of the data lies in the extreme tail of the posterior predictive,” without prescribing the distributional fix).

Benchmark. Our benchmark spans ten model families—linear, robust (Student-t), logistic, count (Poisson/NB/ZIP), binomial, Gamma, Weibull survival, hierarchical, AR(1) time series, a local-level state-space model, and a stochastic-volatility model— and fourteen misspecification types covering the standard failure modes of Bayesian modeling: wrong likelihood, wrong/invalid prior support, over-dispersion, zero-inflation, missing covariate, missing hierarchy, wrong link, wrong transformation, bad (centered) parameterization, missing temporal structure, missing latent state, missing volatility, prior–data conflict, and fixed-scale. Each spec pairs a known-correct reference program with a runnable buggy program and a parametrized data generator; we instantiate 10 random instances per spec (200 instances total). Detection requires no LLM, so it is run at full scale; for the repair experiments we use a representative subset of eight bugs (four visible, four invisible). We also explored finite mixtures, additive non-identifiability, Gaussian processes, and a toy Bayesian neural net but omit them from headline numbers (NUTS fails to converge for the correct mixture/BNN, yielding false positives; the GP marginal is incompatible with our generic held-out likelihood). All reported buggy variants compile and run.

Remark 1 (Do-no-harm fixpoint) If M is already calibrated, the diag map returns “no defect” and the loop terminates with M unchanged: correct programs are a fixpoint. The test map, by contrast, returns “all checks pass” for every executable program—including misspecified ones—so it can neither trigger a needed repair nor protect a correct model from being perturbed by further editing. This asymmetry predicts the empirical finding

Models. Fifteen writers across four open families (DeepSeek, Qwen-Coder, Llama, Phi) and three API families (GPT, Claude, Gemini): DeepSeek-Coder6.7B, DeepSeek-V4-flash/pro, Qwen2.5-Coder-7B/32B, Llama-3.1-8B, Phi-4, GPT-4o-mini, GPT-4.1, GPT-5.1, GPT-5.5, Claude Sonnet 4.6, and Gemini-2.5-flash/pro and 3.1-pro. Open models are run locally; each trajectory alternates GPU generation with CPU inference.

Repair Loop

4

Metric. Fix rate: the fraction of (bug, seed) trajectories judged correct within K rounds (3 seeds per cell). We report fix rate separately for visible and invisible bugs.

Oracle

Reproducibility. All inference uses NUTS with 2 chains and 600 warmup + 600 draws (the detection sweep) or 800+800 (the de-risk study), jax enable x64, fixed PRNG seeds, and the thresholds of Eq. (5). Writers are queried at temperature 0.7 (reasoning models at their default), ≤ 1300 output tokens, repair budget K=3; the exact system prompt, contract, and feedback templates are released with the code. API models are pinned to fixed snapshots (GPT-4o-mini, GPT-4.1, GPT-5.1, GPT-5.5; Claude Sonnet 4.6; DeepSeek-V4flash/pro) and open models to their HuggingFace revisions; total API cost for all repair and generation runs was under US$60. Because frontier snapshots change, we anchor every headline claim to within-model comparisons (feedback regime A vs. B on identical programs and data), which are invariant to the absolute capability of any one snapshot; the paired McNemar tests below are computed on exactly these within-model pairs.

detection

invisible

false-pos.

unit-test reference-free (PPC+sampler+SBC) + GLM baseline + model-search (LOO)

0% 62% 68% 78%

0% 57% 65% 79%

0% 2% 2% 2%

reference (oracle, + correct PPL)

88%

93%

2%

Table 1: Three-tier oracle. Even without any reference program, calibration vastly outperforms unit tests; a classical GLM baseline recovers much of the remaining gap. using only PPC, sampler diagnostics, and SBC (no reference at all); a weak-reference oracle that additionally compares held-out density to a classical covariateaware GLM/MLE baseline (a few lines of lstsq/IRLS, not a correct PPL); and the full reference oracle as an upper bound. Even the reference-free oracle (PPC + sampler + SBC) catches 62% of bugs (57% invisible) versus 0% for unit tests; a trivial GLM baseline raises this to 68%, and—most importantly—an automated model search that fits a small library of standard models and uses the best held-out (LOO-style) density as the floor reaches 78% (79% invisible), all without any hand-written correct program. The 88% “reference” number is best read as an upper bound that assumes the oracle is handed the correct program; the deployable, reference-free figure is 62–78%, still far above the 0% of unit tests. Stronger searches (full PSIS-LOO stacking) should close the remaining gap.

Results Detection at Scale Across all 200 instances (14 misspecification types, 10 families) the calibration oracle flags the buggy program in 88% of cases (93% on code-invisible bugs) with a 2% false-positive rate on the correct program, whereas the unit-test oracle detects 0%—every bug runs. Treating the oracle as a continuous detector (Appendix D) gives an AUC of 0.97; at a 1% false-positive operating point it detects 76% of bugs, and a threshold chosen on a calibration half generalizes to 80% detection at 1% FPR on a disjoint half—so the headline does not hinge on one hand-set operating point. Figure 2 breaks detection down by type. It is essentially perfect for likelihood, dispersion, zero-inflation, support, parameterization, transformation, missing-state, and missingcovariate errors; the hard cases are stochastic volatility (56%) and the visible identity/clamped link error (26%), which often still fits the data’s moments. Each error type trips the diagnostic one would expect: PPC tail p-values collapse for wrong/under-dispersed likelihoods, R̂ and divergences explode for the centered funnel (R̂=1.15, 104 divergences), and held-out density collapses for prior–data conflict.

Phase 2: Repair Across Models Table 2 reports fix rates on the discriminating invisible bugs across fifteen writer models, and Figure 3 the resulting diagnostic advantage. (Visible bugs are nearsaturated for all but the weakest models and do not discriminate between feedback types.) Three findings hold across the board. (1) Calibration feedback is the best repair signal. For every model that can act on it, diag ≥ test and diag ≥ none on invisible bugs. The gains are largest for strong-but-unsaturated models: GPT-5.1 rises from 33% to 92% and Claude from 75% to 100%. The GPT5.1 case is the clearest: with none and test tied at 33%, the diagnostic nearly triples the fix rate, which a blind defensive-rewriting explanation cannot produce. Per-cell rates rest on few trajectories and are noisy in isolation; we therefore base all claims on the pooled, paired tests of finding (4) rather than on any single cell.

How Much Reference Do You Need?

(2) Unit-test feedback is harmful. For capable models, test ≤ none (Claude 25% vs. 75%; GPT-5.5 75% vs. 83%; GPT-4.1 58% vs. 67%). Being told that all unit checks pass induces false confidence and suppresses repair of a model that is in fact misspecified. A

A reviewer may object that real deployments lack a hand-written correct program against which to compare held-out density. We therefore separate the oracle into three tiers (Table 1): a reference-free oracle 5

zero-inflation missing covariate missing temporal invalid support bad param. wrong transform prior conflict fixed scale missing state wrong likelihood over-dispersion missing hierarchy stochastic vol. link error

0

20

40

90

80

56

26

60 detection rate (%)

100 100 100 100 100 100 100 100 100 98

calibration oracle unit test (0%)

80

100

diagnostic advantage (%) (diag better baseline)

Figure 2: Detection rate by misspecification type (calibration oracle vs. unit test) over 200 instances. Calibration catches every type except stochastic volatility and the visible identity/clamped link error; the unit-test oracle catches none.

60 50 40 30 20 10 0 10 20

+59

calibration helps most: strong but unsaturated

+25 +12 +0

DeepSeek Qwen 6.7B 7B

+25

+16 +8

+4

+8 +0

GPT-4o Llama-3.1 Phi-4 mini 8B

+0

Qwen 32B

+0

Gem-2.5 GPT-4.1 flash

+8 +0

DS-V4 pro

already repairs blindly ( no/negative gain) Gem-2.5 GPT-5.1 Claude Gem-3.1 GPT-5.5 pro Sonnet 4.6 pro

-16 DS-V4 flash

Figure 3: Diagnostic advantage on code-invisible bugs—the fix rate with calibration feedback minus the better of the two baselines (no feedback / unit test)—per writer model (raw rates in Table 2). The advantage is non-monotone in capability: ≈ 0 for writers too weak to act on a diagnostic or strong enough to already repair blindly (and negative for DeepSeek-V4-flash), and large precisely for strong-but-unsaturated models (GPT-5.1 +59, Claude +25). test oracle is thus not merely uninformative for statistical correctness—it actively misleads.

runs. Calibration feedback fixes 46% (Wilson 95% CI 39–52%) versus 36% (30–42%) for no feedback and 27% (22–33%) for unit-test feedback. A paired exact McNemar test rejects equality of diag and none (42 vs. 19 discordant, p=4.4 × 10−3 ) and of diag and test (57 vs. 15, p=6.5 × 10−7 ). Unit-test feedback is also significantly worse than no feedback (none vs. test: 29 vs. 10, p=3.4 × 10−3 ), confirming that a passing test actively suppresses repair. The robust, strongly significant findings are diag≫test and test<none; diag>none is significant but with a smaller margin, and we phrase our claims accordingly.

(3) A non-monotone capability effect. The diagnostic advantage (diag minus the better baseline) is non-monotone in capability: near zero for models too weak to translate a diagnostic into a structural fix (DeepSeek-6.7B, Qwen-7B, GPT-4o-mini), and again near zero—or even slightly negative—for models that already repair invisible bugs about equally well with or without a signal (Phi-4, GPT-4.1, the saturated GPT-5.5, and DeepSeek-V4-flash). It is decisive precisely for strong-but-unsaturated models—GPT-5.1 (+59), Claude (+25), Gemini-3.1-pro (+25), Gemini2.5-pro (+8), and DeepSeek-V4-pro (+8)—competent enough to turn a diagnostic into the right structural fix yet not already doing so blindly.

Which Component of the Oracle Matters? Table 3 ablates the calibration oracle into its components on the detection benchmark. The held-out predictive density (the proper scoring rule) and posterior predictive checks are the strongest single components (59% and 56%); sampler diagnostics (R̂, divergences) catch only the few geometry/parameterization

(4) The advantage is statistically significant. Pooling all invisible-bug repair trajectories into paired units keyed by (model, task, seed)—open models at 6 seeds, the rest at 3—gives n=228 pairs across 16 model 6

test

diag

wrong lik.

51

3

DeepSeek-Coder-6.7B Qwen2.5-Coder-7B∗ GPT-4o-mini Llama-3.1-8B∗ Phi-4∗ Qwen2.5-Coder-32B Gemini-2.5-flash GPT-4.1 DeepSeek-V4-pro Gemini-2.5-pro GPT-5.1 Claude Sonnet 4.6 Gemini-3.1-pro GPT-5.5 DeepSeek-V4-flash

0% 0% 0% 17% 12% 17% 58% 67% 67% 58% 33% 75% 75% 83% 83%

0% 0% 0% 17% 12% 0% 67% 58% 42% 75% 33% 25% 33% 75% 50%

0% 12% 8% 21% 12% 33% 67% 67% 75% 83% 92% 100% 100% 83% 67%

over-disp.

6

12

zero-infl.

5

5

invalid supp.

4

6

unit test SBC only sampler diagnostics only PPC only held-out lppd only PPC + sampler full (PPC+sampler+lppd)

detection

false-pos.

0% 5% 6% 56% 59% 62% 88%

0% 5% 2% 0% 0% 2% 2%

0.6 0.4

10 10

prior conflict

20

0.2

10

8

25

0.0

ct. m. lict ng r-d ero- lid su para conf g stru r z va d n wro ove o si ba pri in mis lik.

. isp

l. inf

. pp

predicted

Figure 4: Diagnostic→bug-type confusion (rownormalized). Errors concentrate on the shared “underdispersed predictive” symptom (over-dispersion / invalid support / prior–data conflict); wrong-likelihood and bad-parameterization are cleanly identified.

Table 2: Fix rate on code-invisible misspecification under three feedback regimes. Calibration feedback (diag) is the best signal at every capability level able to use it; unit-test feedback is never better than no feedback and is often worse. ∗ open models de-noised at 6 seeds (others at 3). Oracle variant

0.8

bad param.

missing struct.

1.0

1

row-normalized

none

true bug type

Writer model

curacy across seven classes (n=176 detected bugs). The confusion matrix (Figure 4) concentrates the errors in a single, interpretable place: “under-dispersed posterior predictive” is the shared symptom of over-dispersion, too-tight prior support, and prior–data conflict, so these three are mutually confused while wrong-likelihood and bad-parameterization are cleanly separated. Calibration thus provides not just an alarm but a largely correct structural pointer for repair.

Feedback Design Ablation

Table 3: Component ablation on the detection benchmark. Held-out density is the strongest single signal; SBC alone cannot detect model–data misspecification.

Replacing raw diagnostic numbers with the plainlanguage reading raises Qwen2.5-Coder-32B from 33% to 50% on invisible bugs, but leaves the 7B coder at 0%. Better communication of what is misfit raises the ceiling for models capable enough to exploit it, but cannot substitute for the modeling knowledge a weak model lacks.

bugs (6%). Strikingly, SBC alone is nearly useless for misspecification (5% detection): SBC tests whether inference is self-consistent with the model’s own prior, so a model that is internally coherent but wrong for the data passes it—confirming that detecting misspecification requires confronting the model with the real data through PPC and held-out density. The full oracle (PPC + sampler + held-out) reaches 88%.

From Injected Bugs to Real LLM Programs The experiments so far start from injected bugs. To show the failures are real, we give each model a neutral natural-language brief (no hint about the right distribution) for nine task families and ask it to write a probabilistic program from scratch (5 instances each, 45 programs per model). We then run unit tests and the calibration oracle on whatever it produces. Crucially, each brief draws data from a known generating process with a known-correct reference program, so “misspecified” and “repaired” are judged by held-out predictive density against that ground-truth reference—not by the candidate’s own diagnostics—which breaks the circular-

Does the Oracle Localize the Bug? A repair signal is more useful if it indicates which error is present, not merely that one is. We map each fired diagnostic to a predicted bug type with a simple rule (zeros-tail → zero-inflation; over-dispersed spread → over-dispersion; R̂/divergences → bad parameterization; tail/skew statistics → wrong likelihood; autocorrelation → missing structure) and compare to ground truth over the detected bugs. The diagnostic predicts the correct bug type with 61% top-1 and 77% top-2 ac7

Writer GPT-4o-mini GPT-4.1 GPT-5.1 Claude Sonnet 4.6 DeepSeek-V4-flash DeepSeek-V4-pro Gemini-2.5-flash Gemini-2.5-pro Gemini-3.1-pro

runs (unit-test pass)

of those, misspecified

80% 89% 84% 89% 86% 79% 74% 88% 100%

47% 35% 34% 15% 27% 27% 18% 17% 24%

Table 4: Programs written from scratch for neutral briefs (9 writers). A large fraction of runnable programs are statistically misspecified (oracle-flagged, judged against the known reference); the unit-test oracle flags none of them.

Writer

none test summ. check. judge diag

GPT-4o-mini Gemini-2.5-flash GPT-4.1 GPT-5.1 Claude 4.6 DeepSeek-V4-flash DeepSeek-V4-pro Gemini-2.5-pro Gemini-3.1-pro

41 33 36 54 50 50 44 50 100

12 33 0 46 0 10 0 75 0

0 67 14 46 50 30 0 75 100

0 67 57 77 50 60 33 75 57

12 100 64 46 100 60 67 75 71

18 100 93 92 100 90 67 75 71

pooled‡

46

36

46

63

71

84

Table 5: Repair success (%) on real misspecified programs by feedback regime. ‡ pooled over the seven capable writers (n=98 misspecified programs; the weakest writers excluded as unable to act on any feedback). Calibration (diag) dominates the strongest baselines— LLM-as-judge review, generic checklist, data-summary self-debug—and is significant against each (paired McNemar, all p < 0.05).

ity that would arise if correctness were defined purely by the oracle that also drives repair.

repair success on real misspecified programs (%)

100

Real LLM programs are often statistically wrong—and unit tests never notice. Table 4 shows that 79–89% of generated programs run, but of those that run, 15% (Claude) to 47% (GPT-4o-mini) are flagged as statistically misspecified. The unit-test oracle detects 0 of these—they all compile, run, and return finite samples. The recovered failure modes match the injected taxonomy: wrong likelihood, missing overdispersion, and bad parameterization dominate.

84

80 63

60 40

36

46

71

46

20 0

unit test

data summary no feedback + self-debug

Bayesian checklist

LLM-as-judge code review

calibration (ours)

Figure 5: Repair of real LLM-written misspecified programs, pooled over the seven capable writers (n=98). Calibration feedback significantly outperforms unit tests, data-summary self-debug, a Bayesian checklist, and LLM-as-judge code review (all p < 0.05, paired McNemar).

Calibration repair beats strong baselines on real programs. For the misspecified programs we run the same repair loop, now comparing calibration feedback against progressively stronger baselines: none, test, summary (the program runs, all unit tests pass, plus dataset summary statistics—“data summary + self-debug”), checklist (a generic Bayesian-workflow checklist), and judge (an independent LLM-as-judge code review). Table 5 and Figure 5 report fix rates. Calibration is the best signal on every capable writer, and pooling the seven capable models (n=98 misspecified programs) it repairs 84% versus 71% for LLM-as-judge review, 63% for the checklist, 46% for no feedback and for data-summary self-debug, and 36% for unit-test feedback. The advantage is significant against every baseline by paired exact McNemar: vs. judge p=0.029, checklist p=8.2×10−4 , summary p=1.2×10−7 , none p=9.3×10−9 , test p=2 × 10−11 . Notably even an LLM-as-judge that sees the program and a data summary trails calibration by 13 points: knowing that a model might be wrong is no substitute for measuring how it fails to fit.

Failure Modes of Calibration-asOracle Calibration is not a panacea, and our data expose where it breaks. • Misspecification invisible to the chosen statistics. PPC power depends on the test statistics one tracks. The clamped/identity link error (Figure 2, 26%) reproduces the moments we check, so it largely escapes; catching it needs a statistic tuned to the link or held-out calibration of the predicted probabilities. • Right fit, wrong structure. Predictive checks test distributional fit, not causal/generative correctness. Two programs with the same posterior predictive but different causal structure (confounding, reversed direction, an alternative latent mechanism) are indistinguishable to any predictive oracle. 8

• Over-wide predictive masks errors. An underconfident model can “cover” the data and pass PPC while being wrong; this is why missing-hierarchy detection is only 80%—complete pooling sometimes inflates predictive spread enough to hide the lost between-group variance.

that matter, and—unlike a passing test—it does not lull a capable model into leaving a wrong model untouched. The non-monotone pattern also clarifies where such systems pay off today: pairing a strong-but-unsaturated writer with calibration feedback closes most of the gap to a hand-written reference.

• Diagnosis without a remedy. The oracle is necessary but not sufficient for repair: weak writers receive a correct diagnostic (“the spread is not captured”) yet cannot translate it into Studentt/NB/ZIP (Qwen-7B stays at 0% even with interpretive feedback).

Limitations The detection benchmark is sizable (200 instances, 14 types), but the repair experiments use an 8-bug subset with 3 seeds per cell, so per-cell repair rates are noisy; we therefore rely on pooled paired tests and on the larger real-program study rather than on individual cells. Our controlled benchmark uses injected bugs for clean labels; we complement it with programs LLMs write from scratch, but a large human-labelled corpus of real model-written programs remains valuable. Correctness is judged against a known reference and fixed thresholds rather than full SBC, which we use only for detection; scaling SBC into the repair loop is future work. Finally, our tasks are classical low-dimensional models; extending calibration-as-oracle to high-dimensional and structured models (time series, spatial, deep) is an important next step.

• SBC is expensive in high dimensions. SBC requires many refits from the prior; we could afford it only for detection on small models. For large or costly models it is impractical, leaving PPC and held-out density as the workhorses. • Inference failure masquerades as misspecification. R̂/divergences fire when inference, not the model, is poor, producing false alarms on correctly specified programs—the source of our 2% (and SBC’s 5%) false-positive rate. This also bounds the families we can benchmark cleanly: for finite mixtures and Bayesian neural nets NUTS fails to mix even on the correct model, so the correct program itself is flagged; we therefore exclude them from the headline FPR, which makes our reported 2% a lower bound —on harder-to-fit model classes the oracle conflates a bad posterior approximation with a bad model more often. Better inference (reparameterization, SMC) is the remedy, and narrows the scope of our generality claim accordingly.

Conclusion A probabilistic program can run perfectly and still be statistically wrong. We showed that Bayesian calibration diagnostics detect such errors where unit tests cannot, and that using them as a repair signal lets capable LLMs fix code-invisible misspecification that no testdriven loop can reach—while revealing that test feedback is actively harmful and that the benefit of calibration feedback is non-monotone in model capability. Calibration-as-oracle offers a principled foundation for trustworthy LLM-assisted probabilistic modeling.

Reporting these keeps the claim honest: calibration-asoracle is a strong, reference-light verifier for the common, code-invisible statistical errors LLMs make, not a universal correctness certificate.

Discussion References

Although we implement everything in NumPyro, the oracle is PPL-agnostic: the same diagnostics (R̂, divergences, ESS, PPC, SBC, held-out density) are first-class in Stan, PyMC, and Pyro (Bingham et al., 2019; Carpenter et al., 2017), so the approach transfers directly to the toolchains practitioners already use. Our strongest evidence is not the (noisier, injectedbug) repair sweep but the real-program study: on programs models write from scratch, calibration repair beats every baseline—including LLM-as-judge review and data-summary self-debug—at p as low as 4.5 × 10−10 . Our results suggest a simple design rule for LLMassisted Bayesian modeling: verify with the Bayesian workflow, not with a test suite. The calibration oracle is the only signal that is sensitive to the statistical errors

Michael Betancourt. A conceptual introduction to hamiltonian monte carlo. arXiv preprint arXiv:1701.02434, 2017. Eli Bingham, Jonathan P Chen, Martin Jankowiak, Fritz Obermeyer, Neeraj Pradhan, Theofanis Karaletsos, Rohit Singh, Paul Szerlip, Paul Horsfall, and Noah D Goodman. Pyro: Deep universal probabilistic programming. Journal of machine learning research, 20(28):1–6, 2019. Bob Carpenter, Andrew Gelman, Matthew D Hoffman, Daniel Lee, Ben Goodrich, Michael Betancourt, Marcus Brubaker, Jiqiang Guo, Peter Li, and Allen Rid9

A.

dell. Stan: A probabilistic programming language. Journal of statistical software, 76:1–32, 2017.

Benchmark Catalog

Table 6 lists the full benchmark: model family, datagenerating truth, reference (correct) program, injected bug, misspecification type/class, and the diagnostic that fires. All buggy variants compile, run under NUTS, and return finite samples. We instantiate 10 random data instances per row (200 total) for detection; the eight rows marked † form the repair-loop subset. Four further families we explored—finite mixtures (label switching), additive non-identifiability, Gaussian processes, and a toy Bayesian neural net—are omitted from the headline numbers: NUTS mixes poorly for the correct mixture/BNN (false positives), the weak prior renders the additive split benignly ridge-regularized, and the GP marginal likelihood is incompatible with our generic held-out evaluation.

Andrew Gelman, Xiao-Li Meng, and Hal Stern. Posterior predictive assessment of model fitness via realized discrepancies. Statistica sinica, pages 733–760, 1996. Andrew Gelman, Aki Vehtari, Daniel Simpson, Charles C Margossian, Bob Carpenter, Yuling Yao, Lauren Kennedy, Jonah Gabry, Paul-Christian Bürkner, and Martin Modrák. Bayesian workflow. arXiv preprint arXiv:2011.01808, 2020. Tilmann Gneiting and Adrian E Raftery. Strictly proper scoring rules, prediction, and estimation. Journal of the American statistical Association, 102(477):359– 378, 2007. Matthew D Hoffman, Andrew Gelman, et al. The nou-turn sampler: adaptively setting path lengths in hamiltonian monte carlo. J. Mach. Learn. Res., 15 (1):1593–1623, 2014.

B.

Proofs

Proposition 1 (Blindness of the test oracle).  Proof. By definition Ou (M, D) = h e(M, D) where e = (compiles, runs, finite, shape) is the execution trace and h maps a passing trace to “pass”. The trace e is a functional of the program’s control flow and output tensor only; it does not depend on the induced density pM nor on p⋆ . Hence if M1 , M2 have e(M1 ) = e(M2 ) then Ou (M1 ) = Ou (M2 ). The code-invisible class is by construction a set of programs all sharing the passing trace e⋆ (they compile, run, and return finite samples of the correct shape), so Ou ≡ h(e⋆ ) =“pass” on the entire class regardless of which members satisfy pM = p⋆ . Therefore Ou flags none of them, and its detection rate on the class equals 1 − (pass rate) = 0, independent of correctness. □

Yongchao Huang. Llm-prior: A framework for knowledge-driven prior elicitation and aggregation. arXiv preprint arXiv:2508.03766, 2025. Aman Madaan, Niket Tandon, Prakhar Gupta, Skyler Hallinan, Luyu Gao, Sarah Wiegreffe, Uri Alon, Nouha Dziri, Shrimai Prabhumoye, Yiming Yang, et al. Self-refine: Iterative refinement with selffeedback. Advances in neural information processing systems, 36:46534–46594, 2023. Du Phan, Neeraj Pradhan, and Martin Jankowiak. Composable effects for flexible and accelerated probabilistic programming in numpyro. arXiv preprint arXiv:1912.11554, 2019.

Proposition 2 (Detection by calibration). Proof. We treat the three diagnostic channels in turn. (i) Posterior predictive. Fix a statistic T and let u⋆ = Prỹ∼pM (·|Dtr ) [T (ỹ) ≥ T (yobs )]. The estimator û = P 1 (r) ) ≥ T (yobs )] over R posterior-predictive r 1[T (ỹ R replicates satisfies û → u⋆ almost surely (SLLN). If M is misspecified in T , i.e. T (yobs ) lies in a tail of the predictive law of T (ỹ) with mass min(u⋆ , 1 − u⋆ ) =: β < α, then pT = min(û, 1 − û) → β < α, so for all sufficiently large R the verdict clause 1[minT pT ≥ α] is 0 and O flags M . (ii) Predictive density. By Eq. (4), ∆ := Ey∼p⋆ [log p⋆ (y) − log pM (y)] = KL(p⋆ ∥ pM ) > 0 whenever pM ̸= p⋆ on the test marginal. The empirical gap lppd⋆ − lppd(M ) → ∆ as |Dte | → ∞ (SLLN), so for ∆ > δ the clause 1[lppd ≥ lppd⋆ − δ] is eventually 0 and O flags M . (iii) Sampler geometry. For the centered hierarchical model the conditional prior θj | µ, τ ∼ N (µ, τ ) makes the posterior density’s curvature scale as τ −2 ,

Brendan Leigh Ross, Noël Vouitsis, Atiyeh Ashari Ghomi, Rasa Hosseinzadeh, Ji Xin, Zhaoyan Liu, Yi Sui, Shiyi Hou, Kin Kwan Leung, Gabriel LoaizaGanem, et al. Textual bayes: Quantifying uncertainty in llm-based systems. arXiv preprint arXiv:2506.10060, 2025. Sean Talts, Michael Betancourt, Daniel Simpson, Aki Vehtari, and Andrew Gelman. Validating bayesian inference algorithms with simulation-based calibration. arXiv preprint arXiv:1804.06788, 2018. Aki Vehtari, Andrew Gelman, Daniel Simpson, Bob Carpenter, and Paul-Christian Bürkner. Ranknormalization, folding, and localization: An improved b for assessing convergence of mcmc (with discusR sion). Bayesian analysis, 16(2):667–718, 2021.

10

Family †

Linear Linear† Linear Linear Linear Robust† Logistic† Logistic† Logistic Count† Count† Count Binomial Gamma Survival Hierarchical† Hierarchical Time series State-space Stoch. vol.

Reference (correct) model

Injected bug

Type / class

Firing diagnostic

Normal, learned σ Normal, learned σ Normal, learned σ Normal + x1 , x2 LogNormal Student-t Bernoulli(logits) Bernoulli(logits) logits = a+bx+cx2 Gamma–Poisson (NB) ZeroInflatedPoisson Poisson (log link) Beta–Binomial Gamma (log link) Weibull non-centered non-centered (partial pool) AR(1) via scan local level (RW) RW log-volatility

σ hard-coded = 0.05 slope prior N (−20, 0.3) prior σ ∼ U(0, 0.4) drops covariate x2 Normal on positive y Normal likelihood linear predictor as prob. Normal on {0, 1} drops quadratic term Poisson plain Poisson identity link, clipped Binomial Normal likelihood Exponential centered parameterization complete pooling i.i.d. Normal i.i.d. Normal constant variance

fixed scale / vis. prior conflict / vis. invalid support / vis. missing covariate / inv. wrong transform / inv. wrong likelihood / inv. link error / vis. wrong likelihood / vis. missing covariate / inv. over-dispersion / inv. zero-inflation / inv. link error / vis. over-dispersion / inv. wrong likelihood / inv. wrong likelihood / inv. bad param. / inv. missing hierarchy / inv. missing structure / inv. missing state / inv. missing volatility / inv.

held-out lppd held-out lppd PPC (sd) held-out lppd PPC (max,skew) PPC (max), lppd held-out lppd PPC (sd) held-out lppd PPC (sd) PPC (#zeros) PPC / div. PPC (sd) PPC (min,skew) PPC (max,skew) R̂, divergences PPC (sd) PPC (autocorr) PPC (autocorr) PPC ( |y| autocorr)

Table 6: Benchmark catalog: 20 specs over 10 families and 14 misspecification types. Each reference is the knowncorrect program defining the oracle threshold lppd⋆ ; the bug is a runnable starting point. † marks the eight-bug repair subset. The final column names the diagnostic that drops below threshold. so as τ → 0 no single leapfrog step size is simultaneously stable in the neck and the body of the funnel; the resulting energy error makes the probability of a divergent transition bounded below by a positive constant per draw (Betancourt, 2017), and the poor neck mixing inflates split-R̂ above 1. Hence #div > 15 and R̂ > 1.05 with probability approaching 1 in the number of draws, and O flags M . In each channel the misspecification expressed through that diagnostic is detected at the fixed thresholds of Eq. (5); thus O is a consistent detector. □

nats, R̂<1.05, #div≤15; Appendix D reports their sensitivity. The full system prompt, contract, feedback templates, benchmark generators, and analysis scripts are released with the paper.

D. ROC and Threshold Sensitivity To show the headline does not depend on hand-set thresholds, we form a single continuous misspecification score by normalizing each diagnostic channel to its default operating threshold (PPC tail to α, R̂−1 to 0.05, divergences to 15, held-out lppd gap to δ) and taking the maximum, so score > 1 reproduces the verdict of Eq. (5). Over the 200 correct/buggy pairs this yields a detection AUC of 0.97 (Figure 6). At a 1% falsepositive operating point detection is 76%, rising to 91% at 5%. Choosing the threshold to hit 1% FPR on a random calibration half and evaluating on the disjoint half gives 80% detection at 1% FPR—the operating point transfers across a data split it was not tuned on. Table 7 sweeps each threshold individually. Detection and false positives move smoothly and predictably (looser thresholds trade a little FPR for a little detection); in particular results are essentially invariant to R̂ in [1.01, 1.20] and to the divergence cutoff once it exceeds a handful, and no setting near our defaults changes the qualitative picture.

C. Model Versions and Hyperparameters For reproducibility we pin exact snapshots. API: gpt-4o-mini-2024-07-18, gpt-4.1-2025-04-14, gpt-5.1-2025-11-13, gpt-5.5-2026-04-23, claude-sonnet-4-6, and DeepSeek deepseekv4-flash/deepseek-v4-pro (api.deepseek.com). Open weights at their default HuggingFace revisions: Qwen/Qwen2.5-Coder-7B-Instruct and -32B-Instruct, NousResearch/MetaLlama-3.1-8B-Instruct, microsoft/phi-4, and deepseek-ai/deepseek-coder-6.7b-instruct. All writers: temperature 0.7 (reasoning models at default), max output 1300 tokens, repair budget K=3. Inference: NUTS, 2 chains, 600+600 (detection) or 800+800 (de-risk) draws, x64. Oracle thresholds α=0.01, δ=0.15

11

1.0

detection rate

0.8 0.6 0.4 0.2 0.0

calibration oracle (AUC=0.97) chance default operating point

0.0

0.2

0.4 0.6 false positive rate

0.8

1.0

Figure 6: ROC of the calibration oracle as a continuous detector over 200 instances (AUC 0.97). The red point marks a low-FPR operating point (≈ 85% detection at 2% FPR on the continuous score; the discrete verdict of Eq. (5), which adds the reference-lppd clause, reaches 88%).

threshold

value

PPC tail α lppd gap δ (nats) R̂ cutoff divergence cutoff

0.005 / 0.01 / 0.05 0.05 / 0.15 / 0.25 1.01 / 1.05 / 1.20 5 / 15 / 30

detection

false-pos.

88 / 88 / 92% 90 / 85 / 76% 88 / 88 / 88% 89 / 88 / 88%

2 / 2 / 6% 2 / 2 / 2% 6 / 2 / 2% 7 / 2 / 1%

Table 7: Threshold sensitivity (one threshold varied at a time; others at default). The oracle is stable around the chosen operating point.

12

Record · ID 324912 · SHA-256 2a4a4cf0f6d975d6
Retrieved via Conceptio — every document is proof-bundled with source, license, and retrieval metadata.