joshspeagle / dynesty

Dynamic Nested Sampling package for computing Bayesian posteriors and evidences
https://dynesty.readthedocs.io/
MIT License
347 stars 76 forks source link

DynamicNestedSampler.results.summary function fails because of nlive #184

Closed JohannesBuchner closed 3 years ago

JohannesBuchner commented 4 years ago

I am not sure why, but the following code fails for me at the last line:

# -*- coding: utf-8 -*-
from __future__ import (print_function, division)
import sys
import numpy as np
import dynesty

# EGGBOX
def loglike(x):
    tmax = 5.0 * np.pi
    t = 2.0 * tmax * x - tmax
    return (2.0 + np.cos(t[0] / 2.0) * np.cos(t[1] / 2.0)) ** 5.0

def prior_transform(x):
    return x

print("with nested sampler:")
sampler = dynesty.NestedSampler(loglike, prior_transform, 2)
sampler.run_nested()
sampler.results.summary()

print("with dynamic nested sampler:")
sampler = dynesty.DynamicNestedSampler(loglike, prior_transform, 2)
sampler.run_nested()
sampler.results.summary()

with the output:

± python3 run1.py 
with nested sampler:
3851it [00:06, 560.91it/s, +500 | bound: 7 | nc: 1 | ncall: 22127 | eff(%): 19.664 | loglstar:   -inf < 243.000 <    inf | logz: 236.150 +/-  0.153 | dlogz:  0.001 >  0.509]
Summary
=======
nlive: 500
niter: 3851
ncall: 22127
eff(%): 19.664
logz: 236.150 +/-  0.153
with dynamic nested sampler:
21297it [05:42, 62.15it/s, batch: 11 | bound: 57 | nc: 3 | ncall: 55311 | eff(%): 38.504 | loglstar: 235.513 < 243.000 < 242.767 | logz: 235.858 +/-  0.157 | stop:  0.920]        
Traceback (most recent call last):
  File "/mnt/data/daten/PostDoc2/home/dynesty/dynesty/results.py", line 181, in __getattr__
    return self[name]
KeyError: 'nlive'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "run1.py", line 34, in <module>
    sampler.results.summary()
  File "/mnt/data/daten/PostDoc2/home/dynesty/dynesty/results.py", line 205, in summary
    .format(self.nlive, self.niter, sum(self.ncall),
  File "/mnt/data/daten/PostDoc2/home/dynesty/dynesty/results.py", line 183, in __getattr__
    raise AttributeError(name)
AttributeError: nlive

This is with dynesty 1.0.2.

joshspeagle commented 4 years ago

Ah yes, this is the same bug noted in #175 that arises due to how I treat the static vs dynamic cases internally. I don't have a fix for this yet, but it should be a straightforward edit of the .summary call so I don't keep trying to pull non-existent information. How quickly would you like to see this patched up? I have a little bit more time now that I'm past my PhD defense XD.