dynesty icon indicating copy to clipboard operation
dynesty copied to clipboard

Pool nested sampling with rslice fails

Open damiansuski opened this issue 4 months ago • 4 comments

** Dynesty version ** Version 2.1.4 (pip install) on Windows

Describe the bug I've got a simple task, to find the parameters for noisy sinusoidal signal with 2 jumps. When I run the nested sampler without a pool, dynesty finds a correct solution (mean values of the parameters are very close to the original ones). When I use the pool, the calculations break after some iterations with the "RuntimeError: Slice sampler has failed to find a valid point."

Setup I am running the following dynesty setup:

import numpy as np
import dynesty
import time


def calcX(phi0, T, A, b, c, w, h):
    phi = phi0+2*np.pi*t/T
    step = np.zeros(N)
    step[int(c-1-w/2):int(c-1+w/2)] = h
    phi += step
    x = b+A*np.sin(phi)
    return x


t0 = 0; t1 = 479; N = 480; t = np.linspace(t0, t1, N)
phi0 = 0; T = 100; f = 1/T

A = 0.2; b = 0.6; c = 200; w = 170; h = 9;
xg = calcX(phi0, T, A, b, c, w, h)

errA = 0.01
err = errA * np.random.randn(N)
var = np.power(errA, 2)

xtrue = xg + err
varMax = np.var(xtrue)

ndim = 8
truth = np.array([var, phi0, T, A, b, c, w, h])
bias = np.array([0, 3, 0, 0, 0, 0, 0, 0])
lbw = np.array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5])
ubw = np.array([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5])
lbounds = -bias + truth*lbw
ubounds = bias + truth*ubw

def priors(u):
    x = lbounds+u*(ubounds-lbounds)
    return x

def likelihood(params):

    var, phi0, T, A, b, c, w, h = params
    xm = calcX(phi0, T, A, b, c, w, h)
    loglik = -N/2*np.log(2*np.pi*var) - 0.5*(1/var)*np.sum(np.pow(xtrue-xm, 2))

    return loglik


if __name__ == '__main__':

    start = time.time()
    njobs = 8
    with dynesty.pool.Pool(njobs, likelihood, priors) as pool:
        dsampler=dynesty.NestedSampler(pool.loglike,pool.prior_transform,ndim, pool=pool, sample='rslice', nlive=2000)
        dsampler.run_nested()
    end = time.time()
    print("Execution time = " + str(end - start))

** Dynesty output **

iter: 7974 | bound: 1 | nc: 55 | ncall: 113123 | eff(%):  7.049 | loglstar:   -inf < -46862.037 <    inf | logz: -46874.317 +/-  0.078 | dlogz: 39254.963 >  2.009                                    multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\multiprocessing\pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "C:\Program Files\Python310\lib\multiprocessing\pool.py", line 48, in mapstar
    return list(map(*args))
  File "C:\Users\damian\GitHubRepos\BayInfInOptMet\venv\lib\site-packages\dynesty\sampling.py", line 674, in sample_rslice
    expansion_warning) = generic_slice_step(u, direction, nonperiodic,
  File "C:\Users\damian\GitHubRepos\BayInfInOptMet\venv\lib\site-packages\dynesty\sampling.py", line 469, in generic_slice_step
    raise RuntimeError("Slice sampler has failed to find "
RuntimeError: Slice sampler has failed to find a valid point. Some useful output quantities:
u: [0.56937563 0.43284761 0.50060317 0.34334755 0.62518963 0.62770033
 0.65149697 0.67537055]
nstep_left: -1e-323
nstep_right: 1e-323
nstep_hat: 2e-323
u_prop: [0.56937563 0.43284761 0.50060317 0.34334755 0.62518963 0.62770033
 0.65149697 0.67537055]
loglstar: -46861.373491426835
logl_prop: -46902.87511728766
direction: [ 0.00060745  0.03356997  0.12729417  0.05321926 -0.00706289 -0.03991981
 -0.03722473 -0.07861405]

"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\damian\GitHubRepos\BayInfInOptMet\dynestyMultiProc\dynestyMultiprocTestIssue.py", line 55, in <module>
    dsampler.run_nested()
  File "C:\Users\damian\GitHubRepos\BayInfInOptMet\venv\lib\site-packages\dynesty\sampler.py", line 1025, in run_nested
    for it, results in enumerate(
  File "C:\Users\damian\GitHubRepos\BayInfInOptMet\venv\lib\site-packages\dynesty\sampler.py", line 848, in sample
    u, v, logl, nc = self._new_point(loglstar_new)
  File "C:\Users\damian\GitHubRepos\BayInfInOptMet\venv\lib\site-packages\dynesty\sampler.py", line 421, in _new_point
    u, v, logl, nc, blob = self._get_point_value(loglstar)
  File "C:\Users\damian\GitHubRepos\BayInfInOptMet\venv\lib\site-packages\dynesty\sampler.py", line 404, in _get_point_value
    self._fill_queue(loglstar)
  File "C:\Users\damian\GitHubRepos\BayInfInOptMet\venv\lib\site-packages\dynesty\sampler.py", line 397, in _fill_queue
    self.queue = list(mapper(evolve_point, args))
  File "C:\Users\damian\GitHubRepos\BayInfInOptMet\venv\lib\site-packages\dynesty\pool.py", line 156, in map
    return self.pool.map(F, x)
  File "C:\Program Files\Python310\lib\multiprocessing\pool.py", line 364, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "C:\Program Files\Python310\lib\multiprocessing\pool.py", line 771, in get
    raise self._value
RuntimeError: Slice sampler has failed to find a valid point. Some useful output quantities:
u: [0.56937563 0.43284761 0.50060317 0.34334755 0.62518963 0.62770033
 0.65149697 0.67537055]
nstep_left: -1e-323
nstep_right: 1e-323
nstep_hat: 2e-323
u_prop: [0.56937563 0.43284761 0.50060317 0.34334755 0.62518963 0.62770033
 0.65149697 0.67537055]
loglstar: -46861.373491426835
logl_prop: -46902.87511728766
direction: [ 0.00060745  0.03356997  0.12729417  0.05321926 -0.00706289 -0.03991981
 -0.03722473 -0.07861405]

Bug The behaviour I expected was that the pool will only be used to parallelize the likelihood computations and it won't affect the convergence of the algorithm.

damiansuski avatar Oct 11 '24 09:10 damiansuski