pub fn ncr(expected_size: f64, a: f64, b: f64, x_target: f64) -> f64 {
debug_assert!(b > 0.0, "basis scale must be positive (§2.5.1 K)");
debug_assert!(
x_target > 0.0 && x_target < 1.0,
"x_target must lie in (0,1) (§2.5.1 K)"
);
if b <= 0.0 {
return 0.0;
}
let cr = (expected_size - a) / b;
if cr <= 0.0 {
return 0.0;
}
let lambda = -(1.0 - x_target).ln();
1.0 - (-lambda * cr).exp()
}
pub fn reward(expected_size: f64, a: f64, b: f64, x_target: f64) -> f64 {
1.0 - ncr(expected_size, a, b, x_target)
}
#[cfg(test)]
mod tests {
use super::*;
const X: f64 = 0.8;
#[test]
fn landmark_values() {
let (a, b) = (5.0, 10.0);
assert_eq!(ncr(a, a, b, X), 0.0);
assert!((ncr(a + b, a, b, X) - X).abs() < 1e-12);
let past = ncr(a + b + 10.0, a, b, X);
assert!(past <= 1.0);
assert!(past > X);
}
#[test]
fn equal_representative_sizes_keep_strict_ordering() {
let (a, b) = (5.0, 5.0);
let n6 = ncr(6.0, a, b, X);
let n8 = ncr(8.0, a, b, X);
let n10 = ncr(10.0, a, b, X);
assert!(0.0 < n6 && n6 < n8 && n8 < n10 && n10 < 1.0);
assert!((n10 - X).abs() < 1e-12);
}
}