-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<random>: Improve binomial_distribution accuracy #1531
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c3e29e1
Fixes inaccurate binomial_distribution for certain parameters
MattStephanson 8ab30a0
buildfix
MattStephanson ff17926
code review
MattStephanson 8dfeba4
code review: simplify control flow
MattStephanson a107fd4
buildfix
MattStephanson 498c024
Fix copy-paste mistake
MattStephanson 225f510
Make comment consistent with code
MattStephanson 9203baf
Code review feedback.
StephanTLavavej 8bad736
Handle p ~1 case.
MattStephanson 5182599
typo
MattStephanson 694498b
resolve merge conflict
MattStephanson 22a044f
Merge branch 'main' into gh-1530
CaseyCarter f4807a5
Casey's guess that #1530 caused these libcxx failures was wrong
CaseyCarter 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_matrix.lst |
This file contains hidden or 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,48 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <cassert> | ||
| #include <cmath> | ||
| #include <cstddef> | ||
| #include <random> | ||
| #include <vector> | ||
|
|
||
| using namespace std; | ||
|
|
||
| template <class Generator> | ||
| void test_binomial(const int n, const double mean, Generator& gen) { | ||
| const double p = mean / n; | ||
| const double var = n * p * (1.0 - p); | ||
| constexpr double tol = 0.01; | ||
| constexpr double x = 4.0; // Standard deviation of sample variance should be less than tol / x. | ||
| const size_t it_max = 2 * static_cast<size_t>(pow(var / (tol / x), 2.0)); | ||
| binomial_distribution<> dist(n, p); | ||
|
|
||
| vector<size_t> counts(static_cast<size_t>(n) + 1); | ||
| for (size_t i = 0; i < it_max; ++i) { | ||
| ++counts[static_cast<size_t>(dist(gen))]; | ||
| } | ||
|
|
||
| double sample_mean = 0.0; | ||
| for (size_t i = 1; i < counts.size(); ++i) { | ||
| sample_mean += static_cast<double>(i) * counts[i]; | ||
| } | ||
| sample_mean /= it_max; | ||
|
|
||
| double sample_var = 0.0; | ||
| for (size_t i = 0; i < counts.size(); ++i) { | ||
| sample_var += counts[i] * pow(i - sample_mean, 2); | ||
| } | ||
| sample_var /= it_max - 1; | ||
|
|
||
| assert(abs(sample_mean / mean - 1.0) < tol); | ||
| assert(abs(sample_var / var - 1.0) < tol); | ||
| } | ||
|
|
||
| int main() { | ||
| mt19937 gen; | ||
| constexpr int n = 25; | ||
| test_binomial(n, 0.99, gen); | ||
| test_binomial(n, n - 0.99, gen); | ||
| return 0; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.