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

Fixing NaN sampling for von Mises Fisher distribution #1918

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/samplers/vonmisesfisher.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,20 @@ end
_vmf_genw(rng::AbstractRNG, s::VonMisesFisherSampler) =
_vmf_genw(rng, s.p, s.b, s.x0, s.c, s.κ)

function _vmf_householder_vec(μ::Vector{Float64})
function _vmf_householder_vec(μ::Vector{Float64}, ε::Float64=1e-8)
# assuming μ is a unit-vector (which it should be)
# can compute v in a single pass over μ
# Add small value, ε, to denominator to avoid dividing by zero

p = length(μ)
v = similar(μ)
v[1] = μ[1] - 1.0
s = sqrt(-2*v[1])
v[1] /= s
v[1] /= (s + ε)

@inbounds for i in 2:p
v[i] = μ[i] / s

v[i] = μ[i] / (s + ε)
end

return v
Expand Down
Loading