Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-1021

HypergeometricDistribution.sample suffers from integer overflow

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.3
    • None
    • 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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            brianbloniarz Brian Bloniarz
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: