Last active
January 4, 2022 01:39
-
-
Save tatsy/2575c2471c4e9a3098e37363785ce2f8 to your computer and use it in GitHub Desktop.
Metropolis Hastings method
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Parameters\n", | |
"n_mutate = 10000" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def inv_dist_fun(x):\n", | |
" r = 0.3\n", | |
" mu1 = 1.0\n", | |
" mu2 = -2.0\n", | |
" x1 = x - mu1\n", | |
" x2 = x - mu2\n", | |
" e1 = np.exp(-x1 * x1) / np.sqrt(np.pi)\n", | |
" e2 = np.exp(-x2 * x2) / np.sqrt(np.pi)\n", | |
" return r * e1 + (1.0 - r) * e2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Metropolis(object):\n", | |
" def __init__(self, x, inv_dist_fun, sigma, seed=0):\n", | |
" self.rng = np.random.RandomState(seed)\n", | |
" self.x = x\n", | |
" self.inv_dist_fun = inv_dist_fun\n", | |
" self.sigma = sigma\n", | |
"\n", | |
" def sample(self):\n", | |
" x0 = self.x\n", | |
" x1 = self.rng.normal(self.x, self.sigma)\n", | |
" p0 = self.inv_dist_fun(x0)\n", | |
" p1 = self.inv_dist_fun(x1)\n", | |
" a = min(p1 / (p0 + 1.0e-12), 1.0)\n", | |
" if self.rng.random() < a:\n", | |
" self.x = x1\n", | |
" else:\n", | |
" self.x = x0\n", | |
"\n", | |
" return self.x" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"sampler = Metropolis(0.0, inv_dist_fun, 1.0)\n", | |
"n_burnin = max(n_mutate // 100, 100)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Burn-in\n", | |
"for i in range(n_burnin):\n", | |
" sampler.sample()\n", | |
"\n", | |
"# Sampling\n", | |
"samples = []\n", | |
"for i in range(n_mutate):\n", | |
" samples.append(sampler.sample())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAuPUlEQVR4nO3deXyU1b3H8c+ZyWRfIQGBQBJ2kABCQBRELKUFZFGLC4p6ay2g17q02uJatfZet6q1opS61g2vooiIsisgCAn7jgFCEhIg+zbZZubcPxJogEAmycw8mZnf+/Xi9WLyPHnOd0Ly4+Q85zlHaa0RQgjh/UxGBxBCCOEaUtCFEMJHSEEXQggfIQVdCCF8hBR0IYTwEVLQhRDCRzhV0JVS45VSB5RS6UqpOY0cH6OUKlFKba//84TrowohhLiQgKZOUEqZgbnAOCAbSFVKLdZa7z3r1HVa60luyCiEEMIJzvTQhwPpWuvDWusaYAEw1b2xhBBCNFeTPXSgC5DV4HU2cGkj512mlNoB5AAPaq33nH2CUmomMBMgLCxsaN++fZufWAgh/NiWLVvytdZxjR1zpqCrRj529noBW4EErXW5UmoisAjodc4naT0fmA+QkpKi09LSnGheCCHEKUqpo+c75syQSzbQtcHreOp64adprUu11uX1f18KWJRSsS3IKoQQooWcKeipQC+lVJJSKhC4CVjc8ASl1EVKKVX/9+H11y1wdVghhBDn1+SQi9bappS6B1gGmIG3tdZ7lFKz64/PA6YBdymlbEAlcJOWZRyFEMKjlFF1V8bQhfAvtbW1ZGdnU1VVZXQUrxAcHEx8fDwWi+WMjyultmitUxr7HGduigohRKtlZ2cTERFBYmIi9SO04jy01hQUFJCdnU1SUpLTnyeP/gshPKKqqor27dtLMXeCUor27ds3+7cZKehCCI+RYu68lnytpKALIYSPkIIuhPALxcXFvP76625vZ9GiRezde/ZSV54hBV0I4ReaW9C11jgcjma3IwVdCCHcbM6cORw6dIjBgwfzwAMPMHbsWIYMGUJycjJffvklABkZGfTr14+7776bIUOGkJWVxV/+8hf69u3LuHHjmD59Oi+++CIAhw4dYvz48QwdOpQrrriC/fv3s2HDBhYvXsxDDz3E4MGDOXTokEffo0xbFEIY4uitt53zsYgJ42l38804KivJmjnrnONR115L9HXXYisq4ti9951xLOH9f1+wvWeffZbdu3ezfft2bDYbVquVyMhI8vPzGTFiBFOmTAHgwIEDvPPOO7z++uukpaWxcOFCtm3bhs1mY8iQIQwdOhSAmTNnMm/ePHr16sWmTZu4++67Wb16NVOmTGHSpElMmzatpV+aFpOCLoTwO1prHnnkEdauXYvJZOLYsWOcOHECgISEBEaMGAHA+vXrmTp1KiEhIQBMnjwZgPLycjZs2MD1119/+prV1dUefhfnkoIuhDDEhXrUppCQCx4PiIlpskd+IR9++CF5eXls2bIFi8VCYmLi6TnfYWFhp88735P0DoeD6Ohotm/f3uIM7iBj6EIIvxAREUFZWRkAJSUldOjQAYvFwpo1azh6tPEVaUeNGsVXX31FVVUV5eXlfP311wBERkaSlJTEp59+CtQV/h07dpzTjqdJQRdC+IX27dszcuRIBgwYwPbt20lLSyMlJYUPP/yQ8222M2zYMKZMmcKgQYO47rrrSElJISoqCqjr5b/11lsMGjSIiy+++PSN1ZtuuokXXniBSy65xOM3RWVxLiGER+zbt49+/foZHaPZysvLCQ8Px2q1Mnr0aObPn8+QIUM80nZjXzNZnEsI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I/4fbJwfbEvtRN8AAAAASUVORK5CYII=", | |
"text/plain": [ | |
"<Figure size 432x288 with 1 Axes>" | |
] | |
}, | |
"metadata": { | |
"needs_background": "light" | |
}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"bins = 50\n", | |
"x_min = -5.0\n", | |
"x_max = 5.0\n", | |
"\n", | |
"fig = plt.figure()\n", | |
"ax = fig.add_subplot(111)\n", | |
"ax.hist(samples, bins=bins, range=(x_min, x_max), density=True, color='tab:blue', label=\"samples\")\n", | |
"xs = np.linspace(x_min, x_max, 1000)\n", | |
"ys = inv_dist_fun(xs)\n", | |
"ax.plot(xs, ys, color='tab:red', linestyle='--', label=\"target\")\n", | |
"ax.set_ylim([0.0, 0.5])\n", | |
"ax.legend(loc=\"upper right\")\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"interpreter": { | |
"hash": "02d3c230780613a338b307efe1c8520401457e802ba0c889aabda605c4f627d4" | |
}, | |
"kernelspec": { | |
"display_name": "Python 3.8.11 64-bit ('base': conda)", | |
"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.11" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment