You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Phil Steitz (Commented) (JIRA)" <ji...@apache.org> on 2011/10/16 19:30:11 UTC

[jira] [Commented] (MATH-691) Statistics.setVarianceImpl makes getStandardDeviation produce NaN

    [ https://issues.apache.org/jira/browse/MATH-691?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13128454#comment-13128454 ] 

Phil Steitz commented on MATH-691:
----------------------------------

Thanks for reporting this.  Another workaround is to work with the default impl:
{code}
SummaryStatistics stats = new SummaryStatistics();
Variance variance = (Variance) stats.getVarianceImpl();
variance.setBiasCorrected(false);
{code}
and then just use the stats instance directly. 

The problem in the SummaryStatistics code is in addValue:
{code}
// If mean, variance or geomean have been overridden,
// need to increment these
if (!(meanImpl instanceof Mean)) {
    meanImpl.increment(value);
}
if (!(varianceImpl instanceof Variance)) {
    varianceImpl.increment(value);
}
if (!(geoMeanImpl instanceof GeometricMean)) {
    geoMeanImpl.increment(value);
}
{code}

The default impls get incremented via their embedded moments, so the code above skips incrementing them.  If, however, they have been overridden by instances of the same class (as in this bug report), this causes a problem.

                
> Statistics.setVarianceImpl makes getStandardDeviation produce NaN
> -----------------------------------------------------------------
>
>                 Key: MATH-691
>                 URL: https://issues.apache.org/jira/browse/MATH-691
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.2
>         Environment: Windows 7 64-bit, java version 1.6.0_23
>            Reporter: Warren Tang
>            Priority: Minor
>   Original Estimate: 5h
>  Remaining Estimate: 5h
>
> Invoking SummaryStatistics.setVarianceImpl(new Variance(true/false) makes getStandardDeviation produce NaN. The code to reproduce it:
> {code:title=test.java}
>     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);
> {code}
> A workaround is:
> {code:title=test.java}
>     double sd = FastMath.sqrt(stats.getSecondMoment() / stats.getN());
> {code}
> as suggested by Mikkel.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira