Skip to content

Inference with observed data

The following functions facilitate the use of a trained neural estimator with observed data.

NeuralEstimators.infer Function
julia
infer(estimator::AbstractBayesEstimator, args...; kwargs...)
infer(estimator::Union{PosteriorEstimator, RatioEstimator, TelescopingRatioEstimator}, args...; kwargs...)

Unified inference interface that dispatches to estimate for Bayes estimators and sampleposterior for posterior/ratio estimators.

source

Posterior inference

NeuralEstimators.sampleposterior Function
julia
sampleposterior(estimator::PosteriorEstimator, Z; N = 1000, kwargs...)
sampleposterior(estimator::RatioEstimator, Z; lower = nothing, upper = nothing, grid = nothing, logprior = θ -> 0f0, warmup = 750, N = 1000, kwargs...)
sampleposterior(estimator::TelescopingRatioEstimator, Z; lower, upper, logpriors = nothing, chebyshev_batchsize = 1, N = 1000, kwargs...)

Samples from the approximate posterior distribution implied by estimator.

The keyword argument N controls the size of the posterior sample (default 1000).

Returns a × N × array of posterior samples, where is the dimension of the parameter vector and is the number of independent data sets in Z (so a single data set yields a × N × array).

Remaining keyword arguments are passed onto summarystatistics.

PosteriorEstimator

Draws independent samples from the approximate posterior distribution associated with the estimator.

RatioEstimator

Draw posterior samples by one of two approaches, selected by which keyword arguments are supplied:

  • HMC (supply lower and upper): NUTS (via the AdvancedHMC extension) on the continuous pdf that is proportional to exp(logratio(θ) + logprior(θ)) on the box with bounds lower and upper. One chain per data set with warmup adaptation steps discarded. Requires using AdvancedHMC, ForwardDiff, LogDensityProblems.

  • Grid (supply grid): discrete sampling on a fixed grid (a d × G matrix, one candidate parameter configuration per column). Grid cells are drawn with replacement, with weights proportional to exp(logratio(θ) + logprior(θ)).

Keyword arguments when sampling with RatioEstimators:

  • logprior::Function = θ -> 0f0: log prior density evaluated on a d-vector, up to a constant; the default is uniform. For HMC, it must be differentiable by ForwardDiff.jl.

  • grid::AbstractMatrix: candidate parameter configurations for grid sampling.

  • lower::AbstractVector, upper::AbstractVector: prior box bounds for HMC.

  • warmup::Integer = 750: adaptation steps for HMC, discarded from the output.

TelescopingRatioEstimator

Draw posterior samples sequentially in the coordinates of : first generate  , then  , and so on. Each one-dimensional conditional density is approximated by a Chebyshev polynomial of the given degree on [lower[i], upper[i]], and then sampled by inversion sampling.

Keyword arguments when sampling with TelescopingRatioEstimators:

  • lower::AbstractVector, upper::AbstractVector: prior bounds for each of the d parameters.

  • degree::Integer = 128: degree of the Chebyshev approximation of each conditional density.

  • logpriors = nothing: an iterable collection of d functions where logpriors[i] is the log marginal prior density for the i-th parameter. This must agree with the marginal priors used during training, otherwise samples will be drawn from the wrong distribution. By default, assumes uniform marginal priors with bounds specified by lower and upper.

  • chebyshev_batchsize::Integer = 1: number of data sets fused together (per parameter) for efficiency.

source
NeuralEstimators.logratio Function
julia
logratio(estimator::Union{RatioEstimator, TelescopingRatioEstimator}, Z; grid)

Compute the log likelihood-to-evidence ratio for each parameter configuration in grid.

Arguments

  • Z: observed data

  • grid: matrix of parameter values, where each column is a parameter configuration

Returns

A matrix of log ratios with one row per data set and one column per grid point.

source
NeuralEstimators.posteriormean Function
julia
posteriormean::AbstractMatrix)
posteriormean::AbstractArray{<:Any, 3})
posteriormean(estimator, Z; kwargs...)

Computes the posterior mean based either on a × matrix or a × × array θ of posterior draws, where denotes the number of parameters, the number of draws, and the number of independent data sets, or directly from an estimator that allows for posterior sampling via sampleposterior().

For a × × array the result is a × matrix.

See also posteriormedian(), posteriorquantile().

source
NeuralEstimators.posteriormedian Function
julia
posteriormedian::AbstractMatrix)
posteriormedian::AbstractArray{<:Any, 3})
posteriormedian(estimator, Z; kwargs...)

