-
Notifications
You must be signed in to change notification settings - Fork 421
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
Implement StatsAPI.pvalue
#1719
Merged
Merged
Changes from 3 commits
Commits
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
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,29 @@ | ||
function _check_tail(tail::Symbol) | ||
if tail !== :both && tail !== :left && tail !== :right | ||
throw(ArgumentError("`tail=$(repr(tail))` is invalid")) | ||
end | ||
end | ||
|
||
function StatsAPI.pvalue(dist::DiscreteUnivariateDistribution, x::Number; tail::Symbol=:both) | ||
_check_tail(tail) | ||
if tail === :both | ||
p = 2 * min(ccdf(dist, x-1), cdf(dist, x)) | ||
min(p, oneunit(p)) # if P(X = x) > 0, then possibly p > 1 | ||
elseif tail === :left | ||
cdf(dist, x) | ||
else # tail === :right | ||
ccdf(dist, x-1) | ||
end | ||
end | ||
|
||
function StatsAPI.pvalue(dist::ContinuousUnivariateDistribution, x::Number; tail::Symbol=:both) | ||
_check_tail(tail) | ||
if tail === :both | ||
p = 2 * min(cdf(dist, x), ccdf(dist, x)) | ||
min(p, oneunit(p)) # if P(X = x) > 0, then possibly p > 1 | ||
elseif tail === :left | ||
cdf(dist, x) | ||
else # tail === :right | ||
ccdf(dist, x) | ||
end | ||
end |
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,26 @@ | ||
using Distributions | ||
using StatsAPI: pvalue | ||
|
||
using Test | ||
|
||
@testset "pvalue" begin | ||
# For two discrete and two continuous distribution | ||
for dist in (Binomial(10, 0.3), Poisson(0.3), Normal(1.4, 2.1), Gamma(1.9, 0.8)) | ||
# Draw sample | ||
x = rand(dist) | ||
|
||
# Draw 10^6 additional samples | ||
ys = rand(dist, 1_000_000) | ||
|
||
# Check that empirical frequencies match pvalues of left/right tail approximately | ||
@test pvalue(dist, x; tail=:left) ≈ count(y -> y ≤ x, ys) / length(ys) rtol=5e-3 | ||
@test pvalue(dist, x; tail=:right) ≈ count(y -> y ≥ x, ys) / length(ys) rtol=5e-3 | ||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Check consistency of pvalues of both tails | ||
@test pvalue(dist, x; tail=:both) == | ||
min(1, 2 * min(pvalue(dist, x; tail=:left), pvalue(dist, x; tail=:right))) | ||
|
||
# Incorrect value for keyword argument | ||
@test_throws ArgumentError("`tail=:l` is invalid") pvalue(dist, x; tail=:l) | ||
end | ||
end |
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.
Would it make sense to have Distributions reexport
pvalue
? I'd be generally in favor but don't feel too strongly.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.
Wouldn't this break HypothesisTests?
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.
Not after HypothesisTests extends
pvalue
from StatsAPI.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.
Sure, but all existing releases of HypothesisTests would suddenly be broken, wouldn't they? Certainly, this could be fixed by modifying the registry but I think this makes it less attractive to re-export
pvalue
.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.
I'll merge and release without re-exporting
pvalue
for now but this can be reconsidered and changed in a subsequent release if desired.