Merge pull request #13852 from prometheus/fix-hist-std-dev-var-negative

Fix hist std dev var negative
This commit is contained in:
Björn Rabenstein 2024-03-27 17:58:03 +01:00 committed by GitHub
commit b9a2a4e329
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View File

@ -3514,7 +3514,39 @@ func TestNativeHistogram_HistogramStdDevVar(t *testing.T) {
}, },
NegativeBuckets: []int64{1, 0}, NegativeBuckets: []int64{1, 0},
}, },
stdVar: 1544.8582535368798, // actual variance: 1738.4082 stdVar: 1844.4651144196398, // actual variance: 1738.4082
},
{
name: "-100000, -10000, -1000, -888, -888, -100, -50, -9, -8, -3",
h: &histogram.Histogram{
Count: 10,
ZeroCount: 0,
Sum: -112946,
Schema: 0,
NegativeSpans: []histogram.Span{
{Offset: 2, Length: 3},
{Offset: 1, Length: 2},
{Offset: 2, Length: 1},
{Offset: 3, Length: 1},
{Offset: 2, Length: 1},
},
NegativeBuckets: []int64{1, 0, 0, 0, 0, 2, -2, 0},
},
stdVar: 759352122.1939945, // actual variance: 882690990
},
{
name: "-10 x10",
h: &histogram.Histogram{
Count: 10,
ZeroCount: 0,
Sum: -100,
Schema: 0,
NegativeSpans: []histogram.Span{
{Offset: 4, Length: 1},
},
NegativeBuckets: []int64{10},
},
stdVar: 1.725830020304794, // actual variance: 0
}, },
{ {
name: "-50, -8, 0, 3, 8, 9, 100, NaN", name: "-50, -8, 0, 3, 8, 9, 100, NaN",

View File

@ -1111,11 +1111,17 @@ func funcHistogramStdDev(vals []parser.Value, args parser.Expressions, enh *Eval
it := sample.H.AllBucketIterator() it := sample.H.AllBucketIterator()
for it.Next() { for it.Next() {
bucket := it.At() bucket := it.At()
if bucket.Count == 0 {
continue
}
var val float64 var val float64
if bucket.Lower <= 0 && 0 <= bucket.Upper { if bucket.Lower <= 0 && 0 <= bucket.Upper {
val = 0 val = 0
} else { } else {
val = math.Sqrt(bucket.Upper * bucket.Lower) val = math.Sqrt(bucket.Upper * bucket.Lower)
if bucket.Upper < 0 {
val = -val
}
} }
delta := val - mean delta := val - mean
variance, cVariance = kahanSumInc(bucket.Count*delta*delta, variance, cVariance) variance, cVariance = kahanSumInc(bucket.Count*delta*delta, variance, cVariance)
@ -1144,11 +1150,17 @@ func funcHistogramStdVar(vals []parser.Value, args parser.Expressions, enh *Eval
it := sample.H.AllBucketIterator() it := sample.H.AllBucketIterator()
for it.Next() { for it.Next() {
bucket := it.At() bucket := it.At()
if bucket.Count == 0 {
continue
}
var val float64 var val float64
if bucket.Lower <= 0 && 0 <= bucket.Upper { if bucket.Lower <= 0 && 0 <= bucket.Upper {
val = 0 val = 0
} else { } else {
val = math.Sqrt(bucket.Upper * bucket.Lower) val = math.Sqrt(bucket.Upper * bucket.Lower)
if bucket.Upper < 0 {
val = -val
}
} }
delta := val - mean delta := val - mean
variance, cVariance = kahanSumInc(bucket.Count*delta*delta, variance, cVariance) variance, cVariance = kahanSumInc(bucket.Count*delta*delta, variance, cVariance)