Where can I find complete documents for sample function?

One of thing that is troubling me since I started working in Julia is that many keywords for functions are hard to find in documents.

Example : I will assume that sample (model, NUTS(), MCMCThreads(), 1000, 2; progress = true) is same as sample() function mentioned here - https://juliastats.org/StatsBase.jl/stable/sampling/. or is it different ?

In this documentation I couldn’t find “save_state” keyword that if something like this exist and also “resumed_from” nowhere in Turing example I see these keywords. Am I missing something?

where I can find documentation for all sample() keywords that i can use?

Can you help us a bit mentioning which packages you use? StatsBase does not define MCMCThreads so I am sure the package you use extends the sample function for their own types (similarly NUTS() and the type of your model). The I could check where to best find the docs at least.

So – could you maybe provide an Minimal working example illustrating which packages you use?

I’m using these packages. I think its some additional function provided by Turing but I’m not sure.

using DifferentialEquations, StatsBase, StatsPlots, Serialization
using Turing, Distributions, Random, LabelledArrays, LinearAlgebra, Distributed
using DataFrames, CSV

Without knowing the concrete types it is a bit hard to narrow down. The general one is

But the rest is a bit hard to look for at least for me not being too familiar with the packages you use and without knowing the actual types.

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)

1 Like

A search with juliahub.com can sometimes give you clues:

https://juliahub.com/ui/Search?q=save_state&type=code

1 Like