Skip to content

Commit

Permalink
added tests for D'Agostino test for skewness
Browse files Browse the repository at this point in the history
Updated docs for D'Agostino test

added test, plus code formatting
  • Loading branch information
stanlazic committed Dec 29, 2024
1 parent af41038 commit 817986a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
5 changes: 5 additions & 0 deletions docs/src/parametric.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ LeveneTest
```@docs
BrownForsytheTest
```

## D'Agostino Test
```@docs
AgostinoTest
```
16 changes: 7 additions & 9 deletions src/agostino_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ end


function agostino_stat(x::AbstractVector{<:Real})

#x = sort(x)

n = length(x)

if n < 8 || n > 46340
throw(ArgumentError("Sample size must be ≥ 8 and ≤ 46340"))
end

g₁ = skewness(x)
Y = g₁ * sqrt((n + 1) * (n + 3) / (6 * (n - 2)))
β₂ = 3 * (n^2 + 27 * n - 70) * (n + 1) * (n + 3) / ((n - 2) * (n + 5) * (n + 7) * (n + 9))
Expand All @@ -26,7 +25,7 @@ function agostino_stat(x::AbstractVector{<:Real})
α = sqrt(2 / (W² - 1))
Z = δ * log(Y / α + sqrt((Y / α)^2 + 1))

return (n, g₁, Z)
return (n, g₁, Z)
end


Expand All @@ -41,7 +40,7 @@ D'Agostino, R.B. (1970). Transformation to Normality of the Null Distribution of
Implements: [`pvalue`](@ref)
"""
function AgostinoTest(x::AbstractVector{<:Real})
AgostinoTest(agostino_stat(x)...)
return AgostinoTest(agostino_stat(x)...)
end


Expand All @@ -60,18 +59,17 @@ StatsAPI.nobs(x::AgostinoTest) = x.nobs
function StatsAPI.pvalue(x::AgostinoTest; tail = :both)

check_tail(tail)

left_tail = cdf(Normal(), x.z_statistic)
right_tail = ccdf(Normal(), x.z_statistic)

if tail == :both
p = minimum(2 * [left_tail, right_tail])
elseif tail == :left
p = left_tail
else tail == :right
else
p = right_tail
end

return p
end

23 changes: 23 additions & 0 deletions test/agostino_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using HypothesisTests, Test

@testset "D'Agostino Test" begin
# Check errors
@test_throws ArgumentError AgostinoTest(rand(5)) # too few samples
@test_throws ArgumentError AgostinoTest(rand(50000)) # too many samples

# Check correctness of results (benchmarked against `moments` package in R)
y = [
2.44816, 1.086045, 0.154618, 0.778503, 0.923376,
1.18671, -0.495009, -0.151178, 1.662281, 2.615421,
]

result = @inferred AgostinoTest(y)
@test result.z_statistic 0.28356 atol = 1.0e-6
@test result.skewness 0.158581 atol = 1.0e-6
@test nobs(result) == 10
@test pvalue(result) 0.776748 atol = 1.0e-6
@test pvalue(result, tail = :left) 0.611626 atol = 1.0e-6
@test pvalue(result, tail = :right) 0.388374 atol = 1.0e-6
@test AgostinoTest(exp.(y)).z_statistic 2.10965 atol = 1.0e-5
@test AgostinoTest(-1 .* exp.(y)).z_statistic -2.10965 atol = 1.0e-5
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ macro test_ci_approx(x::Expr, y::Expr)
end
end

include("agostino_test.jl")
include("anderson_darling.jl")
include("augmented_dickey_fuller.jl")
include("bartlett.jl")
Expand Down

0 comments on commit 817986a

Please sign in to comment.