kevinmilner opened a new pull request, #125: URL: https://github.com/apache/commons-statistics/pull/125
Fix for [STATISTICS-92](https://issues.apache.org/jira/browse/STATISTICS-92). This corrects the rejection sampler mirroring test in `TruncatedNormalDistribution` to use the standard normal bounds rather than the original bounds. As is, when `mean > 0`, `lower > 0`, `lower < mean`, and `upper > mean`, samples are only returned that are `> mean`. For example, here is the distribution for: `ContinuousDistribution dist = TruncatedNormalDistribution.of(1, 0.1, 0.7, 1.3).createSampler(RandomSource.XO_RO_SHI_RO_128_PP.create(123456l));` <img width="700" height="450" alt="trunc_dist_1 0_0 1_0 7_1 3" src="https://github.com/user-attachments/assets/faccdcd3-c48e-4cea-b9c8-419c57bbbc2a" /> The black histogram is the current implementation and the orange histogram is with this fix. And when `mean > 0`, `lower > 0`, `lower < mean`, and `upper < mean`, the sampler gets stuck in an infinite loop. The same is true on the negative side, for example: `ContinuousDistribution dist = TruncatedNormalDistribution.of(-1, 0.1, -1.3, -0.7).createSampler(RandomSource.XO_RO_SHI_RO_128_PP.create(123456l));` <img width="700" height="450" alt="trunc_dist_-1 0_0 1_-1 3_-0 7" src="https://github.com/user-attachments/assets/8ad6728a-30f6-47f1-accd-a76fc8cee692" /> The fix is simple. I also added tests that detect the infinite loop cases with a timeout of 1 second; they fail with the current implementation, and pass with the fix. I could add tests for the other cases that don't trigger the infinite loop, for example, to ensure that values on either side of the mean are correctly sampled, but I'm not sure your policy on tests that rely on randomness (even with fixed seeds). The tests that I added demonstrate the issue and cannot suceed with the current implementation. Let me know if you would like any changes! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
