Applies a cap to a nonnegative weight vector and, when feasible, redistributes excess mass across the remaining positive entries so that the total sum is preserved. When the requested cap is too tight to preserve the total mass, all positive entries are set to the cap and the total sum decreases.
trim_weights(weights, cap, tol = 1e-12, warn_tol = 1e-08)A list with components:
weightsnumeric vector of trimmed weights.
trimmed_fractionfraction of entries at or very close to
the cap (within tol).
preserved_sumlogical; TRUE if the total sum of
weights is preserved to within warn_tol.
total_beforenumeric; sum of the original weights.
total_afternumeric; sum of the trimmed weights.
numeric vector of weights.
positive numeric scalar; maximum allowed weight, or Inf to disable trimming.
numeric tolerance used when testing whether a rescaling step respects the cap.
numeric tolerance used when testing whether the total sum has been preserved.
Zero weights remain zero. Only entries that are positive after nonnegativity enforcement can absorb redistributed mass.
Internally, a simple water-filling style algorithm is used on the positive weights: the largest weights are successively saturated at the cap and the remaining weights are rescaled by a common factor chosen to maintain the total sum.