use super::link::Link;
pub(crate) trait Objective {
type Link: Link;
const CONSTANT_HESSIAN: bool;
const APPROX_HESSIAN: bool;
const IS_MULTICLASS: bool;
const CURVATURE_FLOOR: f64;
fn value(raw: f64, target: f64) -> f64;
fn gradient(raw: f64, target: f64) -> f64;
fn hessian(raw: f64, target: f64) -> f64;
fn negative_gradient(raw: f64, target: f64) -> f64;
fn gradient_and_curvature(raw: f64, target: f64) -> (f64, f64) {
(
Self::gradient(raw, target),
Self::hessian(raw, target).max(Self::CURVATURE_FLOOR),
)
}
}
#[cfg(test)]
pub(crate) mod proof {
use super::Objective;
pub(crate) fn finite_differences_agree<O: Objective>(
raws: &[f64],
targets: &[f64],
gradient_tolerance: f64,
hessian_tolerance: f64,
) {
for &target in targets {
for &raw in raws {
let first_step = 1.0e-6_f64.mul_add(raw.abs(), 1.0e-6);
let numeric_gradient = (O::value(raw + first_step, target)
- O::value(raw - first_step, target))
/ (2.0 * first_step);
let gradient = O::gradient(raw, target);
assert!(
(numeric_gradient - gradient).abs() <= gradient_tolerance,
"gradient at raw={raw} target={target}: {gradient} vs finite difference \
{numeric_gradient}"
);
let second_step = 1.0e-3_f64.mul_add(raw.abs(), 1.0e-3);
let numeric_hessian = (O::value(raw + second_step, target)
- 2.0 * O::value(raw, target)
+ O::value(raw - second_step, target))
/ (second_step * second_step);
let hessian = O::hessian(raw, target);
assert!(
(numeric_hessian - hessian).abs() <= hessian_tolerance,
"hessian at raw={raw} target={target}: {hessian} vs finite difference \
{numeric_hessian}"
);
}
}
}
pub(crate) fn declared_properties_are_coherent<O: Objective>(raws: &[f64], targets: &[f64]) {
assert_eq!(
O::APPROX_HESSIAN,
O::CURVATURE_FLOOR > 0.0,
"an approximate hessian is declared exactly when the curvature is floored"
);
assert!(!O::IS_MULTICLASS, "no multiclass objective exists yet");
let mut constant_hessian = None;
for &target in targets {
for &raw in raws {
let gradient = O::gradient(raw, target);
let hessian = O::hessian(raw, target);
let negative = O::negative_gradient(raw, target);
assert_eq!(
negative, -gradient,
"negative gradient at raw={raw} target={target}"
);
if negative == 0.0 {
assert!(
negative.is_sign_positive(),
"an exactly zero negative gradient stays positively signed"
);
}
if O::CONSTANT_HESSIAN {
let first = *constant_hessian.get_or_insert(hessian);
assert_eq!(
hessian.to_bits(),
first.to_bits(),
"a constant hessian must not vary at raw={raw} target={target}"
);
}
let (paired_gradient, curvature) = O::gradient_and_curvature(raw, target);
assert_eq!(
paired_gradient.to_bits(),
gradient.to_bits(),
"paired gradient at raw={raw} target={target}"
);
assert_eq!(
curvature.to_bits(),
hessian.max(O::CURVATURE_FLOOR).to_bits(),
"paired curvature at raw={raw} target={target}"
);
}
}
}
}