tpapp / TransformVariables.jl

Transformations to contrained variables from ℝⁿ.
Other
66 stars 14 forks source link

Compute logistic and logistic_logjac together #42

Open cscherrer opened 5 years ago

cscherrer commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Good point, definitely worth checking the numerical behavior