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

Alex Herbert commented on STATISTICS-83:
----------------------------------------

The correct non-finite value can be computed using a sum of the encountered 
non-finite values. This involves an if statement when processing each input 
value to detect if it is non-finite. It also involves a divide per value which 
is a relatively slow operation. The result is that the branch is efficiently 
avoided by branch prediction and it does not significantly impact performance 
on finite input data:

Benchmarked using JMH on JDK 11.0.21, OpenJDK 64-Bit Server VM, 11.0.21+9
||Length||UpdatingMean||UpdatingMean+||Sum||Sum+||
|1|4.118|4.349|5.156|5.393|
|10|15.63|20.46|9.495|11.877|
|1000|4473|4535|983.6|1041|
|10000|46089|46120|9814|10416|

+ = with non-finite computation

Note:

This issue also effects the Sum. In that case the extra computation to track 
the correct non-finite result is more noticeable (approximately 6% at large 
input sizes).

 

> Mean returns a non-finite value that can be incorrect
> -----------------------------------------------------
>
>                 Key: STATISTICS-83
>                 URL: https://issues.apache.org/jira/browse/STATISTICS-83
>             Project: Commons Statistics
>          Issue Type: Bug
>          Components: descriptive
>            Reporter: Alex Herbert
>            Priority: Trivial
>
> The Mean maintains a non-finite value to return in the event that the input 
> contains non-finite values and the result is not finite.
> The non-finite value is a simple sum of the input. This returns the correct 
> result for most cases of nan/infinte values. However if the values contain 
> only one sign of infinity and the other finite values overflow to an opposite 
> infinity before the infinity is encountered the result is incorrect (due to 
> +inf + -inf == nan), e.g. the following test fails:
> {code:java}
> double[] values = {Double.MAX_VALUE, Double.MAX_VALUE, 
> Double.NEGATIVE_INFINITY};
> // Computed value = NaN
> Assertions.assertEquals(Double.NEGATIVE_INFINITY, 
> Mean.of(values).getAsDouble());
> {code}
> Investigate alternative options for non-finite results and either: correct 
> the support; or drop support if performance is significantly impacted in 
> order to handle for these rare cases.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to