Skip to content

Add unnormalized PDF and log-likelihood functions #1978

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion src/Distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@ export
logdiffcdf, # log of difference between cdf at two values
logdetcov, # log-determinant of covariance
loglikelihood, # log probability of array of IID draws
logulikelihood, # unnormalized log probability of array of IID draws
logpdf, # log probability density
logpdf!, # evaluate log pdf to provided storage
logupdf, # unnormalized log probability density

invscale, # Inverse scale parameter
sqmahal, # squared Mahalanobis distance to Gaussian center
Expand Down Expand Up @@ -267,10 +269,11 @@ export
succprob, # the success probability
support, # the support of a distribution (or a distribution type)
truncated, # truncate a distribution with a lower and upper bound
updf, # unnormalized probability density
var, # variance of distribution
varlogx, # variance of log(x)
expected_logdet, # expected logarithm of random matrix determinant
gradlogpdf, # gradient (or derivative) of logpdf(d,x) wrt x
gradlogpdf, # gradient (or derivative) of logpdf(d,x) and logupdf(d,x) wrt x

# reexport from StatsBase
sample, sample!, # sample from a source array
Expand Down
46 changes: 43 additions & 3 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Instead of `pdf` one should implement `_pdf(d, x)` which does not have to check
`x`. However, since the default definition of `pdf(d, x)` falls back to `logpdf(d, x)`
usually it is sufficient to implement `logpdf`.

See also: [`logpdf`](@ref).
See also: [`logpdf`](@ref), [`updf`](@ref)
"""
@inline function pdf(
d::Distribution{ArrayLikeVariate{N}}, x::AbstractArray{<:Real,M}
Expand Down Expand Up @@ -243,6 +243,19 @@ function _pdf(d::Distribution{ArrayLikeVariate{N}}, x::AbstractArray{<:Real,N})
return exp(@inbounds logpdf(d, x))
end

"""
updf(d::Distribution, x)

Evaluate the unnormalized probability density function of `d` at `x`.

The unnormalized probability density function contains all terms in the probability density
function that are dependent on `x` and not necessarily constant terms or other terms
dependent on parameters of the distribution.

See also: [`pdf`](@ref), [`logupdf`](@ref)
"""
updf(d::Distribution, x) = pdf(d, x)

"""
logpdf(d::Distribution{ArrayLikeVariate{N}}, x::AbstractArray{<:Real,N}) where {N}

Expand All @@ -256,7 +269,7 @@ be disabled by using `@inbounds`.
Instead of `logpdf` one should implement `_logpdf(d, x)` which does not have to check the
size of `x`.

See also: [`pdf`](@ref), [`gradlogpdf`](@ref).
See also: [`pdf`](@ref), [`logupdf`](@ref), [`gradlogpdf`](@ref).
"""
@inline function logpdf(
d::Distribution{ArrayLikeVariate{N}}, x::AbstractArray{<:Real,M}
Expand All @@ -280,14 +293,27 @@ See also: [`pdf`](@ref), [`gradlogpdf`](@ref).
end
end

"""
logupdf(d::Distribution, x)

Evaluate the logarithm of the unnormalized probability density function of `d` at `x`.

The result is equivalent to `log(updf(d, x))` but may be more numerically stable.

See also: [`updf`](@ref), [`logpdf`](@ref), [`gradlogpdf`](@ref)
"""
logupdf(d::Distribution, x) = logpdf(d, x)

"""
gradlogpdf(d::Distribution, x)

Evaluate the gradient of the logarithm of the probability density function of `d` at `x`.

Note that this is the gradient of both [`logpdf`](@ref) and [`logupdf`](@ref).

For univariate distributions, return the derivative.

See also: [`logpdf`](@ref).
See also: [`logpdf`](@ref), [`logupdf`](@ref)
"""
function gradlogpdf end

Expand Down Expand Up @@ -481,6 +507,20 @@ Base.@propagate_inbounds function loglikelihood(
return sum(Base.Fix1(logpdf, d), x)
end

"""
logulikelihood(d::Distribution, x)

The unnormalized log-likelihood of distribution `d` with respect to all variate(s) contained
in `x`.

The unnormalized log-likelihood contains all terms in the log-likelihood that are dependent
on the parameters of the distribution and not necessarily constant terms or other terms
dependent on `x`.

See also: [`loglikelihood`](@ref)
"""
logulikelihood(d::Distribution, x) = loglikelihood(d, x)

## TODO: the following types need to be improved
abstract type SufficientStats end
abstract type IncompleteDistribution end
Expand Down
Loading