def test_dynamic_param(self): """Test numerical results for nested sampling with dynamic_goal=1.""" dynamic_goal = 1 dyPolyChord.run_dypolychord( self.run_func, dynamic_goal, self.settings, init_step=self.ninit, ninit=self.ninit) run = nestcheck.data_processing.process_polychord_run( self.settings['file_root'], self.settings['base_dir']) first_logl = -137.231721859574 if not np.isclose(run['logl'][0], first_logl): warnings.warn( self.random_seed_msg.format(run['logl'][0], first_logl), UserWarning) else: self.assertAlmostEqual(e.param_mean(run), -0.05323120028149568, places=12)
def test_dynamic_evidence(self): """Test numerical results for nested sampling with dynamic_goal=0.""" dynamic_goal = 0 dyPolyChord.run_dypolychord( self.run_func, dynamic_goal, self.settings, init_step=self.ninit, ninit=self.ninit) run = nestcheck.data_processing.process_polychord_run( self.settings['file_root'], self.settings['base_dir']) first_logl = -158.773632799691 if not np.isclose(run['logl'][0], first_logl): warnings.warn( self.random_seed_msg.format(run['logl'][0], first_logl), UserWarning) else: self.assertEqual(e.count_samples(run), 1169) self.assertAlmostEqual(e.param_mean(run), 0.026487985350451874, places=12)
def test_dynamic_param(self): """Check run_dypolychord targeting evidence. This uses dummy PolyChord-format data.""" dynamic_goal = 1 dyPolyChord.run_dypolychord( self.run_func, dynamic_goal, self.settings, init_step=self.ninit, ninit=self.ninit, nlive_const=self.nlive_const, stats_means_errs=False) # Check the mean value using the posteriors file (its hard to make a # dummy run_func which is realistic enough to not fail checks if we try # loading the output normally with # nesthcheck.data_processing.process_polychord_run). posteriors = np.loadtxt(os.path.join( self.settings['base_dir'], self.settings['file_root'] + '.txt')) # posteriors have columns: weight / max weight, -2*logl, [params] p1_mean = (np.sum(posteriors[:, 2] * posteriors[:, 0]) / np.sum(posteriors[:, 0])) self.assertAlmostEqual(p1_mean, 0.614126384660822, places=12)
def test_dynamic_both_evidence_and_param(self): """Test numerical results for nested sampling with dynamic_goal=0.25.""" dynamic_goal = 0.25 dyPolyChord.run_dypolychord( self.run_func, dynamic_goal, self.settings, init_step=self.ninit, ninit=self.ninit) run = nestcheck.data_processing.process_polychord_run( self.settings['file_root'], self.settings['base_dir']) first_logl = -165.502617578541 if not np.isclose(run['logl'][0], first_logl): warnings.warn( self.random_seed_msg.format(run['logl'][0], first_logl), UserWarning) else: self.assertEqual(e.count_samples(run), 1093) self.assertAlmostEqual(e.param_mean(run), -0.0021307716191374263, places=12)
def run(self, verbose=False): """Initiate the dyPolyChord Nested Sampling run.""" #output = pypolychord.run_polychord(self._likelihood, self._nDims, # self._nDerived, self._settings, # self._prior, self._dumper) # Make the dyPolyChord callable for running PolyChord dypc_callable = dyPolyChord.pypolychord_utils.RunPyPolyChord( self._likelihood, self._prior, self._nDims) # Run dyPolyChord dyPolyChord.run_dypolychord(dypc_callable, self.dynamic_goal, self._settings_dict, ninit=self.initial_population_size, nlive_const=self.population_size) # load the dyPolyChord run output into nestcheck run = nestcheck.data_processing.process_polychord_run( self._settings_dict['file_root'], # = settings['file_root'] self._settings_dict['base_dir']) # = settings['base_dir'] self._run = run self._logZ = ncheck_e.logz(run) self._Z = ncheck_e.evidence(run) return self._logZ, None
def PC_fit(self,nlive_const='auto', dynamic=True,dynamic_goal=1.0, ninit=100, basename='dypc_chains', verbose=True, plot=False): ''' Parameters ---------- dynamic_goal : float, opt Parameter in [0,1] determining whether algorithm prioritizes accuracy in evidence accuracy (goal near 0) or parameter estimation (goal near 1). ninit : int, opt Number of live points to use in initial exploratory run. nlive_const : int, opt Total computational budget, equivalent to non-dynamic nested sampling with nlive_const live points. dynamic : bool, opt If True, use dynamic nested sampling via dyPolyChord. Otherwise, use the standard PolyChord. basename : str, opt Location in which chains will be stored. verbose : bool, opt If True, text will be output on the terminal to check how run is proceeding. plot : bool, opt Display some sample plots to check result of dynamic slice nested sampling. ''' if dynamic: print('Dynamic slice nested sampling') else: print('Slice nested sampling') # obtain maximum likelihood fits theta0 = self.lineModel.guessFit() self.result_ml = self.optimizeFit(theta0) self.theta_ml = self.result_ml['x'] # save theta_ml also in the lineModel object, # so that constraints may be set based on ML result self.lineModel.theta_ml = self.theta_ml # save moments obtained from maximum likelihood optimization self.m_ml = self.lineModel.modelMoments(self.theta_ml) # dimensionality of the problem self.ndim = int(self.lineModel.thetaLength()) if dynamic: # dyPolyChord (dynamic slice nested sampling) # ------------------------------ try: import dyPolyChord.pypolychord_utils import dyPolyChord except: print("********************") print("Could not import dyPolyChord! Make sure that this is in your PYTHONPATH.") print("PolyChord must also be on your LD_LIBRARY_PATH") raise ValueError("Abort BSFC fit") #Make a callable for running dyPolyChord my_callable = dyPolyChord.pypolychord_utils.RunPyPolyChord( self.PC_loglike, self.lineModel.hypercube_lnprior_generalized_simplex, self.ndim ) # Specify sampler settings (see run_dynamic_ns.py documentation for more details) settings_dict = {'file_root': 'bsfc', 'base_dir': basename, 'seed': 1} # Run dyPolyChord MPI_parallel=True if MPI_parallel: from mpi4py import MPI comm = MPI.COMM_WORLD dyPolyChord.run_dypolychord(my_callable, dynamic_goal, settings_dict, ninit=ninit, nlive_const=int(25*self.ndim) if nlive_const=='auto' else nlive_const, comm=comm) else: dyPolyChord.run_dypolychord(my_callable, dynamic_goal, settings_dict, ninit=ninit, nlive_const=int(25*self.ndim) if nlive_const=='auto' else nlive_const) else: # PolyChord (slice nested sampling) # ------------------------------ try: import pypolychord from pypolychord.settings import PolyChordSettings except: print("********************") print("Could not import pypolychord! Make sure that this is in your PYTHONPATH.") raise ValueError("Abort BSFC fit") nDerived=0 settings = PolyChordSettings(self.ndim, nDerived) settings.file_root = 'bsfc' settings.base_dir = basename settings.nlive = int(25*self.ndim) if nlive_const=='auto' else int(nlive_const) #settings.do_clustering = True #settings.read_resume = False settings.feedback = 3 def dumper(live, dead, logweights, logZ, logZerr): #print("Last dead point:", dead[-1]) print("logZ = "+str(logZ)+"+/-"+str(logZerr)) self.polychord_output = pypolychord.run_polychord(self.PC_loglike, self.ndim, nDerived, settings, self.lineModel.hypercube_lnprior_generalized_simplex, dumper) self.good=True
def compute_fit(self): self._polychord_output = None data = self._observed.spectrum datastd = self._observed.errorBar sqrtpi = np.sqrt(2 * np.pi) ndim = len(self.fitting_parameters) def polychord_loglike(cube): # log-likelihood function called by polychord fit_params_container = np.array( [cube[i] for i in range(len(self.fitting_parameters))]) chi_t = self.chisq_trans(fit_params_container, data, datastd) # print('---------START---------') # print('chi_t',chi_t) # print('LOG',loglike) loglike = -np.sum(np.log(datastd * sqrtpi)) - 0.5 * chi_t return loglike, [0.0] def polychord_uniform_prior(hypercube): # prior distributions called by polychord. Implements a uniform prior # converting parameters from normalised grid to uniform prior # print(type(cube)) cube = [0.0] * ndim for idx, bounds in enumerate(self.fit_boundaries): # print(idx,self.fitting_parameters[idx]) bound_min, bound_max = bounds cube[idx] = (hypercube[idx] * (bound_max - bound_min)) + bound_min #print('CUBE idx',cube[idx]) # print('-----------') return cube status = None datastd_mean = np.mean(datastd) likelihood = polychord_loglike prior = polychord_uniform_prior # Make a callable for running PolyChord my_callable = dyPolyChord.pypolychord_utils.RunPyPolyChord( likelihood, prior, ndim) # Specify sampler settings (see run_dynamic_ns.py documentation for more details) # whether to maximise parameter estimation or evidence accuracy. dynamic_goal = 1.0 # number of live points to use in initial exploratory run. ninit = ndim * 5 # total computational budget is the same as standard nested sampling with nlive_const live points. nlive_const = ndim * 25 settings_dict = { 'num_repeats': ndim * 5, 'do_clustering': self.do_clustering, 'num_repeats': ndim, 'precision_criterion': self.evidence_tolerance, 'logzero': -1e70, 'read_resume': self.resume, 'base_dir': self.dir_polychord, 'file_root': '1-' } try: from mpi4py import MPI comm = MPI.COMM_WORLD # Run dyPolyChord dyPolyChord.run_dypolychord(my_callable, dynamic_goal, settings_dict, ninit=ninit, nlive_const=nlive_const, comm=comm) except ImportError: dyPolyChord.run_dypolychord(my_callable, dynamic_goal, settings_dict, ninit=ninit, nlive_const=nlive_const) time.sleep(2.0) #pypolychord.run_polychord(polychord_loglike, ndim, 1, settings, polychord_uniform_prior) self._polychord_output = self.store_polychord_solutions()