Distribution sampling
ConsensusBasedX.jl also provides Consensus-Based Sampling.
The package exports sample
, which behaves exactly as minimise
in Function minimisation. It assumes you have defined a function f(x::AbstractVector)
that takes a single vector argument x
of length D = length(x)
.
For instance, if D = 2
, you can sample exp(-αf)
by running:
out = sample(f, D = 2, extended_output=true)
out.sample
The method must be run with the extended_output=true
option in order to receive Extended output and access the full particle sample; without it, sample
returns a single Vector{Float64}
of length D
which contains the candidate to the global minimiser of f
, just like minimise
.
You must always provide D
.
Full example
using ConsensusBasedX
f(x) = ConsensusBasedX.Ackley(x, shift = 1)
out = sample(f, D = 2, N = 20, extended_output = true)
out.sample
Using a config
object
For more advanced usage, you will select several options. You can pass these as extra keyword arguments to sample
, or you can create a NamedTuple
called config
and pass that:
config = (; D = 2, extended_output=true)
out = sample(f, config)
out.sample
If you pass a Dict
instead, it will be converted to a NamedTuple
automatically.
Full example
using ConsensusBasedX
f(x) = ConsensusBasedX.Ackley(x, shift = 1)
config = (; D = 2, N = 20, extended_output = true)
out = sample(f, config)
out.sample
Running on minimisation mode
Consensus-based sampling can also be used for minimisation (see Consensus-Based Sampling). If you want to run it in that mode, pass the option CBS_mode = :minimise
.
Method reference
ConsensusBasedX.sample
— Functionsample(f; keywords...)
sample(f, config::NamedTuple)
Sample the distribution exp(-αf)
using Consensus-Based Sampling (see Distribution sampling).
You must specify the dimension D
of the problem. Other paramters (e.g. the number of particles N
or the number of ensembles M
) can also be specified; see Summary of options.
Examples
out = sample(f, D = 2, extended_output = true);
out.sample
config = (; D = 2, extended_output = true);
out = sample(f, config);
out.sample
out = sample(f, D = 2, N = 20, extended_output = true);
out.sample
config = (; D = 2, N = 20, extended_output = true);
out = sample(f, config);
out.sample