def run_sampler(self): from cpnest import model as cpmodel, CPNest from cpnest.parameter import LivePoint from cpnest.nest2pos import compute_weights class Model(cpmodel.Model): """ A wrapper class to pass our log_likelihood into cpnest """ def __init__(self, names, priors): self.names = names self.priors = priors self._update_bounds() @staticmethod def log_likelihood(x, **kwargs): theta = [x[n] for n in self.search_parameter_keys] return self.log_likelihood(theta) @staticmethod def log_prior(x, **kwargs): theta = [x[n] for n in self.search_parameter_keys] return self.log_prior(theta) def _update_bounds(self): self.bounds = [[ self.priors[key].minimum, self.priors[key].maximum ] for key in self.names] def new_point(self): """Draw a point from the prior""" prior_samples = self.priors.sample() self._update_bounds() point = LivePoint( self.names, array.array('d', [prior_samples[name] for name in self.names])) return point self._resolve_proposal_functions() model = Model(self.search_parameter_keys, self.priors) out = None remove_kwargs = ["proposals", "n_periodic_checkpoint"] while out is None: try: out = CPNest(model, **self.kwargs) except TypeError as e: if len(remove_kwargs) > 0: kwarg = remove_kwargs.pop(0) else: raise TypeError("Unable to initialise cpnest sampler") logger.info( "CPNest init. failed with error {}, please update".format( e)) logger.info( "Attempting to rerun with kwarg {} removed".format(kwarg)) self.kwargs.pop(kwarg) try: out.run() except SystemExit as e: import sys logger.info("Caught exit code {}, exiting with signal {}".format( e.args[0], self.exit_code)) sys.exit(self.exit_code) if self.plot: out.plot() self.calc_likelihood_count() self.result.posterior = DataFrame(out.posterior_samples) self.result.nested_samples = DataFrame( out.get_nested_samples(filename='')) self.result.nested_samples.rename(columns=dict(logL='log_likelihood'), inplace=True) self.result.posterior.rename(columns=dict(logL='log_likelihood', logPrior='log_prior'), inplace=True) _, log_weights = compute_weights( np.array(self.result.nested_samples.log_likelihood), np.array(out.NS.state.nlive)) self.result.nested_samples['weights'] = np.exp(log_weights) self.result.log_evidence = out.NS.state.logZ self.result.log_evidence_err = np.sqrt(out.NS.state.info / out.NS.state.nlive) self.result.information_gain = out.NS.state.info return self.result
def run_sampler(self): from cpnest import model as cpmodel, CPNest from cpnest.parameter import LivePoint from cpnest.nest2pos import compute_weights class Model(cpmodel.Model): """ A wrapper class to pass our log_likelihood into cpnest """ def __init__(self, names, priors): self.names = names self.priors = priors self.bounds = [[ self.priors[key].minimum, self.priors[key].maximum ] for key in self.names] @staticmethod def log_likelihood(x, **kwargs): theta = [x[n] for n in self.search_parameter_keys] return self.log_likelihood(theta) @staticmethod def log_prior(x, **kwargs): theta = [x[n] for n in self.search_parameter_keys] return self.log_prior(theta) def new_point(self): """Draw a point from the prior""" point = LivePoint( self.names, [self.priors[name].sample() for name in self.names]) return point self._resolve_proposal_functions() model = Model(self.search_parameter_keys, self.priors) try: out = CPNest(model, **self.kwargs) except TypeError as e: if 'proposals' in self.kwargs.keys(): logger.warning( 'YOU ARE TRYING TO USE PROPOSALS IN A VERSION OF CPNEST THAT DOES' 'NOT ACCEPT CUSTOM PROPOSALS. SAMPLING WILL COMMENCE WITH THE DEFAULT' 'PROPOSALS.') del self.kwargs['proposals'] out = CPNest(model, **self.kwargs) else: raise TypeError(e) out.run() if self.plot: out.plot() self.calc_likelihood_count() self.result.posterior = DataFrame(out.posterior_samples) self.result.nested_samples = DataFrame( out.get_nested_samples(filename='')) self.result.nested_samples.rename(columns=dict(logL='log_likelihood'), inplace=True) self.result.posterior.rename(columns=dict(logL='log_likelihood', logPrior='log_prior'), inplace=True) _, log_weights = compute_weights( np.array(self.result.nested_samples.log_likelihood), np.array(out.NS.state.nlive)) self.result.nested_samples.weights = np.exp(log_weights) self.result.log_evidence = out.NS.state.logZ self.result.log_evidence_err = np.sqrt(out.NS.state.info / out.NS.state.nlive) return self.result