Created
March 12, 2014 01:42
-
-
Save Smerity/9499035 to your computer and use it in GitHub Desktop.
Issue with sampling uniform for mvslice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mvslice(pdf, x0, widths, sampleSize=1000, dims=2, burnin=0, thin=0): | |
""" | |
:param pdf: function we're trying to sample | |
:param x0: inital point | |
:param widths: prior for widths of our hyperrectangle | |
:param sampleSize: number of samples to generate | |
:param dims: dimension of our multivariate space | |
:param burnin: number of samples to get rid of at beginning | |
:param thin: number of samples to keep | |
""" | |
y0 = np.random.uniform(low=0, high=pdf(x0)) | |
samples = [] | |
# get hyperrectangle | |
rectUnifs = np.random.uniform(size=dims) | |
rectLefts = x0 - widths*rectUnifs | |
rectRights = rectLefts + widths | |
# Get our samples | |
for i in range(sampleSize): | |
while (True): | |
# new proposal | |
xstarUnifs = np.random.uniform(size=dims) | |
xstar = rectLefts + xstarUnifs*(rectRights - rectLefts) | |
if y0 < pdf(xstar): | |
break | |
else: | |
# update rectangle | |
for j in range(dims): | |
if xstar[j] < x0[j]: | |
rectLefts[j] = xstar[j] | |
else: | |
rectRights[j] = xstar[j] | |
# save our sample | |
samples.append(xstar) | |
# Our last sample x0 is now our proposal | |
x0 = xstar | |
# Get our new y0 for next step | |
y0 = np.random.uniform(low=0, high=pdf(x0)) | |
# reset our rectangle | |
rectLefts = x0 - widths*rectUnifs | |
rectRights = rectLefts + widths | |
return samples | |
pdf = lambda x: np.e**(-(x[0]**2)/2 - ((-x[1])**2)/2) | |
X = mvslice(pdf,[0,0], [10,10], sampleSize=10000) | |
Xs = [a[1] for a in X] | |
Ys = [a[0] for a in X] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mvslice(pdf, x0, widths, sampleSize=1000, dims=2, burnin=0, thin=0): | |
""" | |
:param pdf: function we're trying to sample | |
:param x0: inital point | |
:param widths: prior for widths of our hyperrectangle | |
:param sampleSize: number of samples to generate | |
:param dims: dimension of our multivariate space | |
:param burnin: number of samples to get rid of at beginning | |
:param thin: number of samples to keep | |
""" | |
y0 = np.random.uniform(low=0, high=pdf(x0)) | |
samples = [] | |
# get hyperrectangle | |
rectUnifs = np.random.uniform(size=dims) | |
rectLefts = x0 - widths*rectUnifs | |
rectRights = rectLefts + widths | |
# Get our samples | |
for i in range(sampleSize): | |
while (True): | |
# new proposal | |
xstarUnifs = np.random.uniform(size=dims) | |
xstar = rectLefts + xstarUnifs*(rectRights - rectLefts) | |
if y0 < pdf(xstar): | |
break | |
else: | |
# update rectangle | |
for j in range(dims): | |
if xstar[j] < x0[j]: | |
rectLefts[j] = xstar[j] | |
else: | |
rectRights[j] = xstar[j] | |
# save our sample | |
samples.append(xstar) | |
# Our last sample x0 is now our proposal | |
x0 = xstar | |
# Get our new y0 for next step | |
y0 = np.random.uniform(low=0, high=pdf(x0)) | |
# reset our rectangle | |
### rect unifs should be sampled again | |
rectUnifs = np.random.uniform(size=dims) | |
rectLefts = x0 - widths*rectUnifs | |
rectRights = rectLefts + widths | |
return samples | |
pdf = lambda x: np.e**(-(x[0]**2)/2 - ((-x[1])**2)/2) | |
X = mvslice(pdf,[0,0], [10,10], sampleSize=10000) | |
Xs = [a[1] for a in X] | |
Ys = [a[0] for a in X] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment