Commons Math
  1. Commons Math
  2. MATH-1021

HypergeometricDistribution.sample suffers from integer overflow

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.3
    • Labels:
      None

      Description

      Hi, I have an application which broke when ported from commons math 2.2 to 3.2. It looks like the HypergeometricDistribution.sample() method doesn't work as well as it used to with large integer values – the example code below should return a sample between 0 and 50, but usually returns -50.

      import org.apache.commons.math3.distribution.HypergeometricDistribution;
      
      public class Foo {
        public static void main(String[] args) {
          HypergeometricDistribution a = new HypergeometricDistribution(
              43130568, 42976365, 50);
          System.out.printf("%d %d%n", a.getSupportLowerBound(), a.getSupportUpperBound()); // Prints "0 50"
          System.out.printf("%d%n",a.sample());                                             // Prints "-50"
        }
      }
      

      In the debugger, I traced it as far as an integer overflow in HypergeometricDistribution.getNumericalMean() – instead of doing

      return (double) (getSampleSize() * getNumberOfSuccesses()) / (double) getPopulationSize();
      

      it could do:

      return getSampleSize() * ((double) getNumberOfSuccesses() / (double) getPopulationSize());
      

      This seemed to fix it, based on a quick test.

        Activity

        Brian Bloniarz created issue -
        Thomas Neidhart made changes -
        Field Original Value New Value
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 3.3 [ 12324600 ]
        Resolution Fixed [ 1 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Brian Bloniarz
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development