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);