Skip to content

Commit

Permalink
Merge branch 'master' into jpata/abstract-hist
Browse files Browse the repository at this point in the history
  • Loading branch information
jpata committed Jan 23, 2015
2 parents ebce1f8 + 3148a43 commit 352c64f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.3-
ArrayViews 0.4.6-
ArrayViews 0.4.8-
6 changes: 4 additions & 2 deletions docs/source/scalarstats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ Measurements of Variation

Compute the standard error of the mean for ``x``, *i.e.* ``sqrt(var(x) / length(x))``.

.. function:: mad(x)
.. function:: mad(x[, center][; constant=1.4826])

Compute the `median absolute deviation <http://en.wikipedia.org/wiki/Median_absolute_deviation>`_ of ``x``.

One can optionally supply the ``center``. By default, ``constant=1.4826`` for consistent estimation of the standard deviation of a normal distribution.


Z-scores
----------
Expand Down Expand Up @@ -139,7 +141,7 @@ Quantile and Friends

Extended method of *quantile*. Equivalent to ``nquantile(x, 4)``, which returns a vector of quantiles at ``0.0, 0.25, 0.50, 0.75, 1.0``.

.. function:: median(x, w[; checknan=true])
.. function:: median(x, w)

Compute the weighted median of ``x``, using weights given by a weight vector ``w`` (of type ``WeightVec``). The weight and data vectors must have the same length. The weighted median :math:`x_k` is the element of ``x`` that satisfies :math:`\sum_{x_i < x_k} w_i \le \frac{1}{2} \sum_{j} w_j` and :math:`\sum_{x_i > x_k} w_i \le \frac{1}{2} \sum_{j} w_j`. If a weight has value zero, then its associated data point is ignored. If none of the weights are positive, an error is thrown. ``NaN`` is returned if ``x`` contains any ``NaN`` values. An error is raised if ``w`` contains any ``NaN`` values.

Expand Down
12 changes: 11 additions & 1 deletion src/StatsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ module StatsBase

import Base: length, isempty, eltype, values, sum, mean, mean!, show, quantile
import Base: rand, rand!
import Base: Func, evaluate, IdFun, Abs2Fun
import Base: Func, IdFun, Abs2Fun
import Base.LinAlg: BlasReal, BlasFloat
import Base.Cartesian: @ngenerate, @nloops, @nref, @nextract

## tackle compatibility issues

if VERSION < v"0.4.0-dev+1258"
import Base: evaluate
else
evaluate(f::Func{1}, x) = call(f, x)
evaluate(f::Func{2}, x, y) = call(f, x, y)
end


export

# reexport from ArrayViews
Expand Down
3 changes: 1 addition & 2 deletions src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ function indicatormat(x::AbstractArray, c::AbstractArray; sparse::Bool=false)
sparse ? _indicatormat_sparse(x, c) : _indicatormat_dense(x, c)
end

indicatormat{T<:Union(Real,String)}(x::AbstractArray{T}; sparse::Bool=false) = indicatormat(x, sort!(unique(x)); sparse=sparse)
indicatormat(x::AbstractArray; sparse::Bool=false) = indicatormat(x, unique(x); sparse=sparse)
indicatormat(x::AbstractArray; sparse::Bool=false) = indicatormat(x, sort!(unique(x)); sparse=sparse)


function _indicatormat_dense(x::IntegerArray, k::Integer)
Expand Down
6 changes: 3 additions & 3 deletions src/rankcorr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ end

corkendall(x::RealVector, y::RealVector) = corkendall!(float(copy(x)), float(copy(y)))

corkendall(X::RealMatrix, y::RealVector) = [corkendall!(float(X[:,i]), float(copy(y))) for i in 1:size(X, 2)]
corkendall(X::RealMatrix, y::RealVector) = Float64[corkendall!(float(X[:,i]), float(copy(y))) for i in 1:size(X, 2)]

corkendall(x::RealVector, Y::RealMatrix) = (n = size(Y,2); reshape([corkendall!(float(copy(x)), float(Y[:,i])) for i in 1:n], 1, n))
corkendall(x::RealVector, Y::RealMatrix) = (n = size(Y,2); reshape(Float64[corkendall!(float(copy(x)), float(Y[:,i])) for i in 1:n], 1, n))

corkendall(X::RealMatrix, Y::RealMatrix) = [corkendall!(float(X[:,i]), float(Y[:,j])) for i in 1:size(X, 2), j in 1:size(Y, 2)]
corkendall(X::RealMatrix, Y::RealMatrix) = Float64[corkendall!(float(X[:,i]), float(Y[:,j])) for i in 1:size(X, 2), j in 1:size(Y, 2)]

function corkendall(X::RealMatrix)
n = size(X, 2)
Expand Down
14 changes: 5 additions & 9 deletions src/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,16 @@ variation{T<:Real}(x::AbstractArray{T}) = variation(x, mean(x))
sem{T<:Real}(a::AbstractArray{T}) = sqrt(var(a) / length(a))

# Median absolute deviation
mad{T<:Real}(v::AbstractArray{T}, center::Real) = 1.4826 * median!(abs(v .- center))
mad{T<:Real}(v::AbstractArray{T}, args...;arg...) = mad!(copy(v), args...;arg...)
mad{T<:Real}(v::Range{T}, args...;arg...) = mad!([v], args...;arg...)

function mad!{T<:Real}(v::AbstractArray{T}, center::Real)
function mad!{T<:Real}(v::AbstractArray{T}, center::Real=median!(v); constant::Real=1.4826)
for i in 1:length(v)
v[i] = abs(v[i]-center)
@inbounds v[i] = abs(v[i]-center)
end
1.4826 * median!(v)
constant * median!(v)
end

mad!{T<:Real}(v::AbstractArray{T}) = mad!(v, median!(v))

mad{T<:Real}(v::AbstractArray{T}) = mad!(copy(v))
mad{T<:Real}(v::Range{T}) = mad!([v])

# Interquartile range
iqr{T<:Real}(v::AbstractArray{T}) = (q = quantile(v, [.25, .75]); q[2] - q[1])

Expand Down
2 changes: 1 addition & 1 deletion test/rankcorr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using StatsBase
using Base.Test

X = [1 0; 2 1; 3 0; 4 1; 5 10]
X = Float64[1 0; 2 1; 3 0; 4 1; 5 10]

x1 = X[:,1]
x2 = X[:,2]
Expand Down
2 changes: 2 additions & 0 deletions test/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ z2 = [8. 2. 3. 1.; 24. 10. -1. -1.; 20. 12. 1. -2.]
@test_approx_eq mad([1:5], 3) 1.4826
@test_approx_eq mad([1:5]) 1.4826
@test_approx_eq mad(1:5) 1.4826
@test_approx_eq mad([1:5], constant=1.0) 1.0
@test_approx_eq mad([1:5], 3, constant=1.0) 1.0

@test_approx_eq iqr(1:5) 2.0

Expand Down

0 comments on commit 352c64f

Please sign in to comment.