RfastOfficial / Rfast

A collection of Rfast functions for data analysis. Note 1: The vast majority of the functions accept matrices only, not data.frames. Note 2: Do not have matrices or vectors with have missing data (i.e NAs). We do no check about them and C++ internally transforms them into zeros (0), so you may get wrong results. Note 3: In general, make sure you give the correct input, in order to get the correct output. We do no checks and this is one of the many reasons we are fast.
142 stars 19 forks source link

Can't sample using rvmf() with k=0 #114

Open mbojan opened 1 month ago

mbojan commented 1 month ago

Describe the bug Can't sample uniformly using rvmf()

To Reproduce

Rfast::rvmf(3, c(0,0,0), 0)
#> Error in eval(expr, envir, enclos): element-wise division: incompatible matrix dimensions: 3x3 and 3x1

but this works

Rfast::rvmf(3, 0, 0)
##      [,1]
## [1,]    1
## [2,]    1
## [3,]    1

Expected behavior

Works.

Desktop (please complete the following information):

statlink commented 1 month ago

Hi Mbojan, the problem is with the mu vector. The mu must be a unit vector, so the coordinates you give, c(0,0,0), do not lie on the sphere. However, I noticed that you have a point. When k = 0, it still doe not work. We will see this. If you want to sample from a uniform on the sphere of any dimension you may try this

x <- matrnorm(n, p) ## p is the dimensionality of the sphere x <- x / sqrt( rowsums(x^2) )

mbojan commented 1 month ago

Thanks @statlink . I'm going through somebody else's code that is couple of years old and it trips on that call from issue description so I presumed there was some change in recent years (?). Thanks for the suggestion. You're right that it is much simpler in the uniform case.

statlink commented 1 month ago

We also thank you @mbojan for noticing the problem with k=0.