Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: First take on cleaning up EmpiricalUnivariateDistribution #661

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ include("genericfit.jl")

# specific samplers and distributions
include("univariates.jl")
include("empirical.jl")
include("edgeworth.jl")
include("multivariates.jl")
include("matrixvariates.jl")
Expand Down
76 changes: 0 additions & 76 deletions src/empirical.jl

This file was deleted.

30 changes: 30 additions & 0 deletions src/univariate/discrete/empirical.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
struct EmpiricalUnivariateDistribution <: DiscreteUnivariateDistribution
values::Vector{Float64}
cdf::Function
end

@distr_support EmpiricalUnivariateDistribution d.values[1] d.values[end]

EmpiricalUnivariateDistribution(x::Vector) = EmpiricalUnivariateDistribution(sort(x), ecdf(x))

for f in (:entropy, :mean, :var, :skewness, :kurtosis)
@eval ($f)(d::EmpiricalUnivariateDistribution) = ($f)(d.values)
end

function median(d::DiscreteUnivariateDistribution)
v = d.values
n = length(v)
return (v[(n + 1) >> 1] + v[(n + 2) >> 1]) / 2
end

### Evaluation

cdf(d::EmpiricalUnivariateDistribution, x::Real) = d.cdf(x)

pdf(d::EmpiricalUnivariateDistribution, x::Real) = mean(t -> t == x, d.values)

quantile(d::EmpiricalUnivariateDistribution, p::Real) = quantile(d.values, p)

function rand(d::EmpiricalUnivariateDistribution)
d.values[rand(1:length(d.values))]
end
1 change: 1 addition & 0 deletions src/univariates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ const discrete_distributions = [
"binomial",
"categorical",
"discreteuniform",
"empirical",
"geometric",
"hypergeometric",
"negativebinomial",
Expand Down
28 changes: 28 additions & 0 deletions test/empirical.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Distributions, Base.Test

@testset "EmpiricalUnivariateDistribution" begin
n = 100
r = MersenneTwister(123)
@testset "$d data" for (d, x) in (("discrete" , rand( r, 1:10, n)),
("continuous", randn(r, n)))
X = EmpiricalUnivariateDistribution(x)

@testset "test function: $f" for f in (mean, var, std, skewness, kurtosis, median, entropy)
@test f(X) ≈ f(x)
end

ecdfx = StatsBase.ecdf(x)
@testset "cdf" for t in linspace(-10, 10, 100)
@test cdf(X, t) == ecdfx(t)
@test cdf(X, t) == mean(x -> x <= t, x)
end

@testset "quantile" for q in linspace(0, 1, 100)
@test quantile(X, q) == quantile(x, q)
end

@testset "pdf" begin
@test sum(t -> pdf(X, t), unique(x)) ≈ 1
end
end
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ tests = [
"gradlogpdf",
"truncate",
"noncentralt",
"locationscale"]
"locationscale",
"empirical"]

print_with_color(:blue, "Running tests:\n")

Expand Down