Skip to content

Commit

Permalink
better NaN handling with ecdf (#537)
Browse files Browse the repository at this point in the history
* better NaN handling with ecdf

* ecdf tests
  • Loading branch information
joshday authored and andreasnoack committed Dec 3, 2019
1 parent 21abaee commit 93831cc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ SortingAlgorithms = "a2af1166-a08f-5f64-846c-94a0d3cef48c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
julia = "1"

[extras]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Dates", "DelimitedFiles", "Test"]

[compat]
julia = "1"
3 changes: 2 additions & 1 deletion src/empirical.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Empirical estimation of CDF and PDF


## Empirical CDF

struct ECDF{T <: AbstractVector{<:Real}, W <: AbstractWeights{<:Real}}
Expand All @@ -9,6 +8,7 @@ struct ECDF{T <: AbstractVector{<:Real}, W <: AbstractWeights{<:Real}}
end

function (ecdf::ECDF)(x::Real)
isnan(x) && return NaN
n = searchsortedlast(ecdf.sorted_values, x)
evenweights = isempty(ecdf.weights)
weightsum = evenweights ? length(ecdf.sorted_values) : sum(ecdf.weights)
Expand Down Expand Up @@ -54,6 +54,7 @@ evaluate CDF values on other samples.
function is inside the interval ``(0,1)``; the function is defined for the whole real line.
"""
function ecdf(X::RealVector; weights::AbstractVector{<:Real}=Weights(Float64[]))
any(isnan, X) && throw(ArgumentError("ecdf can not include NaN values"))
isempty(weights) || length(X) == length(weights) || throw(ArgumentError("data and weight vectors must be the same size," *
"got $(length(X)) and $(length(weights))"))
ord = sortperm(X)
Expand Down
2 changes: 2 additions & 0 deletions test/empirical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ using Test
fnecdf = ecdf([0.5])
@test fnecdf([zeros(5000); ones(5000)]) == [zeros(5000); ones(5000)]
@test extrema(fnecdf) == (minimum(fnecdf), maximum(fnecdf)) == (0.5, 0.5)
@test isnan(ecdf([1,2,3])(NaN))
@test_throws ArgumentError ecdf([1, NaN])
end

@testset "Weighted ECDF" begin
Expand Down

0 comments on commit 93831cc

Please sign in to comment.