Skip to content

Adding unnormalized logpdf functions #185

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

Open
sethaxen opened this issue May 16, 2025 · 0 comments
Open

Adding unnormalized logpdf functions #185

sethaxen opened this issue May 16, 2025 · 0 comments

Comments

@sethaxen
Copy link
Contributor

JuliaStats/Distributions.jl#1978 will add logupdf and logulikelihood functions to the API: logupdf includes all terms in logpdf that depend on the argument (parameter-only terms are excluded), while logulikelihood includes all terms in logpdf that depend on the parameters (argument-only terms are excluded). This more efficient function evaluations and is especially useful when the omitted constants are expensive.

I propose StatsFuns gets a matching function like logupdf. e.g. if a function <distr>logpdf is implemented, so is a function <distr>logupdf. In some cases <distr>logpdf could just call the logupdf version and add a normalization factor.

Here are a few example implementations:

function gammalogupdf(k::T, θ::T, x::T) where {T <: Real}
    # we ensure that `log(x)` does not error if `x < 0`= max(x, 0) / θ
    val = -# xlogy(k - 1, xθ) - xθ -> -∞ for xθ -> ∞ so we only add the first term
    # when it's safe
    if isfinite(xθ)
        val += xlogy(k - 1, xθ)
    end
    return x < 0 ? oftype(val, -Inf) : val
end
function betalogupdf::T, β::T, x::T) where {T <: Real}
    # we ensure that `log(x)` and `log1p(-x)` do not error
    y = clamp(x, 0, 1)
    val = xlogy- 1, y) + xlog1py- 1, -y)
    return x < 0 || x > 1 ? oftype(val, -Inf) : val
end
normlogupdf(z::Number) = abs2(z) / -2
function normlogupdf::Real, σ::Real, x::Number)
    if iszero(σ) && x == μ
        z = zval(μ, one(σ), x)
    else
        z = zval(μ, σ, x)
    end
    return normlogupdf(z)
end
function poislogupdf::T, x::T) where {T <: Real}
    val = xlogy(x, λ) - loggamma(x + 1)
    return x >= 0 && isinteger(x) ? val : oftype(val, -Inf)
end
function tdistlogupdf::T, x::T) where {T <: Real}
    isinf(ν) && return normlogupdf(x)
    νp12 =+ 1) / 2
    return -νp12 * log1p(x^2 / ν)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant