Quiz 2

Markov Chain Monte Carlo

92 words
1 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Markov Chain Monte Carlo ## 6.1 Metropolis-Hastings Algorithm ## 6.2 Gibbs Sampling Sample from conditional distributions rather than joint. Used in Bayesian hierarchical models.

Markov Chain Monte Carlo

6.1 Metropolis-Hastings Algorithm

python
import numpy as np
# Sample from N(5, 1) using Metropolis-Hastings
target_mean, target_std = 5, 1
n_samples = 10000
samples = np.zeros(n_samples)
current = 0.0
for i in range(n_samples):
    proposal = current + np.random.randn() * 2
    # acceptance probability
    log_ratio = ( -0.5*(proposal-target_mean)**2/target_std**2
                  + 0.5*(current-target_mean)**2/target_std**2 )
    if np.log(np.random.uniform()) < log_ratio:
        current = proposal
    samples[i] = current
print(f"Mean: {samples.mean():.2f}, Std: {samples.std():.2f}")

6.2 Gibbs Sampling

Sample from conditional distributions rather than joint. Used in Bayesian hierarchical models. Join Discord PreviousEM AlgorithmNextStochastic Processes
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.