Teach me MCMC with a simple example
- User
Teach me MCMC with a simple example
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Teach me MCMC with a simple example
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Absolutely! Let’s break down Markov Chain Monte Carlo (MCMC) with a simple, intuitive example.
MCMC is a method to sample from complicated probability distributions when direct sampling is hard. It’s widely used in Bayesian statistics, machine learning, and physics.
Suppose you have a weird-shaped probability distribution (not a normal distribution), and you want to estimate its mean. But you can’t sample directly from it.
Let’s say the distribution is proportional to:
[ P(x) \propto e^{-x^4 + x^2} ]
for ( x ) between (-3) and (3).
Let’s use the Metropolis-Hastings version of MCMC.
[ r = \frac{P(x')}{P(x)} ]
import numpy as np
def P(x):
return np.exp(-x**4 + x**2)
samples = []
x = 0 # Start at 0
for i in range(10000):
x_new = x + np.random.normal(0, 0.5) # Propose new x
if -3 < x_new < 3: # Stay within bounds
r = P(x_new) / P(x)
if r >= 1 or np.random.rand() < r:
x = x_new # Accept move
samples.append(x)
Would you like to see a plot or a more detailed step-by-step with numbers?