Skip to content

Commit

Permalink
move base/statistics.jl to Statistics stdlib
Browse files Browse the repository at this point in the history
fix #27374
  • Loading branch information
fredrikekre committed Jun 28, 2018
1 parent c88dbcc commit ca5cd12
Show file tree
Hide file tree
Showing 28 changed files with 366 additions and 358 deletions.
19 changes: 3 additions & 16 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,6 @@ deprecate(Base, :DSP, 2)
using .DSP
export conv, conv2, deconv, filt, filt!, xcorr

# PR #21709
@deprecate cov(x::AbstractVector, corrected::Bool) cov(x, corrected=corrected)
@deprecate cov(x::AbstractMatrix, vardim::Int, corrected::Bool) cov(x, dims=vardim, corrected=corrected)
@deprecate cov(X::AbstractVector, Y::AbstractVector, corrected::Bool) cov(X, Y, corrected=corrected)
@deprecate cov(X::AbstractVecOrMat, Y::AbstractVecOrMat, vardim::Int, corrected::Bool) cov(X, Y, dims=vardim, corrected=corrected)

# PR #22325
# TODO: when this replace is removed from deprecated.jl:
# 1) rename the function replace_new from strings/util.jl to replace
Expand Down Expand Up @@ -1372,16 +1366,6 @@ export readandwrite
@deprecate findmax(A::AbstractArray, dims) findmax(A, dims=dims)
@deprecate findmin(A::AbstractArray, dims) findmin(A, dims=dims)

@deprecate mean(A::AbstractArray, dims) mean(A, dims=dims)
@deprecate varm(A::AbstractArray, m::AbstractArray, dims; kwargs...) varm(A, m; kwargs..., dims=dims)
@deprecate var(A::AbstractArray, dims; kwargs...) var(A; kwargs..., dims=dims)
@deprecate std(A::AbstractArray, dims; kwargs...) std(A; kwargs..., dims=dims)
@deprecate cov(X::AbstractMatrix, dim::Int; kwargs...) cov(X; kwargs..., dims=dim)
@deprecate cov(x::AbstractVecOrMat, y::AbstractVecOrMat, dim::Int; kwargs...) cov(x, y; kwargs..., dims=dim)
@deprecate cor(X::AbstractMatrix, dim::Int) cor(X, dims=dim)
@deprecate cor(x::AbstractVecOrMat, y::AbstractVecOrMat, dim::Int) cor(x, y, dims=dim)
@deprecate median(A::AbstractArray, dims; kwargs...) median(A; kwargs..., dims=dims)

@deprecate mapreducedim(f, op, A::AbstractArray, dims) mapreduce(f, op, A, dims=dims)
@deprecate mapreducedim(f, op, A::AbstractArray, dims, v0) mapreduce(f, op, v0, A, dims=dims)
@deprecate reducedim(op, A::AbstractArray, dims) reduce(op, A, dims=dims)
Expand Down Expand Up @@ -1703,6 +1687,9 @@ end
# PR #25168
@deprecate ipermute!(a, p::AbstractVector) invpermute!(a, p)

# #27140, #27152
@deprecate_moved linreg "StatsBase"

# ?? more special functions to SpecialFunctions.jl
@deprecate_moved gamma "SpecialFunctions"
@deprecate_moved lgamma "SpecialFunctions"
Expand Down
15 changes: 0 additions & 15 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -626,21 +626,6 @@ export
get_zero_subnormals,
set_zero_subnormals,

# statistics
cor,
cov,
mean!,
mean,
median!,
median,
middle,
quantile!,
quantile,
std,
stdm,
var,
varm,

# iteration
done,
next,
Expand Down
7 changes: 0 additions & 7 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -847,13 +847,6 @@ function sum(r::AbstractRange{<:Real})
: (step(r) * l) * ((l-1)>>1))
end

function mean(r::AbstractRange{<:Real})
isempty(r) && throw(ArgumentError("mean of an empty range is undefined"))
(first(r) + last(r)) / 2
end

median(r::AbstractRange{<:Real}) = mean(r)

function _in_range(x, r::AbstractRange)
if step(r) == 0
return !isempty(r) && first(r) == x
Expand Down
19 changes: 15 additions & 4 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,6 @@ using .StackTraces

include("initdefs.jl")

# statistics
include("statistics.jl")

# worker threads
include("threadcall.jl")

Expand Down Expand Up @@ -525,6 +522,7 @@ let
:Pkg,
:Test,
:REPL,
:Statistics,
]

maxlen = maximum(textwidth.(string.(stdlibs)))
Expand Down Expand Up @@ -746,7 +744,6 @@ end
# @deprecate_stdlib kron LinearAlgebra true
@deprecate_stdlib ldltfact LinearAlgebra true
@deprecate_stdlib ldltfact! LinearAlgebra true
@deprecate_stdlib linreg LinearAlgebra true
@deprecate_stdlib logabsdet LinearAlgebra true
@deprecate_stdlib logdet LinearAlgebra true
@deprecate_stdlib lu LinearAlgebra true
Expand Down Expand Up @@ -896,6 +893,20 @@ end
@deprecate_stdlib send Sockets true
@deprecate_stdlib TCPSocket Sockets true
@deprecate_stdlib UDPSocket Sockets true

