xgboost

probability_distribution.h
Go to the documentation of this file.
1
8 #ifndef XGBOOST_COMMON_PROBABILITY_DISTRIBUTION_H_
9 #define XGBOOST_COMMON_PROBABILITY_DISTRIBUTION_H_
10
11 #include <cmath>
12
13 namespace xgboost {
14 namespace common {
15
16 #ifndef __CUDACC__
17
18 using std::exp;
19 using std::sqrt;
20 using std::isinf;
21 using std::isnan;
22
23 #endif // __CUDACC__
24
26 constexpr double kPI = 3.14159265358979323846;
28 constexpr double kEulerMascheroni = 0.57721566490153286060651209008240243104215933593992;
29
31 enum class ProbabilityDistributionType : int {
32 kNormal = 0, kLogistic = 1, kExtreme = 2
33 };
34
35 struct NormalDistribution {
36 XGBOOST_DEVICE static double PDF(double z) {
37 return exp(-z * z / 2.0) / sqrt(2.0 * kPI);
38 }
39
40 XGBOOST_DEVICE static double CDF(double z) {
41 return 0.5 * (1 + erf(z / sqrt(2.0)));
42 }
43
44 XGBOOST_DEVICE static double GradPDF(double z) {
45 return -z * PDF(z);
46 }
47
48 XGBOOST_DEVICE static double HessPDF(double z) {
49 return (z * z - 1.0) * PDF(z);
50 }
51
52 XGBOOST_DEVICE static ProbabilityDistributionType Type() {
53 return ProbabilityDistributionType::kNormal;
54 }
55 };
56
57 struct LogisticDistribution {
58 XGBOOST_DEVICE static double PDF(double z) {
59 const double w = exp(z);
60 const double sqrt_denominator = 1 + w;
61 if (isinf(w) || isinf(w * w)) {
62 return 0.0;
63 } else {
64 return w / (sqrt_denominator * sqrt_denominator);
65 }
66 }
67
68 XGBOOST_DEVICE static double CDF(double z) {
69 const double w = exp(z);
70 return isinf(w) ? 1.0 : (w / (1 + w));
71 }
72
73 XGBOOST_DEVICE static double GradPDF(double z) {
74 const double w = exp(z);
75 return isinf(w) ? 0.0 : (PDF(z) * (1 - w) / (1 + w));
76 }
77
78 XGBOOST_DEVICE static double HessPDF(double z) {
79 const double w = exp(z);
80 if (isinf(w) || isinf(w * w)) {
81 return 0.0;
82 } else {
83 return PDF(z) * (w * w - 4 * w + 1) / ((1 + w) * (1 + w));
84 }
85 }
86
87 XGBOOST_DEVICE static ProbabilityDistributionType Type() {
88 return ProbabilityDistributionType::kLogistic;
89 }
90 };
91
92 struct ExtremeDistribution {
93 XGBOOST_DEVICE static double PDF(double z) {
94 const double w = exp(z);
95 return isinf(w) ? 0.0 : (w * exp(-w));
96 }
97
98 XGBOOST_DEVICE static double CDF(double z) {
99 const double w = exp(z);
100 return 1 - exp(-w);
101 }
102
103 XGBOOST_DEVICE static double GradPDF(double z) {
104 const double w = exp(z);
105 return isinf(w) ? 0.0 : ((1 - w) * PDF(z));
106 }
107
108 XGBOOST_DEVICE static double HessPDF(double z) {
109 const double w = exp(z);
110 if (isinf(w) || isinf(w * w)) {
111 return 0.0;
112 } else {
113 return (w * w - 3 * w + 1) * PDF(z);
114 }
115 }
116
117 XGBOOST_DEVICE static ProbabilityDistributionType Type() {
118 return ProbabilityDistributionType::kExtreme;
119 }
120 };
121
122 } // namespace common
123 } // namespace xgboost
124
125 #endif // XGBOOST_COMMON_PROBABILITY_DISTRIBUTION_H_
xgboost::common::LogisticDistribution::CDF
static XGBOOST_DEVICE double CDF(double z)
Definition: probability_distribution.h:68
xgboost::common::ExtremeDistribution::GradPDF
static XGBOOST_DEVICE double GradPDF(double z)
Definition: probability_distribution.h:103
xgboost::common::ProbabilityDistributionType
ProbabilityDistributionType
Enum encoding possible choices of probability distribution.
Definition: probability_distribution.h:31
xgboost::common::LogisticDistribution::Type
static XGBOOST_DEVICE ProbabilityDistributionType Type()
Definition: probability_distribution.h:87
xgboost::common::NormalDistribution::Type
static XGBOOST_DEVICE ProbabilityDistributionType Type()
Definition: probability_distribution.h:52
xgboost::common::ExtremeDistribution::CDF
static XGBOOST_DEVICE double CDF(double z)
Definition: probability_distribution.h:98
xgboost::common::NormalDistribution::PDF
static XGBOOST_DEVICE double PDF(double z)
Definition: probability_distribution.h:36
xgboost::common::kEulerMascheroni
constexpr double kEulerMascheroni
The Euler-Mascheroni_constant.
Definition: probability_distribution.h:28
xgboost::common::ExtremeDistribution
Definition: probability_distribution.h:92
xgboost::common::ExtremeDistribution::PDF
static XGBOOST_DEVICE double PDF(double z)
Definition: probability_distribution.h:93
xgboost::common::LogisticDistribution::GradPDF
static XGBOOST_DEVICE double GradPDF(double z)
Definition: probability_distribution.h:73
xgboost::common::ProbabilityDistributionType::kLogistic
@ kLogistic
xgboost::common::NormalDistribution::GradPDF
static XGBOOST_DEVICE double GradPDF(double z)
Definition: probability_distribution.h:44
xgboost::common::LogisticDistribution::PDF
static XGBOOST_DEVICE double PDF(double z)
Definition: probability_distribution.h:58
xgboost::common::LogisticDistribution::HessPDF
static XGBOOST_DEVICE double HessPDF(double z)
Definition: probability_distribution.h:78
xgboost::common::ExtremeDistribution::HessPDF
static XGBOOST_DEVICE double HessPDF(double z)
Definition: probability_distribution.h:108
xgboost::common::kPI
constexpr double kPI
Constant PI.
Definition: probability_distribution.h:26
xgboost::common::ProbabilityDistributionType::kNormal
@ kNormal
xgboost::common::LogisticDistribution
Definition: probability_distribution.h:57
XGBOOST_DEVICE
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:84
xgboost::common::ProbabilityDistributionType::kExtreme
@ kExtreme
xgboost::common::NormalDistribution::HessPDF
static XGBOOST_DEVICE double HessPDF(double z)
Definition: probability_distribution.h:48
xgboost::common::NormalDistribution::CDF
static XGBOOST_DEVICE double CDF(double z)
Definition: probability_distribution.h:40
xgboost::common::ExtremeDistribution::Type
static XGBOOST_DEVICE ProbabilityDistributionType Type()
Definition: probability_distribution.h:117
xgboost::common::NormalDistribution
Definition: probability_distribution.h:35
xgboost
namespace of xgboost
Definition: base.h:110