github.com/m3db/m3@v1.5.0/src/query/functions/aggregation/quantile.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package aggregation
    22  
    23  import (
    24  	"math"
    25  	"sort"
    26  )
    27  
    28  const (
    29  	// QuantileType takes the n-th non nan quantile element in a list of series
    30  	// Special cases are:
    31  	// 	 n < 0 = -Inf
    32  	// 	 n > 1 = +Inf
    33  	QuantileType = "quantile"
    34  )
    35  
    36  // Creates a quantile aggregation function for a given q-quantile measurement
    37  func makeQuantileFn(opType string, q float64) (aggregationFn, bool) {
    38  	if opType != QuantileType {
    39  		return nil, false
    40  	}
    41  	return func(values []float64, bucket []int) float64 {
    42  		return bucketedQuantileFn(q, values, bucket)
    43  	}, true
    44  }
    45  
    46  func bucketedQuantileFn(q float64, values []float64, bucket []int) float64 {
    47  	if math.IsNaN(q) || len(bucket) == 0 || len(values) == 0 {
    48  		return math.NaN()
    49  	}
    50  
    51  	if q < 0 || q > 1 {
    52  		// Use math.Inf(0) == +Inf by truncating q and subtracting 1 to give
    53  		// the correctly signed infinity
    54  		return math.Inf(int(q) - 1)
    55  	}
    56  
    57  	bucketVals := make([]float64, 0, len(bucket))
    58  	for _, idx := range bucket {
    59  		val := values[idx]
    60  		if !math.IsNaN(val) {
    61  			bucketVals = append(bucketVals, values[idx])
    62  		}
    63  	}
    64  
    65  	return quantileFn(q, bucketVals)
    66  }
    67  
    68  func quantileFn(q float64, values []float64) float64 {
    69  	l := float64(len(values))
    70  	if l == 0 {
    71  		// No non-NaN values
    72  		return math.NaN()
    73  	}
    74  
    75  	sort.Float64s(values)
    76  	// When the quantile lies between two samples,
    77  	// use a weighted average of the two samples.
    78  	rank := q * (l - 1)
    79  
    80  	leftIndex := math.Max(0, math.Floor(rank))
    81  	rightIndex := math.Min(l-1, leftIndex+1)
    82  
    83  	weight := rank - math.Floor(rank)
    84  	weightedLeft := values[int(leftIndex)] * (1 - weight)
    85  	weightedRight := values[int(rightIndex)] * weight
    86  	return weightedLeft + weightedRight
    87  }