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

Statistics.setVarianceImpl makes getStandardDeviation produce NaN

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.2
    • Fix Version/s: 3.0
    • Labels:
      None
    • Environment:

      Windows 7 64-bit, java version 1.6.0_23

      Description

      Invoking SummaryStatistics.setVarianceImpl(new Variance(true/false) makes getStandardDeviation produce NaN. The code to reproduce it:

      int[] scores = {1, 2, 3, 4};
      SummaryStatistics stats = new SummaryStatistics();
      stats.setVarianceImpl(new Variance(false)); //use "population variance"
      for(int i : scores) {
        stats.addValue(i);
      }
      double sd = stats.getStandardDeviation();
      System.out.println(sd);
      

      A workaround suggested by Mikkel is:

        double sd = FastMath.sqrt(stats.getSecondMoment() / stats.getN());
      

        Attachments

          Activity

            People

            • Assignee:
              psteitz Phil Steitz
              Reporter:
              warrentang Warren Tang
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 5h
                5h
                Remaining:
                Remaining Estimate - 5h
                5h
                Logged:
                Time Spent - Not Specified
                Not Specified