-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Adding the Poisson distribution #15814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| import org.apache.commons.math3.distribution.BetaDistribution; | ||
| import org.apache.commons.math3.distribution.BinomialDistribution; | ||
| import org.apache.commons.math3.distribution.ChiSquaredDistribution; | ||
| import org.apache.commons.math3.distribution.PoissonDistribution; | ||
| import org.apache.commons.math3.special.Erf; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
@@ -761,6 +762,32 @@ public static double chiSquaredCdf( | |
| return distribution.cumulativeProbability(value); | ||
| } | ||
|
|
||
| @Description("Inverse of Poisson cdf given lambda (mean) parameter and probability") | ||
| @ScalarFunction | ||
| @SqlType(StandardTypes.INTEGER) | ||
| public static long inversePoissonCdf( | ||
| @SqlType(StandardTypes.DOUBLE) double lambda, | ||
| @SqlType(StandardTypes.DOUBLE) double p) | ||
| { | ||
| checkCondition(p >= 0 && p < 1, INVALID_FUNCTION_ARGUMENT, "p must be in the interval [0, 1)"); | ||
| checkCondition(lambda > 0, INVALID_FUNCTION_ARGUMENT, "lambda must be greater than 0"); | ||
| PoissonDistribution distribution = new PoissonDistribution(lambda); | ||
| return distribution.inverseCumulativeProbability(p); | ||
| } | ||
|
|
||
| @Description("Poisson cdf given the lambda (mean) parameter and value") | ||
| @ScalarFunction | ||
| @SqlType(StandardTypes.DOUBLE) | ||
| public static double poissonCdf( | ||
| @SqlType(StandardTypes.DOUBLE) double lambda, | ||
| @SqlType(StandardTypes.INTEGER) long value) | ||
| { | ||
| checkCondition(value >= 0, INVALID_FUNCTION_ARGUMENT, "value must be a non-negative integer"); | ||
| checkCondition(lambda > 0, INVALID_FUNCTION_ARGUMENT, "lambda must be greater than 0"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the library API is already doing this check, I say just do a try/catch and throw user_error
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting. Since this is the method used by all the distribution functions (i.e.: normal, beta, chi-square, binomial), do you think it should be changed there as well?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just do something like: Look at StandardErrorCodes.java and other files to see the pattern what they do.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And yeah it will be good to do for all of them. |
||
| PoissonDistribution distribution = new PoissonDistribution(lambda); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the lambda going to be generally fixed in a query? If so, you should find a way to avoid new object creation to improve memory perf.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also interesting. As I wrote to your other comment - this is the method used by all the distribution functions (i.e.: normal, beta, chi-square, binomial), do you think it should be changed there as well? Thanks upfront.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a good suggestion here :( I looked in the code but can't find other examples. I will look further and comment here if I find something. |
||
| return distribution.cumulativeProbability((int) value); | ||
| } | ||
|
|
||
| @Description("round to nearest integer") | ||
| @ScalarFunction("round") | ||
| @SqlType(StandardTypes.TINYINT) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.