GPU implementation of AFT survival objective and metric (#5714)
* Add interval accuracy * De-virtualize AFT functions * Lint * Refactor AFT metric using GPU-CPU reducer * Fix R build * Fix build on Windows * Fix copyright header * Clang-tidy * Fix crashing demo * Fix typos in comment; explain GPU ID * Remove unnecessary #include * Add C++ test for interval accuracy * Fix a bug in accuracy metric: use log pred * Refactor AFT objective using GPU-CPU Transform * Lint * Fix lint * Use Ninja to speed up build * Use time, not /usr/bin/time * Add cpu_build worker class, with concurrency = 1 * Use concurrency = 1 only for CUDA build * concurrency = 1 for clang-tidy * Address reviewer's feedback * Update link to AFT paper
This commit is contained in:
committed by
GitHub
parent
7c2686146e
commit
71b0528a2f
@@ -1,107 +0,0 @@
|
||||
/*!
|
||||
* Copyright 2020 by Contributors
|
||||
* \file probability_distribution.cc
|
||||
* \brief Implementation of a few useful probability distributions
|
||||
* \author Avinash Barnwal and Hyunsu Cho
|
||||
*/
|
||||
|
||||
#include <xgboost/logging.h>
|
||||
#include <cmath>
|
||||
#include "probability_distribution.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
|
||||
ProbabilityDistribution* ProbabilityDistribution::Create(ProbabilityDistributionType dist) {
|
||||
switch (dist) {
|
||||
case ProbabilityDistributionType::kNormal:
|
||||
return new NormalDist;
|
||||
case ProbabilityDistributionType::kLogistic:
|
||||
return new LogisticDist;
|
||||
case ProbabilityDistributionType::kExtreme:
|
||||
return new ExtremeDist;
|
||||
default:
|
||||
LOG(FATAL) << "Unknown distribution";
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
double NormalDist::PDF(double z) {
|
||||
const double pdf = std::exp(-z * z / 2) / std::sqrt(2 * probability_constant::kPI);
|
||||
return pdf;
|
||||
}
|
||||
|
||||
double NormalDist::CDF(double z) {
|
||||
const double cdf = 0.5 * (1 + std::erf(z / std::sqrt(2)));
|
||||
return cdf;
|
||||
}
|
||||
|
||||
double NormalDist::GradPDF(double z) {
|
||||
const double pdf = this->PDF(z);
|
||||
const double grad = -1 * z * pdf;
|
||||
return grad;
|
||||
}
|
||||
|
||||
double NormalDist::HessPDF(double z) {
|
||||
const double pdf = this->PDF(z);
|
||||
const double hess = (z * z - 1) * pdf;
|
||||
return hess;
|
||||
}
|
||||
|
||||
double LogisticDist::PDF(double z) {
|
||||
const double w = std::exp(z);
|
||||
const double sqrt_denominator = 1 + w;
|
||||
const double pdf
|
||||
= (std::isinf(w) || std::isinf(w * w)) ? 0.0 : (w / (sqrt_denominator * sqrt_denominator));
|
||||
return pdf;
|
||||
}
|
||||
|
||||
double LogisticDist::CDF(double z) {
|
||||
const double w = std::exp(z);
|
||||
const double cdf = std::isinf(w) ? 1.0 : (w / (1 + w));
|
||||
return cdf;
|
||||
}
|
||||
|
||||
double LogisticDist::GradPDF(double z) {
|
||||
const double pdf = this->PDF(z);
|
||||
const double w = std::exp(z);
|
||||
const double grad = std::isinf(w) ? 0.0 : pdf * (1 - w) / (1 + w);
|
||||
return grad;
|
||||
}
|
||||
|
||||
double LogisticDist::HessPDF(double z) {
|
||||
const double pdf = this->PDF(z);
|
||||
const double w = std::exp(z);
|
||||
const double hess
|
||||
= (std::isinf(w) || std::isinf(w * w)) ? 0.0 : pdf * (w * w - 4 * w + 1) / ((1 + w) * (1 + w));
|
||||
return hess;
|
||||
}
|
||||
|
||||
double ExtremeDist::PDF(double z) {
|
||||
const double w = std::exp(z);
|
||||
const double pdf = std::isinf(w) ? 0.0 : (w * std::exp(-w));
|
||||
return pdf;
|
||||
}
|
||||
|
||||
double ExtremeDist::CDF(double z) {
|
||||
const double w = std::exp(z);
|
||||
const double cdf = 1 - std::exp(-w);
|
||||
return cdf;
|
||||
}
|
||||
|
||||
double ExtremeDist::GradPDF(double z) {
|
||||
const double pdf = this->PDF(z);
|
||||
const double w = std::exp(z);
|
||||
const double grad = std::isinf(w) ? 0.0 : ((1 - w) * pdf);
|
||||
return grad;
|
||||
}
|
||||
|
||||
double ExtremeDist::HessPDF(double z) {
|
||||
const double pdf = this->PDF(z);
|
||||
const double w = std::exp(z);
|
||||
const double hess = (std::isinf(w) || std::isinf(w * w)) ? 0.0 : ((w * w - 3 * w + 1) * pdf);
|
||||
return hess;
|
||||
}
|
||||
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Copyright 2020 by Contributors
|
||||
* Copyright 2019-2020 by Contributors
|
||||
* \file probability_distribution.h
|
||||
* \brief Implementation of a few useful probability distributions
|
||||
* \author Avinash Barnwal and Hyunsu Cho
|
||||
@@ -8,85 +8,115 @@
|
||||
#ifndef XGBOOST_COMMON_PROBABILITY_DISTRIBUTION_H_
|
||||
#define XGBOOST_COMMON_PROBABILITY_DISTRIBUTION_H_
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
|
||||
namespace probability_constant {
|
||||
#ifndef __CUDACC__
|
||||
|
||||
using std::exp;
|
||||
using std::sqrt;
|
||||
using std::isinf;
|
||||
using std::isnan;
|
||||
|
||||
#endif // __CUDACC__
|
||||
|
||||
/*! \brief Constant PI */
|
||||
const double kPI = 3.14159265358979323846;
|
||||
constexpr double kPI = 3.14159265358979323846;
|
||||
/*! \brief The Euler-Mascheroni_constant */
|
||||
const double kEulerMascheroni = 0.57721566490153286060651209008240243104215933593992;
|
||||
|
||||
} // namespace probability_constant
|
||||
constexpr double kEulerMascheroni = 0.57721566490153286060651209008240243104215933593992;
|
||||
|
||||
/*! \brief Enum encoding possible choices of probability distribution */
|
||||
enum class ProbabilityDistributionType : int {
|
||||
kNormal = 0, kLogistic = 1, kExtreme = 2
|
||||
};
|
||||
|
||||
/*! \brief Interface for a probability distribution */
|
||||
class ProbabilityDistribution {
|
||||
public:
|
||||
/*!
|
||||
* \brief Evaluate Probability Density Function (PDF) at a particular point
|
||||
* \param z point at which to evaluate PDF
|
||||
* \return Value of PDF evaluated
|
||||
*/
|
||||
virtual double PDF(double z) = 0;
|
||||
/*!
|
||||
* \brief Evaluate Cumulative Distribution Function (CDF) at a particular point
|
||||
* \param z point at which to evaluate CDF
|
||||
* \return Value of CDF evaluated
|
||||
*/
|
||||
virtual double CDF(double z) = 0;
|
||||
/*!
|
||||
* \brief Evaluate first derivative of PDF at a particular point
|
||||
* \param z point at which to evaluate first derivative of PDF
|
||||
* \return Value of first derivative of PDF evaluated
|
||||
*/
|
||||
virtual double GradPDF(double z) = 0;
|
||||
/*!
|
||||
* \brief Evaluate second derivative of PDF at a particular point
|
||||
* \param z point at which to evaluate second derivative of PDF
|
||||
* \return Value of second derivative of PDF evaluated
|
||||
*/
|
||||
virtual double HessPDF(double z) = 0;
|
||||
struct NormalDistribution {
|
||||
XGBOOST_DEVICE static double PDF(double z) {
|
||||
return exp(-z * z / 2.0) / sqrt(2.0 * kPI);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Factory function to instantiate a new probability distribution object
|
||||
* \param dist kind of probability distribution
|
||||
* \return Reference to the newly created probability distribution object
|
||||
*/
|
||||
static ProbabilityDistribution* Create(ProbabilityDistributionType dist);
|
||||
virtual ~ProbabilityDistribution() = default;
|
||||
XGBOOST_DEVICE static double CDF(double z) {
|
||||
return 0.5 * (1 + erf(z / sqrt(2.0)));
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static double GradPDF(double z) {
|
||||
return -z * PDF(z);
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static double HessPDF(double z) {
|
||||
return (z * z - 1.0) * PDF(z);
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static ProbabilityDistributionType Type() {
|
||||
return ProbabilityDistributionType::kNormal;
|
||||
}
|
||||
};
|
||||
|
||||
/*! \brief The (standard) normal distribution */
|
||||
class NormalDist : public ProbabilityDistribution {
|
||||
public:
|
||||
double PDF(double z) override;
|
||||
double CDF(double z) override;
|
||||
double GradPDF(double z) override;
|
||||
double HessPDF(double z) override;
|
||||
struct LogisticDistribution {
|
||||
XGBOOST_DEVICE static double PDF(double z) {
|
||||
const double w = exp(z);
|
||||
const double sqrt_denominator = 1 + w;
|
||||
if (isinf(w) || isinf(w * w)) {
|
||||
return 0.0;
|
||||
} else {
|
||||
return w / (sqrt_denominator * sqrt_denominator);
|
||||
}
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static double CDF(double z) {
|
||||
const double w = exp(z);
|
||||
return isinf(w) ? 1.0 : (w / (1 + w));
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static double GradPDF(double z) {
|
||||
const double w = exp(z);
|
||||
return isinf(w) ? 0.0 : (PDF(z) * (1 - w) / (1 + w));
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static double HessPDF(double z) {
|
||||
const double w = exp(z);
|
||||
if (isinf(w) || isinf(w * w)) {
|
||||
return 0.0;
|
||||
} else {
|
||||
return PDF(z) * (w * w - 4 * w + 1) / ((1 + w) * (1 + w));
|
||||
}
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static ProbabilityDistributionType Type() {
|
||||
return ProbabilityDistributionType::kLogistic;
|
||||
}
|
||||
};
|
||||
|
||||
/*! \brief The (standard) logistic distribution */
|
||||
class LogisticDist : public ProbabilityDistribution {
|
||||
public:
|
||||
double PDF(double z) override;
|
||||
double CDF(double z) override;
|
||||
double GradPDF(double z) override;
|
||||
double HessPDF(double z) override;
|
||||
};
|
||||
struct ExtremeDistribution {
|
||||
XGBOOST_DEVICE static double PDF(double z) {
|
||||
const double w = exp(z);
|
||||
return isinf(w) ? 0.0 : (w * exp(-w));
|
||||
}
|
||||
|
||||
/*! \brief The extreme distribution, also known as the Gumbel (minimum) distribution */
|
||||
class ExtremeDist : public ProbabilityDistribution {
|
||||
public:
|
||||
double PDF(double z) override;
|
||||
double CDF(double z) override;
|
||||
double GradPDF(double z) override;
|
||||
double HessPDF(double z) override;
|
||||
XGBOOST_DEVICE static double CDF(double z) {
|
||||
const double w = exp(z);
|
||||
return 1 - exp(-w);
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static double GradPDF(double z) {
|
||||
const double w = exp(z);
|
||||
return isinf(w) ? 0.0 : ((1 - w) * PDF(z));
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static double HessPDF(double z) {
|
||||
const double w = exp(z);
|
||||
if (isinf(w) || isinf(w * w)) {
|
||||
return 0.0;
|
||||
} else {
|
||||
return (w * w - 3 * w + 1) * PDF(z);
|
||||
}
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE static ProbabilityDistributionType Type() {
|
||||
return ProbabilityDistributionType::kExtreme;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace common
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Copyright 2019 by Contributors
|
||||
* Copyright 2019-2020 by Contributors
|
||||
* \file survival_util.cc
|
||||
* \brief Utility functions, useful for implementing objective and metric functions for survival
|
||||
* analysis
|
||||
@@ -7,258 +7,12 @@
|
||||
*/
|
||||
|
||||
#include <dmlc/registry.h>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include "survival_util.h"
|
||||
|
||||
/*
|
||||
- Formulas are motivated from document -
|
||||
http://members.cbio.mines-paristech.fr/~thocking/survival.pdf
|
||||
- Detailed Derivation of Loss/Gradient/Hessian -
|
||||
https://github.com/avinashbarnwal/GSOC-2019/blob/master/doc/Accelerated_Failure_Time.pdf
|
||||
*/
|
||||
|
||||
namespace {
|
||||
|
||||
// Allowable range for gradient and hessian. Used for regularization
|
||||
constexpr double kMinGradient = -15.0;
|
||||
constexpr double kMaxGradient = 15.0;
|
||||
constexpr double kMinHessian = 1e-16; // Ensure that no data point gets zero hessian
|
||||
constexpr double kMaxHessian = 15.0;
|
||||
|
||||
constexpr double kEps = 1e-12; // A denomitor in a fraction should not be too small
|
||||
|
||||
// Clip (limit) x to fit range [x_min, x_max].
|
||||
// If x < x_min, return x_min; if x > x_max, return x_max; if x_min <= x <= x_max, return x.
|
||||
// This function assumes x_min < x_max; behavior is undefined if this assumption does not hold.
|
||||
inline double Clip(double x, double x_min, double x_max) {
|
||||
if (x < x_min) {
|
||||
return x_min;
|
||||
}
|
||||
if (x > x_max) {
|
||||
return x_max;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
using xgboost::common::ProbabilityDistributionType;
|
||||
|
||||
enum class CensoringType : uint8_t {
|
||||
kUncensored, kRightCensored, kLeftCensored, kIntervalCensored
|
||||
};
|
||||
|
||||
using xgboost::GradientPairPrecise;
|
||||
|
||||
inline GradientPairPrecise GetLimitAtInfPred(ProbabilityDistributionType dist_type,
|
||||
CensoringType censor_type,
|
||||
double sign, double sigma) {
|
||||
switch (censor_type) {
|
||||
case CensoringType::kUncensored:
|
||||
switch (dist_type) {
|
||||
case ProbabilityDistributionType::kNormal:
|
||||
return sign ? GradientPairPrecise{ kMinGradient, 1.0 / (sigma * sigma) }
|
||||
: GradientPairPrecise{ kMaxGradient, 1.0 / (sigma * sigma) };
|
||||
case ProbabilityDistributionType::kLogistic:
|
||||
return sign ? GradientPairPrecise{ -1.0 / sigma, kMinHessian }
|
||||
: GradientPairPrecise{ 1.0 / sigma, kMinHessian };
|
||||
case ProbabilityDistributionType::kExtreme:
|
||||
return sign ? GradientPairPrecise{ kMinGradient, kMaxHessian }
|
||||
: GradientPairPrecise{ 1.0 / sigma, kMinHessian };
|
||||
default:
|
||||
LOG(FATAL) << "Unknown distribution type";
|
||||
}
|
||||
case CensoringType::kRightCensored:
|
||||
switch (dist_type) {
|
||||
case ProbabilityDistributionType::kNormal:
|
||||
return sign ? GradientPairPrecise{ kMinGradient, 1.0 / (sigma * sigma) }
|
||||
: GradientPairPrecise{ 0.0, kMinHessian };
|
||||
case ProbabilityDistributionType::kLogistic:
|
||||
return sign ? GradientPairPrecise{ -1.0 / sigma, kMinHessian }
|
||||
: GradientPairPrecise{ 0.0, kMinHessian };
|
||||
case ProbabilityDistributionType::kExtreme:
|
||||
return sign ? GradientPairPrecise{ kMinGradient, kMaxHessian }
|
||||
: GradientPairPrecise{ 0.0, kMinHessian };
|
||||
default:
|
||||
LOG(FATAL) << "Unknown distribution type";
|
||||
}
|
||||
case CensoringType::kLeftCensored:
|
||||
switch (dist_type) {
|
||||
case ProbabilityDistributionType::kNormal:
|
||||
return sign ? GradientPairPrecise{ 0.0, kMinHessian }
|
||||
: GradientPairPrecise{ kMaxGradient, 1.0 / (sigma * sigma) };
|
||||
case ProbabilityDistributionType::kLogistic:
|
||||
return sign ? GradientPairPrecise{ 0.0, kMinHessian }
|
||||
: GradientPairPrecise{ 1.0 / sigma, kMinHessian };
|
||||
case ProbabilityDistributionType::kExtreme:
|
||||
return sign ? GradientPairPrecise{ 0.0, kMinHessian }
|
||||
: GradientPairPrecise{ 1.0 / sigma, kMinHessian };
|
||||
default:
|
||||
LOG(FATAL) << "Unknown distribution type";
|
||||
}
|
||||
case CensoringType::kIntervalCensored:
|
||||
switch (dist_type) {
|
||||
case ProbabilityDistributionType::kNormal:
|
||||
return sign ? GradientPairPrecise{ kMinGradient, 1.0 / (sigma * sigma) }
|
||||
: GradientPairPrecise{ kMaxGradient, 1.0 / (sigma * sigma) };
|
||||
case ProbabilityDistributionType::kLogistic:
|
||||
return sign ? GradientPairPrecise{ -1.0 / sigma, kMinHessian }
|
||||
: GradientPairPrecise{ 1.0 / sigma, kMinHessian };
|
||||
case ProbabilityDistributionType::kExtreme:
|
||||
return sign ? GradientPairPrecise{ kMinGradient, kMaxHessian }
|
||||
: GradientPairPrecise{ 1.0 / sigma, kMinHessian };
|
||||
default:
|
||||
LOG(FATAL) << "Unknown distribution type";
|
||||
}
|
||||
default:
|
||||
LOG(FATAL) << "Unknown censoring type";
|
||||
}
|
||||
|
||||
return { 0.0, 0.0 };
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
|
||||
DMLC_REGISTER_PARAMETER(AFTParam);
|
||||
|
||||
double AFTLoss::Loss(double y_lower, double y_upper, double y_pred, double sigma) {
|
||||
const double log_y_lower = std::log(y_lower);
|
||||
const double log_y_upper = std::log(y_upper);
|
||||
|
||||
double cost;
|
||||
|
||||
if (y_lower == y_upper) { // uncensored
|
||||
const double z = (log_y_lower - y_pred) / sigma;
|
||||
const double pdf = dist_->PDF(z);
|
||||
// Regularize the denominator with eps, to avoid INF or NAN
|
||||
cost = -std::log(std::max(pdf / (sigma * y_lower), kEps));
|
||||
} else { // censored; now check what type of censorship we have
|
||||
double z_u, z_l, cdf_u, cdf_l;
|
||||
if (std::isinf(y_upper)) { // right-censored
|
||||
cdf_u = 1;
|
||||
} else { // left-censored or interval-censored
|
||||
z_u = (log_y_upper - y_pred) / sigma;
|
||||
cdf_u = dist_->CDF(z_u);
|
||||
}
|
||||
if (std::isinf(y_lower)) { // left-censored
|
||||
cdf_l = 0;
|
||||
} else { // right-censored or interval-censored
|
||||
z_l = (log_y_lower - y_pred) / sigma;
|
||||
cdf_l = dist_->CDF(z_l);
|
||||
}
|
||||
// Regularize the denominator with eps, to avoid INF or NAN
|
||||
cost = -std::log(std::max(cdf_u - cdf_l, kEps));
|
||||
}
|
||||
|
||||
return cost;
|
||||
}
|
||||
|
||||
double AFTLoss::Gradient(double y_lower, double y_upper, double y_pred, double sigma) {
|
||||
const double log_y_lower = std::log(y_lower);
|
||||
const double log_y_upper = std::log(y_upper);
|
||||
double numerator, denominator, gradient; // numerator and denominator of gradient
|
||||
CensoringType censor_type;
|
||||
bool z_sign; // sign of z-score
|
||||
|
||||
if (y_lower == y_upper) { // uncensored
|
||||
const double z = (log_y_lower - y_pred) / sigma;
|
||||
const double pdf = dist_->PDF(z);
|
||||
const double grad_pdf = dist_->GradPDF(z);
|
||||
censor_type = CensoringType::kUncensored;
|
||||
numerator = grad_pdf;
|
||||
denominator = sigma * pdf;
|
||||
z_sign = (z > 0);
|
||||
} else { // censored; now check what type of censorship we have
|
||||
double z_u = 0.0, z_l = 0.0, pdf_u, pdf_l, cdf_u, cdf_l;
|
||||
censor_type = CensoringType::kIntervalCensored;
|
||||
if (std::isinf(y_upper)) { // right-censored
|
||||
pdf_u = 0;
|
||||
cdf_u = 1;
|
||||
censor_type = CensoringType::kRightCensored;
|
||||
} else { // interval-censored or left-censored
|
||||
z_u = (log_y_upper - y_pred) / sigma;
|
||||
pdf_u = dist_->PDF(z_u);
|
||||
cdf_u = dist_->CDF(z_u);
|
||||
}
|
||||
if (std::isinf(y_lower)) { // left-censored
|
||||
pdf_l = 0;
|
||||
cdf_l = 0;
|
||||
censor_type = CensoringType::kLeftCensored;
|
||||
} else { // interval-censored or right-censored
|
||||
z_l = (log_y_lower - y_pred) / sigma;
|
||||
pdf_l = dist_->PDF(z_l);
|
||||
cdf_l = dist_->CDF(z_l);
|
||||
}
|
||||
z_sign = (z_u > 0 || z_l > 0);
|
||||
numerator = pdf_u - pdf_l;
|
||||
denominator = sigma * (cdf_u - cdf_l);
|
||||
}
|
||||
gradient = numerator / denominator;
|
||||
if (denominator < kEps && (std::isnan(gradient) || std::isinf(gradient))) {
|
||||
gradient = GetLimitAtInfPred(dist_type_, censor_type, z_sign, sigma).GetGrad();
|
||||
}
|
||||
|
||||
return Clip(gradient, kMinGradient, kMaxGradient);
|
||||
}
|
||||
|
||||
double AFTLoss::Hessian(double y_lower, double y_upper, double y_pred, double sigma) {
|
||||
const double log_y_lower = std::log(y_lower);
|
||||
const double log_y_upper = std::log(y_upper);
|
||||
double numerator, denominator, hessian; // numerator and denominator of hessian
|
||||
CensoringType censor_type;
|
||||
bool z_sign; // sign of z-score
|
||||
|
||||
if (y_lower == y_upper) { // uncensored
|
||||
const double z = (log_y_lower - y_pred) / sigma;
|
||||
const double pdf = dist_->PDF(z);
|
||||
const double grad_pdf = dist_->GradPDF(z);
|
||||
const double hess_pdf = dist_->HessPDF(z);
|
||||
censor_type = CensoringType::kUncensored;
|
||||
numerator = -(pdf * hess_pdf - grad_pdf * grad_pdf);
|
||||
denominator = sigma * sigma * pdf * pdf;
|
||||
z_sign = (z > 0);
|
||||
} else { // censored; now check what type of censorship we have
|
||||
double z_u = 0.0, z_l = 0.0, grad_pdf_u, grad_pdf_l, pdf_u, pdf_l, cdf_u, cdf_l;
|
||||
censor_type = CensoringType::kIntervalCensored;
|
||||
if (std::isinf(y_upper)) { // right-censored
|
||||
pdf_u = 0;
|
||||
cdf_u = 1;
|
||||
grad_pdf_u = 0;
|
||||
censor_type = CensoringType::kRightCensored;
|
||||
} else { // interval-censored or left-censored
|
||||
z_u = (log_y_upper - y_pred) / sigma;
|
||||
pdf_u = dist_->PDF(z_u);
|
||||
cdf_u = dist_->CDF(z_u);
|
||||
grad_pdf_u = dist_->GradPDF(z_u);
|
||||
}
|
||||
if (std::isinf(y_lower)) { // left-censored
|
||||
pdf_l = 0;
|
||||
cdf_l = 0;
|
||||
grad_pdf_l = 0;
|
||||
censor_type = CensoringType::kLeftCensored;
|
||||
} else { // interval-censored or right-censored
|
||||
z_l = (log_y_lower - y_pred) / sigma;
|
||||
pdf_l = dist_->PDF(z_l);
|
||||
cdf_l = dist_->CDF(z_l);
|
||||
grad_pdf_l = dist_->GradPDF(z_l);
|
||||
}
|
||||
const double cdf_diff = cdf_u - cdf_l;
|
||||
const double pdf_diff = pdf_u - pdf_l;
|
||||
const double grad_diff = grad_pdf_u - grad_pdf_l;
|
||||
const double sqrt_denominator = sigma * cdf_diff;
|
||||
z_sign = (z_u > 0 || z_l > 0);
|
||||
numerator = -(cdf_diff * grad_diff - pdf_diff * pdf_diff);
|
||||
denominator = sqrt_denominator * sqrt_denominator;
|
||||
}
|
||||
hessian = numerator / denominator;
|
||||
if (denominator < kEps && (std::isnan(hessian) || std::isinf(hessian))) {
|
||||
hessian = GetLimitAtInfPred(dist_type_, censor_type, z_sign, sigma).GetHess();
|
||||
}
|
||||
|
||||
return Clip(hessian, kMinHessian, kMaxHessian);
|
||||
}
|
||||
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Copyright 2019 by Contributors
|
||||
* Copyright 2019-2020 by Contributors
|
||||
* \file survival_util.h
|
||||
* \brief Utility functions, useful for implementing objective and metric functions for survival
|
||||
* analysis
|
||||
@@ -8,8 +8,16 @@
|
||||
#ifndef XGBOOST_COMMON_SURVIVAL_UTIL_H_
|
||||
#define XGBOOST_COMMON_SURVIVAL_UTIL_H_
|
||||
|
||||
/*
|
||||
* For the derivation of the loss, gradient, and hessian for the Accelerated Failure Time model,
|
||||
* refer to the paper "Survival regression with accelerated failure time model in XGBoost"
|
||||
* at https://arxiv.org/abs/2006.04920.
|
||||
*/
|
||||
|
||||
#include <xgboost/parameter.h>
|
||||
#include <memory>
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include "probability_distribution.h"
|
||||
|
||||
DECLARE_FIELD_ENUM_CLASS(xgboost::common::ProbabilityDistributionType);
|
||||
@@ -17,6 +25,51 @@ DECLARE_FIELD_ENUM_CLASS(xgboost::common::ProbabilityDistributionType);
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
|
||||
#ifndef __CUDACC__
|
||||
|
||||
using std::log;
|
||||
using std::fmax;
|
||||
|
||||
#endif // __CUDACC__
|
||||
|
||||
enum class CensoringType : uint8_t {
|
||||
kUncensored, kRightCensored, kLeftCensored, kIntervalCensored
|
||||
};
|
||||
|
||||
namespace aft {
|
||||
|
||||
// Allowable range for gradient and hessian. Used for regularization
|
||||
constexpr double kMinGradient = -15.0;
|
||||
constexpr double kMaxGradient = 15.0;
|
||||
constexpr double kMinHessian = 1e-16; // Ensure that no data point gets zero hessian
|
||||
constexpr double kMaxHessian = 15.0;
|
||||
|
||||
constexpr double kEps = 1e-12; // A denomitor in a fraction should not be too small
|
||||
|
||||
// Clip (limit) x to fit range [x_min, x_max].
|
||||
// If x < x_min, return x_min; if x > x_max, return x_max; if x_min <= x <= x_max, return x.
|
||||
// This function assumes x_min < x_max; behavior is undefined if this assumption does not hold.
|
||||
XGBOOST_DEVICE
|
||||
inline double Clip(double x, double x_min, double x_max) {
|
||||
if (x < x_min) {
|
||||
return x_min;
|
||||
}
|
||||
if (x > x_max) {
|
||||
return x_max;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
template<typename Distribution>
|
||||
XGBOOST_DEVICE inline double
|
||||
GetLimitGradAtInfPred(CensoringType censor_type, bool sign, double sigma);
|
||||
|
||||
template<typename Distribution>
|
||||
XGBOOST_DEVICE inline double
|
||||
GetLimitHessAtInfPred(CensoringType censor_type, bool sign, double sigma);
|
||||
|
||||
} // namespace aft
|
||||
|
||||
/*! \brief Parameter structure for AFT loss and metric */
|
||||
struct AFTParam : public XGBoostParameter<AFTParam> {
|
||||
/*! \brief Choice of probability distribution for the noise term in AFT */
|
||||
@@ -39,47 +92,245 @@ struct AFTParam : public XGBoostParameter<AFTParam> {
|
||||
};
|
||||
|
||||
/*! \brief The AFT loss function */
|
||||
class AFTLoss {
|
||||
private:
|
||||
std::unique_ptr<ProbabilityDistribution> dist_;
|
||||
ProbabilityDistributionType dist_type_;
|
||||
template<typename Distribution>
|
||||
struct AFTLoss {
|
||||
XGBOOST_DEVICE inline static
|
||||
double Loss(double y_lower, double y_upper, double y_pred, double sigma) {
|
||||
const double log_y_lower = log(y_lower);
|
||||
const double log_y_upper = log(y_upper);
|
||||
|
||||
public:
|
||||
/*!
|
||||
* \brief Constructor for AFT loss function
|
||||
* \param dist_type Choice of probability distribution for the noise term in AFT
|
||||
*/
|
||||
explicit AFTLoss(ProbabilityDistributionType dist_type)
|
||||
: dist_(ProbabilityDistribution::Create(dist_type)),
|
||||
dist_type_(dist_type) {}
|
||||
double cost;
|
||||
|
||||
public:
|
||||
/*!
|
||||
* \brief Compute the AFT loss
|
||||
* \param y_lower Lower bound for the true label
|
||||
* \param y_upper Upper bound for the true label
|
||||
* \param y_pred Predicted label
|
||||
* \param sigma Scaling factor to be applied to the distribution of the noise term
|
||||
*/
|
||||
double Loss(double y_lower, double y_upper, double y_pred, double sigma);
|
||||
/*!
|
||||
* \brief Compute the gradient of the AFT loss
|
||||
* \param y_lower Lower bound for the true label
|
||||
* \param y_upper Upper bound for the true label
|
||||
* \param y_pred Predicted label
|
||||
* \param sigma Scaling factor to be applied to the distribution of the noise term
|
||||
*/
|
||||
double Gradient(double y_lower, double y_upper, double y_pred, double sigma);
|
||||
/*!
|
||||
* \brief Compute the hessian of the AFT loss
|
||||
* \param y_lower Lower bound for the true label
|
||||
* \param y_upper Upper bound for the true label
|
||||
* \param y_pred Predicted label
|
||||
* \param sigma Scaling factor to be applied to the distribution of the noise term
|
||||
*/
|
||||
double Hessian(double y_lower, double y_upper, double y_pred, double sigma);
|
||||
if (y_lower == y_upper) { // uncensored
|
||||
const double z = (log_y_lower - y_pred) / sigma;
|
||||
const double pdf = Distribution::PDF(z);
|
||||
// Regularize the denominator with eps, to avoid INF or NAN
|
||||
cost = -log(fmax(pdf / (sigma * y_lower), aft::kEps));
|
||||
} else { // censored; now check what type of censorship we have
|
||||
double z_u, z_l, cdf_u, cdf_l;
|
||||
if (isinf(y_upper)) { // right-censored
|
||||
cdf_u = 1;
|
||||
} else { // left-censored or interval-censored
|
||||
z_u = (log_y_upper - y_pred) / sigma;
|
||||
cdf_u = Distribution::CDF(z_u);
|
||||
}
|
||||
if (y_lower <= 0.0) { // left-censored
|
||||
cdf_l = 0;
|
||||
} else { // right-censored or interval-censored
|
||||
z_l = (log_y_lower - y_pred) / sigma;
|
||||
cdf_l = Distribution::CDF(z_l);
|
||||
}
|
||||
// Regularize the denominator with eps, to avoid INF or NAN
|
||||
cost = -log(fmax(cdf_u - cdf_l, aft::kEps));
|
||||
}
|
||||
|
||||
return cost;
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE inline static
|
||||
double Gradient(double y_lower, double y_upper, double y_pred, double sigma) {
|
||||
const double log_y_lower = log(y_lower);
|
||||
const double log_y_upper = log(y_upper);
|
||||
double numerator, denominator, gradient; // numerator and denominator of gradient
|
||||
CensoringType censor_type;
|
||||
bool z_sign; // sign of z-score
|
||||
|
||||
if (y_lower == y_upper) { // uncensored
|
||||
const double z = (log_y_lower - y_pred) / sigma;
|
||||
const double pdf = Distribution::PDF(z);
|
||||
const double grad_pdf = Distribution::GradPDF(z);
|
||||
censor_type = CensoringType::kUncensored;
|
||||
numerator = grad_pdf;
|
||||
denominator = sigma * pdf;
|
||||
z_sign = (z > 0);
|
||||
} else { // censored; now check what type of censorship we have
|
||||
double z_u = 0.0, z_l = 0.0, pdf_u, pdf_l, cdf_u, cdf_l;
|
||||
censor_type = CensoringType::kIntervalCensored;
|
||||
if (isinf(y_upper)) { // right-censored
|
||||
pdf_u = 0;
|
||||
cdf_u = 1;
|
||||
censor_type = CensoringType::kRightCensored;
|
||||
} else { // interval-censored or left-censored
|
||||
z_u = (log_y_upper - y_pred) / sigma;
|
||||
pdf_u = Distribution::PDF(z_u);
|
||||
cdf_u = Distribution::CDF(z_u);
|
||||
}
|
||||
if (y_lower <= 0.0) { // left-censored
|
||||
pdf_l = 0;
|
||||
cdf_l = 0;
|
||||
censor_type = CensoringType::kLeftCensored;
|
||||
} else { // interval-censored or right-censored
|
||||
z_l = (log_y_lower - y_pred) / sigma;
|
||||
pdf_l = Distribution::PDF(z_l);
|
||||
cdf_l = Distribution::CDF(z_l);
|
||||
}
|
||||
z_sign = (z_u > 0 || z_l > 0);
|
||||
numerator = pdf_u - pdf_l;
|
||||
denominator = sigma * (cdf_u - cdf_l);
|
||||
}
|
||||
gradient = numerator / denominator;
|
||||
if (denominator < aft::kEps && (isnan(gradient) || isinf(gradient))) {
|
||||
gradient = aft::GetLimitGradAtInfPred<Distribution>(censor_type, z_sign, sigma);
|
||||
}
|
||||
|
||||
return aft::Clip(gradient, aft::kMinGradient, aft::kMaxGradient);
|
||||
}
|
||||
|
||||
XGBOOST_DEVICE inline static
|
||||
double Hessian(double y_lower, double y_upper, double y_pred, double sigma) {
|
||||
const double log_y_lower = log(y_lower);
|
||||
const double log_y_upper = log(y_upper);
|
||||
double numerator, denominator, hessian; // numerator and denominator of hessian
|
||||
CensoringType censor_type;
|
||||
bool z_sign; // sign of z-score
|
||||
|
||||
if (y_lower == y_upper) { // uncensored
|
||||
const double z = (log_y_lower - y_pred) / sigma;
|
||||
const double pdf = Distribution::PDF(z);
|
||||
const double grad_pdf = Distribution::GradPDF(z);
|
||||
const double hess_pdf = Distribution::HessPDF(z);
|
||||
censor_type = CensoringType::kUncensored;
|
||||
numerator = -(pdf * hess_pdf - grad_pdf * grad_pdf);
|
||||
denominator = sigma * sigma * pdf * pdf;
|
||||
z_sign = (z > 0);
|
||||
} else { // censored; now check what type of censorship we have
|
||||
double z_u = 0.0, z_l = 0.0, grad_pdf_u, grad_pdf_l, pdf_u, pdf_l, cdf_u, cdf_l;
|
||||
censor_type = CensoringType::kIntervalCensored;
|
||||
if (isinf(y_upper)) { // right-censored
|
||||
pdf_u = 0;
|
||||
cdf_u = 1;
|
||||
grad_pdf_u = 0;
|
||||
censor_type = CensoringType::kRightCensored;
|
||||
} else { // interval-censored or left-censored
|
||||
z_u = (log_y_upper - y_pred) / sigma;
|
||||
pdf_u = Distribution::PDF(z_u);
|
||||
cdf_u = Distribution::CDF(z_u);
|
||||
grad_pdf_u = Distribution::GradPDF(z_u);
|
||||
}
|
||||
if (y_lower <= 0.0) { // left-censored
|
||||
pdf_l = 0;
|
||||
cdf_l = 0;
|
||||
grad_pdf_l = 0;
|
||||
censor_type = CensoringType::kLeftCensored;
|
||||
} else { // interval-censored or right-censored
|
||||
z_l = (log_y_lower - y_pred) / sigma;
|
||||
pdf_l = Distribution::PDF(z_l);
|
||||
cdf_l = Distribution::CDF(z_l);
|
||||
grad_pdf_l = Distribution::GradPDF(z_l);
|
||||
}
|
||||
const double cdf_diff = cdf_u - cdf_l;
|
||||
const double pdf_diff = pdf_u - pdf_l;
|
||||
const double grad_diff = grad_pdf_u - grad_pdf_l;
|
||||
const double sqrt_denominator = sigma * cdf_diff;
|
||||
z_sign = (z_u > 0 || z_l > 0);
|
||||
numerator = -(cdf_diff * grad_diff - pdf_diff * pdf_diff);
|
||||
denominator = sqrt_denominator * sqrt_denominator;
|
||||
}
|
||||
hessian = numerator / denominator;
|
||||
if (denominator < aft::kEps && (isnan(hessian) || isinf(hessian))) {
|
||||
hessian = aft::GetLimitHessAtInfPred<Distribution>(censor_type, z_sign, sigma);
|
||||
}
|
||||
|
||||
return aft::Clip(hessian, aft::kMinHessian, aft::kMaxHessian);
|
||||
}
|
||||
};
|
||||
|
||||
namespace aft {
|
||||
|
||||
template <>
|
||||
XGBOOST_DEVICE inline double
|
||||
GetLimitGradAtInfPred<NormalDistribution>(CensoringType censor_type, bool sign, double sigma) {
|
||||
switch (censor_type) {
|
||||
case CensoringType::kUncensored:
|
||||
return sign ? kMinGradient : kMaxGradient;
|
||||
case CensoringType::kRightCensored:
|
||||
return sign ? kMinGradient : 0.0;
|
||||
case CensoringType::kLeftCensored:
|
||||
return sign ? 0.0 : kMaxGradient;
|
||||
case CensoringType::kIntervalCensored:
|
||||
return sign ? kMinGradient : kMaxGradient;
|
||||
}
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
|
||||
template <>
|
||||
XGBOOST_DEVICE inline double
|
||||
GetLimitHessAtInfPred<NormalDistribution>(CensoringType censor_type, bool sign, double sigma) {
|
||||
switch (censor_type) {
|
||||
case CensoringType::kUncensored:
|
||||
return 1.0 / (sigma * sigma);
|
||||
case CensoringType::kRightCensored:
|
||||
return sign ? (1.0 / (sigma * sigma)) : kMinHessian;
|
||||
case CensoringType::kLeftCensored:
|
||||
return sign ? kMinHessian : (1.0 / (sigma * sigma));
|
||||
case CensoringType::kIntervalCensored:
|
||||
return 1.0 / (sigma * sigma);
|
||||
}
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
|
||||
template <>
|
||||
XGBOOST_DEVICE inline double
|
||||
GetLimitGradAtInfPred<LogisticDistribution>(CensoringType censor_type, bool sign, double sigma) {
|
||||
switch (censor_type) {
|
||||
case CensoringType::kUncensored:
|
||||
return sign ? (-1.0 / sigma) : (1.0 / sigma);
|
||||
case CensoringType::kRightCensored:
|
||||
return sign ? (-1.0 / sigma) : 0.0;
|
||||
case CensoringType::kLeftCensored:
|
||||
return sign ? 0.0 : (1.0 / sigma);
|
||||
case CensoringType::kIntervalCensored:
|
||||
return sign ? (-1.0 / sigma) : (1.0 / sigma);
|
||||
}
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
|
||||
template <>
|
||||
XGBOOST_DEVICE inline double
|
||||
GetLimitHessAtInfPred<LogisticDistribution>(CensoringType censor_type, bool sign, double sigma) {
|
||||
switch (censor_type) {
|
||||
case CensoringType::kUncensored:
|
||||
case CensoringType::kRightCensored:
|
||||
case CensoringType::kLeftCensored:
|
||||
case CensoringType::kIntervalCensored:
|
||||
return kMinHessian;
|
||||
}
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
|
||||
template <>
|
||||
XGBOOST_DEVICE inline double
|
||||
GetLimitGradAtInfPred<ExtremeDistribution>(CensoringType censor_type, bool sign, double sigma) {
|
||||
switch (censor_type) {
|
||||
case CensoringType::kUncensored:
|
||||
return sign ? kMinGradient : (1.0 / sigma);
|
||||
case CensoringType::kRightCensored:
|
||||
return sign ? kMinGradient : 0.0;
|
||||
case CensoringType::kLeftCensored:
|
||||
return sign ? 0.0 : (1.0 / sigma);
|
||||
case CensoringType::kIntervalCensored:
|
||||
return sign ? kMinGradient : (1.0 / sigma);
|
||||
}
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
|
||||
template <>
|
||||
XGBOOST_DEVICE inline double
|
||||
GetLimitHessAtInfPred<ExtremeDistribution>(CensoringType censor_type, bool sign, double sigma) {
|
||||
switch (censor_type) {
|
||||
case CensoringType::kUncensored:
|
||||
case CensoringType::kRightCensored:
|
||||
return sign ? kMaxHessian : kMinHessian;
|
||||
case CensoringType::kLeftCensored:
|
||||
return kMinHessian;
|
||||
case CensoringType::kIntervalCensored:
|
||||
return sign ? kMaxHessian : kMinHessian;
|
||||
}
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
|
||||
} // namespace aft
|
||||
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
|
||||
|
||||
Reference in New Issue
Block a user