Skip to content

Commit

Permalink
Rename MetropolisHasting to MH
Browse files Browse the repository at this point in the history
  • Loading branch information
nabriis committed May 26, 2023
1 parent 7419837 commit a4426bb
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cuqi/sampler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
from ._hmc import NUTS
from ._langevin_algorithm import ULA, MALA
from ._laplace_approximation import UGLA
from ._mh import MetropolisHastings
from ._mh import MH
from ._pcn import pCN
from ._rto import Linear_RTO
4 changes: 2 additions & 2 deletions cuqi/sampler/_mh.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from cuqi.sampler import ProposalBasedSampler


class MetropolisHastings(ProposalBasedSampler):
class MH(ProposalBasedSampler):
"""Metropolis Hastings sampler.
Allows sampling of a target distribution by random-walk sampling of a proposal distribution along with an accept/reject step.
Expand Down Expand Up @@ -48,7 +48,7 @@ class MetropolisHastings(ProposalBasedSampler):
target = cuqi.distribution.UserDefinedDistribution(dim=dim, logpdf_func=logpdf_func)
# Set up sampler
sampler = cuqi.sampler.MetropolisHastings(target, scale=1)
sampler = cuqi.sampler.MH(target, scale=1)
# Sample
samples = sampler.sample(2000)
Expand Down
2 changes: 1 addition & 1 deletion demos/demo18_DistributionGallery.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Test Metropolis Hastings sampler on simple bivariate distributions \n","\n","This example visualizes MCMC chains and convergence for simple bivariate distributions. The chains are generated using a random walk Metropolis Hastings sampler. \n","\n","Import required packages and modules."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["# Imports:\n","import sys\n","sys.path.append(\"../\")\n","import time\n","import numpy as np\n","import matplotlib.pyplot as plt\n","from cuqi.sampler import MetropolisHastings\n","from cuqi.distribution import DistributionGallery\n","from cuqi.geometry import Discrete"]},{"cell_type":"markdown","metadata":{},"source":["Create a distribution from the gallery. (Can try \"CalSom91\" instead of \"BivariateGaussian\")."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["dist = DistributionGallery(\"BivariateGaussian\")"]},{"cell_type":"markdown","metadata":{},"source":["Plot the density function."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["m, n = 200, 200\n","X, Y = np.meshgrid(np.linspace(-4, 4, m), np.linspace(-4, 4, n))\n","Xf, Yf = X.flatten(), Y.flatten()\n","pos = np.vstack([Xf, Yf]).T # pos is (m*n, d)\n","Z = dist.pdf(pos).reshape((m, n))\n","\n","plt.contourf(X, Y, Z, 10)\n","plt.contour(X, Y, Z, 4, colors='k')\n","plt.gca().set_aspect('equal', adjustable='box')\n"]},{"cell_type":"markdown","metadata":{},"source":["Set up and run the sampler to sample the distribution. (Can change initial point, step size) \n","MCMC_MH = MetropolisHastings(target, proposal=None, scale=1, x0=None)\n","- Can change initial point and step size\n","- Try sample and sample_adapt"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["MCMC_MH = MetropolisHastings(dist)\n","\n","Ns = int(1e2) # number of samples\n","Nb = int(0.2*Ns) # burn-in\n","\n","ti = time.time()\n","x_s_MH, target_eval, acc = MCMC_MH.sample(Ns, Nb)\n","print('Elapsed time:', time.time() - ti)"]},{"cell_type":"markdown","metadata":{},"source":["Plot sample points"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["plt.contourf(X, Y, Z, 4)\n","plt.contour(X, Y, Z, 4, colors='k')\n","plt.gca().set_aspect('equal', adjustable='box')\n","plt.plot(x_s_MH.samples[0,:], x_s_MH.samples[1,:], 'r.-', alpha=0.3)"]},{"cell_type":"markdown","metadata":{},"source":["Plot chains (plot chains for both variables?)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["x_s_MH.plot_chain(0)"]},{"cell_type":"markdown","metadata":{},"source":["Plot credibility interval"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["x_s_MH.plot_ci(95)"]},{"cell_type":"markdown","metadata":{},"source":["Change geometry to plot the credibility interval using discrete geometry?\n"]}],"metadata":{"interpreter":{"hash":"8a2a2a6f000eefafb6ab14e86e333a3522b00875ca02312d09d70808f888a31d"},"kernelspec":{"display_name":"Python 3.6.9 64-bit","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.8.8"},"metadata":{"interpreter":{"hash":"31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"}},"orig_nbformat":3},"nbformat":4,"nbformat_minor":2}
{"cells":[{"attachments":{},"cell_type":"markdown","metadata":{},"source":["# Test Metropolis Hastings sampler on simple bivariate distributions \n","\n","This example visualizes MCMC chains and convergence for simple bivariate distributions. The chains are generated using a random walk Metropolis Hastings sampler. \n","\n","Import required packages and modules."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["# Imports:\n","import sys\n","sys.path.append(\"../\")\n","import time\n","import numpy as np\n","import matplotlib.pyplot as plt\n","from cuqi.sampler import MH\n","from cuqi.distribution import DistributionGallery\n","from cuqi.geometry import Discrete"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["Create a distribution from the gallery. (Can try \"CalSom91\" instead of \"BivariateGaussian\")."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["dist = DistributionGallery(\"BivariateGaussian\")"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["Plot the density function."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["m, n = 200, 200\n","X, Y = np.meshgrid(np.linspace(-4, 4, m), np.linspace(-4, 4, n))\n","Xf, Yf = X.flatten(), Y.flatten()\n","pos = np.vstack([Xf, Yf]).T # pos is (m*n, d)\n","Z = dist.pdf(pos).reshape((m, n))\n","\n","plt.contourf(X, Y, Z, 10)\n","plt.contour(X, Y, Z, 4, colors='k')\n","plt.gca().set_aspect('equal', adjustable='box')\n"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["Set up and run the sampler to sample the distribution. (Can change initial point, step size) \n","MCMC_MH = MetropolisHastings(target, proposal=None, scale=1, x0=None)\n","- Can change initial point and step size\n","- Try sample and sample_adapt"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["MCMC_MH = MH(dist)\n","\n","Ns = int(1e2) # number of samples\n","Nb = int(0.2*Ns) # burn-in\n","\n","ti = time.time()\n","x_s_MH, target_eval, acc = MCMC_MH.sample(Ns, Nb)\n","print('Elapsed time:', time.time() - ti)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["Plot sample points"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["plt.contourf(X, Y, Z, 4)\n","plt.contour(X, Y, Z, 4, colors='k')\n","plt.gca().set_aspect('equal', adjustable='box')\n","plt.plot(x_s_MH.samples[0,:], x_s_MH.samples[1,:], 'r.-', alpha=0.3)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["Plot chains (plot chains for both variables?)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["x_s_MH.plot_chain(0)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["Plot credibility interval"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["x_s_MH.plot_ci(95)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["Change geometry to plot the credibility interval using discrete geometry?\n"]}],"metadata":{"interpreter":{"hash":"8a2a2a6f000eefafb6ab14e86e333a3522b00875ca02312d09d70808f888a31d"},"kernelspec":{"display_name":"Python 3.6.9 64-bit","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.8.8"},"metadata":{"interpreter":{"hash":"31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"}},"orig_nbformat":3},"nbformat":4,"nbformat_minor":2}
6 changes: 3 additions & 3 deletions demos/demo18_DistributionGallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import matplotlib.pyplot as plt

# myfuns
from cuqi.sampler import pCN, MetropolisHastings, NUTS
from cuqi.sampler import pCN, MH, NUTS
from cuqi.distribution import DistributionGallery

#%%
Expand Down Expand Up @@ -81,7 +81,7 @@
Nb = int(0.5*Ns) # burn-in

# MH
MCMC = MetropolisHastings(dist)
MCMC = MH(dist)
ti = time.time()
x_s_MH = MCMC.sample_adapt(Ns, Nb)
print('Elapsed time MH:', time.time() - ti, '\n')
Expand Down Expand Up @@ -120,4 +120,4 @@
x_s_NUTS.plot_chain([0, 1])
plt.xlim(0, Ns)
plt.title('NUTS chains')
plt.show()
plt.show()
2 changes: 1 addition & 1 deletion demos/demo22_poisson.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@
"outputs": [],
"source": [
"np.random.seed(0)\n",
"mySampler = cuqi.sampler.MetropolisHastings(posterior)\n",
"mySampler = cuqi.sampler.MH(posterior)\n",
"samples = mySampler.sample_adapt(5000)\n",
"samples.plot_ci(95,exact=x_exact)"
]
Expand Down
2 changes: 1 addition & 1 deletion demos/demo23_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
# %% 2-dimensional distribution with circular shape
dist = cuqi.distribution.DistributionGallery("CalSom91")
sampler = cuqi.sampler.MetropolisHastings(dist)
sampler = cuqi.sampler.MH(dist)
samples1 = sampler.sample_adapt(50000)
samples2 = sampler.sample_adapt(50000)
# %%
Expand Down
6 changes: 3 additions & 3 deletions demos/demo23_diagnostics_visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cuqi
# %% 2-dimensional distribution with circular shape
dist = cuqi.distribution.DistributionGallery("CalSom91")
sampler = cuqi.sampler.MetropolisHastings(dist)
sampler = cuqi.sampler.MH(dist)
samples = sampler.sample_adapt(50000)

# Switch to discrete geometry (easiest for "variable" names)
Expand All @@ -26,7 +26,7 @@
import numpy as np
#dist4 = cuqi.distribution.Gaussian(np.array([1,2,3,4]),1)
dist4 = cuqi.distribution.LMRF(np.array([1,2,3,4,5,6]), 1, 6, 1, 'zero')
sampler4 = cuqi.sampler.MetropolisHastings(dist4)
sampler4 = cuqi.sampler.MH(dist4)
samples4 = sampler4.sample_adapt(50000)
samples4.geometry = cuqi.geometry.Discrete(["a","b","c","d","e","f"])
# %%
Expand All @@ -45,4 +45,4 @@
# %%
ax_4 = samples4_bt.plot_autocorrelation()
# %%
ax_22 = samples4_bt.plot_autocorrelation(np.arange(6), grid=(3,2))
ax_22 = samples4_bt.plot_autocorrelation(np.arange(6), grid=(3,2))
2 changes: 1 addition & 1 deletion demos/demo35_FD_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#%% Sample from the posterior using Metropolis-Hastings
print('Sampling from the posterior using Metropolis-Hastings:')
MH_sampler = cuqi.sampler.MetropolisHastings(posterior)
MH_sampler = cuqi.sampler.MH(posterior)
MH_samples = MH_sampler.sample_adapt(1000)
MH_samples.plot_ci(95,exact=TP.exactSolution)

Expand Down
4 changes: 2 additions & 2 deletions demos/try07_PosteriorClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# myfuns
import cuqi
from cuqi.sampler import pCN, MetropolisHastings
from cuqi.sampler import pCN, MH
from cuqi.distribution import Gaussian, Posterior, DistributionGallery
from cuqi.samples import Samples
#
Expand All @@ -28,7 +28,7 @@
prior = cuqi.distribution.Gaussian(np.zeros(dim), 0.2**2)
posterior = cuqi.distribution.Posterior(L, prior)

MCMC_MH = MetropolisHastings(posterior, scale=0.31)
MCMC_MH = MH(posterior, scale=0.31)
MH_samples = MCMC_MH.sample_adapt(10000,100)
#%%
plt.figure()
Expand Down
6 changes: 3 additions & 3 deletions demos/tutorials/multiple_likelihoods.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~

# Sample from the posterior
sampler = cuqi.sampler.MetropolisHastings(z1)
sampler = cuqi.sampler.MH(z1)
samples = sampler.sample_adapt(8000)

# Plot the credible interval and compute the ESS
Expand Down Expand Up @@ -223,7 +223,7 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~

# Sample from the posterior
sampler = cuqi.sampler.MetropolisHastings(z2)
sampler = cuqi.sampler.MH(z2)
samples = sampler.sample_adapt(8000)

# Plot the credible interval and compute the ESS
Expand Down Expand Up @@ -257,7 +257,7 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~

# Sample from the posterior
sampler = cuqi.sampler.MetropolisHastings(z_joint)
sampler = cuqi.sampler.MH(z_joint)
samples = sampler.sample_adapt(8000)

# Plot the credible interval and compute the ESS
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_RWMH_sample_regression():

scale = 0.1
x0 = 0.5*np.ones(d)
MCMC2 = cuqi.sampler.MetropolisHastings( dist,proposal = ref,scale =scale, x0=x0)
MCMC2 = cuqi.sampler.MH( dist,proposal = ref,scale =scale, x0=x0)

# run sampler
Ns = int(1e1) # number of samples
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_sampler_UserDefined_basic():
Ns = 500 # number of samples
Nb = 100 # burn-in

s_MH = cuqi.sampler.MetropolisHastings(distX).sample_adapt(Ns,Nb)
s_MH = cuqi.sampler.MH(distX).sample_adapt(Ns,Nb)
s_CWMH = cuqi.sampler.CWMH(distX).sample_adapt(Ns,Nb)
s_NUTS = cuqi.sampler.NUTS(distX).sample_adapt(Ns,Nb)

Expand Down
12 changes: 6 additions & 6 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_samples_plot(geom,is_par,plot_par):
def test_samples_plot_autocorrelation(kwargs):
# Make basic distribution and sample
dist = cuqi.distribution.DistributionGallery("CalSom91")
sampler = cuqi.sampler.MetropolisHastings(dist)
sampler = cuqi.sampler.MH(dist)
samples = sampler.sample_adapt(1000)

# Switch to discrete geometry (easiest for "variable" names)
Expand All @@ -60,7 +60,7 @@ def test_samples_plot_autocorrelation(kwargs):
def test_samples_plot_trace(kwargs):
# Make basic distribution and sample
dist = cuqi.distribution.DistributionGallery("CalSom91")
sampler = cuqi.sampler.MetropolisHastings(dist)
sampler = cuqi.sampler.MH(dist)
samples = sampler.sample_adapt(1000)

# Switch to discrete geometry (easiest for "variable" names)
Expand All @@ -86,7 +86,7 @@ def test_samples_plot_trace(kwargs):
def test_samples_plot_pair(kwargs):
# Make basic distribution and sample
dist = cuqi.distribution.Gaussian(np.array([1,2,3,4]),1)
sampler = cuqi.sampler.MetropolisHastings(dist)
sampler = cuqi.sampler.MH(dist)
samples = sampler.sample_adapt(1000)
samples.geometry = cuqi.geometry.Discrete(["a","b","c","d"])

Expand Down Expand Up @@ -123,7 +123,7 @@ def test_rhat_geometry():

def test_ess():
dist = cuqi.distribution.DistributionGallery("CalSom91")
sampler = cuqi.sampler.MetropolisHastings(dist)
sampler = cuqi.sampler.MH(dist)
samples = sampler.sample_adapt(500)
assert samples.compute_ess().shape == samples.geometry.par_shape

Expand Down Expand Up @@ -151,7 +151,7 @@ def test_samples_funvals(geometry):
cuqi.geometry.Continuous1D(2), map=lambda x: x**2)])
def test_compute_ci(percent, compute_on_par, geometry):
dist = cuqi.distribution.DistributionGallery("CalSom91")
sampler = cuqi.sampler.MetropolisHastings(dist)
sampler = cuqi.sampler.MH(dist)
par_samples = sampler.sample_adapt(500)
par_samples.geometry = geometry

Expand Down Expand Up @@ -292,7 +292,7 @@ def test_cuqiarray_ispar_false_without_geometry():
def test_violin_plot():
""" Test that the violin plot is generated correctly. """
dist = cuqi.distribution.DistributionGallery("CalSom91")
sampler = cuqi.sampler.MetropolisHastings(dist)
sampler = cuqi.sampler.MH(dist)
samples = sampler.sample_adapt(1000)
samples.geometry = cuqi.geometry.Discrete(["alpha","beta"])

Expand Down

0 comments on commit a4426bb

Please sign in to comment.