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.
ImportanceSamplers.WeightedSampleView — Type
WeightedSampleViewAn 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.
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.
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.
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.
Statistics
Statistics.mean — Function
Statistics.mean(result)Return the self-normalized weighted mean of the sampled parameters.
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.
Statistics.mean(samples::UnweightedSamples)
Statistics.mean(f, samples::UnweightedSamples)Return an ordinary unweighted expectation over logical samples.
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.
Statistics.var(f, result; corrected=false)Return the weighted target variance of the scalar function f. As with var(result), corrected=true is unsupported.
Statistics.var(samples::UnweightedSamples; corrected=true)
Statistics.var(f, samples::UnweightedSamples; corrected=true)Return the ordinary unweighted sample variance.
Statistics.std — Function
Statistics.std(result; corrected=false)Return the square root of the corresponding weighted target variance.
Statistics.std(f, result; corrected=false)Return the square root of var(f, result; corrected).
Statistics.cov — Function
Statistics.cov(result; corrected=false)Return the uncorrected weighted covariance matrix for vector-valued samples. corrected=true is unsupported.
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.
Statistics.median — Function
Statistics.median(result)Return Statistics.quantile(result, 0.5).
Resampling
ImportanceSamplers.AbstractResamplingMethod — Type
AbstractResamplingMethodAbstract supertype for concrete resampling algorithms.
ImportanceSamplers.MultinomialResampling — Type
MultinomialResampling()Independent weighted sampling with replacement.
ImportanceSamplers.resample — Function
resample(rng, result, count; method=MultinomialResampling())
resample(rng, result; method=MultinomialResampling())Draw unweighted samples with replacement from normalized importance weights. The count-free form returns length(result) draws. Output stays on the input device and carries no importance weights.