@deprecate_stdlib cor Statistics true
@deprecate_stdlib cov Statistics true
@deprecate_stdlib std Statistics true
@deprecate_stdlib stdm Statistics true
@deprecate_stdlib var Statistics true
@deprecate_stdlib varm Statistics true
@deprecate_stdlib mean! Statistics true
@deprecate_stdlib mean Statistics true
@deprecate_stdlib median! Statistics true
@deprecate_stdlib median Statistics true
@deprecate_stdlib middle Statistics true
@deprecate_stdlib quantile! Statistics true
@deprecate_stdlib quantile Statistics true
end
end

Expand Down
18 changes: 0 additions & 18 deletions doc/src/base/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,3 @@ Base.widemul
Base.Math.@evalpoly
Base.FastMath.@fastmath
```

## Statistics

```@docs
Base.mean
Base.mean!
Base.std
Base.stdm
Base.var
Base.varm
Base.middle
Base.median
Base.median!
Base.quantile
Base.quantile!
Base.cov
Base.cor
```
11 changes: 7 additions & 4 deletions doc/src/manual/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ julia> struct Squares
end
julia> Base.iterate(S::Squares, state=1) = state > S.count ? nothing : (state*state, state+1)
```

With only [`iterate`](@ref) definition, the `Squares` type is already pretty powerful.
Expand All @@ -84,12 +83,16 @@ julia> for i in Squares(7)
49
```

We can use many of the builtin methods that work with iterables, like [`in`](@ref), [`mean`](@ref) and [`std`](@ref):
We can use many of the builtin methods that work with iterables,
like [`in`](@ref), or [`mean`](@ref) and [`std`](@ref) from the
`Statistics` standard library module:

```jldoctest squaretype
julia> 25 in Squares(10)
true
julia> using Statistics
julia> mean(Squares(100))
3383.5
Expand Down Expand Up @@ -388,8 +391,8 @@ julia> A[SquaresVector(3)]
4.0
9.0
julia> mean(A)
5.0
julia> sum(A)
45.0
```

If you are defining an array type that allows non-traditional indexing (indices that start at
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/missing.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ julia> sum(skipmissing([1, missing]))

This convenience function returns an iterator which filters out `missing` values
efficiently. It can therefore be used with any function which supports iterators
```jldoctest
```jldoctest; setup = :(using Statistics)
julia> maximum(skipmissing([3, missing, 2, 1]))
3
Expand Down
1 change: 0 additions & 1 deletion stdlib/LinearAlgebra/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ Base.inv(::AbstractMatrix)
LinearAlgebra.pinv
LinearAlgebra.nullspace
Base.kron
LinearAlgebra.linreg
LinearAlgebra.exp(::StridedMatrix{<:LinearAlgebra.BlasFloat})
LinearAlgebra.log(::StridedMatrix)
LinearAlgebra.sqrt(::StridedMatrix{<:Real})
Expand Down
1 change: 0 additions & 1 deletion stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export
ldiv!,
ldlt!,
ldlt,
linreg,
logabsdet,
logdet,
lowrankdowndate,
Expand Down
40 changes: 0 additions & 40 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1143,46 +1143,6 @@ isdiag(A::AbstractMatrix) = isbanded(A, 0, 0)
isdiag(x::Number) = true


"""
linreg(x, y)
Perform simple linear regression using Ordinary Least Squares. Returns `a` and `b` such
that `a + b*x` is the closest straight line to the given points `(x, y)`, i.e., such that
the squared error between `y` and `a + b*x` is minimized.
# Examples
```julia
using PyPlot
x = 1.0:12.0
y = [5.5, 6.3, 7.6, 8.8, 10.9, 11.79, 13.48, 15.02, 17.77, 20.81, 22.0, 22.99]
a, b = linreg(x, y) # Linear regression
plot(x, y, "o") # Plot (x, y) points
plot(x, a + b*x) # Plot line determined by linear regression
```
See also:
`\\`, [`cov`](@ref), [`std`](@ref), [`mean`](@ref).
"""
function linreg(x::AbstractVector, y::AbstractVector)
# Least squares given
# Y = a + b*X
# where
# b = cov(X, Y)/var(X)
# a = mean(Y) - b*mean(X)
if size(x) != size(y)
throw(DimensionMismatch("x has size $(size(x)) and y has size $(size(y)), " *
"but these must be the same size"))
end
mx = mean(x)
my = mean(y)
# don't need to worry about the scaling (n vs n - 1) since they cancel in the ratio
b = Base.covm(x, mx, y, my)/Base.varm(x, mx)
a = my - b*mx
return (a, b)
end

# BLAS-like in-place y = x*α+y function (see also the version in blas.jl
# for BlasFloat Arrays)
function axpy!(α, x::AbstractArray, y::AbstractArray)
Expand Down
47 changes: 0 additions & 47 deletions stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,53 +89,6 @@ n = 5 # should be odd
end
end

@testset "linrange" begin
# make sure unequal input arrays throw an error
x = [2; 5; 6]
y = [3; 7; 10; 10]
@test_throws DimensionMismatch linreg(x, y)
x = [2 5 6]
y = [3; 7; 10]
@test_throws MethodError linreg(x, y)

# check (UnitRange, Array)
x = 1:12
y = [5.5; 6.3; 7.6; 8.8; 10.9; 11.79; 13.48; 15.02; 17.77; 20.81; 22.0; 22.99]
@test [linreg(x,y)...] [2.5559090909090867, 1.6960139860139862]
@test [linreg(view(x,1:6),view(y,1:6))...] [3.8366666666666642,1.3271428571428574]

# check (LinRange, UnitRange)
x = range(1.0, stop=12.0, length=100)
y = -100:-1
@test [linreg(x, y)...] [-109.0, 9.0]

# check (UnitRange, UnitRange)
x = 1:12
y = 12:-1:1
@test [linreg(x, y)...] [13.0, -1.0]

# check (LinRange, LinRange)
x = range(-5, stop=10, length=100)
y = range(50, stop=200, length=100)
@test [linreg(x, y)...] [100.0, 10.0]

# check (Array, Array)
# Anscombe's quartet (https://en.wikipedia.org/wiki/Anscombe%27s_quartet)
x123 = [10.0; 8.0; 13.0; 9.0; 11.0; 14.0; 6.0; 4.0; 12.0; 7.0; 5.0]
y1 = [8.04; 6.95; 7.58; 8.81; 8.33; 9.96; 7.24; 4.26; 10.84; 4.82; 5.68]
@test [linreg(x123,y1)...] [3.0,0.5] atol=15e-5

y2 = [9.14; 8.14; 8.74; 8.77; 9.26; 8.10; 6.12; 3.10; 9.13; 7.26; 4.74]
@test [linreg(x123,y2)...] [3.0,0.5] atol=10e-3

y3 = [7.46; 6.77; 12.74; 7.11; 7.81; 8.84; 6.08; 5.39; 8.15; 6.42; 5.73]
@test [linreg(x123,y3)...] [3.0,0.5] atol=10e-3

x4 = [8.0; 8.0; 8.0; 8.0; 8.0; 8.0; 8.0; 19.0; 8.0; 8.0; 8.0]
y4 = [6.58; 5.76; 7.71; 8.84; 8.47; 7.04; 5.25; 12.50; 5.56; 7.91; 6.89]
@test [linreg(x4,y4)...] [3.0,0.5] atol=10e-3
end

@testset "diag" begin
A = Matrix(1.0I, 4, 4)
@test diag(A) == fill(1, 4)
Expand Down
10 changes: 5 additions & 5 deletions stdlib/Markdown/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,20 @@ end

ref(x) = Reference(x)

ref(mean)
ref(sum)

show(io::IO, m::MIME"text/plain", r::Reference) =
print(io, "$(r.ref) (see Julia docs)")

mean_ref = md"Behaves like $(ref(mean))"
@test plain(mean_ref) == "Behaves like mean (see Julia docs)\n"
@test html(mean_ref) == "<p>Behaves like mean &#40;see Julia docs&#41;</p>\n"
sum_ref = md"Behaves like $(ref(sum))"
@test plain(sum_ref) == "Behaves like sum (see Julia docs)\n"
@test html(sum_ref) == "<p>Behaves like sum &#40;see Julia docs&#41;</p>\n"

show(io::IO, m::MIME"text/html", r::Reference) =
Markdown.withtag(io, :a, :href=>"test") do
Markdown.htmlesc(io, Markdown.plaininline(r))
end
@test html(mean_ref) == "<p>Behaves like <a href=\"test\">mean &#40;see Julia docs&#41;</a></p>\n"
@test html(sum_ref) == "<p>Behaves like <a href=\"test\">sum &#40;see Julia docs&#41;</a></p>\n"

@test md"""
````julia
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Pkg/src/GraphType.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ function prune_graph!(graph::Graph)

# Done

log_event_global!(graph, "pruned graph — stats (n. of packages, mean connectivity): before = ($np,$(mean(spp))) after = ($new_np,$(mean(new_spp)))")
log_event_global!(graph, "pruned graph — stats (n. of packages, mean connectivity): before = ($np,$(sum(spp)/length(spp))) after = ($new_np,$(sum(new_spp)/length(new_spp)))")

# Replace old data with new
data.pkgs = new_pkgs
Expand Down
2 changes: 1 addition & 1 deletion stdlib/REPL/src/docview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ function fuzzyscore(needle, haystack)
score += (acro ? 2 : 1)*length(is) # Matched characters
score -= 2(length(needle)-length(is)) # Missing characters
!acro && (score -= avgdistance(is)/10) # Contiguous
!isempty(is) && (score -= mean(is)/100) # Closer to beginning
!isempty(is) && (score -= sum(is)/length(is)/100) # Closer to beginning
return score
end

Expand Down
Loading

0 comments on commit ca5cd12

Please sign in to comment.