1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
//! This module implements some math functions used for gradient boosting process.
#[cfg(all(feature = "mesalock_sgx", not(target_env = "sgx")))]
use std::prelude::v1::*;
use crate::decision_tree::{DataVec, PredVec, ValueType};
/// Comparing two number with a costomized floating error threshold.
///
/// # Example
/// ```rust
/// use gbdt::fitness::almost_equal_thrs;
/// assert_eq!(true, almost_equal_thrs(1.0, 0.998, 0.01));
/// ```
#[inline(always)]
pub fn almost_equal_thrs(a: ValueType, b: ValueType, thrs: f64) -> bool {
f64::from((a - b).abs()) < thrs
}
/// Comparing two number with default floating error threshold.
///
/// # Example
/// ```rust
/// use gbdt::fitness::almost_equal;
/// assert_eq!(false, almost_equal(1.0, 0.998));
/// assert_eq!(true, almost_equal(1.0, 0.999998));
/// ```
pub fn almost_equal(a: ValueType, b: ValueType) -> bool {
f64::from((a - b).abs()) < 1.0e-5
}
/// Return whether the first n data in data vector have same target values.
///
/// # Panic
/// If the specified length is greater than the length of data vector, it will panic.
pub fn same(dv: &DataVec, len: usize) -> bool {
assert!(dv.len() >= len);
if len < 1 {
return false;
}
let t: ValueType = dv[0].target;
for i in dv.iter().skip(1) {
if !(almost_equal(t, i.target)) {
return false;
}
}
true
}
/// Logistic value function.
pub fn logit(f: ValueType) -> ValueType {
1.0 / (1.0 + (-2.0 * f).exp())
}
/// Negative binomial log-likelyhood loss function.
pub fn logit_loss(y: ValueType, f: ValueType) -> ValueType {
2.0 * (1.0 + (-2.0 * y * f)).ln()
}
/// Log-likelyhood gradient calculation.
pub fn logit_loss_gradient(y: ValueType, f: ValueType) -> ValueType {
2.0 * y / (1.0 + (2.0 * y * f).exp())
}
/// LAD loss function.
pub fn lad_loss(y: ValueType, f: ValueType) -> ValueType {
(y - f).abs()
}
/// LAD gradient calculation.
pub fn lad_loss_gradient(y: ValueType, f: ValueType) -> ValueType {
if y - f > 0.0 {
1.0
} else {
-1.0
}
}
/// RMSE (Root-Mean-Square deviation) calculation for first n element in data vector.
/// See [wikipedia](https://en.wikipedia.org/wiki/Root-mean-square_deviation) for detailed algorithm.
///
/// # Panic
/// If the specified length is greater than the length of data vector, it will panic.
///
/// If the length of data vector and predicted vector is not same, it will panic.
#[allow(non_snake_case)]
pub fn RMSE(dv: &DataVec, predict: &PredVec, len: usize) -> ValueType {
assert_eq!(dv.len(), predict.len());
assert!(dv.len() >= len);
let mut s: f64 = 0.0;
let mut c: f64 = 0.0;
for i in 0..dv.len() {
s += (f64::from(predict[i]) - f64::from(dv[i].label)).powf(2.0) * f64::from(dv[i].weight);
c += f64::from(dv[i].weight);
}
if c.abs() < 1e-10 {
0.0
} else {
(s / c) as ValueType
}
}
/// MAE (Mean Absolute Error) calculation for first n element in data vector.
/// See [wikipedia](https://en.wikipedia.org/wiki/Mean_absolute_error) for detail for detailed algorithm.
///
/// # Panic
/// If the specified length is greater than the length of data vector, it will panic.
///
/// If the length of data vector and predicted vector is not same, it will panic.
#[allow(non_snake_case)]
pub fn MAE(dv: &DataVec, predict: &PredVec, len: usize) -> ValueType {
assert_eq!(dv.len(), predict.len());
assert!(dv.len() >= len);
let mut s: ValueType = 0.0;
let mut c: ValueType = 0.0;
for i in 0..dv.len() {
s += (predict[i] - dv[i].label).abs() * dv[i].weight;
c += dv[i].weight;
}
s / c
}
struct AucPred {
score: ValueType,
label: ValueType,
}
/// AUC (Area Under the Curve) calculation for first n element in data vector.
/// See [wikipedia](https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve) for detailed algorithm.
///
/// # Panic
/// If the specified length is greater than the length of data vector, it will panic.
///
/// If the length of data vector and predicted vector is not same, it will panic.
///
/// If the data vector contains only one class or more than two classes, it will panic.
#[allow(non_snake_case)]
pub fn AUC(dv: &DataVec, predict: &PredVec, len: usize) -> ValueType {
assert_eq!(dv.len(), predict.len());
assert!(dv.len() >= len);
let mut classes: Vec<ValueType> = Vec::new();
for i in dv {
if !classes.contains(&i.label) {
classes.push(i.label);
}
}
assert!(classes.len() == 2);
let mut preds: Vec<AucPred> = Vec::new();
for i in 0..predict.len() {
preds.push(AucPred {
score: predict[i],
label: dv[i].label,
});
}
preds.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
let mut tp: ValueType = 0.0;
let mut fp: ValueType = 0.0;
let (mut tps, mut fps) = (vec![], vec![]);
for x in preds.iter() {
tps.push(tp);
fps.push(fp);
if almost_equal(x.label, 1.0) {
tp += 1.0;
} else {
fp += 1.0;
}
}
tps.push(tp);
fps.push(fp);
let true_positives = tps[tps.len() - 1];
let false_positives = fps[fps.len() - 1];
// println!("tps={}, fps={}", true_positives, false_positives);
for (tp, fp) in tps.iter_mut().zip(fps.iter_mut()) {
*tp /= true_positives;
*fp /= false_positives;
// println!("fp={}, tp={}", fp, tp);
}
let mut prev_y: ValueType = *tps.first().unwrap();
let mut prev_x: ValueType = *fps.first().unwrap();
let mut auc: ValueType = 0.0;
for (&x, &y) in fps.iter().skip(1).zip(tps.iter().skip(1)) {
auc += (x - prev_x) * (prev_y + y) / 2.0;
prev_x = x;
prev_y = y;
}
auc
}
/// Return the weighted target average for first n data in data vector.
///
/// # Example
/// ```rust
/// use gbdt::decision_tree::{DataVec, Data, VALUE_TYPE_UNKNOWN};
/// use gbdt::fitness::{average, almost_equal};
/// let mut dv: DataVec = Vec::new();
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 1.0,
/// weight: 0.1,
/// label: 1.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 1.0,
/// weight: 0.2,
/// label: 0.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 0.0,
/// weight: 0.3,
/// label: 1.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 0.0,
/// weight: 0.4,
/// label: 0.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// assert!(almost_equal(0.3, average(&dv, dv.len())));
/// ```
///
/// # Panic
/// If the specified length is greater than the length of data vector, it will panic.
pub fn average(dv: &DataVec, len: usize) -> ValueType {
assert!(dv.len() >= len);
if len == 0 {
return 0.0;
}
let mut s: ValueType = 0.0;
let mut c: ValueType = 0.0;
for d in dv {
s += d.weight * d.target;
c += d.weight;
}
s / c
}
/// Return the weighted label average for first n data in data vector.
///
/// # Example
/// ```rust
/// use gbdt::decision_tree::{DataVec, Data, VALUE_TYPE_UNKNOWN};
/// use gbdt::fitness::{label_average, almost_equal};
/// let mut dv: DataVec = Vec::new();
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 1.0,
/// weight: 0.1,
/// label: 1.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 1.0,
/// weight: 0.2,
/// label: 0.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 0.0,
/// weight: 0.3,
/// label: 1.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 0.0,
/// weight: 0.4,
/// label: 0.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// assert!(almost_equal(0.4, label_average(&dv, dv.len())));
/// ```
///
/// # Panic
/// If the specified length is greater than the length of data vector, it will panic.
pub fn label_average(dv: &DataVec, len: usize) -> ValueType {
assert!(dv.len() >= len);
let mut s: f64 = 0.0;
let mut c: f64 = 0.0;
for d in dv {
s += f64::from(d.label) * f64::from(d.weight);
c += f64::from(d.weight);
}
if c.abs() < 1e-10 {
0.0
} else {
(s / c) as ValueType
}
}
/// Return the weighted label median for first n data in data vector.
///
/// # Example
/// ```rust
/// use gbdt::decision_tree::{DataVec, Data, VALUE_TYPE_UNKNOWN};
/// use gbdt::fitness::{weighted_label_median, almost_equal};
/// let mut dv: DataVec = Vec::new();
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 1.0,
/// weight: 0.1,
/// label: 1.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 1.0,
/// weight: 0.2,
/// label: 0.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 0.0,
/// weight: 0.3,
/// label: 1.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 0.0,
/// weight: 0.4,
/// label: 0.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// assert!(almost_equal(0.0, weighted_label_median(&dv, dv.len())));
/// ```
///
/// # Panic
/// If the specified length is greater than the length of data vector, it will panic.
pub fn weighted_label_median(dv: &DataVec, len: usize) -> ValueType {
assert!(dv.len() >= len);
let mut dv_copy = dv.to_vec();
dv_copy.sort_by(|a, b| a.label.partial_cmp(&b.label).unwrap());
let mut all_weight: f64 = 0.0;
for d in &dv_copy {
all_weight += f64::from(d.weight);
}
let mut weighted_median: ValueType = 0.0;
let mut weight: f64 = 0.0;
for i in 0..len {
weight += f64::from(dv_copy[i].weight);
if weight * 2.0 > all_weight {
if i - 1 > 0 {
weighted_median = (dv_copy[i].label + dv_copy[i - 1].label) / 2.0;
} else {
weighted_median = dv_copy[i].label;
}
break;
}
}
weighted_median
}
/// Return the weighted residual median for first n data in data vector.
///
/// # Example
/// ```rust
/// use gbdt::decision_tree::{DataVec, Data, VALUE_TYPE_UNKNOWN};
/// use gbdt::fitness::{weighted_residual_median, almost_equal};
/// let mut dv: DataVec = Vec::new();
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 1.0,
/// weight: 0.1,
/// label: 1.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 1.0,
/// weight: 0.2,
/// label: 0.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 0.0,
/// weight: 0.3,
/// label: 1.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// dv.push(Data {
/// feature: Vec::new(),
/// target: 0.0,
/// weight: 0.4,
/// label: 0.0,
/// residual: 0.5,
/// initial_guess: VALUE_TYPE_UNKNOWN,
/// });
/// assert!(almost_equal(0.5, weighted_residual_median(&dv, dv.len())));
/// ```
///
/// # Panic
/// If the specified length is greater than the length of data vector, it will panic.
pub fn weighted_residual_median(dv: &DataVec, len: usize) -> ValueType {
assert!(dv.len() >= len);
let mut dv_copy = dv.to_vec();
dv_copy.sort_by(|a, b| a.residual.partial_cmp(&b.residual).unwrap());
let mut all_weight: ValueType = 0.0;
for d in &dv_copy {
all_weight += d.weight;
}
let mut weighted_median: ValueType = 0.0;
let mut weight: ValueType = 0.0;
for i in 0..len {
weight += dv_copy[i].weight;
if weight * 2.0 > all_weight {
if i - 1 > 0 {
weighted_median = (dv_copy[i].residual + dv_copy[i - 1].residual) / 2.0;
} else {
weighted_median = dv_copy[i].residual;
}
break;
}
}
weighted_median
}