-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from CUQI-DTU/sprint19_MetropolishHastings_re…
…naming Rename MetropolisHasting to MH
- Loading branch information
Showing
12 changed files
with
25 additions
and
25 deletions.
There are no files selected for viewing
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
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
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
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} |
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
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
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
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
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
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
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
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
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