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

Richard Penney commented on SPARK-33678:
----------------------------------------

Thanks [~srowen]  - I agree that there are definitely use-cases where operating 
directly in log-space is the right thing to do (obviously working with 
log-likelihoods is very common in the Bayesian world). Equally, there are 
clearly situations where log1p and expm1 are the right solutions when one 
knows, _a priori_, that one is dealing with arguments close to 1 and 0 
respectively.

My main argument is that there are common probabilistic use-cases where one 
incurs quite a lot of verbosity that might involve combinations of log1p, 
expm1, adding a small offset to avoid log(0), etc., when a simple "product" 
aggregation would be clearer and less likely to rely on delicate calculation of 
higher-order terms in exp(log(x)) when one neglects to re-express this as 
exp(log1p(x-1)).

Anyway, for the probabilistic use case, which is the one that I frequently deal 
with, I'd expect the situation where some of the probabilities are exactly zero 
is quite common. A use-case involving negative quantities might be constructing 
Lagrangian interpolation functions when modelling time-series. I'd admit that 
this isn't a going to be used by everyone, but having a product() aggregation 
seems like a very general-purpose tool.

> Numerical product aggregation
> -----------------------------
>
>                 Key: SPARK-33678
>                 URL: https://issues.apache.org/jira/browse/SPARK-33678
>             Project: Spark
>          Issue Type: Improvement
>          Components: SQL
>    Affects Versions: 2.4.7, 3.0.0, 3.1.0
>            Reporter: Richard Penney
>            Priority: Minor
>
> There is currently no facility in {{spark.sql.functions}} to allow 
> computation of the product of all numbers in a grouping expression. Such a 
> facility would likely be useful when computing statistical quantities such as 
> the combined probability of a set of independent events, or in financial 
> applications when calculating a cumulative interest rate.
> Although it is certainly possible to emulate this by an expression of the 
> form {{exp(sum(log(column)))}}, this has a number of significant drawbacks:
>  * It involves computationally costly functions (exp, log)
>  * It is more verbose than something like {{product(column)}}
>  * It is more prone to numerical inaccuracies when handling quantities that 
> are close to one than by directly multiplying a set of numbers
>  * It will not handle zeros or negative numbers cleanly
> I am currently developing an addition to {{sql.functions}}, which involves [a 
> new Catalyst aggregation 
> expression|https://github.com/rwpenney/spark/blob/feature/agg-product/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Product.scala].
>  This needs some additional testing, and I hope to issue a pull-request soon.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@spark.apache.org
For additional commands, e-mail: issues-h...@spark.apache.org

Reply via email to