Skip to content

Commit 850d3e6

Browse files
committed
empirical.jl and hist.jl
1 parent 3f29a9c commit 850d3e6

File tree

9 files changed

+96
-56
lines changed

9 files changed

+96
-56
lines changed

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ makedocs(
1313
"scalarstats.md",
1414
"cov.md",
1515
"robust.md",
16-
"ranking.md"]
16+
"ranking.md",
17+
"empirical.md"]
1718
)
1819

1920
deploydocs(

docs/src/empirical.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Empirical Estimation of Distributions
2+
3+
## Histograms
4+
5+
The `Histogram` type represents data that has been tabulated into intervals
6+
(known as *bins*) along the real line, or in higher dimensions, over the real
7+
plane.
8+
9+
Histograms can be fitted to data using the `fit` method.
10+
11+
```@docs
12+
fit(::Type{Histogram}, args...; kwargs...)
13+
```
14+
15+
Additional methods
16+
```@docs
17+
merge!
18+
merge
19+
midpoints
20+
norm
21+
normalize
22+
normalize!
23+
zero
24+
```
25+
26+
## Empirical Cumulative Distribution Function
27+
28+
```@docs
29+
ecdf
30+
```

docs/src/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Statistics can be weighted, and several weights types are distinguished to apply
1010
corrections where necessary.
1111

1212
```@contents
13-
Pages = ["weights.md", "scalarstats.md", "cov.md", "robust.md", "ranking.jl"]
13+
Pages = ["weights.md", "scalarstats.md", "cov.md", "robust.md", "ranking.jl",
14+
"empirical.md"]
1415
Depth = 2
1516
```

src/Statistics.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ export std, stdm, var, varm, mean!, mean,
3535
trim, trim!, trimvar, winsor, winsor!,
3636
# ranking.jl
3737
ordinalrank, competerank, denserank, tiedrank,
38-
# rankcorr
39-
corkendall, corspearman
38+
# rankcorr.jl
39+
corkendall, corspearman,
40+
# empirical.jl
41+
ecdf, ECDF,
42+
# hist.jl
43+
fit, AbstractHistogram, Histogram, midpoints, norm, normalize, normalize!
4044

4145
include("common.jl")
4246
include("weights.jl")
@@ -50,6 +54,8 @@ include("signalcorr.jl")
5054
include("robust.jl")
5155
include("ranking.jl")
5256
include("rankcorr.jl")
57+
include("empirical.jl")
58+
include("hist.jl")
5359

5460
##### mean #####
5561

src/empirical.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ function is inside the interval ``(0,1)``; the function is defined for the whole
4949
"""
5050
ecdf(X::RealVector{T}) where T<:Real = ECDF(sort(X))
5151

52-
minimum(ecdf::ECDF) = first(ecdf.sorted_values)
52+
Base.minimum(ecdf::ECDF) = first(ecdf.sorted_values)
5353

54-
maximum(ecdf::ECDF) = last(ecdf.sorted_values)
54+
Base.maximum(ecdf::ECDF) = last(ecdf.sorted_values)
5555

56-
extrema(ecdf::ECDF) = (minimum(ecdf), maximum(ecdf))
56+
Base.extrema(ecdf::ECDF) = (minimum(ecdf), maximum(ecdf))

src/hist.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function histrange(lo::F, hi::F, n::Integer, closed::Symbol=:left) where F
9696
len += one(F)
9797
end
9898
end
99-
Base.floatrange(start,step,len,divisor)
99+
Base.floatrange(start,step,Int(len),divisor)
100100
end
101101

102102
histrange(vs::NTuple{N,AbstractVector},nbins::NTuple{N,Integer},closed::Symbol) where {N} =
@@ -397,7 +397,7 @@ arrays appropriately. See description of `normalize` for details. Returns `h`.
397397
if mode == :pdf || mode == :density
398398
# Divide weights by bin volume, for :pdf also divide by sum of weights
399399
SumT = norm_type(h)
400-
vs_0 = (mode == :pdf) ? sum(SumT(x) for x in weights) : one(SumT)
400+
vs_0 = (mode == :pdf) ? sum(SumT, weights) : one(SumT)
401401
@inbounds @nloops $N i weights d->(vs_{$N-d+1} = vs_{$N-d} * _edge_binvolume(SumT, edges[d], i_d)) begin
402402
(@nref $N weights i) /= $(Symbol("vs_$N"))
403403
for A in aux_weights

test/empirical.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using StatsBase
1+
using Statistics
22
using Test
33

44
@testset "ECDF" begin

test/hist.jl

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using StatsBase
1+
using Statistics
22
using LinearAlgebra, Random, Test
33

4-
@testset "StatsBase.Histogram" begin
4+
@testset "Histogram" begin
55

66

77
@testset "Histogram binindex and binvolume" begin
@@ -14,15 +14,15 @@ using LinearAlgebra, Random, Test
1414

1515
@test h1 == Histogram(edg1, :left, false)
1616

17-
@test @inferred StatsBase.binindex(h1, -0.5) == 4
18-
@test @inferred StatsBase.binindex(h2, (1.5, 2)) == (8, 3)
17+
@test @inferred Statistics.binindex(h1, -0.5) == 4
18+
@test @inferred Statistics.binindex(h2, (1.5, 2)) == (8, 3)
1919

20-
@test [StatsBase.binvolume(h1, i) for i in axes(h1.weights, 1)] diff(edg1)
21-
@test [StatsBase.binvolume(h2, (i,j)) for i in axes(h2.weights, 1), j in axes(h2.weights, 2)] diff(edg1) * diff(edg2)'
20+
@test [Statistics.binvolume(h1, i) for i in axes(h1.weights, 1)] diff(edg1)
21+
@test [Statistics.binvolume(h2, (i,j)) for i in axes(h2.weights, 1), j in axes(h2.weights, 2)] diff(edg1) * diff(edg2)'
2222

23-
@test typeof(@inferred(StatsBase.binvolume(h2, (1,1)))) == Float64
24-
@test typeof(@inferred(StatsBase.binvolume(h3, (1,1)))) == Float32
25-
@test typeof(@inferred(StatsBase.binvolume(Float64, h3, (1,1)))) == Float64
23+
@test typeof(@inferred(Statistics.binvolume(h2, (1,1)))) == Float64
24+
@test typeof(@inferred(Statistics.binvolume(h3, (1,1)))) == Float32
25+
@test typeof(@inferred(Statistics.binvolume(Float64, h3, (1,1)))) == Float64
2626
end
2727

2828

@@ -75,44 +75,44 @@ end
7575

7676
@testset "histrange" begin
7777
# Note: atm histrange must be qualified
78-
@test @inferred(StatsBase.histrange(Float64[], 0, :left)) == 0.0:1.0:0.0
79-
@test StatsBase.histrange(Float64[1:5;], 1, :left) == 0.0:5.0:10.0
80-
@test StatsBase.histrange(Float64[1:10;], 1, :left) == 0.0:10.0:20.0
81-
@test StatsBase.histrange(1.0, 10.0, 1, :left) == 0.0:10.0:20.0
82-
83-
@test StatsBase.histrange([0.201,0.299], 10, :left) == 0.2:0.01:0.3
84-
@test StatsBase.histrange([0.2,0.299], 10, :left) == 0.2:0.01:0.3
85-
@test StatsBase.histrange([0.2,0.3], 10, :left) == 0.2:0.01:0.31
86-
@test StatsBase.histrange(0.2, 0.3, 10, :left) == 0.2:0.01:0.31
87-
@test StatsBase.histrange([0.2,0.3], 10, :right) == 0.19:0.01:0.3
88-
@test StatsBase.histrange(0.2, 0.3, 10, :right) == 0.19:0.01:0.3
89-
90-
@test StatsBase.histrange([200.1,299.9], 10, :left) == 200.0:10.0:300.0
91-
@test StatsBase.histrange([200.0,299.9], 10, :left) == 200.0:10.0:300.0
92-
@test StatsBase.histrange([200.0,300.0], 10, :left) == 200.0:10.0:310.0
93-
@test StatsBase.histrange([200.0,300.0], 10, :right) == 190.0:10.0:300.0
94-
95-
@test @inferred(StatsBase.histrange(Int64[1:5;], 1, :left)) == 0:5:10
96-
@test StatsBase.histrange(Int64[1:10;], 1, :left) == 0:10:20
97-
98-
@test StatsBase.histrange([0, 1, 2, 3], 4, :left) == 0.0:1.0:4.0
99-
@test StatsBase.histrange([0, 1, 1, 3], 4, :left) == 0.0:1.0:4.0
100-
@test StatsBase.histrange([0, 9], 4, :left) == 0.0:5.0:10.0
101-
@test StatsBase.histrange([0, 19], 4, :left) == 0.0:5.0:20.0
102-
@test StatsBase.histrange([0, 599], 4, :left) == 0.0:200.0:600.0
103-
@test StatsBase.histrange([-1, -1000], 4, :left) == -1000.0:500.0:0.0
78+
@test @inferred(Statistics.histrange(Float64[], 0, :left)) == 0.0:1.0:0.0
79+
@test Statistics.histrange(Float64[1:5;], 1, :left) == 0.0:5.0:10.0
80+
@test Statistics.histrange(Float64[1:10;], 1, :left) == 0.0:10.0:20.0
81+
@test Statistics.histrange(1.0, 10.0, 1, :left) == 0.0:10.0:20.0
82+
83+
@test Statistics.histrange([0.201,0.299], 10, :left) == 0.2:0.01:0.3
84+
@test Statistics.histrange([0.2,0.299], 10, :left) == 0.2:0.01:0.3
85+
@test Statistics.histrange([0.2,0.3], 10, :left) == 0.2:0.01:0.31
86+
@test Statistics.histrange(0.2, 0.3, 10, :left) == 0.2:0.01:0.31
87+
@test Statistics.histrange([0.2,0.3], 10, :right) == 0.19:0.01:0.3
88+
@test Statistics.histrange(0.2, 0.3, 10, :right) == 0.19:0.01:0.3
89+
90+
@test Statistics.histrange([200.1,299.9], 10, :left) == 200.0:10.0:300.0
91+
@test Statistics.histrange([200.0,299.9], 10, :left) == 200.0:10.0:300.0
92+
@test Statistics.histrange([200.0,300.0], 10, :left) == 200.0:10.0:310.0
93+
@test Statistics.histrange([200.0,300.0], 10, :right) == 190.0:10.0:300.0
94+
95+
@test @inferred(Statistics.histrange(Int64[1:5;], 1, :left)) == 0:5:10
96+
@test Statistics.histrange(Int64[1:10;], 1, :left) == 0:10:20
97+
98+
@test Statistics.histrange([0, 1, 2, 3], 4, :left) == 0.0:1.0:4.0
99+
@test Statistics.histrange([0, 1, 1, 3], 4, :left) == 0.0:1.0:4.0
100+
@test Statistics.histrange([0, 9], 4, :left) == 0.0:5.0:10.0
101+
@test Statistics.histrange([0, 19], 4, :left) == 0.0:5.0:20.0
102+
@test Statistics.histrange([0, 599], 4, :left) == 0.0:200.0:600.0
103+
@test Statistics.histrange([-1, -1000], 4, :left) == -1000.0:500.0:0.0
104104

105105
# Base issue #13326
106-
l,h = extrema(StatsBase.histrange([typemin(Int),typemax(Int)], 10, :left))
106+
l,h = extrema(Statistics.histrange([typemin(Int),typemax(Int)], 10, :left))
107107
@test l <= typemin(Int)
108108
@test h >= typemax(Int)
109109

110-
@test_throws ArgumentError StatsBase.histrange([1, 10], 0, :left)
111-
@test_throws ArgumentError StatsBase.histrange([1, 10], -1, :left)
112-
@test_throws ArgumentError StatsBase.histrange([1.0, 10.0], 0, :left)
113-
@test_throws ArgumentError StatsBase.histrange([1.0, 10.0], -1, :left)
114-
@test_throws ArgumentError StatsBase.histrange(Float64[],-1, :left)
115-
@test_throws ArgumentError StatsBase.histrange([0.], 0, :left)
110+
@test_throws ArgumentError Statistics.histrange([1, 10], 0, :left)
111+
@test_throws ArgumentError Statistics.histrange([1, 10], -1, :left)
112+
@test_throws ArgumentError Statistics.histrange([1.0, 10.0], 0, :left)
113+
@test_throws ArgumentError Statistics.histrange([1.0, 10.0], -1, :left)
114+
@test_throws ArgumentError Statistics.histrange(Float64[],-1, :left)
115+
@test_throws ArgumentError Statistics.histrange([0.], 0, :left)
116116
end
117117

118118

@@ -220,8 +220,8 @@ end
220220
end
221221

222222
@testset "midpoints" begin
223-
@test StatsBase.midpoints([1, 2, 4]) == [1.5, 3.0]
224-
@test StatsBase.midpoints(range(0, stop = 1, length = 5)) == 0.125:0.25:0.875
223+
@test Statistics.midpoints([1, 2, 4]) == [1.5, 3.0]
224+
@test Statistics.midpoints(range(0, stop = 1, length = 5)) == 0.125:0.25:0.875
225225
end
226226

227-
end # @testset "StatsBase.Histogram"
227+
end # @testset "Statistics.Histogram"

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,4 +896,6 @@ include("partialcor.jl")
896896
include("signalcorr.jl")
897897
include("robust.jl")
898898
include("ranking.jl")
899-
include("rankcorr.jl")
899+
include("rankcorr.jl")
900+
include("empirical.jl")
901+
include("hist.jl")

0 commit comments

Comments
 (0)