Skip to content

Instantly share code, notes, and snippets.

@ipashchenko
Created March 14, 2019 21:57
Show Gist options
  • Save ipashchenko/bdd8a9984d1a0782affca7e66c0a4a3e to your computer and use it in GitHub Desktop.
Save ipashchenko/bdd8a9984d1a0782affca7e66c0a4a3e to your computer and use it in GitHub Desktop.
Example of the forced sorted priors from polychord.
import numpy as np
# From pypolychord/priors.py
class UniformPrior:
def __init__(self, a, b):
self.a = a
self.b = b
def __call__(self, x):
return self.a + (self.b-self.a) * x
def forced_indentifiability_transform(x):
N = len(x)
t = np.zeros(N)
t[N-1] = x[N-1]**(1./N)
for n in range(N-2, -1, -1):
t[n] = x[n]**(1./(n+1)) * t[n+1]
return t
class SortedUniformPrior(UniformPrior):
def __call__(self, x):
t = forced_indentifiability_transform(x)
return super(SortedUniformPrior, self).__call__(t)
def prior(hypercube):
""" Uniform prior from [-10,10]^D. """
return SortedUniformPrior(-10, 10)(hypercube)
# Polychord generate in unit cude
ndim = 5
hyper_points = np.atleast_2d([np.random.uniform(0, 1, ndim) for i in range(10000)])
# This corresponds to physical space
phys_points = np.atleast_2d([prior(hyper_point) for hyper_point in hyper_points])
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 1)
for dim in range(ndim):
axes.hist(hyper_points[:, dim], alpha=0.2, label="dim = {}".format(dim),
density=True)
axes.legend()
axes.set_title("HyperCube space")
fig.show()
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 1)
for dim in range(ndim):
axes.hist(phys_points[:, dim], alpha=0.2, label="dim = {}".format(dim),
density=True)
axes.legend()
axes.set_title("Phys space")
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment