Skip to content

fix #905: use the correct mad() normalization #949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StatsBase"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
authors = ["JuliaStats"]
version = "0.34.4"
version = "0.35.0"

[deps]
AliasTables = "66dad0bd-aa9a-41b7-9441-69ab47430ed8"
Expand Down
4 changes: 0 additions & 4 deletions src/deprecates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ end

@deprecate norepeats(a::AbstractArray) allunique(a)

@deprecate(mad!(v::AbstractArray{<:Real}, center;
constant::Real = BigFloat("1.482602218505601860547076529360423431326703202590312896536266275245674447622701")),
mad!(v, center=center, constant=constant))

### Deprecated January 2019
@deprecate scattermatm(x::DenseMatrix, mean, dims::Int) scattermat(x, mean=mean, dims=dims)
@deprecate scattermatm(x::DenseMatrix, mean, wv::AbstractWeights, dims::Int) scattermat(x, wv, mean=mean, dims=dims)
Expand Down
31 changes: 9 additions & 22 deletions src/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,10 @@ function sem(x::AbstractArray, weights::ProbabilityWeights; mean=nothing)
end

# Median absolute deviation
@irrational mad_constant 1.4826022185056018 BigFloat("1.482602218505601860547076529360423431326703202590312896536266275245674447622701")
@irrational mad_to_std_multiplier 1.4826022185056018 BigFloat("1.482602218505601860547076529360423431326703202590312896536266275245674447622701")

"""
mad(x; center=median(x), normalize=true)
mad(x; center=median(x), normalize=false)

Compute the median absolute deviation (MAD) of collection `x` around `center`
(by default, around the median).
Expand All @@ -536,12 +536,12 @@ If `normalize` is set to `true`, the MAD is multiplied by
`1 / quantile(Normal(), 3/4) ≈ 1.4826`, in order to obtain a consistent estimator
of the standard deviation under the assumption that the data is normally distributed.
"""
function mad(x; center=nothing, normalize::Union{Bool, Nothing}=nothing, constant=nothing)
mad!(Base.copymutable(x); center=center, normalize=normalize, constant=constant)
function mad(x; center=nothing, normalize::Bool=false)
mad!(Base.copymutable(x); center=center, normalize=normalize)
end

"""
StatsBase.mad!(x; center=median!(x), normalize=true)
StatsBase.mad!(x; center=median!(x), normalize=false)

Compute the median absolute deviation (MAD) of array `x` around `center`
(by default, around the median), overwriting `x` in the process.
Expand All @@ -552,27 +552,14 @@ of the standard deviation under the assumption that the data is normally distrib
"""
function mad!(x::AbstractArray;
center=median!(x),
normalize::Union{Bool,Nothing}=true,
constant=nothing)
normalize::Bool=false)
isempty(x) && throw(ArgumentError("mad is not defined for empty arrays"))
c = center === nothing ? median!(x) : center
T = promote_type(typeof(c), eltype(x))
T = promote_type(typeof(center), eltype(x))
U = eltype(x)
x2 = U == T ? x : isconcretetype(U) && isconcretetype(T) && sizeof(U) == sizeof(T) ? reinterpret(T, x) : similar(x, T)
x2 .= abs.(x .- c)
x2 .= abs.(x .- center)
m = median!(x2)
if normalize isa Nothing
Base.depwarn("the `normalize` keyword argument will be false by default in future releases: set it explicitly to silence this deprecation", :mad)
normalize = true
end
if !isa(constant, Nothing)
Base.depwarn("keyword argument `constant` is deprecated, use `normalize` instead or apply the multiplication directly", :mad)
m * constant
elseif normalize
m * mad_constant
else
m
end
normalize ? m * mad_to_std_multiplier : m
end

# Interquartile range
Expand Down