Skip to content

Commit

Permalink
Add tests of variability metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmyleswhite committed Sep 22, 2013
1 parent c9d4812 commit cb60342
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion run_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
using Stats

my_tests = ["test/01.jl"
"test/means.jl"]
"test/means.jl",
"test/variability.jl"]
#"test/statquiz.jl"]

println("Running tests:")
Expand Down
3 changes: 2 additions & 1 deletion src/scalar_stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ end


# Skewness
# This is Type 1 definition according to Joanes and Gill (1998)
function skewness{T<:Real}(v::AbstractVector{T}, m::Real)
n = length(v)
cm2 = 0.0 # empirical 2nd centered moment (variance)
Expand All @@ -69,8 +70,8 @@ end

skewness{T<:Real}(v::AbstractVector{T}) = skewness(v, mean(v))


# (excessive) Kurtosis
# This is Type 1 definition according to Joanes and Gill (1998)
function kurtosis{T<:Real}(v::AbstractVector{T}, m::Real)
n = length(v)
cm2 = 0.0 # empirical 2nd centered moment (variance)
Expand Down
30 changes: 30 additions & 0 deletions test/variability.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Stats
using Base.Test

# Should this be 4 - 2?
# @test_approx_eq iqr([1, 2, 3, 4, 5])

@test_approx_eq kurtosis([1, 2, 3, 4, 5]) -1.3
@test_approx_eq kurtosis([1, 2, 3, 3, 2]) -1.1530612244897953

@test_approx_eq mad([1, 2, 3, 4, 5]) 1.4826
# TODO: This shouldn't fail
# @test_approx_eq mad(1:5) 1.4826

@test_approx_eq midrange([1, 2, 3, 4, 5]) 3.0
@test_approx_eq midrange([1, 2, 2, 2, 5]) 3.0
@test_approx_eq midrange([1, 4, 4, 4, 5]) 3.0
@test_approx_eq midrange([1:5]) 3.0

@test_approx_eq range([1, 2, 3, 4, 5]) 4.0
@test_approx_eq range([1, 2, 2, 2, 5]) 4.0
@test_approx_eq range([1, 4, 4, 4, 5]) 4.0
@test_approx_eq range([1:15]) 14.0

@test_approx_eq skewness([1, 2, 3, 4, 5]) 0.0
@test_approx_eq skewness([1, 2, 2, 2, 5]) 1.1731251294063556
@test_approx_eq skewness([1, 4, 4, 4, 5]) -1.1731251294063556

# sem()

# variation()

0 comments on commit cb60342

Please sign in to comment.