ipa: rpi: Allow the mean of an empty histogram interval

Don't assert when taking the weighted mean of a zero-width or
zero-weight interval; return its upper bound. That is certainly
correct in the zero-width case, and plausible otherwise.

Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
Reviewed-by: David Plowman <david.plowman@raspberrypi.com>
Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Nick Hollinghurst
2023-12-06 17:27:43 +00:00
committed by Kieran Bingham
parent 12fb8ed5cb
commit e71d63ce1b

View File

@@ -47,7 +47,7 @@ double Histogram::quantile(double q, int first, int last) const
double Histogram::interBinMean(double binLo, double binHi) const
{
assert(binHi > binLo);
assert(binHi >= binLo);
double sumBinFreq = 0, cumulFreq = 0;
for (double binNext = floor(binLo) + 1.0; binNext <= ceil(binHi);
binLo = binNext, binNext += 1.0) {
@@ -57,13 +57,19 @@ double Histogram::interBinMean(double binLo, double binHi) const
sumBinFreq += bin * freq;
cumulFreq += freq;
}
if (cumulFreq == 0) {
/* interval had zero width or contained no weight? */
return binHi;
}
/* add 0.5 to give an average for bin mid-points */
return sumBinFreq / cumulFreq + 0.5;
}
double Histogram::interQuantileMean(double qLo, double qHi) const
{
assert(qHi > qLo);
assert(qHi >= qLo);
double pLo = quantile(qLo);
double pHi = quantile(qHi, (int)pLo);
return interBinMean(pLo, pHi);