Skip to content

Commit 28f70ec

Browse files
author
Carlos Parada
authored
Merge pull request #856 from itsdebartha/master
Optional argument for `variation()`
2 parents 9f08f3a + 2ffbdcc commit 28f70ec

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/scalarstats.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,17 @@ span(x) = ((a, b) = extrema(x); a:b)
404404

405405
# Variation coefficient: std / mean
406406
"""
407-
variation(x, m=mean(x))
407+
variation(x, m=mean(x); corrected=true)
408408
409409
Return the coefficient of variation of collection `x`, optionally specifying
410-
a precomputed mean `m`. The coefficient of variation is the ratio of the
411-
standard deviation to the mean.
412-
"""
413-
variation(x, m) = stdm(x, m) / m
414-
variation(x) = ((m, s) = mean_and_std(x); s/m)
410+
a precomputed mean `m`, and the optional correction parameter `corrected`.
411+
The coefficient of variation is the ratio of the
412+
standard deviation to the mean. If `corrected` is `false`,
413+
then `std` is calculated with denominator `n`. Else, the `std` is
414+
calculated with denominator `n-1`.
415+
"""
416+
variation(x, m; corrected::Bool=true) = stdm(x, m; corrected=corrected) / m
417+
variation(x; corrected::Bool=true) = ((m, s) = mean_and_std(x; corrected=corrected); s/m)
415418

416419
# Standard error of the mean: std / sqrt(len)
417420
# Code taken from var in the Statistics stdlib module

test/scalarstats.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ z2 = [8. 2. 3. 1.; 24. 10. -1. -1.; 20. 12. 1. -2.]
163163
@test span(skipmissing([1, missing, 5, missing])) == 1:5
164164

165165
@test variation([1:5;]) 0.527046276694730
166+
@test variation([1:5;]; corrected=false) 0.471404520791032
166167
@test variation(skipmissing([missing; 1:5; missing])) 0.527046276694730
168+
@test isnan(variation(1))
169+
@test variation(1; corrected=false) == 0
170+
# Possibly deprecated
171+
@test variation([1:5;],4) 0.4841229182759271
172+
@test variation([1:5;],4; corrected=false) 0.4330127018922193
167173

168174
@test @inferred(sem([1:5;])) 0.707106781186548
169175
@test @inferred(sem(skipmissing([missing; 1:5; missing]))) 0.707106781186548

0 commit comments

Comments
 (0)