From dbfa4dad41b0ba47bbe92955314f292bb0cb1a0e Mon Sep 17 00:00:00 2001 From: "Tianlang (Jackie) Chen" <305351710+jackiectl2@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:51:57 -0500 Subject: [PATCH] fix(math): preserve fractional Gamma samples Gamma::sample currently casts each draw to usize before converting it back to f64, truncating a continuous distribution and producing zeros. Keep the original floating-point draw and add a regression for positive finite, non-integral samples. Add the required changelog entry. Signed-off-by: Tianlang (Jackie) Chen <305351710+jackiectl2@users.noreply.github.com> --- CHANGELOG.md | 3 +++ crates/RustQuant_math/src/distributions/gamma.rs | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dd81058..2bbf1221 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- Preserve continuous Gamma samples instead of truncating them to integers. (@jackiectl2) + ## [0.2.9](https://github.com/avhz/RustQuant/compare/v0.2.8...v0.2.9) - 2024-10-27 ### Added diff --git a/crates/RustQuant_math/src/distributions/gamma.rs b/crates/RustQuant_math/src/distributions/gamma.rs index cad82623..a82284cb 100644 --- a/crates/RustQuant_math/src/distributions/gamma.rs +++ b/crates/RustQuant_math/src/distributions/gamma.rs @@ -204,7 +204,7 @@ impl Distribution for Gamma { let mut variates: Vec = Vec::with_capacity(n); for _ in 0..variates.capacity() { - variates.push(dist.sample(&mut rng) as usize as f64); + variates.push(dist.sample(&mut rng)); } Ok(variates) @@ -220,6 +220,15 @@ mod tests { use super::*; use RustQuant_utils::{assert_approx_equal, RUSTQUANT_EPSILON as EPS}; + #[test] + fn test_gamma_samples_retain_fractional_values() { + let samples = Gamma::new(2.0, 3.0).sample(64).unwrap(); + + assert_eq!(samples.len(), 64); + assert!(samples.iter().all(|x| x.is_finite() && *x > 0.0)); + assert!(samples.iter().any(|x| x.fract() != 0.0)); + } + #[test] fn test_gamma_characteristic_function() { let dist: Gamma = Gamma::new(1.0, 1.0);