-
Notifications
You must be signed in to change notification settings - Fork 423
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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) | ||
mschauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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 valuesDiscrete
(Samples take discreteInt
values) andContinuous
(samples take continuous realFloat64
values). So in this sense, it seems that the Dirac delta should beContinuous
. 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)There was a problem hiding this comment.
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.