@@ -532,41 +532,14 @@ If `normalize` is set to `true`, the MAD is multiplied by
532
532
of the standard deviation under the assumption that the data is normally distributed.
533
533
"""
534
534
function mad (x; center= nothing , normalize:: Union{Bool, Nothing} = nothing , constant= nothing )
535
- isempty (x) && throw (ArgumentError (" mad is not defined for empty arrays" ))
536
- T = eltype (x)
537
- # Knowing the eltype allows allocating a single array able to hold both original values
538
- # and differences from the center, instead of two arrays
539
- S = isconcretetype (T) ? promote_type (T, typeof (middle (zero (T)))) : T
540
- x2 = x isa AbstractArray ? copyto! (similar (x, S), x) : collect (S, x)
541
- c = center === nothing ? median! (x2) : center
542
- if isconcretetype (T)
543
- x2 .= abs .(x2 .- c)
544
- else
545
- x2 = abs .(x2 .- c)
546
- end
547
- m = median! (x2)
548
- if normalize isa Nothing
549
- Base. depwarn (" the `normalize` keyword argument will be false by default in future releases: set it explicitly to silence this deprecation" , :mad )
550
- normalize = true
551
- end
552
- if ! isa (constant, Nothing)
553
- Base. depwarn (" keyword argument `constant` is deprecated, use `normalize` instead or apply the multiplication directly" , :mad )
554
- m * constant
555
- elseif normalize
556
- m * mad_constant
557
- else
558
- m
559
- end
535
+ mad! (Base. copymutable (x); center= center, normalize= normalize, constant= constant)
560
536
end
561
537
562
538
"""
563
539
StatsBase.mad!(x; center=median!(x), normalize=true)
564
540
565
541
Compute the median absolute deviation (MAD) of array `x` around `center`
566
542
(by default, around the median), overwriting `x` in the process.
567
- `x` must be able to hold values of generated by calling `middle` on its elements
568
- (for example an integer vector is not appropriate since `middle` can produce
569
- non-integer values).
570
543
571
544
If `normalize` is set to `true`, the MAD is multiplied by
572
545
`1 / quantile(Normal(), 3/4) ≈ 1.4826`, in order to obtain a consistent estimator
@@ -577,8 +550,12 @@ function mad!(x::AbstractArray;
577
550
normalize:: Union{Bool,Nothing} = true ,
578
551
constant= nothing )
579
552
isempty (x) && throw (ArgumentError (" mad is not defined for empty arrays" ))
580
- x .= abs .(x .- center)
581
- m = median! (x)
553
+ c = center === nothing ? median! (x) : center
554
+ T = promote_type (typeof (c), eltype (x))
555
+ U = eltype (x)
556
+ x2 = U == T ? x : isconcretetype (U) && isconcretetype (T) && sizeof (U) == sizeof (T) ? reinterpret (T, x) : similar (x, T)
557
+ x2 .= abs .(x .- c)
558
+ m = median! (x2)
582
559
if normalize isa Nothing
583
560
Base. depwarn (" the `normalize` keyword argument will be false by default in future releases: set it explicitly to silence this deprecation" , :mad )
584
561
normalize = true
0 commit comments