Computes the vector of marginal posterior medians based either on a × matrix or a × × array θ of posterior draws, where denotes the number of parameters, the number of draws, and the number of independent data sets, or directly from an estimator that allows for posterior sampling via sampleposterior().

For a × × array the result is a × matrix.

See also posteriormean(), posteriorquantile().

source
NeuralEstimators.posteriorquantile Function
julia
posteriorquantile::AbstractMatrix, probs)
posteriorquantile::AbstractArray{<:Any, 3}, probs)
posteriorquantile(estimator, Z, probs; kwargs...)

Computes the vector of marginal posterior quantiles with (a collection of) probability levels probs, based either on a × matrix or a × × array θ of posterior draws, where denotes the number of parameters, the number of draws, and the number of independent data sets, or directly from an estimator that allows for posterior sampling via sampleposterior().

The return value is a × length(probs) matrix for a single data set, or a × length(probs) × array for data sets.

See also posteriormedian(), posteriormean().

source
NeuralEstimators.spikeprobability Function
julia
spikeprobability(estimator::PosteriorEstimator, Z)

For a PosteriorEstimator whose approximate distribution is a SpikeAndSlab, returns the estimated posterior probability that the parameter equals the spike value (i.e., the mixture probability ), given data Z.

Returns a scalar for a single data set, or a vector of probabilities for a collection of data sets.

See also SpikeAndSlab and sampleposterior.

source

Point estimation

NeuralEstimators.estimate Function
julia
estimate(estimator::AbstractBayesEstimator, Z; batchsize::Integer = 32, use_gpu::Bool = true, kwargs...)

Applies estimator to data Z and returns the resulting estimates.

source
NeuralEstimators.bootstrap Function
julia
bootstrap(estimator::PointEstimator, parameters::P, Z; use_gpu = true) where P <: Union{AbstractMatrix, AbstractParameterSet}
bootstrap(estimator::PointEstimator, parameters::P, simulator, m::Integer; B = 400, use_gpu = true) where P <: Union{AbstractMatrix, AbstractParameterSet}
bootstrap(estimator::PointEstimator, Z; B = 400, blocks = nothing, trim = true, use_gpu = true)

Generates B bootstrap estimates using estimator.

Parametric bootstrapping is facilitated by passing a single parameter configuration, parameters, and corresponding simulated data, Z, whose length implicitly defines B. Alternatively, one may provide a simulator and the desired sample size, in which case the data will be simulated using simulator(parameters, m).

Non-parametric bootstrapping is facilitated by passing a single data set, Z. The argument blocks caters for block bootstrapping, and it should be a vector of integers specifying the block for each replicate. For example, with 5 replicates, the first two corresponding to block 1 and the remaining three corresponding to block 2, blocks should be [1, 1, 2, 2, 2]. The resampling algorithm generates resampled data sets by sampling blocks with replacement. If trim = true, the final block is trimmed as needed to ensure that the resampled data set matches the original size of Z.

The return type is a × B matrix, where is the dimension of the parameter vector.

source
NeuralEstimators.interval Function
julia
interval::Matrix; probs = [0.05, 0.95], parameter_names = nothing)
interval::AbstractArray{<:Any, 3}; probs = [0.05, 0.95], parameter_names = nothing)
interval(estimator::IntervalEstimator, Z; parameter_names = nothing, use_gpu = true)

Computes a confidence/credible interval based either on a × matrix θ of parameters (typically containing bootstrap estimates or posterior draws), where denotes the number of parameters to make inference on, or from an IntervalEstimator and data Z.

When given θ, the intervals are constructed by computing quantiles with probability levels controlled by the keyword argument probs.

The return type is a × 2 matrix, whose first and second columns respectively contain the lower and upper bounds of the interval. The rows of this matrix can be named by passing a vector of strings to the keyword argument parameter_names. When θ is a × × array, a vector of such matrices is returned (a single matrix when  ).

source
NeuralEstimators.quantiles Function
julia
quantiles(estimator::QuantileEstimator, Z; parameter_names = nothing, use_gpu = true)

Computes marginal posterior quantiles from a QuantileEstimator and data Z. For full-conditional estimators (those initialised with i set), pass the input as a tuple (Z, θ₋ᵢ) where θ₋ᵢ contains the conditioning values.

The return type is a × matrix, whose columns correspond to the probability levels stored in estimator.probs and whose rows correspond to the parameters. The rows of this matrix can be named by passing a vector of strings to the keyword argument parameter_names.

source