Post-training assessment
The function assess can be used to assess a trained estimator. The resulting Assessment object contains ground-truth parameters, estimates, and other quantities that can be used to compute quantitative and qualitative diagnostics.
NeuralEstimators.assess Function
assess(estimator, θ, Z; ...)
assess(estimators::Vector, θ, Z; ...)
assess(estimator::PriorEstimator, θ; N = size(θ, 2), ...)
assess(estimator::PriorEstimator; N = 1000, ...)Assesses an estimator (or a collection of estimators) based on true parameters θ and corresponding simulated data Z.
The parameters θ should be given as a
When Z contains more simulated data sets than the number θ will be recycled via horizontal concatenation: θ = repeat(θ, outer = (1, J)), where J = numobs(Z) ÷ K is the number of simulated data sets for each parameter vector. This allows assessment of the estimator's sampling distribution under fixed parameters.
The return value is of type Assessment.
A PriorEstimator is assessed without data. Instead, N draws from the learned distribution are compared with test samples θ from the true distribution, if given as a N is risk). The draws can be visualised with plot(assessment). The keyword arguments parameter_names, estimator_name, device, and use_gpu (the latter two passed to sample and logdensity) also apply.
Keyword arguments
estimator_name::String(orestimator_names::Vector{String}for multiple estimators): name(s) of the estimator(s) (sensible defaults provided).parameter_names::Vector{String}: names of the parameters (sensible default provided).use_gpu = true:BoolorVector{Bool}with length equal to the number of estimators.probs = nothing(applicable only toPointEstimator): probability levels taking values between 0 and 1. By default, no bootstrap uncertainty quantification is done; ifprobsis provided, it must be a two-element vector specifying the lower and upper probability levels for non-parametric bootstrap intervals (note that parametric bootstrap is not currently supported withassess()).B::Integer = 400(applicable only toPointEstimator): number of bootstrap samples.pointsummary::Function = mean(applicable only to estimators that yield posterior samples): a function that summarises a vector of posterior samples into a single point estimate for each marginal; any function mapping a vector to a scalar is valid (e.g.,medianfor the posterior median).kwargs...(applicable only to estimators that yield posterior samples): additional keyword arguments passed tosampleposterior.
NeuralEstimators.Assessment Type
AssessmentA type for storing the output of assess(). The field runtime contains the total time taken for each estimator. The field estimates is a long-form DataFrame with columns:
parameter: the name of the parametertruth: the true value of the parameterestimate: the estimated value of the parameterk: the index of the parameter vectorj: the index of the data set (only relevant in the case that multiple data sets are associated with each parameter vector)
If the estimator is a PosteriorEstimator or a RatioEstimator, in addition to the fields listed above, the field samples stores the posterior samples as a long-form DataFrame with the columns parameter, truth, k, j (as given above), as well as:
draw: the index of the draw within the posterior samplesvalue: the value of the posterior sample for a given parameter and draw.
If the estimator is an IntervalEstimator, the column estimate will be replaced by the columns lower and upper, containing the lower and upper bounds of the interval, respectively.
If the estimator is a QuantileEstimator, there will also be a column prob indicating the probability level of the corresponding quantile estimate.
If the estimator is a PriorEstimator, the field estimates is nothing, and the field samples is a long-form DataFrame with the columns parameter, draw, and value (as given above), and distribution, which is "learned" for draws from the learned distribution and "true" for test samples from the true distribution. The field risk then contains the average negative log density of the test samples under the learned distribution (or nothing if no test samples were given).
Use merge() to combine assessments from multiple estimators of the same type or join() to combine assessments from a PointEstimator and an IntervalEstimator.
Makie.plot Method
plot(assessment::Assessment; plots = nothing, ...)Visualise the performance of a neural estimator, given the Assessment object returned by assess. Returns a Makie Figure with one panel for each parameter.
Extension
This function is defined in the NeuralEstimatorsPlottingMakieExt extension and requires CairoMakie (or another Makie backend) to be loaded.
The plots that are available depend on the type of estimator that was assessed. By default all of them are drawn; use the keyword argument plots to select a subset.
Point and interval estimates (PointEstimator, IntervalEstimator):
:recovery: estimates against the true values. Accurate estimates lie close to the dashed identity line. For anIntervalEstimator, the intervals are drawn as vertical line segments; when the assessment contains both point estimates and intervals, the intervals are drawn only ifintervals = true.
Quantile estimates (QuantileEstimator):
:calibration: the proportion of estimated quantiles that are greater than the true value, against the nominal probability level τ. Specifically, for k = 1,…,K, sample pairs (θᵏ, Zᵏ) with θᵏ ∼ p(θ) and Zᵏ ∼ p(Z ∣ θᵏ), so that θᵏ is a draw from the posterior p(θ ∣ Zᵏ); then, for each τ, plot the proportion of the estimated quantiles Q(Zᵏ, τ) that are greater than θᵏ. A well-calibrated estimator follows the dashed identity line.
Posterior samples (PosteriorEstimator, RatioEstimator, TelescopingRatioEstimator):
:recovery: point estimates (thepointsummarygiven toassess, by default the posterior mean) against the true values. Ifintervals = true, central 95% credible intervals are drawn as vertical line segments.:ecdf: simulation-based calibration. For each parameter, the empirical distribution function of the fractional rank of the true value among the posterior draws, together with a simultaneousprob-level confidence band (Säilynoja et al., 2022). A well-calibrated posterior gives a curve that stays within the band.:zscore: the posterior z-score, (posterior mean − true value) / posterior standard deviation, against the posterior contraction, 1 − posterior variance / prior variance. Ideally the z-scores are centred on zero and the contractions are close to one.
Learned distributions (PriorEstimator): draws from the learned distribution (filled histograms and coloured points), compared with test samples from the true distribution (outlined histograms and points in the text colour) if these were given to assess; otherwise, the learned distribution is shown alone.
:pairs(default): with one parameter, a histogram; with two parameters, the two marginal histograms and a scatter plot of the joint draws; with three or more parameters, a pairs plot with the marginal histograms on the diagonal and scatter plots of the joint draws off the diagonal (seeoverlay).:marginals: the marginal histograms only, in a single row (seencols).
Keyword arguments
plots = nothing: the plots to draw, given as aSymbolor a collection ofSymbols from those listed above, in the order in which they should appear (e.g.,plots = (:recovery, :ecdf)). By default, all available plots are drawn. For aPriorEstimator, give one of:pairs(the default) or:marginals.intervals = false: whether to draw intervals behind the point estimates in the:recoveryplot. They are not drawn by default, since they tend to hide the points when there are many test data sets.prob = 0.99: simultaneous coverage of the confidence band in the:ecdfplot.difference = true: iftrue, the:ecdfplot shows the difference between the empirical distribution function and that of the uniform distribution, which makes departures from calibration easier to see; iffalse, it shows the empirical distribution function itself.grid = false: when the assessment contains several estimators (seemerge()), they are by default drawn in the same panels in different colours. Ifgrid = true, each estimator is instead given its own row of panels, which is easier to read with more than three estimators.ncols = nothing: the number of panels in each row, after which the parameters wrap onto a new row. By default, at most four, balanced across rows; for the:marginalsplot of aPriorEstimator, all panels are in one row.transpose = nothing: by default, the figure has one row of panels for each plot and one column for each parameter. Iftranspose = true, it instead has one row for each parameter and one column for each plot (e.g., 2 × 3 for two parameters and three plots), andncolshas no effect. With a single parameter, the figure is transposed unlesstranspose = false.overlay = false: for the:pairsplot of aPriorEstimatorwith three or more parameters, the learned draws are drawn below the diagonal and the test samples above it. Ifoverlay = true, both are instead drawn below the diagonal, and the panels above it are left empty.figure = (;),axis = (;): attributes passed to theFigureand to everyAxis, respectively. By default the panels are of a fixed size and the figure is sized to fit them; givefigure = (; size = (w, h))to fix the size of the figure instead. Colours and fonts are taken from the current Makie theme.
Examples
using NeuralEstimators, CairoMakie
# Given an estimator and test parameters and data (see `assess`)
assessment = assess(estimator, θ_test, Z_test)
plot(assessment) # all available plots
plot(assessment; plots = :recovery) # a single plot
plot(assessment; plots = (:recovery, :ecdf)) # a subset (posterior samples)
plot(assessment; transpose = true) # one row per parameter, one column per plot
# Learned distributions (PriorEstimator), with or without test samples from the true distribution
assessment = assess(estimator, θ_test)
plot(assessment) # pairs plot
plot(assessment; plots = :marginals) # marginal histograms onlyNeuralEstimators.risk Function
risk(assessment::Assessment; ...)Computes a Monte Carlo approximation of an estimator's Bayes risk,
where
If the Assessment object corresponds to an estimator with a self-defined loss (e.g., PosteriorEstimator), the precomputed risk is returned directly. For a PriorEstimator, this is the average negative log density of the test samples under the learned distribution. Otherwise, the risk is computed from the estimates and true parameters using the provided loss function.
Keyword arguments
loss = (x, y) -> abs(x - y): a binary operator defining the loss function (default: absolute-error loss)average_over_parameters::Bool = false: iftrue, the loss is averaged over all parameters; otherwise (default), it is computed separately for each parameter.
NeuralEstimators.bias Function
bias(assessment::Assessment; average_over_parameters = false)Computes a Monte Carlo approximation of an estimator's bias,
where
NeuralEstimators.rmse Function
rmse(assessment::Assessment; average_over_parameters = false)Computes a Monte Carlo approximation of an estimator's root-mean-squared error,
where
NeuralEstimators.coverage Function
coverage(assessment::Assessment; ...)Computes a Monte Carlo approximation of an interval estimator's expected coverage, as defined in Hermans et al. (2022, Definition 2.1), and the proportion of parameters below and above the lower and upper bounds, respectively.
Keyword arguments
average_over_parameters::Bool = false: if true, the coverage is averaged over all parameters; otherwise (default), it is computed over each parameter separately.