Skip to content

Commit

Permalink
Special functions (mad!) (#367)
Browse files Browse the repository at this point in the history
* Fix for mad!

Drops SpecialFunctions

* Why the segerrs?

* Revert "Why the segerrs?"

This reverts commit faa2162.

* Possible workaround

JuliaLang/julia/issues/22108

* Addressed comments

Will check if it solves the BigFloat issue.

* Fix syntax

* Remove unused constant definition

* Restore Real

* Attempt irrational

* Implied rational precision.

* Named scale factor `mad_constant`
  • Loading branch information
Nosferican authored and andreasnoack committed Apr 15, 2018
1 parent 4cf13cd commit f70a012
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
1 change: 0 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
julia 0.6
DataStructures 0.5.0
SpecialFunctions 0.1.0
SortingAlgorithms
Compat 0.61.0
Missings
3 changes: 1 addition & 2 deletions src/StatsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ __precompile__()
module StatsBase
import Base: length, isempty, eltype, values, sum, mean, mean!, show, quantile
import Base.Cartesian: @nloops, @nref, @nextract
using Base: @irrational
import DataStructures: heapify!, heappop!, percolate_down!

import SpecialFunctions: erfcinv

using Compat, SortingAlgorithms, Missings
using Compat.LinearAlgebra
using Compat.Random
Expand Down
4 changes: 2 additions & 2 deletions src/deprecates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ rand(s::RandIntSampler) = rand(Compat.Random.GLOBAL_RNG, s)
@deprecate randi(rng::AbstractRNG, a::Int, b::Int) rand(rng, a:b)
@deprecate randi(a::Int, b::Int) rand(a:b)

@deprecate(mad!(v::AbstractArray{T}, center;
constant::Real = 1 / (-sqrt(2 * one(T)) * erfcinv(3 * one(T) / 2))) where T<:Real,
@deprecate(mad!(v::AbstractArray{<:Real}, center;
constant::Real = BigFloat("1.482602218505601860547076529360423431326703202590312896536266275245674447622701")),
mad!(v, center=center, constant=constant))
23 changes: 11 additions & 12 deletions src/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function mad(v::AbstractArray{T};
mad!(v2, center=center === nothing ? median!(v2) : center, normalize=normalize)
end


@irrational mad_constant 1.4826022185056018 BigFloat("1.482602218505601860547076529360423431326703202590312896536266275245674447622701")
"""
StatsBase.mad!(v; center=median!(v), normalize=true)
Expand All @@ -274,25 +274,24 @@ 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!(v::AbstractArray{T};
function mad!(v::AbstractArray{<:Real};
center::Real=median!(v),
normalize::Union{Bool,Nothing}=true,
constant=nothing) where T<:Real
for i in 1:length(v)
@inbounds v[i] = abs(v[i]-center)
end
k = 1 / (-sqrt(2 * one(T)) * erfcinv(3 * one(T) / 2))
if normalize === nothing
constant=nothing)
isempty(v) && throw(ArgumentError("mad is not defined for empty arrays"))
v .= abs.(v .- center)
m = median!(v)
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 constant !== nothing
if !isa(constant, Nothing)
Base.depwarn("keyword argument `constant` is deprecated, use `normalize` instead or apply the multiplication directly", :mad)
constant * median!(v)
m * constant
elseif normalize
k * median!(v)
m * mad_constant
else
one(k) * median!(v)
m
end
end

Expand Down

0 comments on commit f70a012

Please sign in to comment.