Medial Code Documentation
Loading...
Searching...
No Matches
auc.h
1
4#ifndef XGBOOST_METRIC_AUC_H_
5#define XGBOOST_METRIC_AUC_H_
6#include <array>
7#include <cmath>
8#include <limits>
9#include <memory>
10#include <tuple>
11#include <utility>
12
13#include "../collective/communicator-inl.h"
14#include "../common/common.h"
15#include "../common/threading_utils.h"
16#include "xgboost/base.h"
17#include "xgboost/data.h"
18#include "xgboost/metric.h"
19#include "xgboost/span.h"
20
21namespace xgboost {
22namespace metric {
23/***********
24 * ROC AUC *
25 ***********/
26XGBOOST_DEVICE inline double TrapezoidArea(double x0, double x1, double y0, double y1) {
27 return std::abs(x0 - x1) * (y0 + y1) * 0.5f;
28}
29
30struct DeviceAUCCache;
31
32std::tuple<double, double, double> GPUBinaryROCAUC(common::Span<float const> predts,
33 MetaInfo const &info, std::int32_t device,
34 std::shared_ptr<DeviceAUCCache> *p_cache);
35
36double GPUMultiClassROCAUC(Context const *ctx, common::Span<float const> predts,
37 MetaInfo const &info, std::shared_ptr<DeviceAUCCache> *p_cache,
38 std::size_t n_classes);
39
40std::pair<double, std::uint32_t> GPURankingAUC(Context const *ctx, common::Span<float const> predts,
41 MetaInfo const &info,
42 std::shared_ptr<DeviceAUCCache> *cache);
43
44/**********
45 * PR AUC *
46 **********/
47std::tuple<double, double, double> GPUBinaryPRAUC(common::Span<float const> predts,
48 MetaInfo const &info, std::int32_t device,
49 std::shared_ptr<DeviceAUCCache> *p_cache);
50
51double GPUMultiClassPRAUC(Context const *ctx, common::Span<float const> predts,
52 MetaInfo const &info, std::shared_ptr<DeviceAUCCache> *p_cache,
53 std::size_t n_classes);
54
55std::pair<double, std::uint32_t> GPURankingPRAUC(Context const *ctx,
56 common::Span<float const> predts,
57 MetaInfo const &info,
58 std::shared_ptr<DeviceAUCCache> *cache);
59
60namespace detail {
61XGBOOST_DEVICE inline double CalcH(double fp_a, double fp_b, double tp_a,
62 double tp_b) {
63 return (fp_b - fp_a) / (tp_b - tp_a);
64}
65
66XGBOOST_DEVICE inline double CalcB(double fp_a, double h, double tp_a, double total_pos) {
67 return (fp_a - h * tp_a) / total_pos;
68}
69
70XGBOOST_DEVICE inline double CalcA(double h) { return h + 1; }
71
72XGBOOST_DEVICE inline double CalcDeltaPRAUC(double fp_prev, double fp,
73 double tp_prev, double tp,
74 double total_pos) {
75 double pr_prev = tp_prev / total_pos;
76 double pr = tp / total_pos;
77
78 double h{0}, a{0}, b{0};
79
80 if (tp == tp_prev) {
81 a = 1.0;
82 b = 0.0;
83 } else {
84 h = detail::CalcH(fp_prev, fp, tp_prev, tp);
85 a = detail::CalcA(h);
86 b = detail::CalcB(fp_prev, h, tp_prev, total_pos);
87 }
88
89 double area = 0;
90 if (b != 0.0) {
91 area = (pr - pr_prev -
92 b / a * (std::log(a * pr + b) - std::log(a * pr_prev + b))) /
93 a;
94 } else {
95 area = (pr - pr_prev) / a;
96 }
97 return area;
98}
99} // namespace detail
100
101inline void InvalidGroupAUC() {
102 LOG(INFO) << "Invalid group with less than 3 samples is found on worker "
103 << collective::GetRank() << ". Calculating AUC value requires at "
104 << "least 2 pairs of samples.";
105}
106
108 XGBOOST_DEVICE bool operator()(float y) { return y < 0.0f || y > 1.0f; }
109};
110
111inline void InvalidLabels() {
112 LOG(FATAL) << "PR-AUC supports only binary relevance for learning to rank.";
113}
114} // namespace metric
115} // namespace xgboost
116#endif // XGBOOST_METRIC_AUC_H_
Copyright 2015-2023 by XGBoost Contributors.
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition base.h:64
Copyright 2015-2023 by XGBoost Contributors.
detail namespace with internal helper functions
Definition json.hpp:249
int GetRank()
Get rank of current process.
Definition communicator-inl.h:76
namespace of xgboost
Definition base.h:90
Copyright 2014-2023 by XGBoost Contributors.