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

Sampling from von Mises Fisher yields NaN #1423

Open
lanceXwq opened this issue Nov 15, 2021 · 2 comments
Open

Sampling from von Mises Fisher yields NaN #1423

lanceXwq opened this issue Nov 15, 2021 · 2 comments

Comments

@lanceXwq
Copy link

This happens when the mean direction unit vector μ is along the x-axis. For instance:

julia> vMF = VonMisesFisher([1, 0], 1.0)
VonMisesFisher{Float64}(μ=[1.0, 0.0], κ=1.0)
julia> rand(vMF)
2-element Vector{Float64}:
 NaN
 NaN

This problem seems to persist in higher dimensions.

@devmotion
Copy link
Member

I'm not familiar with the implementation of the VonMisesFisher sampler but I suspect that the NaNs arise in

: for μ=[1.0, 0.0] we have v[1] = s = 0 in this line, and hence v[1] /= s should result in v[1] = NaN.

@trahflow
Copy link

I just stumbled over this as well.

I'm not familiar with the implementation of the VonMisesFisher sampler but I suspect that the NaNs arise in


: for μ=[1.0, 0.0] we have v[1] = s = 0 in this line, and hence v[1] /= s should result in v[1] = NaN.

Yes, this is what happens.
It seems this was introduced in #1162. At least a quick check shows the previous version which constructs an explicit rotation matrix does not suffer from this.

A very quick, albeit a bit dirty fix would be to return a zero vector in

function _vmf_householder_vec::Vector{Float64})
# assuming μ is a unit-vector (which it should be)
# can compute v in a single pass over μ
p = length(μ)
v = similar(μ)
v[1] = μ[1] - 1.0
s = sqrt(-2*v[1])
v[1] /= s
@inbounds for i in 2:p
v[i] = μ[i] / s
end
return v
end

for the case that s is numerically close to zero. That should give the correct (identity) rotation in
return _vmf_rot!(spl.v, x)

Maybe @emerali has a suggestion?

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

3 participants