-
Notifications
You must be signed in to change notification settings - Fork 86
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
Error handling: Panics vs Result #41
Comments
I think consistency is huge here. I did a cursory glance at other crates and noticed the following: Quick SurveySTD
Rand crate
Num/Num-Traits
It seems to me that a takeaway could be:
|
@cbreeden Thanks for the feedback and yes, I agree consistency is key which I feel like overall the API is pretty consistent with the interface it provides. My main concern is whether or not the API as it stands is ergonomic for users. I've taken a page from the |
Yeah ergonomics is good question. Some complaints that I've heard (about rust in general) is how difficult it is to write expressive numerical code. When I was using statrs, I was using it to simulate some markov chains. Personally, I had no interest in checking for the validity of my inputs for my distributions (and would rather let it panic, though I do understand why someone would disagree for their use case). Honestly, I think the default API should panic and having a Provided that the naming scheme is hashed out now, it may not be necessary to do any work before a 1.0 release if the idea is to provide |
@cbreeden I agree and personally am leaning towards a |
I agree that it's best to However, the cumulative distribution function at the point x is defined (for all distributions, discrete or not) as the probability that a sample is less than (or equal to) x. This makes sense for all x, for all distributions. The cdf of a distribution that can only take positive values is 0 at all negative input values, and that's what statrs should return too. Similarly, the cdf of a Bernoulli distribution at any x > 1 is simply 1, and that's also what statrs should return. The function Similarly, the probability density function is also defined for any x and any distribution. If a distribution cannot produce the value x (or get arbitrarily close to it), then the probability density at x is 0. So Even at infinity, the pdf and cdf are well-defined.: pdf(-∞) = 0, pdf(∞) = 0, cdf(-∞) = 0, cdf(∞) = 1 for all distributions. The only exception is If you agree with these changes, I can implement them and submit a pull request. |
@migi Semantically you are correct and as long as we're consistent I have no issue accepting the changes. |
See GitHub issue statrs-dev#41. Don't panic when calculating a pdf, cdf or pmf whenever the input is a value that the distribution cannot attain. The pdf, pmf and cdf are still defined at those inputs and we should return the correct values. This commit also adds a few tests to each distribution to test that pdf(-inf)=0, pdf(inf)=0, cdf(-inf)=0 and cdf(inf)=1. It also uses simple numerical integration to test for continuous distributions that the integral of the pdf is approximately equal to the cdf, and for discrete distributions that the sum of the pmf is equal to the cdf.
See GitHub issue statrs-dev#41. Don't panic when calculating a pdf, cdf or pmf whenever the input is a value that the distribution cannot attain. The pdf, pmf and cdf are still defined at those inputs and we should return the correct values. This commit also adds a few tests to each distribution to test that pdf(-inf)=0, pdf(inf)=0, cdf(-inf)=0 and cdf(inf)=1. It also uses simple numerical integration to test for continuous distributions that the integral of the pdf is approximately equal to the cdf, and for discrete distributions that the sum of the pmf is equal to the cdf.
Currently the responsibility for guarding against exceptional cases (e.g. input not in valid domain, mathematically invalid operations etc) is passed to the user. We
panic
when an operation does not make mathematical sense (e.g. calculating the cumulative distribution function for discrete distributions at a negative input) which forces users to double check to make sure their inputs are valid. While this results in technically correct and predictable behavior from the API, I'm not sure if it's ergonomic or idiomatic and have been mulling over possibly introducing aResult
based API either replacing or in addition to the stricterpanic
based API. This however warrants some discussion and I would love feedback from the communityThe text was updated successfully, but these errors were encountered: