def sample_nuts_to_df(distribution, n_samples=5000, n_burn_in=5000, **kwargs): """ runs NUTS on distribution for num_steps returns a dataframe containing samples, number of gradient evaluations at each step, distributions : initialized distributions object, must have nbatch=1 sample_steps: the number of sampling steps concatenated into one time step. LAHMC used 10 n_batch: the number of sampling runs from nuts concatenated into a batch n_burn_in: number of steps for NUTS to burn in and """ assert distribution.nbatch == 1 x_init = distribution.Xinit[:, 0] samples = nuts6(distribution, n_samples, n_burn_in, x_init)[0] grad_per_sample_step = distribution.dEdX_count / float(n_samples + n_burn_in) e_per_sample_step = distribution.E_count / float(n_samples + n_burn_in) print "grad per sample step: {}".format(grad_per_sample_step) recs = {} for t in xrange(n_samples): recs[t] = { 'X': samples[t].reshape(distribution.ndims, 1), 'num grad' : grad_per_sample_step * t, 'num energy' : e_per_sample_step * t } df = pd.DataFrame.from_records(recs).T # might want to truncate df at gradient count return df
def sample(self, pos0, M, Madapt, delta=0.6, **kwargs): """ Runs NUTS6 """ samples, lnprob, epsilon = nuts6(self._sample_fn, M, Madapt, pos0, delta) self._chain = samples self._lnprob = lnprob self._epsilon = epsilon return samples
def test_nuts6(): # Generate toy data. Nstars = int(1e5) alpha = 2.35 M_min = 1.0 M_max = 100.0 Masses = random_PowerLaw(Nstars, alpha, M_min, M_max) LogM = np.log(Masses) D = np.mean(LogM) * Nstars #NUTS pars M, Madapt = 1000, 1000 theta0 = np.asarray([3.0]) delta = 0.5 nuts_fn = NutsSampler_fn_wrapper(logLikelihood, grad_logLikelihood, D, Nstars, M_min, M_max) #nuts_fn.verbose = True t_start = time.time() print("Starting Sampling at %s" % time.ctime(t_start)) A, lnprob, epsilon = nuts6(nuts_fn, M, Madapt, theta0, delta, progress=True) t_stop = time.time() print("Sampling Completed in %0.2f seconds" % (t_stop - t_start)) plt.figure(1) # Print Monte-Carlo estimate of alpha. print("Mean: " + str(np.mean(A))) per = np.percentile(A, [16, 50, 84]) print("Alpha = {} (+{} / - {})".format(per[1], per[2] - per[1], per[1] - per[0])) n, b = np.histogram(A, 30) x = 0.5 * (b[:-1] + b[1:]) y = n.astype(float) / n.sum() plt.step(x, y, color='b', lw=3, where='mid') plt.vlines(per, 0., max(y), linestyle='--', color='b', lw=1) ylim = plt.ylim() plt.vlines(alpha, 0, ylim[1], color='r', lw=3) plt.ylim(ylim) plt.xlabel(r'$\alpha$', fontsize=24) plt.ylabel(r'$\cal L($Data$;\alpha)$', fontsize=24) plt.show()
def test_nuts6(): # Generate toy data. Nstars = int(1e5) alpha = 2.35 M_min = 1.0 M_max = 100.0 Masses = random_PowerLaw(Nstars, alpha, M_min, M_max) LogM = np.log(Masses) D = np.mean(LogM) * Nstars #NUTS pars M, Madapt = 1000, 1000 theta0 = np.asarray([3.0]) delta = 0.5 nuts_fn = NutsSampler_fn_wrapper(logLikelihood, grad_logLikelihood, D, Nstars, M_min, M_max) #nuts_fn.verbose = True t_start = time.time() print("Starting Sampling at %s" % time.ctime(t_start)) A, lnprob, epsilon = nuts6(nuts_fn, M, Madapt, theta0, delta) t_stop = time.time() print("Sampling Completed in %0.2f seconds" % (t_stop - t_start)) plt.figure(1) # Print Monte-Carlo estimate of alpha. print("Mean: " + str(np.mean(A))) per = np.percentile(A, [16, 50, 84]) print("Alpha = {} (+{} / - {})".format( per[1], per[2] - per[1], per[1] - per[0] )) n, b = np.histogram(A, 30) x = 0.5 * (b[:-1] + b[1:]) y = n.astype(float) / n.sum() plt.step(x, y, color='b', lw=3, where='mid') plt.vlines(per, 0., max(y), linestyle='--', color='b', lw=1) ylim = plt.ylim() plt.vlines(alpha, 0, ylim[1], color='r', lw=3) plt.ylim(ylim) plt.xlabel(r'$\alpha$', fontsize=24) plt.ylabel(r'$\cal L($Data$;\alpha)$', fontsize=24) plt.show()
numRej += 1 #plot of resulting distribution output = np.array(ptArr) output.shape plt.hist(invlogit(output[1, :])) #If using anaconda, call: conda install -c conda-forge from nuts import nuts6 D = beta0.shape[0] M = 1000 Madapt = 1000 delta = 0.2 def exampletargetfornuts(beta): """ Example of a target distribution that could be sampled from using NUTS. (Although of course you could sample from it more efficiently) Doesn't include the normalizing constant. """ return mylogpost(beta, ydata, nsamp, A, sens, spec), mylogpost_grad(beta, ydata, nsamp, A, sens, spec) samples, lnprob, epsilon = nuts6(exampletargetfornuts, M, Madapt, opval.x, delta) plt.hist(invlogit(samples[:, 1]))
def sample(self, quiet=False): assert self._is_setup, "You forgot to setup the sampler!" loud = not quiet self._update_free_parameters() n_dim = len(list(self._free_parameters.keys())) # Get starting point p0 = self._get_starting_points(1)[0] print(p0) # Deactivate memoization in astromodels, which is useless in this case since we will never use twice the # same set of parameters with use_astromodels_memoization(False): if threeML_config["parallel"]["use-parallel"]: c = ParallelClient() view = c[:] pool = view else: pool = None def logp(theta): return self.get_posterior(theta) def grad(theta): return numerical_grad(theta, self.get_posterior) nuts_fn = nuts.NutsSampler_fn_wrapper(self.get_posterior, grad) samples, lnprob, epsilon = nuts.nuts6(nuts_fn, self._n_iterations, self._n_adapt, p0) # sampler = nuts.NUTSSampler(n_dim, self.get_posterior, gradfn=None) # # if a seed is provided, set the random number seed # if self._seed is not None: # sampler._random.seed(self._seed) # # Run the true sampling # samples = sampler.run_mcmc( # initial_state=p0, # M=self._n_iterations, # Madapt=self._n_adapt, # delta=self._delta, # progress=loud, # ) self._sampler = None self._raw_samples = samples # Compute the corresponding values of the likelihood self._test = lnprob # First we need the prior log_prior = np.array([self._log_prior(x) for x in self._raw_samples]) # Now we get the log posterior and we remove the log prior self._log_like_values = np.array([self._log_like(x) for x in self._raw_samples]) # we also want to store the log probability self._log_probability_values = log_prior + self._log_like_values self._marginal_likelihood = None self._build_samples_dictionary() self._build_results() # Display results if loud: self._results.display() return self.samples