They are not the same:
julia> using StatsBase, Turing
help?> sample
search: sample sampler sample! samplepair Sampleable wsample wsample! nsamples
sample([rng], a, [wv::AbstractWeights])
Select a single random element of a. Sampling probabilities are proportional to the weights given in wv, if provided.
Optionally specify a random number generator rng as the first argument (defaults to Random.GLOBAL_RNG).
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample([rng], a, [wv::AbstractWeights], n::Integer; replace=true, ordered=false)
Select a random, optionally weighted sample of size n from an array a using a polyalgorithm. Sampling probabilities are proportional to the weights given in wv, if provided. replace dictates whether sampling
is performed with replacement. ordered dictates whether an ordered sample (also called a sequential sample, i.e. a sample where items appear in the same order as in a) should be taken.
Optionally specify a random number generator rng as the first argument (defaults to Random.GLOBAL_RNG).
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample([rng], a, [wv::AbstractWeights], dims::Dims; replace=true, ordered=false)
Select a random, optionally weighted sample from an array a specifying the dimensions dims of the output array. Sampling probabilities are proportional to the weights given in wv, if provided. replace
dictates whether sampling is performed with replacement. ordered dictates whether an ordered sample (also called a sequential sample, i.e. a sample where items appear in the same order as in a) should be
taken.
Optionally specify a random number generator rng as the first argument (defaults to Random.GLOBAL_RNG).
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample([rng], wv::AbstractWeights)
Select a single random integer in 1:length(wv) with probabilities proportional to the weights given in wv.
Optionally specify a random number generator rng as the first argument (defaults to Random.GLOBAL_RNG).
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample(
rng::Random.AbatractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
N_or_isdone;
kwargs...,
)
Sample from the model with the Markov chain Monte Carlo sampler and return the samples.
If N_or_isdone is an Integer, exactly N_or_isdone samples are returned.
Otherwise, sampling is performed until a convergence criterion N_or_isdone returns true. The convergence criterion has to be a function with the signature
isdone(rng, model, sampler, samples, state, iteration; kwargs...)
where state and iteration are the current state and iteration of the sampler, respectively. It should return true when sampling should end, and false otherwise.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample(
rng::Random.AbstractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
parallel::AbstractMCMCEnsemble,
N::Integer,
nchains::Integer;
kwargs...,
)
Sample nchains Monte Carlo Markov chains from the model with the sampler in parallel using the parallel algorithm, and combine them into a single chain.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample(
rng::Random.AbstractRNG=Random.default_rng(),
logdensity,
sampler::AbstractSampler,
N_or_isdone;
kwargs...,
)
Wrap the logdensity function in a LogDensityModel, and call sample with the resulting model instead of logdensity.
The logdensity function has to support the LogDensityProblems.jl (https://github.com/tpapp/LogDensityProblems.jl) interface.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample(
rng::Random.AbstractRNG=Random.default_rng(),
logdensity,
sampler::AbstractSampler,
parallel::AbstractMCMCEnsemble,
N::Integer,
nchains::Integer;
kwargs...,
)
Wrap the logdensity function in a LogDensityModel, and call sample with the resulting model instead of logdensity.
The logdensity function has to support the LogDensityProblems.jl (https://github.com/tpapp/LogDensityProblems.jl) interface.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample([rng,] chn::Chains, [wv::AbstractWeights,] n; replace=true, ordered=false)
Sample n samples from the pooled (!) chain chn.
The keyword arguments replace and ordered determine whether sampling is performed with replacement and whether the sample is ordered, respectively. If specified, sampling probabilities are proportional to
weights wv.
│ Note
│
│ If chn contains multiple chains, they are pooled (i.e., appended) before sampling. This ensures that even in this case exactly n samples are returned:
│
│ julia> chn = Chains(randn(11, 4, 3));
│
│ julia> size(sample(chn, 7)) == (7, 4, 1)
│ true
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample(
rng::AbstractRNG,
h::Hamiltonian,
κ::AbstractMCMCKernel,
θ::AbstractVecOrMat{T},
n_samples::Int,
adaptor::AbstractAdaptor=NoAdaptation(),
n_adapts::Int=min(div(n_samples, 10), 1_000);
drop_warmup::Bool=false,
verbose::Bool=true,
progress::Bool=false
)
Sample n_samples samples using the proposal κ under Hamiltonian h.
• The randomness is controlled by rng.
• If rng is not provided, GLOBAL_RNG will be used.
• The initial point is given by θ.
• The adaptor is set by adaptor, for which the default is no adaptation.
• It will perform n_adapts steps of adaptation, for which the default is the minimum of 1_000 and 10% of n_samples
• drop_warmup controls to drop the samples during adaptation phase or not
• verbose controls the verbosity
• progress controls whether to show the progress meter or not
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sample(
model::AbstractMCMC.LogDensityModel,
kernel::AdvancedHMC.AbstractMCMCKernel,
metric::AdvancedHMC.AbstractMetric,
adaptor::AdvancedHMC.Adaptation.AbstractAdaptor,
N::Integer;
kwargs...
) -> Any
A convenient wrapper around AbstractMCMC.sample avoiding explicit construction of HMCSampler.
You can also use @which
to find out which method of a function is called with a given set of arguments and where that method is defined:
julia> @which sample(gdemo(1.5, 2), SMC(), 1000)
sample(model::AbstractMCMC.AbstractModel, alg::Turing.Inference.InferenceAlgorithm, N::Integer; kwargs...)
@ Turing.Inference C:\Users\ngudat\.julia\packages\Turing\fLFsI\src\inference\Inference.jl:130
(Here I’ve used the first example in the Turing docs for the call as I don’t have your model
to hand)