σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Bayesian Inference skill›Module 2 · Sampling the posterior›Topic 2.1·~1 hr 5 minAdvanced

Metropolis-Hastings from scratch

Skill positionModule 2 of 3 · Topic 1 of 2
2.1 Metropolis-Hastings from scratch
—Lab score— / 100Total XP—Streak—

Exploring a posterior you cannot write down

Often you can write the posterior only up to a constant, as prior times likelihood, and you cannot add it up or find its mean with algebra. Metropolis-Hastings builds a random walk that visits places in proportion to the posterior. At the current point θ\thetaθ, propose a nearby point θ′\theta'θ′, for example θ\thetaθ plus a bit of normal noise. Compare the posterior heights with the ratio r=p(θ′)/p(θ)r=p(\theta')/p(\theta)r=p(θ′)/p(θ). If r≥1r\ge1r≥1 the new point is at least as good, so move. Otherwise move with probability rrr. If you do not move, write down the current point again. The unknown constant is the same in both heights, so it cancels in the ratio. That is why the method works.

r=p(θ′)p(θ),α=min⁡(1, r),ln⁡α=min⁡(0, ln⁡p(θ′)−ln⁡p(θ))r=\frac{p(\theta')}{p(\theta)},\qquad \alpha=\min(1,\,r),\qquad \ln\alpha=\min\big(0,\ \ln p(\theta')-\ln p(\theta)\big)r=p(θ)p(θ′)​,α=min(1,r),lnα=min(0, lnp(θ′)−lnp(θ))(1)

The list of visited points is the chain. After a warm-up, called burn-in, it behaves like a sample from the posterior, so the mean and the interval come from plain averages and percentiles of the chain. The step size is the main knob. If it is too small, nearly every move is accepted but the chain crawls. If it is too large, most proposals land in bad places and are rejected, so the chain sits still. A good chain accepts roughly a quarter to a half of the proposals. Always look at the chain: plot it, and start it from different places to see that they agree.

One proposal, by handLet the posterior height be p(θ)∝θ18(1−θ)10p(\theta)\propto\theta^{18}(1-\theta)^{10}p(θ)∝θ18(1−θ)10, the Beta(19,11)\mathrm{Beta}(19,11)Beta(19,11) shape. From θ=0.6\theta=0.6θ=0.6 propose θ′=0.7\theta'=0.7θ′=0.7. The ratio is (0.7/0.6)18(0.3/0.4)10=0.903(0.7/0.6)^{18}(0.3/0.4)^{10}=0.903(0.7/0.6)18(0.3/0.4)10=0.903, so accept with probability 0.900.900.90. Propose θ′=0.55\theta'=0.55θ′=0.55 instead: the ratio is 0.6780.6780.678, so accept with probability 0.680.680.68. Neither number needed the constant that makes the density add up to 111.
  • Proposal and ratio: propose a nearby point and compare posterior heights. The unknown constant cancels.
  • Chain: the visited points, repeated when a move is rejected. Drop the burn-in, then average.
  • Step size: too small crawls, too large gets stuck. Aim at accepting about a quarter to a half.

Lab · what's graded

  • A good proposal15 pts
  • A worse proposal15 pts
  • A log ratio10 pts
  • Averaging a chain10 pts
  • A step that is too large10 pts
  • One iteration10 pts
  • Match the vocabulary15 pts
  • Why the constant is not needed15 pts
0/ 8 answered
Tasks
1Numeric answer15 ptsNot answered

With p(θ)∝θ18(1−θ)10p(\theta)\propto\theta^{18}(1-\theta)^{10}p(θ)∝θ18(1−θ)10, go from θ=0.6\theta=0.6θ=0.6 to θ′=0.7\theta'=0.7θ′=0.7. What is the ratio r=p(θ′)/p(θ)r=p(\theta')/p(\theta)r=p(θ′)/p(θ)? Give four decimals.

r=r =r=accepted: ± 0.003
2Numeric answer15 ptsNot answered

Now go from θ=0.6\theta=0.6θ=0.6 to θ′=0.55\theta'=0.55θ′=0.55. What is the ratio rrr? Give four decimals.

r=r =r=accepted: ± 0.003
3Numeric answer10 ptsNot answered

Working in logs, what is ln⁡p(θ′)−ln⁡p(θ)\ln p(\theta')-\ln p(\theta)lnp(θ′)−lnp(θ) for the move from 0.60.60.6 to 0.70.70.7, that is ln⁡0.9029\ln 0.9029ln0.9029? Give three decimals.

l=l =l=accepted: ± 0.004
4Numeric answer10 ptsNot answered

After the burn-in, a short chain holds the values 0.62, 0.65, 0.65, 0.58, 0.700.62,\ 0.65,\ 0.65,\ 0.58,\ 0.700.62, 0.65, 0.65, 0.58, 0.70 (the second 0.650.650.65 is a rejected move). What is the posterior mean estimate? Give two decimals.

m=m =m=accepted: ± 0.005
5Choice10 ptsNot answered

What happens when the proposal step is far too large?

6Order the steps10 ptsNot answered

Put the steps of one Metropolis-Hastings iteration in order.

Drag to reorder, or focus an item, press Space to lift it, the arrow keys to move it and Space to drop it.

  1. 1Record the point, new or repeated
  2. 2Draw a uniform number u between 0 and 1
  3. 3Propose a nearby point
  4. 4Move if u is below the ratio, otherwise stay
  5. 5Compute the ratio of posterior heights
7Match15 ptsNot answered

Match each term with its meaning.

Pick the partner of each item on the left.

Chain
Burn-in
Proposal
Acceptance rate
8Explain in words15 ptsNot answered

In one or two sentences, explain why Metropolis-Hastings works even when the normalising constant of the posterior is unknown.

Math modeKeyboard or LaTeX inputTask 1 of 8 · Ctrl ⏎ to check

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub