def TGN_sample(size, gamma, alpha, x_min, x_max): domain = (x_min, x_max) return ars.adaptive_rejection_sampling( logpdf=lambda x: log_TGN_pdf(x, gamma, alpha, x_min, x_max), a=x_min, b=x_max, domain=domain, n_samples=size)
def ars_(self, beta, j): samples = adaptive_rejection_sampling( logpdf=lambda x: self.fi_pos_logdist(fi_j=x, beta=beta, j=j), a=self.ars_a, b=self.ars_b, domain=self.ars_domain, n_samples=self.m) return samples
def _run(test_name): input_dict = tests[test_name] # name = input_dict["name"] a = input_dict["a"] b = input_dict["b"] domain = input_dict["domain"] n_samples = input_dict["n_samples"] logpdf = input_dict["func"] python_result = adaptive_rejection_sampling( logpdf=logpdf, a=a, b=b, domain=domain, n_samples=n_samples, random_stream=np.random.RandomState(seed=1)) # load old result computed by other implementation (julia) julia_result = np.load(input_dict["data"]) assert (allclose(julia_result, python_result, atol=3e-01))
domain = [-float('inf'), 0] n_samples = 20000 sigma = 3 def halfgaussian_logpdf(x): out = np.log(np.exp(-x**2 / sigma)) * np.heaviside(-x, 1) return out xs = np.arange(-3 * sigma, 3 * sigma, 0.1) y = np.exp(halfgaussian_logpdf(xs)) samples = adaptive_rejection_sampling(logpdf=halfgaussian_logpdf, a=a, b=b, domain=domain, n_samples=n_samples) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(18, 6)) # Title ax1.set_title("f(x) half-guassian") # Fix the plot size ax1.set_xlim(-3 * sigma, 3 * sigma) ax1.set_ylim(0, 1) ax1.plot(xs, y) # Title
def _sample_relativistic_momentum(m, c, n_params, bounds=(float("-inf"), float("inf")), seed=None): """ Use adaptive rejection sampling (here: provided by external library `ARSpy`) to sample initial values for relativistic momentum `p`. The relativistic momentum variable in Relativistic MCMC has (marginal) distribution .. math:: \\propto e^{-K(p)} where :math:`K(p)` is the relativistic kinetic energy. This distribution is a multivariate generalisation of the symmetric hyperbolic distribution, which cannot easily be sampled directly. Therefore we resort to *adaptive rejection sampling* to generate our samples and initialize our momentum terms properly. See `the paper "Relativistic Monte Carlo" <http://proceedings.mlr.press/v54/lu17b/lu17b.pdf#page=2>`_ for more information on Relativistic Hamiltonian Dynamics. See `Generalized hyperbolic distribution <https://en.wikipedia.org/wiki/Generalised_hyperbolic_distribution>`_ for more information on our target distribution. Parameters ---------- m : float Mass constant used for sampling. c : float Speed of light constant used for sampling. n_params : int Number of target parameters of the target log pdf to sample from. bounds : Tuple[float, float], optional Adaptive rejection sampling bounds to use during sampling. Defaults to `(float("-inf"), float("inf"))`, i.e. unbounded adaptive rejection sampling. seed : int Random seed to use for adaptive rejection sampling. Returns ---------- momentum_samples : list Samples used to initialize our samplers momentum variables. Examples ---------- Drawing 10 momentum values for 10 target parameters via (unbounded) adaptive rejection sampling: >>> n_params = 10 >>> momentum_values = _sample_relativistic_momentum(m=1.0, c=1.0, n_params=n_params) >>> len(momentum_values) == n_params True See also ---------- `ARSpy`: Our external dependency that handles adaptive rejection sampling. Available `here <https://github.com/MFreidank/pyars>`_. """ # XXX: Remove when more is supported, currently only floats for mass # and c are. assert isinstance(m, float) assert isinstance(c, float) def generate_relativistic_logpdf(m, c): def relativistic_log_pdf(p): """ Logarithm of pdf of (multivariate) generalized hyperbolic distribution. """ from numpy import sqrt return -m * c**2 * sqrt(p**2 / (m**2 * c**2) + 1.) return relativistic_log_pdf momentum_log_pdf = generate_relativistic_logpdf(m=m, c=c) return adaptive_rejection_sampling(logpdf=momentum_log_pdf, a=-10.0, b=10.0, domain=bounds, n_samples=n_params, seed=seed)