[
https://issues.apache.org/jira/browse/STATISTICS-92?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18066038#comment-18066038
]
Alex Herbert commented on STATISTICS-92:
----------------------------------------
Thanks for the bug report. Sorry for the error. Can you add these two files to
the PR:
{noformat}
commons-statistics-distribution/src/test/resources/org/apache/commons/statistics/distribution/
{noformat}
[^test.truncatednormal.10.properties]
[^test.truncatednormal.11.properties]
The test case for the fully positive side matches your example. I shifted the
bounds for the fully negative side to test a non-symmetric truncation. These
cases fail in the sampling test for the distribution as all samples are either
above or below the mean as you report.
> TruncatedNormalDistribution rejection-sampler uses incorrect bounds for
> mirroring check
> ---------------------------------------------------------------------------------------
>
> Key: STATISTICS-92
> URL: https://issues.apache.org/jira/browse/STATISTICS-92
> Project: Commons Statistics
> Issue Type: Bug
> Components: distribution
> Affects Versions: 1.0
> Reporter: Kevin Milner
> Priority: Major
> Labels: pull-request-available
> Attachments: test.truncatednormal.10.properties,
> test.truncatednormal.11.properties
>
>
> The rejection sampler implementation of TruncatedNormalDistribution
> incorrectly uses the original upper and lower bounds rather than the standard
> normal bounds for the mirroring check; this leads to inaccurate sampling if
> the original bounds are fully-positive or fully-negative, as well as infinite
> loops if the bounds are positive but fully below the mean value or are
> negative but fully above the mean value.
> For example, this will only return samples >1 (even though they should be in
> the range [0.7, 1.3]:
> {code:java}
> ContinuousDistribution dist = TruncatedNormalDistribution.of(1, 0.1, 0.7,
> 1.3).createSampler(RandomSource.XO_RO_SHI_RO_128_PP.create(123456l));{code}
> ...and this will get stuck in an infinite loop on the first sampling attempt:
> {code:java}
> ContinuousDistribution dist = TruncatedNormalDistribution.of(1, 0.1, 0.7,
> 0.99).createSampler(RandomSource.XO_RO_SHI_RO_128_PP.create(123456l));{code}
> Similarly, this will only return samples <1:
> {code:java}
> ContinuousDistribution dist = TruncatedNormalDistribution.of(-1, 0.1, -1.3,
> -0.7).createSampler(RandomSource.XO_RO_SHI_RO_128_PP.create(123456l));{code}
> ...and this will get stuck in an infinite loop on the first sampling attempt:
> {code:java}
> ContinuousDistribution dist = TruncatedNormalDistribution.of(-1, 0.1, -0.99,
> -0.7).createSampler(RandomSource.XO_RO_SHI_RO_128_PP.create(123456l));{code}
> The bug seems to have been present since v1.0. It's a simple fix and I will
> submit a PR imminently.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)