commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Murphy (JIRA)" <j...@apache.org>
Subject [jira] [Created] (MATH-1253) Skewness could get more precision from slightly reordered code.
Date Mon, 03 Aug 2015 22:33:05 GMT
Bill Murphy created MATH-1253:
---------------------------------

             Summary: Skewness could get more precision from slightly reordered code.
                 Key: MATH-1253
                 URL: https://issues.apache.org/jira/browse/MATH-1253
             Project: Commons Math
          Issue Type: Bug
    Affects Versions: 3.5
            Reporter: Bill Murphy
            Priority: Minor


In Skewness.java, approx line 180, there is code like:

{noformat}
            double accum3 = 0.0;
            for (int i = begin; i < begin + length; i++) {
                final double d = values[i] - m;
                accum3 += d * d * d;
            }
            accum3 /= variance * FastMath.sqrt(variance);
{noformat}

If the division was moved into the for loop, accum3 would be less likely to overflow to Infinity
(or -Infinity). This might allow computation to return a result in a case such as:

{noformat}
double[] numArray = { 1.234E11, 1.234E51, 1.234E101, 1.234E151 };

Skewness    skew = new Skewness();
double    sk = skew.evaluate( numArray );
{noformat}

Currently, this returns NaN, but I'd prefer it returned approx 1.154700538379252.

The change I'm proposing would have the code instead read like:
{noformat}
            double accum3 = 0.0;
            double divisor = variance * FastMath.sqrt(variance);

            for (int i = begin; i < begin + length; i++) {
                final double d = values[i] - m;
                accum3 += d * d * d / divisor;
            }
{noformat}

Thanks!



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message