Repository navigation
Description
Overview
Emcee.samples_via_internal_from builds each sample's likelihood from two arrays that
are not in correspondence: parameter_lists is the chain after burn-in discard and
thinning, while log_posterior_list is a contiguous tail slice of the raw, unthinned,
undiscarded flat log-probability array (with an unexplained one-element offset). Sample
i's reported likelihood therefore belongs to a different sample. Zeus has the same
defect via a silent zip() truncation. Found while calibrating a HowToFit tutorial;
verified independently twice.
Plan
- Ask the sampler for log-probabilities under the same
discard/thinas the chain. - Bind
discard/thinin both the test-mode and normal branches, since only the normal
branch defines them today. - Apply the equivalent fix to
Zeus, whosezip()hides a length mismatch. - Add a regression test asserting the invariant — the reported max-likelihood equals
theAnalysislikelihood recomputed from the reported instance — rather than pinning a
number. - Sweep the libraries and workspaces for pinned Emcee/Zeus likelihood values that this
correction will move.
Detailed implementation plan
Affected Repositories
- PyAutoFit (primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoFit | main | clean |
Suggested branch: feature/emcee-zeus-log-prob-alignment
The defect
autofit/non_linear/search/mcmc/emcee/search.py, samples_via_internal_from:
samples_after_burn_in = search_internal.get_chain(discard=discard, thin=thin, flat=True)
parameter_lists = samples_after_burn_in.tolist()
total_samples = len(parameter_lists)
log_posterior_list = search_internal.get_log_prob(flat=True)[-total_samples - 1 : -1].tolist()autofit/non_linear/search/mcmc/zeus/search.py (~line 373):
log_posterior_list = search_internal.get_log_prob(flat=True).tolist()…against a possibly-thinned samples_after_burn_in; the following zip() truncates
silently.
Consequences
result.max_log_likelihood_instance/samples.max_log_likelihood()are the argmax of
a scrambled array — effectively a random post-burn-in draw, not the MLE.- The "Maximum Log Likelihood" line of every Emcee and Zeus
result.infois wrong. log_likelihood_listis wrong per-sample, so anything derived from it inherits the error.
Nested sampling and the MLE searches are unaffected.
Evidence (verified twice)
On the HowToFit tutorial 3 dataset with af.Emcee(nwalkers=20, nsteps=500):
reported max_log_likelihood_sample.log_likelihood |
188.9628 |
actual analysis.log_likelihood_function(max_log_likelihood()) |
188.8337 |
A second run showed a far larger gap — reported 188.34 for parameters whose true
likelihood is 176.52. The error scales with how much post-burn-in samples differ, so a
poorly converged chain is punished hardest: exactly when a trustworthy MLE matters most.
Implementation Steps
- In
emcee/search.py, binddiscard/thinin both branches (test mode uses
discard=5, thin=5), then replace the tail-slice with
search_internal.get_log_prob(discard=discard, thin=thin, flat=True).tolist(). - Apply the equivalent in
zeus/search.pyfor whichever branch produced
samples_after_burn_in, including its fallback path. - Assert
len(parameter_lists) == len(log_posterior_list)before zipping, so a future
mismatch fails loudly instead of truncating. - Regression test: run a short Emcee fit and assert
samples.max_log_likelihood_sample.log_likelihoodequals theAnalysislikelihood
recomputed fromsamples.max_log_likelihood()to floating-point tolerance. Do not pin
a literal likelihood. - Run the full PyAutoFit suite and grep the workspaces for pinned Emcee/Zeus likelihoods.
Key Files
autofit/non_linear/search/mcmc/emcee/search.pyautofit/non_linear/search/mcmc/zeus/search.pytest_autofit/non_linear/mcmc/
Blast radius
This moves the reported maximum-likelihood numbers of every existing Emcee and Zeus fit.
They were wrong before, so this is a correction — but it will move any pinned value.
Original Prompt
Click to expand starting prompt
# Emcee and Zeus pair thinned parameters with unthinned log-probabilities
Type: bug
Target: PyAutoFit
Repos:
- PyAutoFit
Themes:
- mcmc
- samples
- correctness
Difficulty: small
Autonomy: safe
Priority: high
Status: formalised
Consequence: judge
Review-minutes: 15
Unattended: ready
Filed: 2026-09-14
## The bug
`autofit/non_linear/search/mcmc/emcee/search.py`, `samples_via_internal_from`:
```python
samples_after_burn_in = search_internal.get_chain(discard=discard, thin=thin, flat=True)
parameter_lists = samples_after_burn_in.tolist()
total_samples = len(parameter_lists)
log_posterior_list = search_internal.get_log_prob(flat=True)[-total_samples - 1 : -1].tolist()parameter_lists is the chain after burn-in discard and thinning.
log_posterior_list is a contiguous tail slice of the raw, unthinned,
undiscarded flat log-probability array — plus an unexplained one-element
offset ([-total_samples - 1 : -1] drops the last entry and shifts by one).
The two are not in correspondence. Sample i's reported likelihood belongs to
some other sample.
autofit/non_linear/search/mcmc/zeus/search.py (~line 373) has the same defect
in a different form:
log_posterior_list = search_internal.get_log_prob(flat=True).tolist()against a samples_after_burn_in that may be thinned. The subsequent zip()
silently truncates to the shorter list, pairing thinned parameters with the
first N unthinned log-probs.
Consequences
result.max_log_likelihood_instance/samples.max_log_likelihood()return
the argmax of a scrambled array — effectively a random post-burn-in draw, not
the maximum likelihood sample.- The "Maximum Log Likelihood" line of every Emcee and Zeus
result.infois
wrong. log_likelihood_listis wrong per-sample, so anything deriving from it
(errors at sigma, PDF summaries computed from likelihoods, model comparison)
inherits the error.
Nested sampling and the MLE searches are unaffected.
Evidence (verified twice, 2026-09-14)
Independent reproduction in-session on the HowToFit tutorial 3 dataset,
af.Emcee(nwalkers=20, nsteps=500):
- reported
max_log_likelihood_sample.log_likelihood= 188.9628 - actual
analysis.log_likelihood_function(instance=max_log_likelihood())=
188.8337
A second run during calibration showed a much larger gap: reported 188.34
for parameters whose true likelihood is 176.52. The size of the error scales
with how much the post-burn-in samples differ, so a poorly converged chain is
punished hardest — exactly the case where a trustworthy MLE matters most.
Fix
Ask emcee for the log-probabilities under the same discard/thin as the chain:
log_posterior_list = search_internal.get_log_prob(
discard=discard, thin=thin, flat=True
).tolist()discard / thin currently exist only in the non-test-mode branch, so both
branches must bind them (test mode uses discard=5, thin=5). Zeus needs the
equivalent treatment for whichever branch produced its samples_after_burn_in.
Regression test
Assert the invariant directly rather than pinning a number: for a completed
Emcee fit, samples.max_log_likelihood_sample.log_likelihood must equal the
Analysis log likelihood recomputed from samples.max_log_likelihood(), to
floating-point tolerance. Assert len(parameter_lists) == len(log_posterior_list)
too — the current code can only be caught by the values, not the lengths.
Blast radius
This changes the reported maximum-likelihood numbers of every existing Emcee and
Zeus fit. They were wrong before, so this is a correction, but it will move
numbers in any test or doc that pinned them — check for pinned Emcee likelihoods
across the libraries and workspaces.
</details>
Activity
Jammy2211 commentedon Sep 14, 2026
Shipped
Merged in #1629, CI green on all 4 checks; full suite 2852 passed / 2 skipped.
Both
EmceeandZeusnow request log posteriors under the samediscardandthinas the chain, bind those in both branches of the test-mode conditional, and raiseexc.SamplesExceptionon a length mismatch — replacing Zeus's silentzip()truncation. emcee's and zeus's API semantics were introspected rather than assumed.Verified independently of the implementing agent: the reproduction re-run against the patched library gave a worst-case
|reported - actual|of exactly 0.000e+00 over three trials, against 0.129 before (and 11.82 on a poorly converged chain). The regression tests were re-run by hand against the reverted source and fail there, so they genuinely catch the bug.Pinned-value sweep across PyAutoFit and
autofit_workspacefound none, so nothing else moved.Follow-ups deliberately not bundled, since both change values or behaviour rather than correcting a misalignment:
thincan be 0 (ValueError: slice step cannot be zero), andcheck_size=100raisesIndexErroron chains shorter than 100 — always true underPYAUTO_TEST_MODE=1; Zeus guards it, Emcee does not. Filed inPyAutoMind/draft/bug/autofit/.refactor: one samples adapter; samples_via_internal_from implemented …
refactor: ChainPosterior.thin shared by Emcee, Zeus and NUTS