Monte Carlo
How Monte Carlo methods use random sampling to estimate integrals, simulate stochastic systems and explore statistical-mechanical ensembles, from random numbers to the Metropolis algorithm.
What it is
Monte Carlo (MC) methods solve problems by repeated random sampling. Instead of computing an answer analytically or by a deterministic grid, a computer draws many random inputs, evaluates the quantity of interest for each, and averages the outcomes, relying on the law of large numbers. The name comes from the casino, coined by Stanislaw Ulam, John von Neumann and Nicholas Metropolis during the Manhattan Project in the 1940s. Monte Carlo is used in physics (particle transport, statistical mechanics, lattice models), chemistry (molecular simulation), engineering (reliability, uncertainty), finance (option pricing), computer graphics (ray tracing), medicine (radiation dose) and statistics (Bayesian inference, see Bayesian inference and MCMC).
It answers questions such as: What is the value of a high-dimensional integral that no formula or grid can handle? What are the equilibrium properties of a fluid, magnet or polymer? How do neutrons or photons travel through a shield or tissue? How uncertain is the output of a model when its inputs are uncertain? Its strength is that the error decreases as 1/√N regardless of the number of dimensions.
Fundamentals
Random numbers and basic Monte Carlo integration
Computers use pseudorandom number generators (PRNGs), deterministic algorithms producing sequences that pass statistical tests of randomness; good modern choices are Mersenne Twister, PCG and Philox (for parallel use). The seed makes results reproducible. To estimate I = ∫ f(x) dx over [a, b], draw N points xi uniformly and compute I ≈ (b − a)(1/N) Σ f(xi). The estimate has a standard error σ/√N, where σ is the standard deviation of f, so 100 times more samples give only 10 times less error. In d dimensions grid methods need nd points, but MC error stays ∝ N−1/2, which is why MC is the method of choice for high-dimensional integrals.
Sampling from distributions and variance reduction
To draw from a nonuniform distribution p(x), use the inverse transform method (x = F−1(u), with u uniform in [0, 1] and F the cumulative distribution), rejection sampling (propose from an easier distribution and accept with probability proportional to the ratio), or special methods (Box–Muller for Gaussians). Importance sampling draws from a distribution q(x) concentrated where the integrand is large and reweights: I = Eq[f(x) p(x)/q(x)]; a good q reduces variance dramatically, while a poor one makes it infinite. Other variance reduction techniques are stratified sampling, control variates, antithetic variables and quasi-Monte Carlo (low-discrepancy Sobol’ or Halton sequences giving errors close to 1/N for smooth integrands).
Metropolis Monte Carlo for statistical mechanics
For a system in thermal equilibrium, the probability of a configuration is proportional to the Boltzmann factor, p(x) ∝ exp[−U(x)/kBT]. Sampling it directly is impossible for many particles, so the Metropolis algorithm (1953) generates a Markov chain: from the current state x, propose a random trial move to x′ (for instance, displace a particle by a small random vector) and accept it with probability min[1, exp(−ΔU/kBT)], where ΔU = U(x′) − U(x). Moves that lower energy are always accepted, and moves that raise it are accepted with the Boltzmann probability; if rejected, the old state is counted again. This satisfies detailed balance, so that the chain converges to the correct distribution. Averages of observables are taken over the chain after an initial equilibration period is discarded. The maximum step size is tuned for an acceptance ratio of about 30–50%. Unlike molecular dynamics (see molecular dynamics), MC has no real time and uses only energies, not forces, which allows unphysical moves that sample faster, such as swapping molecule identities, inserting or deleting molecules, or regrowing chain segments.
Ensembles, algorithms and applications
MC can sample different ensembles: canonical (NVT, as above), isothermal–isobaric (NPT, with volume change moves accepted with a pressure term), and grand canonical (μVT, with particle insertions and deletions, widely used for adsorption in porous materials such as MOFs and zeolites, and for gas solubility). The Gibbs ensemble simulates phase coexistence directly. For lattice models, such as the Ising model (spins si = ±1, H = −J Σ sisj), single spin flips are proposed, and cluster algorithms (Swendsen–Wang, Wolff) beat critical slowing down near phase transitions (the 2D Ising critical temperature is Tc = 2J/[kB ln(1 + √2)] ≈ 2.269 J/kB). Beyond equilibrium simulation, MC radiation transport (codes such as MCNP, Geant4, EGSnrc, PENELOPE and FLUKA) follows individual particles with randomly sampled interactions and is the reference method in dosimetry and shielding.
Statistical analysis of results
Samples from a Markov chain are correlated, so the effective number of independent samples is N/(2τ), where τ is the integrated autocorrelation time, and error bars must account for it by block averaging, binning analysis or the autocorrelation function. Also check equilibration (plot observables against step), use several independent runs from different starting points, and look for finite-size effects and ergodicity problems, where the chain is trapped in a region of configuration space.
Typical applications and limits
Used in phase equilibria and adsorption, polymer physics, magnetism, nuclear and medical physics, risk analysis, ray-traced rendering and global optimization (simulated annealing). Limits: slow 1/√N convergence for high precision, no dynamics in standard MC, sampling difficulties in rugged landscapes and the “sign problem” in some quantum simulations.
Basic operation
- Define the problem. State the integral, system or process and what quantity to estimate.
- Choose the sampling scheme. Use direct sampling, importance sampling or Metropolis moves.
- Set up the random number generator. Pick a reliable PRNG and record the seed.
- Initialize and equilibrate. Start from a reasonable state and discard the initial transient.
- Run the sampling. Generate many samples or Markov chain steps, tuning the step size for good acceptance.
- Accumulate averages. Record observables, and estimate uncertainty with block averaging.
- Test and validate. Compare with known limits and check convergence with multiple seeds.
- Collect the output. You obtain estimates with error bars; variance reduction and advanced sampling are covered in the video.
Good practice
- Report error bars. Account for autocorrelation in Markov chains.
- Use good random number generators. Avoid simple linear congruential generators for serious work.
- Check detailed balance. Test code on cases with exact results.
- Discard the equilibration phase. Verify stability of averages.
- Use independent streams in parallel runs. Do not reuse the same seed.
- Be transparent. Record seeds, software versions and number of samples, and do not overstate precision of results in safety or risk assessments.
Key terms
- Pseudorandom number generator — an algorithm producing number sequences that appear random.
- Importance sampling — drawing samples where the integrand matters and reweighting.
- Metropolis algorithm — an accept/reject rule that samples the Boltzmann distribution.
- Detailed balance — a condition ensuring a Markov chain converges to the target distribution.
- Autocorrelation time — the number of steps over which samples remain correlated.
- Grand canonical MC — simulation with insertion and deletion of particles at fixed chemical potential.
- Variance reduction — techniques that lower statistical error for a given number of samples.
Video coming soon
About five minutes on Monte Carlo: how it works in detail, how the data is acquired, and how it is analyzed.
More in 4.2 Computational Modeling and Simulation
- Density functional theory (DFT)
- Ab initio methods (Hartree-Fock, post-HF)
- Semi-empirical/DFTB
- Molecular dynamics
- Coarse-grained modeling
- QM/MM
- Monte Carlo
- Kinetic Monte Carlo
- Finite element analysis
- Finite difference method
- Finite volume method
- Computational fluid dynamics
- Discrete element method
- Lattice Boltzmann
- Phase-field modeling
- FDTD
- Multiscale modeling
- CALPHAD
- Topology optimization
- Reduced-order modeling
- Digital twins
- Agent-based modeling
- System dynamics
- Climate modeling (GCM/RCM)
- Hydrological modeling
- Species distribution modeling
- SPICE circuit simulation
- TCAD device simulation
- EM simulation
- MIMO/OFDM simulation
- Power system load-flow and stability analysis
- Quantum circuit design and simulation
