API reference
MetropolisAlgorithm.bin
Base.count
MetropolisAlgorithm.metropolis
MetropolisAlgorithm.metropolis!
MetropolisAlgorithm.metropolis!
MetropolisAlgorithm.pdf
MetropolisAlgorithm.bin
— Methodbin(A::Vector{<:Vector}; number = fill(10,length(first(A))))
This function creates a data for 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)
Base.count
— Methodcount(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
MetropolisAlgorithm.metropolis!
— Methodmetropolis!(f::Function, R::Vector{<:Vector}, r_ini::Vector{<:Real}; type=typeof(first(r_ini)), d::Real=one(type))
This function calculates many-steps of one-walker and overwrites the second argument R
. Each child vector in R
is a point of the trajectory.
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 defined as a vector. Each child vector (point) has the same size asr_ini
.R::Vector{<:Vector}
: Vector of vectors (points). Each child vector is a point of the walker. The first element ofR
is same asr_ini
.n_steps::Int=10^5
: Number of steps. It is same as the length of the output parent vector.type::Type=typeof(first(r_ini))
: Type of trajectory points. e.g., Float32, Float64, etc..d::Real=one(type)
: Maximum step size. Default value is 1.
MetropolisAlgorithm.metropolis!
— Methodmetropolis!(f::Function, R::Vector{<:Vector}; type=typeof(first(first(R))), d::Real=one(type))
This function calculates one-step of many-walkers and overwrites the second argument R
. Each child vector in R
is a point of the walker (not a trajectory).
Arguments
f::Function
: Distribution function. It does not need to be normalized.R::Vector{<:Vector}
: Vector of vectors (points). Each child vector is a point of the walker.type::Type=typeof(first(first(R)))
: Type of trajectory points. e.g., Float32, Float64, etc..d::Real=one(type)
: Maximum step size.
MetropolisAlgorithm.metropolis
— Methodmetropolis(f::Function, r_ini::Vector{<:Real}; n_steps::Int=10^5, type=typeof(first(r_ini)), d::Real=one(type))
This function calculates many-steps of one-walker using metropolis!(f, R, r_ini)
and returns the trajectory R
as a vector of vectors (points) with memory allocation.
MetropolisAlgorithm.pdf
— Methodpdf(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
s
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