-
Notifications
You must be signed in to change notification settings - Fork 85
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
hierarchy of errors #247
Closed
Closed
hierarchy of errors #247
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb4f99c
feat(errors)!: deprecate crate-wide error
YeungOnion d93c061
feat(errors): define distribution level errors
YeungOnion 5f0e781
feat(errors)!: use new distribution level errors
YeungOnion a6a7ee7
doc(errors): create example for new errors
YeungOnion ae60338
test(errors): drop tests for values from degenerate distributions
YeungOnion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
extern crate statrs; | ||
|
||
use anyhow::{anyhow, Context, Result as AnyhowResult}; | ||
use statrs::distribution::{ | ||
Continuous, Discrete, Gamma, GammaError, NegativeBinomial, Normal, ParametrizationError, | ||
}; | ||
|
||
pub fn main() -> AnyhowResult<()> { | ||
gamma_pdf(1.0, 1.0, 1.0).map(|x| println!("val = {}", x))?; | ||
// val = 0.36787944117144233 | ||
|
||
normal_pdf(1.0, 1.0, 1.0).map(|x| println!("val = {}", x))?; | ||
// val = 0.39894228040143265 | ||
|
||
gamma_pdf_with_negative_shape_correction(-0.5, 1.0, 1.0).map(|x| println!("val = {}", x))?; | ||
// without shape correction would emit, the below | ||
// Error: failed creating gamma(-0.5,1) | ||
// | ||
// Caused by: | ||
// 0: shape must be finite, positive, and not nan | ||
// 1: expected positive, got -0.5 | ||
// after re-attempt, output is | ||
// Error: gamma provided invalid shape | ||
// attempting to correct shape to 0.5 | ||
// val = 0.2075537487102974 | ||
|
||
nb_pmf(1, 1.0, 1).map(|x| println!("val = {}", x))?; | ||
// Error: failed creating nb(1,1) | ||
// | ||
// Caused by: | ||
// mean of 0 is degenerate | ||
|
||
nb_pmf(1, 0., 1).map(|x| println!("val = {}", x))?; | ||
// Error: failed creating nb(1,0) | ||
// | ||
// Caused by: | ||
// mean of inf is degenerate | ||
|
||
normal_pdf(1.0, f64::INFINITY, 1.0).map(|x| println!("val = {}", x))?; | ||
// Error: failed creating normal(1, inf) | ||
// | ||
// Caused by: | ||
// variance of inf is degenrate | ||
|
||
normal_pdf(1.0, 0.0, 1.0).map(|x| println!("val = {}", x))?; | ||
// Error: failed creating normal(1, 0) | ||
// | ||
// Caused by: | ||
// variance of 0 is degenerate | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn gamma_pdf(shape: f64, rate: f64, x: f64) -> AnyhowResult<f64> { | ||
Ok(Gamma::new(shape, rate) | ||
.context(format!("failed creating gamma({},{})", shape, rate))? | ||
.pdf(x)) | ||
} | ||
|
||
pub fn gamma_pdf_with_negative_shape_correction( | ||
shape: f64, | ||
rate: f64, | ||
x: f64, | ||
) -> AnyhowResult<f64> { | ||
match gamma_pdf(shape, rate, x) { | ||
Ok(x) => Ok(x), | ||
Err(ee) => { | ||
if let GammaError::InvalidShape(e) = ee.downcast::<GammaError>()? { | ||
eprintln!("Error: gamma provided invalid shape"); | ||
if let ParametrizationError::ExpectedPositive(shape) = e { | ||
eprintln!("\tattempting to correct shape to {}", shape.abs()); | ||
// fails again for 0 and INF | ||
gamma_pdf(shape.abs(), rate, x) | ||
} else { | ||
Err(anyhow!("cannot recover valid shape from this error")) | ||
} | ||
} else { | ||
Err(anyhow!( | ||
"cannot recover both valid shape and rate from this error" | ||
)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn nb_pmf(r: u64, p: f64, x: u64) -> AnyhowResult<f64> { | ||
Ok(NegativeBinomial::new(r, p) | ||
.context(format!("failed creating nb({},{})", r, p))? | ||
.pmf(x)) | ||
} | ||
|
||
pub fn normal_pdf(location: f64, scale: f64, x: f64) -> AnyhowResult<f64> { | ||
Ok(Normal::new(location, scale) | ||
.context(format!("failed creating normal({}, {})", location, scale))? | ||
.pdf(x)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why you added this abstraction? You could also check the parameters inside
Gamma::new
and return a suitable error from there, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly just playing around with having input checking in another type to support the use of infallible
new
methods.But more interestingly perhaps it could be a way to get alternative parametrizations #198 as well as
new_from_moments
to do something like,But I've lots of thoughts floating around about that none motivated by something I've actually needed thus far, so happy to drop it until specific need arises. As a solution to #198 could be adding just one more method.