MetropolisAlgorithm.jl

Stable Dev Build Status

Minimal implementation of the Metropolis algorithm.

Install

Run the following code to install this package.

import Pkg; Pkg.add(url="https://github.com/ohno/MetropolisAlgorithm.jl.git")

Usage

Run the following code before each use.

using MetropolisAlgorithm

Here is a minimal working example for 1-walker x 10000-steps, 2-dimensional Metropolis-walk using metropolis(). This function returns the trajectory as a vector of vectors (points), where the first argument is the (normalized or unnormalized) distribution function, and the second argument is the initial value vector. Optional arguments can be used to set the number of steps, element type (Float32, Float64, etc.), and the maximum step size.

using MetropolisAlgorithm
p(r) = exp(- r' * r)  # distribution function
r₀ = [0.0, 0.0]       # initial position
R = metropolis(p, r₀) # 1-walker x 10000-steps
10000-element Vector{Vector{Float64}}:
 [0.0, 0.0]
 [-0.16019102845573407, 0.30505044660351666]
 [-0.309797182809722, -0.06024652652290241]
 [-0.7883872390512094, 0.3192109693188687]
 [-0.6383550519119972, 0.6640963887876735]
 [-1.134085818191973, 0.5847919225407727]
 [-0.7764175639440242, 0.31816765830724014]
 [-0.7764175639440242, 0.31816765830724014]
 [-0.5828195260944246, -0.16804356067977533]
 [-0.5420365839177284, 0.2187708849283705]
 ⋮
 [0.6073944057906099, -1.5253695364046191]
 [1.0603474850658055, -1.160412360562392]
 [0.6081430687876129, -1.1187719666452902]
 [0.6081430687876129, -1.1187719666452902]
 [0.6081430687876129, -1.1187719666452902]
 [0.522476938583362, -0.9120814440423577]
 [0.29935511100544376, -1.091199637179203]
 [0.42469749120613887, -0.712492255134698]
 [0.04389432530115667, -0.38844975202147414]

In the variational Monte Carlo method (VMC), sampling is performed simultaneously with multiple walkers (rather than just one walker). Here is an example of 10000-walkers x 5-steps, 2-dimensional Metropolis-walk using metropolis!(). This function overwrites its second argument without memory allocation, where the first argument is the (normalized or unnormalized) distribution function, and the second argument is the vector of the initial value vectors. Use the For statement to repeat as many times as you like. Remove the first several steps by yourself to ensure equilibrium.

using MetropolisAlgorithm
p(x) = exp(- x' * x)              # distribution function
R = [[0.0, 0.0] for i in 1:10000] # initial position(s)
metropolis!(p, R)                 # the 1st step of 10000-walkers
metropolis!(p, R)                 # the 2nd step of 10000-walkers
metropolis!(p, R)                 # the 3rd step of 10000-walkers
metropolis!(p, R)                 # the 4th step of 10000-walkers
metropolis!(p, R)                 # the 5th step of 10000-walkers

Please see Examples and API reference for more information.

Citation

Please cite this package, the original paper by Metropolis et al. and the textbook by Thijssen:

@misc{MetropolisAlgorithm.jl,
  author  = {Shuhei Ohno},
  title   = {MetropolisAlgorithm.jl},
  url     = {https://github.com/ohno/MetropolisAlgorithm.jl},
  version = {v0.0.1},
  year    = {2025},
  month   = {6}
}

@article{Metropolis1953,
  author    = {Metropolis, Nicholas and Rosenbluth, Arianna W. and Rosenbluth, Marshall N. and Teller, Augusta H. and Teller, Edward},
  title     = {Equation of State Calculations by Fast Computing Machines},
  journal   = {The Journal of Chemical Physics},
  volume    = {21},
  number    = {6},
  pages     = {1087-1092},
  publisher = {AIP Publishing},
  year      = {1953},
  month     = jun,
  url       = {http://dx.doi.org/10.1063/1.1699114},
  DOI       = {10.1063/1.1699114},
  ISSN      = {1089-7690}
}

@book{Thijssen2007,
  author    = {Thijssen, Jos M.},
  title     = {Computational Physics},
  year      = {2007},
  month     = mar,
  publisher = {Cambridge University Press},
  url       = {http://dx.doi.org/10.1017/CBO9781139171397},
  DOI       = {10.1017/cbo9781139171397},
  ISBN      = {9781107677135}
}