Skip to content

Commit

Permalink
Merge pull request #856 from itsdebartha/master
Browse files Browse the repository at this point in the history
Optional argument for `variation()`
  • Loading branch information
ParadaCarleton committed Mar 18, 2023
2 parents 9f08f3a + 2ffbdcc commit 28f70ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,17 @@ span(x) = ((a, b) = extrema(x); a:b)

# Variation coefficient: std / mean
"""
variation(x, m=mean(x))
variation(x, m=mean(x); corrected=true)
Return the coefficient of variation of collection `x`, optionally specifying
a precomputed mean `m`. The coefficient of variation is the ratio of the
standard deviation to the mean.
"""
variation(x, m) = stdm(x, m) / m
variation(x) = ((m, s) = mean_and_std(x); s/m)
a precomputed mean `m`, and the optional correction parameter `corrected`.
The coefficient of variation is the ratio of the
standard deviation to the mean. If `corrected` is `false`,
then `std` is calculated with denominator `n`. Else, the `std` is
calculated with denominator `n-1`.
"""
variation(x, m; corrected::Bool=true) = stdm(x, m; corrected=corrected) / m
variation(x; corrected::Bool=true) = ((m, s) = mean_and_std(x; corrected=corrected); s/m)

# Standard error of the mean: std / sqrt(len)
# Code taken from var in the Statistics stdlib module
Expand Down
6 changes: 6 additions & 0 deletions test/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ z2 = [8. 2. 3. 1.; 24. 10. -1. -1.; 20. 12. 1. -2.]
@test span(skipmissing([1, missing, 5, missing])) == 1:5

@test variation([1:5;]) 0.527046276694730
@test variation([1:5;]; corrected=false) 0.471404520791032
@test variation(skipmissing([missing; 1:5; missing])) 0.527046276694730
@test isnan(variation(1))
@test variation(1; corrected=false) == 0
# Possibly deprecated
@test variation([1:5;],4) 0.4841229182759271
@test variation([1:5;],4; corrected=false) 0.4330127018922193

@test @inferred(sem([1:5;])) 0.707106781186548
@test @inferred(sem(skipmissing([missing; 1:5; missing]))) 0.707106781186548
Expand Down

0 comments on commit 28f70ec

Please sign in to comment.