Skip to content

Commit

Permalink
Dirac: Implement location, scale, affine transformation
Browse files Browse the repository at this point in the history
closes #1731

* location implemented
* scale implemented (= one)
* shifting = :+ implemented

* Scaling = :*, errors

Widening the Dirac cannot be done in a type stable way.
Dirac can be interpreted as limit of some distributions
in the limit σ^2 -> 0.0 (e.g. Normal, Uniform, Cosine).
Falling back to one of these is arbitrarily and not
type stable. Therefore it's up to the user what to do
when recaling (= lowering certainty) needs to be done.

Adding two Dirac distributions is allowed. The sum
of Dirac distributed random variables is interpreted
as the sum of their values. This result can be obtained
by either interpretation as certain bare numbers or
by convolution.

resolves #1695

Type T is reduced to T<:Real to be consistent with other
distributions. Non-scalar values are no longer allowed
as arguments.
This is a **potentially breaking** change, if user code
relied on unintended behaviour.
  • Loading branch information
i9e1 committed Jun 6, 2023
1 parent 2dee35e commit 01acb8c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/univariate/discrete/dirac.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ External links:
* [Dirac measure on Wikipedia](http://en.wikipedia.org/wiki/Dirac_measure)
"""
struct Dirac{T} <: DiscreteUnivariateDistribution
struct Dirac{T<:Real} <: DiscreteUnivariateDistribution
value::T
end

Expand All @@ -29,6 +29,9 @@ minimum(d::Dirac) = d.value
maximum(d::Dirac) = d.value
support(d::Dirac) = (d.value,)

location(d::Dirac) = d.value
scale(d::Dirac{T}) where {T} = one(T)

#### Properties
mean(d::Dirac) = d.value
var(d::Dirac{T}) where {T} = zero(T)
Expand All @@ -53,6 +56,11 @@ mgf(d::Dirac, t) = exp(t * d.value)
cgf(d::Dirac, t) = t*d.value
cf(d::Dirac, t) = cis(t * d.value)

#### Affine transformation
Base.:+(d::Dirac, c::Real) = Dirac(d.value + c)
Base.:+(a::Dirac, b::Dirac) = Dirac(a.value + b.value)
Base.:*(c::Real, d::Dirac) = error("Rescaling of Dirac is prohibited.")

#### Sampling

rand(rng::AbstractRNG, d::Dirac) = d.value

0 comments on commit 01acb8c

Please sign in to comment.