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

Compute logistic and logistic_logjac together #42

Open
cscherrer opened this issue Apr 23, 2019 · 2 comments
Open

Compute logistic and logistic_logjac together #42

cscherrer opened this issue Apr 23, 2019 · 2 comments

Comments

@cscherrer
Copy link

I noticed this bit of code:

logistic(x::Real) = inv(one(x) + exp(-x))

function logistic_logjac(x::Real)
    mx = -abs(x)
    mx - 2*log1p(exp(mx))
end

There's a neat trick that can speed this up. If we write σ=logistic (for brevity) then
σ'=σ*(1-σ)

So something like

function logistic_with_logjac(x::Real)
    y = logistic(x)
    (y, log(y*(1-y)))
end
@tpapp
Copy link
Owner

tpapp commented Apr 24, 2019

I think there was an earlier version that did something like this, but I think the current solution is better numerically when y is close to 1 or 0. But perhaps investigating this (eg by comparing to a higher precision float, or BigFloat) and also benchmarking could be worthwhile.

@cscherrer
Copy link
Author

Good point, definitely worth checking the numerical behavior

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