Skip to content

Commit

Permalink
Address comments from review
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed May 6, 2021
1 parent c6ac970 commit df2cee1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/Distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export
params!, # provide storage space to calculate the tuple of parameters for a multivariate distribution like mvlognormal
partype, # returns a type large enough to hold all of a distribution's parameters' element types
pdf, # probability density function (ContinuousDistribution)
pdfL2norm, # L2 norm of the probability distribution function
probs, # Get the vector of probabilities
probval, # The pdf/pmf value for a uniform distribution
product_distribution, # product of univariate distributions
Expand Down
50 changes: 21 additions & 29 deletions src/pdfnorm.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export pdfL2norm

"""
pdfL2norm(d::Distribution)
Expand All @@ -13,46 +11,40 @@ where `S` is the support of `f(x)`.
"""
pdfL2norm

pdfL2norm(d::Distribution) = throw(ArgumentError("L2 norm not implemented for $(d)"))

function pdfL2norm(d::Beta{T}) where T
if d.α > 0.5 && d.β > 0.5
return beta(2 * d.α - 1, 2 * d.β - 1) / beta(d.α, d.β) ^ 2
else
return T(Inf)
end
function pdfL2norm(d::Beta)
α, β = params(d)
z = beta(2 * α - 1, 2 * β - 1) / beta(α, β) ^ 2
# L2 norm of the pdf converges only for α > 0.5 and β > 0.5
return α > 0.5 && β > 0.5 ? z : oftype(z, Inf)
end

pdfL2norm(d::Cauchy) = 1 / (d.σ * 2 * π)
pdfL2norm(d::Cauchy) = inv(twoπ * d.σ)

function pdfL2norm(d::Chi{T}) where {T}
if d.ν > 0.5
return (2 ^ (1 - d.ν) * gamma((2 * d.ν - 1) / 2)) / gamma(d.ν / 2) ^ 2
else
return T(Inf)
end
function pdfL2norm(d::Chi)
ν = d.ν
z = (2 ^ (1 - ν) * gamma((2 * ν - 1) / 2)) / gamma/ 2) ^ 2
# L2 norm of the pdf converges only for ν > 0.5
return d.ν > 0.5 ? z : oftype(z, Inf)
end

function pdfL2norm(d::Chisq{T}) where {T}
if d.ν > 1
return gamma(d.ν - 1) / (gamma(d.ν / 2) ^ 2 * 2 ^ d.ν)
else
return T(Inf)
end
function pdfL2norm(d::Chisq)
ν = d.ν
z = gamma(d.ν - 1) / (gamma(d.ν / 2) ^ 2 * 2 ^ d.ν)
# L2 norm of the pdf converges only for ν > 1
return ν > 1 ? z : oftype(z, Inf)
end

pdfL2norm(d::Exponential) = 1 / (2 * d.θ)

function pdfL2norm(d::Gamma{T}) where {T}
if d.α > 0.5
return (2^(1 - 2 * d.α) * gamma(2 * d.α - 1)) / (gamma(d.α) ^ 2 * d.θ)
else
return T(Inf)
end
α, θ = params(d)
z = (2^(1 - 2 * α) * gamma(2 * α - 1)) / (gamma(α) ^ 2 * θ)
# L2 norm of the pdf converges only for α > 0.5
return α > 0.5 ? z : oftype(z, Inf)
end

pdfL2norm(d::Logistic) = 1 / (6 * d.θ)

pdfL2norm(d::Normal{T}) where {T} = 1 / (2 * sqrt(T(π)) * d.σ)
pdfL2norm(d::Normal) = inv(sqrt4π * d.σ)

pdfL2norm(d::Uniform) = 1 / (d.b - d.a)
2 changes: 1 addition & 1 deletion test/pdfnorm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Test, Distributions, SpecialFunctions

@testset "pdf L2 norm" begin
# Test error on a non implemented norm.
@test_throws ArgumentError pdfL2norm(Gumbel())
@test_throws MethodError pdfL2norm(Gumbel())

@testset "Beta" begin
@test pdfL2norm(Beta(1, 1)) 1
Expand Down

0 comments on commit df2cee1

Please sign in to comment.