Skip to content
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

Fix a bug in the uniform continuous distribution #60

Merged
merged 8 commits into from Jun 25, 2017

Conversation

mp4096
Copy link
Contributor

@mp4096 mp4096 commented Jun 22, 2017

Hi and thanks for the library!

Problem

I think there's a bug in the implementation of the continuous uniform distribution. According to the documentation, it should return values in the range [min, max], i.e. min ≤ random value ≤ max.

Unfortunately, the current implementation generates values in the range [min, max + 1), i.e. min ≤ random value < max + 1.

I think it might be a copy-paste bug 'inherited' from the discrete uniform distribution, where you really need to add 1 to the upper bound.

Solution

This PR is a partial fix for this bug: It changes the range of the continuous uniform distribution to [min, max), i.e. min ≤ random value < max. I don't have a quick fix to include the upper bound, but I think it's important to at least fix the + 1.0 issue. As of 0.7.0, you cannot really sample values between 0 and 1 without resorting to wild workarounds.

References

@vks
Copy link
Contributor

vks commented Jun 22, 2017

Could you please add a test verifying it works now?

@mp4096
Copy link
Contributor Author

mp4096 commented Jun 22, 2017

Sure, but I'm not sure how can you test this in a deterministic way. However, I can just sample a lot of values and see whether they are all in the range [min, max).

@vks
Copy link
Contributor

vks commented Jun 22, 2017

Yes, I was thinking of something like this:

diff --git a/src/distribution/uniform.rs b/src/distribution/uniform.rs
index 11b03da..b839849 100644
--- a/src/distribution/uniform.rs
+++ b/src/distribution/uniform.rs
@@ -464,4 +464,17 @@ mod test {
         test::check_continuous_distribution(&try_create(0.0, 10.0), 0.0, 10.0);
         test::check_continuous_distribution(&try_create(-2.0, 15.0), -2.0, 15.0);
     }
+
+    #[test]
+    fn test_sample() {
+        use super::Sample;
+
+        let mut u = Uniform::new(0., 1.).unwrap();
+        let mut r = ::rand::StdRng::new().unwrap();
+        for _ in 0..100000 {
+            let x = u.sample(&mut r);
+            assert!(0. <= x);
+            assert!(x < 1.);
+        }
+    }
 }

Warning: This test is probabilistic!
I hope that 1000 trials should be enough, though.
@vks
Copy link
Contributor

vks commented Jun 22, 2017

If you want it to be deterministic, you can seed the RNG.

@mp4096
Copy link
Contributor Author

mp4096 commented Jun 22, 2017

Wow, we've written the tests within a minute ✋ 😎

@mp4096
Copy link
Contributor Author

mp4096 commented Jun 22, 2017

Seeding the PRNG is surely a good idea! I'll take a look at it tomorrow.

@@ -464,4 +464,23 @@ mod test {
test::check_continuous_distribution(&try_create(0.0, 10.0), 0.0, 10.0);
test::check_continuous_distribution(&try_create(-2.0, 15.0), -2.0, 15.0);
}

#[test]
fn test_in_range() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could re-name this to something like test_sample_in_range to be a little more explicit that would be awesome

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, fixed it!

@boxtown
Copy link
Collaborator

boxtown commented Jun 23, 2017

This is an excellent catch. If you could address the comment I'll merge asap. Also if you could update the CHANGELOG.md under v0.8.0 to reflect what you did

@mp4096
Copy link
Contributor Author

mp4096 commented Jun 23, 2017

I struggled a bit with the wording for changelog, but I hope one can understand what I mean.

@mp4096 mp4096 changed the title Partially fix a bug in the uniform continuous distribution Fix a bug in the uniform continuous distribution Jun 25, 2017
@boxtown boxtown merged commit 5d07c04 into statrs-dev:master Jun 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants