Skip to content
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

Formulas for Stable Calculation of Log Prob #4

Open
TheAeryan opened this issue Jul 22, 2023 · 1 comment
Open

Formulas for Stable Calculation of Log Prob #4

TheAeryan opened this issue Jul 22, 2023 · 1 comment

Comments

@TheAeryan
Copy link

TheAeryan commented Jul 22, 2023

Hi,
For my current project I need a numerically-stable version of the truncated gaussian distribution in Python. I have been taking a look at the mathematical methods you detail in the file https://github.com/cossio/TruncatedNormal.jl/blob/23bfc7d0189ca6857e2e498006bbbed2a8b58be7/notes/normal.pdf for implementing the calculation of the mean and variance in a stable manner in Python.
Nonetheless, I also need a numerically stable method for calculating the log-probability of some value under the truncated gaussian (the f function you detail in the first paragraph of the PDF). Do you know how this can be done? I assume a similar method, based on Taylor expansions, could be used, but I'm not sure.

@cossio
Copy link
Owner

cossio commented Aug 5, 2023

Hi, maybe something like this can work:

function tnlogpdf(x::Real, a::Real, b::Real)
    result = -x^2/2 - lognormcdf(a, b)
    if x < a || x > b
        return oftype(result, -Inf)
    else
        return result - log(two(result) * pi) / 2
    end
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

2 participants