Skip to content

Commit

Permalink
Merge pull request #46 from JuliaStats/anj/ad
Browse files Browse the repository at this point in the history
Don't remove -Inf when computing the Anderson-Darling test statistic.
  • Loading branch information
andreasnoack committed Feb 6, 2016
2 parents 7d4ad60 + a6df312 commit 1fa7961
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/anderson_darling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ function adstats{T<:Real}(x::AbstractVector{T}, d::UnivariateDistribution)
y = sort(x)
μ = mean(y)
σ = std(y)
z = cdf(d, (y-μ)/σ)
i = 1:n
S = ((2*i-1.0)/n) .* (log(z[i])+log(1-z[n+1-i]))
S[isinf(S)] = 0. # remove infinity
= -n-sum(S)
= convert(typeof(μ), -n)
for i = 1:n
zi = (y[i] - μ)/σ
zni1 = (y[n - i + 1] - μ)/σ
lcdfz = logcdf(d, zi)
lccdfz = logccdf(d, zni1)
-= (2*i - 1)/n * (lcdfz + lccdfz)
end
(n, μ, σ, A²)
end

Expand Down Expand Up @@ -51,8 +54,10 @@ function pvalue(x::OneSampleADTest)
1.0 - exp(-8.318+42.796z-59.938z^2)
elseif .340 < z < .600
exp(0.9177-4.279z-1.38z^2)
else
elseif z < 153.467
exp(1.2937-5.709z+0.0186z^2)
else
0.0
end
end

Expand Down
6 changes: 4 additions & 2 deletions test/anderson_darling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ t = OneSampleADTest(x, Normal())

x = rand(Cauchy(), n)
t = OneSampleADTest(x, Normal())
@test_approx_eq_eps t.278.0190 0.1^4
@test pvalue(t) < 1e-100

x = rand(LogNormal(), n)
t = OneSampleADTest(x, Normal())
@test_approx_eq_eps t.85.5217 0.1^4
@test pvalue(t) < 1e-100

# k-sample test
samples = Any[
Expand Down Expand Up @@ -48,3 +48,5 @@ t = KSampleADTest(samples...)
samples = Any[rand(Normal(), 50), rand(Normal(), 30), rand(Normal(), 20)]
t = KSampleADTest(samples...)
@test pvalue(t) > 0.05

@test pvalue(OneSampleADTest(vcat(rand(Normal(),500), rand(Beta(2,2),500)), Beta(2,2))) == 0

0 comments on commit 1fa7961

Please sign in to comment.