Results and statistics

See Weighted samples for weighted estimates, indexing, and device rules.

Containers and weights

ImportanceSamplers.WeightedSamples — Type
WeightedSamples(samples, logweights; provenance=NamedTuple(), diagnostics=NamedTuple())

Construct a complete weighted-sample result with independent copies of the input arrays.

samples is a numeric vector, numeric matrix, or nonempty named-tuple tree of those arrays. Its final logical axis must align with the raw, unnormalized logweights. Plain importance sampling normally constructs this type through importance_sample or importance_sample!.

Scalar indexing and iteration return aligned named tuples with sample, logweight, and provenance fields. Range, integer-vector, and Boolean-mask indexing return a WeightedSampleView.

Apply an MLDataDevices.AbstractDevice to a complete result to create an independent copy on that device. Device-resident results must be transferred to CPU before scalar indexing, iteration, quantiles, or medians.

source
ImportanceSamplers.WeightedSampleView — Type
WeightedSampleView

An aligned descriptive view of a subset of weighted samples.

Views support indexing, iteration, and normalized_weights, but not lognormalizer, because an arbitrary subset is not a complete estimator result. Construct views by indexing a WeightedSamples. Device-resident views retain device storage. Apply an MLDataDevices.AbstractDevice directly to create an independent aligned copy; transfer to CPU before scalar indexing or iteration.

source
ImportanceSamplers.UnweightedSamples — Type
UnweightedSamples(samples)

Construct a batch of unweighted samples by copying the input arrays. Logical samples occupy the final array axis and support scalar indexing and iteration on CPU storage.

source
ImportanceSamplers.normalized_weights — Function
normalized_weights(result)

Derive an array of weights that sums to one from a complete result or descriptive view.

The raw result.logweights remain unchanged. If every raw log weight is -Inf, throw AllZeroWeightsError rather than inventing uniform weights. For a device-resident result, the returned weight array stays on the same device. Float16 log weights use Float32 accumulation and normalized output.

source
ImportanceSamplers.lognormalizer — Function
lognormalizer(result::WeightedSamples)

Return logsumexp(result.logweights) - log(length(result)), the log of the plain-importance-sampling normalizer estimate.

This is Bayesian log evidence only if the supplied target retains every required normalizing constant. It returns -Inf when all raw weights are zero. It is unavailable on WeightedSampleView, which is not a complete estimator. Device-resident complete results use a device reduction and transfer only its two-scalar log-sum-exp accumulator.

source

Statistics

Statistics.mean — Function
Statistics.mean(result)

Return the self-normalized weighted mean of the sampled parameters.

source
Statistics.mean(f, result)

Return the self-normalized weighted expectation of the scalar function f. Evaluate one value per sample without transferring a device-resident result to CPU.

source
Statistics.mean(samples::UnweightedSamples)
Statistics.mean(f, samples::UnweightedSamples)

Return an ordinary unweighted expectation over logical samples.

source
Statistics.var — Function
Statistics.var(result; corrected=false)

Return the uncorrected weighted target variance. corrected=true is unsupported because importance weights have no general sample-variance correction.

source
Statistics.var(f, result; corrected=false)

Return the weighted target variance of the scalar function f. As with var(result), corrected=true is unsupported.

source
Statistics.var(samples::UnweightedSamples; corrected=true)
Statistics.var(f, samples::UnweightedSamples; corrected=true)

Return the ordinary unweighted sample variance.

source
Statistics.std — Function
Statistics.std(result; corrected=false)

Return the square root of the corresponding weighted target variance.

source
Statistics.std(f, result; corrected=false)

Return the square root of var(f, result; corrected).

source
Statistics.cov — Function
Statistics.cov(result; corrected=false)

Return the uncorrected weighted covariance matrix for vector-valued samples. corrected=true is unsupported.

source
Statistics.quantile — Function
Statistics.quantile(result, probability)
Statistics.quantile(result, probabilities)

Return component-wise weighted quantiles using the StatsBase generic-weight interpolation rule. Transfer device-resident results to CPU before calling.

source

Resampling