API reference

MetropolisAlgorithm.binMethod

bin(A::Vector{<:Vector}; number = fill(10,length(first(A))))

This function creates data for a multidimensional histogram (for testing).

Examples

using Random
A = [randn(2) for i in 1:10000]

using MetropolisAlgorithm
b = MetropolisAlgorithm.bin(A, number=[10,10])

using CairoMakie
X = b.center[1]
Y = b.center[2]
Z = b.counter
heatmap(X, Y, Z)
source
Base.countMethod
count(center::Vector, width::Vector, A::Vector{<:Vector})

This function counts the number of points in A that fall within the bin defined by center and width.

Arguments

  • center::Vector: Center of the bin. A vector of coordinates.
  • width::Vector: Width of the bin (rectangle, hypercube). A vector of coordinates.
  • A::Vector{<:Vector}: Vector of vectors (points). Each child vector is a point.

Examples

julia> count([0], [2], [randn(1) for i in 1:100000]) / 100 # ≈ 68.3% ()
68.395

julia> count([0], [0.1], [randn(1) for i in 1:100000]) / 100000 / 0.1
0.3988

julia> exp(0) / sqrt(2*π)
0.3989422804014327
source
MetropolisAlgorithm.metropolis!Method

metropolis!(f::Function, R::Vector{<:Vector}, rini::Vector{<:Real}; type=typeof(first(rini)), d::Real=one(type))

This function performs many steps for one walker and overwrites the second argument R.

Arguments

  • f::Function: Distribution function. It does not need to be normalized.
  • r_ini::Vector{<:Real}: Initial value vector. Even in the one-dimensional case the initial value must be a vector. Each element (point) has the same size as r_ini.
  • R::Vector{<:Vector}: Vector of vectors (points). Each element is a point of the trajectory. The first element of R is the same as r_ini.
  • n_steps::Int=10^4: Number of steps; this is the length of the output vector R and matches the default used in metropolis.
  • type::Type=typeof(first(r_ini)): Type of trajectory points, e.g. Float32 or Float64.
  • d::Real=one(type): Maximum step size. Default value is 1.
source
MetropolisAlgorithm.metropolis!Method

metropolis!(f::Function, R::Vector{<:Vector}; type=typeof(first(first(R))), d::Real=one(type))

This function performs one step for many walkers and overwrites the second argument R. Each element of R is a point (not a trajectory).

Arguments

  • f::Function: Distribution function. It does not need to be normalized.
  • R::Vector{<:Vector}: Vector of vectors (points). Each element is a point of a walker.
  • type::Type=typeof(first(first(R))): Type of trajectory points, e.g. Float32 or Float64.
  • d::Real=one(type): Maximum step size.
source
MetropolisAlgorithm.metropolisMethod

metropolis(f::Function, rini::Vector{<:Real}; nsteps::Int=10^4, type=typeof(first(r_ini)), d::Real=one(type))

This function performs many steps for one walker using metropolis!(f, R, r_ini) and returns the trajectory R as a vector of vectors (points) with memory allocation.

source
MetropolisAlgorithm.pdfMethod
pdf(center::Vector, width::Vector, A::Vector{<:Vector})

This function approximates the probability density function (PDF) with normalizing count(center, width, A). For the Metropolis algorithm, this function is not needed since the distribution function is known. It is used for testing the algorithm.

Arguments

  • center::Vector: Center of the bin. A vector of coordinates.
  • width::Vector: Width of the bin. A vector of coordinates.
  • A::Vector{<:Vector}: Vector of vectors (points). Each child vector is a point.

Examples

julia> pdf([0.0], [0.2], [randn(1) for i in 1:1000000])
0.39926999999999996

julia> exp(0) / sqrt(2*π)
0.3989422804014327

julia> pdf([0.0, 0.0], [0.2, 0.2], [randn(2) for i in 1:1000000])
0.15389999999999998
julia> exp(0) / sqrt(2*π)^2
0.15915494309189537

julia> pdf([0.0, 0.0, 0.0], [0.2, 0.2, 0.2], [randn(3) for i in 1:1000000])
0.06162499999999998

julia> exp(0) / sqrt(2*π)^3
0.06349363593424098
source