{"id":223,"date":"2026-11-13T12:35:04","date_gmt":"2026-11-13T04:35:04","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-metropolis-hastings-samples-a-target-density\/"},"modified":"2026-11-13T21:53:07","modified_gmt":"2026-11-13T13:53:07","slug":"how-metropolis-hastings-samples-a-target-density","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-metropolis-hastings-samples-a-target-density\/","title":{"rendered":"How Metropolis-Hastings samples a target density"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Metropolis-Hastings<\/strong> draws samples from a target density \u03c0 that is known up to a constant. From the current state it proposes a neighbor from a density q, and it accepts the proposal with probability <strong>min(1, \u03c0(y)q(x|y) \/ \u03c0(x)q(y|x))<\/strong>. The \u03c0 constants cancel, so an unnormalized density is enough. If the chain is irreducible and aperiodic, its stationary distribution is \u03c0. A finite run produces dependent samples, not independent draws, and the early samples follow the start rather than \u03c0.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Rejection sampling and inverse transform need a handle on the normalized density or a tight envelope. A Bayesian posterior often supplies an unnormalized product of likelihood and prior. The Metropolis rule uses only ratios of \u03c0, so the unknown normalizer cancels. Detailed balance for this acceptance probability makes \u03c0 the stationary distribution of the chain.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The proposal q is a design choice. A symmetric random walk has q(y|x) = q(x|y), and the acceptance ratio simplifies to \u03c0(y)\/\u03c0(x). A proposal that rarely moves mixes slowly. A proposal that jumps too far is rarely accepted. The algorithm does not choose q for you.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Under standard regularity, the chain has stationary distribution \u03c0, and sample averages converge to expectations under \u03c0 as the number of steps goes to infinity. The samples are correlated. Effective sample size is smaller than the number of iterations. A burn-in prefix is discarded because the start may be atypical. There is no finite-time certificate that burn-in is over. Diagnostics are heuristics.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The acceptance probability must use the proposal ratio when q is not symmetric. Dropping it samples the wrong distribution. Hamiltonian Monte Carlo and Gibbs sampling are other MCMC methods. Metropolis-Hastings is the accept\/reject correction on a general proposal.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Start at a state x where \u03c0(x) &gt; 0. Choose a proposal density q.<\/li>\n<li>Draw y from q(\u00b7|x). Compute a = \u03c0(y) q(x|y) \/ (\u03c0(x) q(y|x)). If \u03c0 is unnormalized, use the unnormalized values. The constant cancels.<\/li>\n<li>Accept y with probability min(1, a). Otherwise stay at x. Either way, emit the resulting state as the next sample.<\/li>\n<li>Discard a burn-in prefix. Use the remaining samples, remembering they are dependent, to estimate expectations.<\/li>\n<li>If q is symmetric, a reduces to \u03c0(y)\/\u03c0(x). Do not use that reduction for an asymmetric proposal.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Stationarity is an asymptotic statement. A short chain started far from the mass of \u03c0 does not yield draws from \u03c0. The acceptance formula is the algorithm. Always accepting is a different chain. Simulated annealing uses the Metropolis rule with a changing target. Here the target stays fixed because the goal is sampling, not minimization.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the proposal ratio is dropped<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Accept with probability min(1, \u03c0(y)\/\u03c0(x)) for an asymmetric proposal<\/td>\n<td>That ratio is correct only when q is symmetric. An asymmetric q samples a different stationary distribution unless the proposal ratio is included.<\/td>\n<\/tr>\n<tr>\n<td>Treat successive samples as independent<\/td>\n<td>The next state depends on the current one by construction. Variance estimates that assume independence are too small.<\/td>\n<\/tr>\n<tr>\n<td>Skip burn-in from an arbitrary start and average everything<\/td>\n<td>The early samples reflect the start. There is no universal burn-in length, and ignoring the issue biases the average.<\/td>\n<\/tr>\n<tr>\n<td>Always accept to mix faster<\/td>\n<td>The chain then follows q, not \u03c0. The rejections are the correction.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Optimization by cooling this rule is simulated annealing. A special proposal that uses gradients is Hamiltonian Monte Carlo. Metropolis-Hastings is the general accept\/reject MCMC step.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment proposes a neighbor on {0, 1, 2} and accepts it with probability min(1, \u03c0(y)\/\u03c0(x)). The target weights are 1, 2, 3, so the chain spends the most time at 2. It prints <code>2<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import random\nweights = [1, 2, 3]\nrng = random.Random(0)\nstate = 0\ncounts = [0, 0, 0]\nfor step in range(4000):\n    proposal = state + rng.choice((-1, 1))\n    if proposal &lt; 0 or proposal &gt; 2:\n        proposal = state\n    if rng.random() &lt; min(1.0, weights[proposal] \/ weights[state]):\n        state = proposal\n    if step &gt;= 500:\n        counts[state] += 1\nassert counts[2] &gt; counts[1] &gt; counts[0]\nprint(max(range(3), key=lambda i: counts[i]))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Metropolis-Hastings draws samples from a density a Bayesian model or a physics simulation can evaluate but cannot integrate. <strong>Stan<\/strong> and similar tools often use Hamiltonian Monte Carlo, which is a later sampler on the same Markov-chain idea. A plain random walk is the proposal this algorithm accepts or rejects.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop a short chain as a substitute for independent draws. Run it long enough that diagnostics are boring, and keep the proposal ratio in the acceptance probability.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Metropolis-Hastings returns the acceptance ratio that includes the proposal density and the fact that the samples are dependent. <strong>Metropolis-Hastings samples an unnormalized density by accepting a proposal with a probability that restores detailed balance, and a finite run yields dependent samples.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Metropolis-Hastings draws samples from a target density \u03c0 that is known up to a constant. From the current state it proposes a neighbor from a density q, and it accepts the proposal with probability min(1, \u03c0(y)q(x|y) \/ \u03c0(x)q(y|x)). The \u03c0 constants cancel, so an unnormalized density is&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,60],"tags":[],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-sampling"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":622,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/223\/revisions\/622"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}