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

Implement Dirac distribution #861

Closed
wants to merge 3 commits 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: 1 addition & 0 deletions src/Distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export
Cosine,
DiagNormal,
DiagNormalCanon,
Dirac,
Dirichlet,
DirichletMultinomial,
DiscreteUniform,
Expand Down
30 changes: 30 additions & 0 deletions src/univariate/discrete/dirac.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Dirac(value)

A *Dirac distribution* is parametrized by its only value, and takes its value with probability 1.

```julia
d = Dirac(2.0) # Dirac distribution with value = 2.
rand(d) # Always returns the same value
```
"""
struct Dirac{T} <: DiscreteUnivariateDistribution
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dirac Delta is a Continuous distribution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't have a density with respect to the Lebesgue measure, so it is not a "continuous probability distribution" (not to mix with distributions in Schwartz' sense.)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the documentation ValueSupport can take values Discrete (Samples take discrete Int values) and Continuous (samples take continuous real Float64 values). So in this sense, it seems that the Dirac delta should be Continuous. I think that your interpretation would leave many useful measures completely out of the hierarchy (e.g. mixtures of continuous and discrete such as Spike-and-Slab)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is, according to the documentation the trait "Continuous" seems to refer only to the set on which the distribution is defined (the real line), not to the fact that it is absolutely continuous with respect to the Lebesgue measure.

value::T
end

eltype(::Dirac{T}) where {T} = T
rand(rng::AbstractRNG, d::Dirac) = d.value
pdf(d::Dirac, x::Real) = x == d.value ? 1.0 : 0.0
cdf(d::Dirac, x::Real) = x < d.value ? 0.0 : 1.0
quantile(d::Dirac{T}, p) where {T} = 0 <= p <= 1 ? d.value : T(NaN)
minimum(d::Dirac) = d.value
maximum(d::Dirac) = d.value
insupport(d::Dirac, x) = x == d.value
mean(d::Dirac) = d.value
var(d::Dirac) = 0.0
mode(d::Dirac) = d.value
skewness(d::Dirac) = 0.0
kurtosis(d::Dirac) = 0.0
entropy(d::Dirac) = 0.0
mgf(d::Dirac, t) = exp(t * d.value)
cf(d::Dirac, t) = exp(im * t * d.value)
1 change: 1 addition & 0 deletions src/univariates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ const discrete_distributions = [
"bernoulli",
"betabinomial",
"binomial",
"dirac",
"discreteuniform",
"discretenonparametric",
"categorical",
Expand Down
20 changes: 20 additions & 0 deletions test/dirac.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Distributions
using Test

@testset "Dirac tests" begin
for val in [3, 3.0]
d = Dirac(val)
@test minimum(d) == val
@test maximum(d) == val
@test pdf(d, val - 1) == 0
@test pdf(d, val) == 1
@test pdf(d, val + 1) == 0
@test cdf(d, val - 1) == 0
@test cdf(d, val) == 1
@test cdf(d, val + 1) == 1
@test quantile(d, 0) == val
@test quantile(d, 0.5) == val
@test quantile(d, 1) == val
@test rand(d) == val
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Random
using StatsBase

const tests = [
"dirac",
"truncate",
"truncnormal",
"truncated_exponential",
Expand Down