MCPcopy Create free account
hub / github.com/catboost/catboost / CalcSampleQuantileBinarySearch

Function CalcSampleQuantileBinarySearch

catboost/libs/helpers/quantile.cpp:18–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16}
17
18static double CalcSampleQuantileBinarySearch(
19 TConstArrayRef<float> sampleRef,
20 TConstArrayRef<float> weightsRef,
21 const double alpha
22) {
23 constexpr int BINARY_SEARCH_ITERATIONS = 100;
24
25 const double totalWeight = Accumulate(weightsRef, 0.0);
26 const double needWeight = totalWeight * alpha;
27
28 const auto minMaxSamples = MinMaxElement(sampleRef.begin(), sampleRef.end());
29 double lQ = *minMaxSamples.first - DBL_EPSILON;
30 double rQ = *minMaxSamples.second;
31
32 const size_t sampleSize = sampleRef.size();
33 TVector<TValueWithWeight> elements;
34 elements.yresize(sampleSize);
35 for (auto i : xrange(sampleSize)) {
36 elements[i] = {sampleRef[i], weightsRef[i]};
37 }
38 /*
39 * We will support the following invariant:
40 * total weight of elements with sample values <= lQ is strictly less than needWeight
41 * total weight of elements with sample values <= rQ is greater or equal than needWeight
42 * elements with indices < l are less or equal than q
43 * elements with indices > r are greater than q
44 */
45 int l = 0, r = sampleSize;
46 double collectedLeftWeight = 0;
47 for (auto it : xrange(BINARY_SEARCH_ITERATIONS)) {
48 Y_UNUSED(it);
49 const double q = (lQ + rQ) / 2;
50 auto partitionIt = std::partition(
51 elements.begin() + l,
52 elements.begin() + r,
53 [q](const TValueWithWeight& element) {
54 return element.Value <= q;
55 }
56 );
57 const double partitionLeftWeight = Accumulate(
58 elements.begin() + l,
59 partitionIt,
60 0.0,
61 [](double sum, const TValueWithWeight& element) {
62 return sum + element.Weight;
63 }
64 );
65 const int partitionPoint = partitionIt - elements.begin();
66
67 if (collectedLeftWeight + partitionLeftWeight < needWeight - DBL_EPSILON) {
68 l = partitionPoint;
69 lQ = q;
70 collectedLeftWeight += partitionLeftWeight;
71 } else {
72 r = partitionPoint;
73 rQ = q;
74 }
75 }

Callers 1

CalcSampleQuantileFunction · 0.85

Calls 9

AccumulateFunction · 0.85
MinMaxElementFunction · 0.85
xrangeFunction · 0.85
Y_UNUSEDFunction · 0.85
yresizeMethod · 0.80
partitionFunction · 0.50
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected