Marx Max中文
Menu

MCMC sampling

Open in Marx Max

Compare three random-walk Metropolis proposal scales against an analytic one-parameter posterior. Inspect traces, acceptance rates and retained states; download code, simulated data, 15,000 states, figures and rerun instructions.

Compare random-walk Metropolis proposal SDs 0.01, 0.35 and 3 on the public simulated data, against one analytic posterior. Fix intercept and error SD at 1 with slope prior N(0,5²). Start each setting at 0, run 5,000 transitions and discard the first 1,000 only for summary comparisons. Retain all 15,000 states and acceptance flags, repeating the previous state on rejection. Read README.md, download the Python package into reference/, retain reference outputs and direct current outputs into rerun/. Check traces, acceptance, mean and spread. Settings share a seed, are not independent chains for convergence diagnostics and must not be pooled; acceptance or a discarded prefix does not prove convergence.
State traces at three proposal scales and distributions after discarding 1,000 states, compared with the same analytic posterior.
Shading marks the discarded prefix; rows share coordinate scales.

Download the complete rerun package (ZIP)

This case separates the statistical target from exploration by a finite chain. Data are entirely simulated and the target has an analytic reference. The executed program records every state and acceptance decision at three proposal scales.

Read the method and usage guide · Review OLS sampling distributions

Fix the data and posterior target first

Thirty simulated observations follow y=1+2x+ε with independent standard-normal x and ε, seed 42. Inference fixes intercept 1 and error SD 1, with slope prior N(0,5²). Only the slope is inferred, not intercept or variance. The analytic posterior mean is about 1.853 and SD 0.239.

Symmetric normal random-walk proposals have SD 0.01, 0.35 and 3, all starting at 0. Each setting runs 5,000 transitions and discards the first 1,000 states for comparison, retaining 4,000. Settings use the same seed and are not independent multiple chains for convergence diagnostics; they are not pooled into one posterior sample.

Proposal SDFull-run acceptanceRetained meanRetained SDEmpirical 2.5%–97.5% quantiles
0.0197.0%1.6960.3810.683–2.187
0.3560.4%1.8620.2521.381–2.362
39.6%1.8720.1991.465–2.236

The small scale accepts about 97%, but its retained mean of about 1.696 deviates from the analytic mean. The large scale accepts about 9.6% and often stays at one state. Scale 0.35 has mean and spread closer to the reference, but one run does not prove convergence. Acceptance covers all 5,000 transitions; other summaries use states after discarding 1,000.

Rejected proposals retain the previous state instead of exporting only accepted points. Discarding a prefix does not guarantee convergence, and empirical quantiles may be unreliable posterior intervals under poor exploration. Formal analysis needs appropriate independent chains and diagnostics such as effective sample size, which this exercise does not claim to provide. Stan posterior diagnostics

Download code, data and actual output

  1. Extract into a fresh directory and retain reference outputs. Prepare Python dependencies following README. Installation accesses a package repository; analysis is offline.
  2. Run python analyze.py from the package root and check the completion marker, row counts and output. Defaults overwrite named files under the package results/.
  3. Use README commands to change seed or run length in a new output directory. Seed changes affect both data and proposal RNG; increasing iterations with the same seed keeps data and target fixed.

Runs used Python 3.11.11 on macOS; the heteroskedasticity case was independently checked in Stata 19.5. Windows hardware is not covered. Simulations illustrate methods and computation, not real research findings, classroom effects or automatic model performance.

Return to this case guide · Return to the econometrics teaching guide