Skip to content

Compute logistic and logistic_logjac together #42

Open

Description

@cscherrer

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

Activity

  1. tpapp commented on Apr 24, 2019

    @tpapp
    Owner

    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.

  2. cscherrer commented on Apr 24, 2019

    @cscherrer
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions