Parijat Biswas created MATH-1696:
------------------------------------

             Summary: EnumeratedRealDistribution computes incorrect quantiles 
for unsorted input
                 Key: MATH-1696
                 URL: https://issues.apache.org/jira/browse/MATH-1696
             Project: Commons Math
          Issue Type: Bug
          Components: legacy
    Affects Versions: 3.3
            Reporter: Parijat Biswas


h3. Problem

EnumeratedRealDistribution allows unsorted singletons and duplicate values, but 
inverseCumulativeProbability accumulates their probabilities in input order 
rather than numerical order. This returns incorrect quantiles, including 
incorrect support endpoints.

{code:java}
EnumeratedRealDistribution d = new EnumeratedRealDistribution(
    new double[] {7, 3, -1, 3},
    new double[] {0.25, 0.25, 0.25, 0.25});
System.out.println(d.inverseCumulativeProbability(0)); // actual 7, expected -1
System.out.println(d.inverseCumulativeProbability(0.5)); // actual 3, expected 3
System.out.println(d.inverseCumulativeProbability(1)); // actual 3, expected 7
{code}

The data-only constructor is affected too: new EnumeratedRealDistribution(new 
double[] {7, 3, -1, 3}). The cumulative distribution itself correctly combines 
duplicate masses regardless of their input order.

Reproduced on current master 36cefd500d2b6a193bd81654665944db1bb1aaed and the 
published Commons Math 3.3 jar. This is distinct from MATH-1065, which fixed 
returning points outside the sample set, and MATH-1533, which concerns seeded 
sampling order.

h3. Proposed fix

Sort the copied PMF returned by getPmf() only inside 
inverseCumulativeProbability. This leaves the stored input order and seeded 
sampling unchanged. Add three regression tests covering both constructors, 
duplicate masses, zero-probability outliers, p=0 and p=1, and unchanged seeded 
sampling after a quantile query.

h3. Validation

* Both unsorted-input regression tests fail on unchanged production code; the 
fixed class has 15 passing tests.
* Complete split local test validation on Linux, JDK 17: 3,826 test cases, zero 
final failures/errors, 33 existing skips. The repository's configured retry 
policy was unchanged; 48 cases were reported as flakes and passed on configured 
retries.
* Maven verification phases completed by module, including zero Checkstyle 
violations. Existing warning-only PMD and coverage diagnostics remain; no claim 
of 100% coverage.
* Javadoc generation passes with UTF-8 locale.
* The initial uninterrupted clean verify command exceeded the execution time 
limit. The default legacy test execution completed, then the separate 
configured flaky-test execution was rerun to completion. This is split 
validation, not a claim that one uninterrupted default build completed. No 
test, assertion or retry setting was changed.

AI disclosure: This report and the proposed fix/tests were generated with AI 
assistance (Instinct) and checked with the validation above.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to