-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement `StatsAPI.pvalue` * Add tests * Update test/runtests.jl * Update test/statsapi.jl Co-authored-by: Alex Arslan <ararslan@comcast.net> --------- Co-authored-by: Alex Arslan <ararslan@comcast.net>
- Loading branch information
Showing
5 changed files
with
63 additions
and
1 deletion.
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) ≈ mean(≤(x), ys) rtol=5e-3 | ||
@test pvalue(dist, x; tail=:right) ≈ mean(≥(x), ys) rtol=5e-3 | ||
|
||
# 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 |
4fd8be8
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.
@JuliaRegistrator register
4fd8be8
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.
Registration pull request created: JuliaRegistries/General/83231
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: