Skip to content

Commit

Permalink
Enable more use of bin edges in resampling functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brenhinkeller committed Oct 27, 2024
1 parent 7de3a31 commit eb32b99
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StatGeochem"
uuid = "df4de05a-b714-11e8-3c2a-c30fb13e804c"
authors = ["C. Brenhin Keller <[email protected]>"]
version = "0.8.1"
version = "0.8.2"

[deps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Expand Down
44 changes: 33 additions & 11 deletions src/utilities/Resampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@
See also `histcounts` for a more efficient implementation without variable bin width.
"""
bincounts(x::Collection, edges::AbstractRange; kwargs...) = bincounts(x,minimum(edges),maximum(edges),length(edges); kwargs...)
bincounts(x::Collection, edges::AbstractRange; kwargs...) = bincounts(x,minimum(edges),maximum(edges),length(edges)-1; kwargs...)
function bincounts(x::Collection, xmin::Number, xmax::Number, nbins::Integer;
relbinwidth::Number=1
)
Expand Down Expand Up @@ -486,7 +486,7 @@
(c,m,e) = binmeans(x, y, 0, 4000, 40)
```
"""
binmeans(x::Collection, y::Collection, edges::AbstractRange, args...; kwargs...) = binmeans(x,y,minimum(edges),maximum(edges),length(edges), args...; kwargs...)
binmeans(x::Collection, y::Collection, edges::AbstractRange, args...; kwargs...) = binmeans(x,y,minimum(edges),maximum(edges),length(edges)-1, args...; kwargs...)
function binmeans(x::Collection, y::Collection, xmin::Number, xmax::Number, nbins::Integer;
resamplingratio::Number=1,
relbinwidth::Number=1
Expand Down Expand Up @@ -533,10 +533,8 @@

"""
```julia
(c,m,e) = binmedians(x::AbstractArray, y::AbstractArray, min::Number, max::Number, nbins::Integer;
\tresamplingratio::Number=1
\trelbinwidth::Number=1
)
binmedians(x, y, xmin:step:xmax; resamplingratio=1 relbinwidth=1)
binmedians(x, y, xmin, xmax, nbins; resamplingratio=1 relbinwidth=1)
```
The medians (ignoring NaNs) of `y` values binned by `x`, into each of `nbins`
Expand All @@ -550,10 +548,11 @@
### Examples
```julia
(c,m,e) = binmedians(x, y, 0:100:4000)
(c,m,e) = binmedians(x, y, 0, 4000, 40)
```
"""
binmedians(x::Collection, y::Collection, edges::AbstractRange; kwargs...) = binmedians(x,y,minimum(edges),maximum(edges),length(edges); kwargs...)
binmedians(x::Collection, y::Collection, edges::AbstractRange; kwargs...) = binmedians(x,y,minimum(edges),maximum(edges),length(edges)-1; kwargs...)
function binmedians(x::Collection, y::Collection, min::Number, max::Number, nbins::Integer;
resamplingratio::Number=1,
relbinwidth::Number=1
Expand All @@ -580,6 +579,13 @@

"""
```julia
bin_bsr([f!::Function=nanbinmean!], x::Vector, y::VecOrMat, xmin:step:xmax, [w];
\tx_sigma = zeros(size(x)),
\ty_sigma = zeros(size(y)),
\tnresamplings = 1000,
\tsem = :sigma,
\tp = 0.2
)
bin_bsr([f!::Function=nanbinmean!], x::Vector, y::VecOrMat, xmin, xmax, nbins, [w];
\tx_sigma = zeros(size(x)),
\ty_sigma = zeros(size(y)),
Expand Down Expand Up @@ -632,7 +638,8 @@
(c,m,el,eu) = bin_bsr(nanbinmean!, x, y, 0, 4000, 40, x_sigma=0.05x, p=probability, sem=:pctile)
```
"""
function bin_bsr(f!::Function, x::AbstractVector, y::AbstractVector, xmin, xmax, nbins::Integer;
bin_bsr(f!::Function, x::AbstractVector, y::AbstractVecOrMat, edges::AbstractRange, args...; kwargs...) = bin_bsr(f!, x, y, minimum(edges),maximum(edges),length(edges)-1, args...; kwargs...)
function bin_bsr(f!::Function, x::AbstractVector, y::AbstractVector, xmin::Number, xmax::Number, nbins::Integer;
x_sigma = zeros(size(x)),
y_sigma = zeros(size(y)),
nresamplings = 1000,
Expand Down Expand Up @@ -675,7 +682,7 @@
return c, means
end
end
function bin_bsr(f!::Function, x::AbstractVector, y::AbstractMatrix, xmin, xmax, nbins::Integer;
function bin_bsr(f!::Function, x::AbstractVector, y::AbstractMatrix, xmin::Number, xmax::Number, nbins::Integer;
x_sigma = zeros(size(x)),
y_sigma = zeros(size(y)),
nresamplings = 1000,
Expand Down Expand Up @@ -726,7 +733,7 @@
return c, means
end
end
function bin_bsr(f!::Function, x::AbstractVector, y::AbstractVector, xmin, xmax, nbins::Integer, w::AbstractVector;
function bin_bsr(f!::Function, x::AbstractVector, y::AbstractVector, xmin::Number, xmax::Number, nbins::Integer, w::AbstractVector;
x_sigma = zeros(size(x)),
y_sigma = zeros(size(x)),
nresamplings = 1000,
Expand Down Expand Up @@ -781,7 +788,14 @@

"""
```julia
(c, m, el, eu) = bin_bsr_ratios([f!::Function=nanbinmean!], x::Vector, num::Vector, denom::Vector, xmin, xmax, nbins, [w];
bin_bsr_ratios([f!::Function=nanbinmean!], x::Vector, num::Vector, denom::Vector, xmin:step:xmax, [w];
\tx_sigma = zeros(size(x)),
\tnum_sigma = zeros(size(num)),
\tdenom_sigma = zeros(size(denom)),
\tnresamplings = 1000,
\tp::Union{Number,Vector} = 0.2
)
bin_bsr_ratios([f!::Function=nanbinmean!], x::Vector, num::Vector, denom::Vector, xmin, xmax, nbins, [w];
\tx_sigma = zeros(size(x)),
\tnum_sigma = zeros(size(num)),
\tdenom_sigma = zeros(size(denom)),
Expand All @@ -793,7 +807,15 @@
Returns the bincenters `c`, means `m`, as well as upper (`el`) and lower (`eu`) 95% CIs of the mean
for a ratio `num`/`den` binned by `x` into `nbins` equal bins between `xmin` and `xmax`,
after `nresamplings` boostrap resamplings with acceptance probability `p`.
### Examples
```julia
julia> (c, m, el, eu) = bin_bsr_ratios(nanbinmean!, x, num, denom, xmin:step:xmax; x_sigma=0.05x)
julia> (c, m, el, eu) = bin_bsr_ratios(nanbinmean!, x, num, denom, xmin, xmax, nbins; x_sigma=0.05x)
```
"""
bin_bsr_ratios(f!::Function, x::AbstractVector, num::AbstractVector, denom::AbstractVector, edges::AbstractRange, args...; kwargs...) = bin_bsr_ratios(f!, x, num, denom, minimum(edges), maximum(edges), length(edges)-1, args...; kwargs...)
function bin_bsr_ratios(f!::Function, x::AbstractVector, num::AbstractVector, denom::AbstractVector, xmin, xmax, nbins::Integer;
x_sigma::AbstractVector=zeros(size(x)),
num_sigma::AbstractVector=zeros(size(num)),
Expand Down
26 changes: 23 additions & 3 deletions test/testResampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
@test isapprox(mean(dbs[:y]), 11, atol=1)
@test isapprox(std(dbs[:y]), 6.06, atol=1)

@test bincounts(1:100, 0.0:11.11111111111111:100.0) == (5:10:95, fill(10,10))
@test bincounts(1:100, 0.0:10:100.0) == (5:10:95, fill(10,10))
@test bincounts(1:100, 0, 100, 10) == (5:10:95, fill(10,10))
c,N = bincounts(1:100, 0, 100, 10; relbinwidth=3)
@test c == 5:10:95
@test N == [20, 30, 30, 30, 30, 30, 30, 30, 30, 20]

@test binmeans(1:100, 1:100, 0.0:11.11111111111111:100.0) == (5:10:95, 5.5:10:95.5, fill(0.9574271077563381,10))
@test binmeans(1:100, 1:100, 0.0:10:100.0) == (5:10:95, 5.5:10:95.5, fill(0.9574271077563381,10))
@test binmeans(1:100, 1:100, 0, 100, 10) == (5:10:95, 5.5:10:95.5, fill(0.9574271077563381,10))
c,m,e = binmeans(1:100, 1:100, 0, 100, 10; relbinwidth=3)
@test c == 5:10:95
Expand All @@ -71,7 +71,7 @@
@test m == [10.5, 15.5, 25.5, 35.5, 45.5, 55.5, 65.5, 75.5, 85.5, 90.5]
@test e [1.3228756555322954, 1.607275126832159, 1.607275126832159, 1.607275126832159, 1.607275126832159, 1.607275126832159, 1.607275126832159, 1.607275126832159, 1.607275126832159, 1.3228756555322954]

@test binmedians(1:100,1:100,0.0:11.11111111111111:100.0) == (5:10:95, 5.5:10:95.5, fill(1.1720982147414096,10))
@test binmedians(1:100,1:100,0.0:10:100.0) == (5:10:95, 5.5:10:95.5, fill(1.1720982147414096,10))
@test binmedians(1:100,1:100,0,100,10) == (5:10:95, 5.5:10:95.5, fill(1.1720982147414096,10))
c,m,e = binmedians(1:100, 1:100, 0, 100, 10; relbinwidth=3)
@test c == 5:10:95
Expand Down Expand Up @@ -108,18 +108,31 @@

x = 0:100; y = 0:100
xmin = 0; xmax = 100; nbins = 5
step = (xmax-xmin)/nbins
(c,m,e) = bin_bsr(x, y, xmin, xmax, nbins, x_sigma=ones(101))
@test c == 10.0:20.0:90.0
@test isapprox(m, [10.04, 29.94, 49.94, 69.92, 89.83], atol=0.5)
@test isapprox(e, [1.17, 1.21, 1.23, 1.26, 1.28], atol=0.5)

(c,m,e) = bin_bsr(x, y, xmin:step:xmax, x_sigma=ones(101))
@test c == 10.0:20.0:90.0
@test isapprox(m, [10.04, 29.94, 49.94, 69.92, 89.83], atol=0.5)
@test isapprox(e, [1.17, 1.21, 1.23, 1.26, 1.28], atol=0.5)

# Upper and lower CIs
(c,m,el,eu) = bin_bsr(nanbinmean!, x, y, xmin, xmax, nbins, x_sigma=ones(101))
@test c == 10.0:20.0:90.0
@test isapprox(m, [10.04, 29.94, 49.94, 69.92, 89.83], atol=0.5)
@test isapprox(el, [2.29, 2.38, 2.41, 2.49, 2.51], atol=1.0)
@test isapprox(eu, [2.3, 2.37, 2.42, 2.51, 2.51], atol=1.0)

# Upper and lower CIs
(c,m,el,eu) = bin_bsr(nanbinmean!, x, y, xmin:step:xmax, x_sigma=ones(101))
@test c == 10.0:20.0:90.0
@test isapprox(m, [10.04, 29.94, 49.94, 69.92, 89.83], atol=0.5)
@test isapprox(el, [2.29, 2.38, 2.41, 2.49, 2.51], atol=1.0)
@test isapprox(eu, [2.3, 2.37, 2.42, 2.51, 2.51], atol=1.0)

# Medians, upper and lower CIs
(c,m,el,eu) = bin_bsr(nanbinmedian!, x, y, xmin, xmax, nbins, x_sigma=ones(101))
@test c == 10.0:20.0:90.0
Expand Down Expand Up @@ -153,12 +166,19 @@

x = 0:100; num = 0:100; denom=reverse(num)
xmin = 0; xmax = 100; nbins = 5
step = (xmax-xmin)/nbins
(c,m,el,eu) = bin_bsr_ratios(x, num, denom, xmin, xmax, nbins, x_sigma=ones(101))
@test c == 10.0:20.0:90.0
@test isapprox(m, [0.11, 0.43, 1.0, 2.33, 8.99], rtol=0.1)
@test isapprox(el, [0.03, 0.05, 0.09, 0.26, 2.11], rtol=0.4)
@test isapprox(eu, [0.03, 0.05, 0.1, 0.29, 3.03], rtol=0.4)

(c,m,el,eu) = bin_bsr_ratios(x, num, denom, xmin:step:xmax, x_sigma=ones(101))
@test c == 10.0:20.0:90.0
@test isapprox(m, [0.11, 0.43, 1.0, 2.33, 8.99], rtol=0.1)
@test isapprox(el, [0.03, 0.05, 0.09, 0.26, 2.11], rtol=0.4)
@test isapprox(eu, [0.03, 0.05, 0.1, 0.29, 3.03], rtol=0.4)

# With weights
(c,m,el,eu) = bin_bsr_ratios(x, num, denom, xmin, xmax, nbins, ones(101), x_sigma=ones(101))
@test c == 10.0:20.0:90.0
Expand Down

0 comments on commit eb32b99

Please sign in to comment.