Skip to content

Commit

Permalink
fix JuliaStats#905: use the correct mad() normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin authored Jan 29, 2025
1 parent 58780c9 commit b522736
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
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
24 changes: 8 additions & 16 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::Union{Bool, Nothing}=nothing)
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,24 +552,16 @@ 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::Union{Bool,Nothing}=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))
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)
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
if normalize === true
m * mad_to_std_multiplier
else
m
end
Expand Down

0 comments on commit b522736

Please sign in to comment.