Barycenter of Histograms
histbary.RdCompute the finite 1D 2-Wasserstein barycenter of midpoint-discrete histograms, then return its projection onto the original histogram bins. All input histograms must have identical breaks. The unprojected finite measure is retained separately.
Arguments
- hists
a length-\(N\) list of histograms (
"histogram"objects) of same breaks.- weights
a weight for each histogram; if
NULL(default), uniform weights are used. Otherwise, it should be a length-\(N\) vector of nonnegative weights.- L
retained for compatibility and validated as a positive integer. The finite quantile backend uses exact mass intervals, so this argument no longer controls an approximation grid.
Value
a "histogram" display of the barycenter, with the
unprojected estimate and projection metadata.
Details
Inputs are discrete measures at bin midpoints, using normalized
counts (or the canonical mass field of an estimated histogram).
The unprojected finite result is saved in unprojected_measure.
For the legacy histogram display it is binned onto the original breaks,
using right-closed bins with the lowest endpoint included, and represented
at bin midpoints. This projection generally changes the measure; it is
not an exact unrestricted Wasserstein summary or geodesic.
Downstream histogram functions consistently use the displayed midpoint
measure, not the separate unprojected result.
Break values must agree exactly across inputs; no grid snapping is done.
When an estimated histogram is reused, its previous computational metadata
is discarded. Returned diagnostics describe the current calculation.
Returned mass and counts are the same probabilities;
density equals mass divided by bin width. Counts are not rounded
observations, and effective_sample_size is NA because an
estimated measure has no implied sample size. The objective/history and
solver status refer to the unprojected summary.
Examples
# \donttest{
#----------------------------------------------------------------------
# Binned from Two Gaussians
#
# EXAMPLE : Very Small Example for CRAN; just showing how to use it!
#----------------------------------------------------------------------
# GENERATE FROM TWO GAUSSIANS WITH DIFFERENT MEANS
set.seed(100)
x = stats::rnorm(1000, mean=-4, sd=0.5)
y = stats::rnorm(1000, mean=+4, sd=0.5)
bk = seq(from=-10, to=10, length.out=20)
# HISTOGRAMS WITH COMMON BREAKS
histxy = list()
histxy[[1]] = hist(x, breaks=bk, plot=FALSE)
histxy[[2]] = hist(y, breaks=bk, plot=FALSE)
# COMPUTE
hh = histbary(histxy)
# VISUALIZE
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,2), pty="s")
barplot(histxy[[1]]$density, col=rgb(0,0,1,1/4),
ylim=c(0, 0.75), main="Two Histograms")
barplot(histxy[[2]]$density, col=rgb(1,0,0,1/4),
ylim=c(0, 0.75), add=TRUE)
barplot(hh$density, main="Barycenter",
ylim=c(0, 0.75))
par(opar)
# }