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
ENH: Support Random Unit Vector #16205
Comments
Seems a bit too specific to be on its own as a function to be honest. The following is sufficient (similar to what you have instead you want a uniform distribution, you can also center around 0. by subtracting 0.5 from import numpy as np
def unit_vector(n: int):
rng = np.default_rng()
vec = rng.random(n)
vec /= np.linalg.norm(vec)
return vec It gets even shorter if you don't mind using |
Hey @ilayn , thank you for the quick response. I respectfully disagree. I've encountered the need for randomizing a unit vector uniformly many times for the past few years during my physics degree, and in my job as an ML team lead. This is a quite needed ability that I'm sure people would be happy to have here at Scipy. As for the implementation you've suggested, the problem with it that there's a tendency of the random vector to be in the diagonal line between the origin and the corner of the unit cube. This is very elegnatly explained in the blog post I've linked above. That means that the distribution in the implementation you've suggested is not uniform as most users would assume it is. This is the reason one must use normal distribution to randomize the components of the vector and not uniform distribution between -1 and 1. As many others thing, this is not something that is a "must have", we agree on that. People could implement this using one of the implementations you and I suggested. |
I have quite some technical issues with that blogpost but regardless of the distribution which involves no additional code for changing it normal distribution, this is still a two-liner code that I would prefer burdening the user instead of maintaining within scipy or numpy. |
Uniformly-random vectors on the unit hypersphere is an important multivariate distribution. It's not hard to implement oneself, if one knows the necessary trick (and yes, the normal distribution is necessary). If one doesn't know the appropriate trick, ad hoc implementations tend to fail in ways that are hard to diagnose in higher dimensions than 2. |
What could be a good API for this? |
The former, I think. It would fit well as a subclass of |
Good to see that other people find this feature needed and usefull. Do I have a go on implementing it? |
I asked on the mailing list if there are any objections against this and a function for spherical mean/variance. If there are none , I think it can be taken as a yes in a few days. |
I think that's true, @dschmitz89. @saroad2 One thing to look out for is to not start with the shortcomings we're addressing in gh-16278. If you use another multivariate distribution as a reference, you may be tempted to add |
What's the difference between |
stats.multivariate_normal.pdf(x, mean, cov)
my_norm = stats.multivariate_normal(mean, cov)
my_norm.pdf(x) This scheme is relatively complicated for the multivariate distributions because each one is fairly different from the others. The univariate distributions have much more shared infrastructure and only need to implement the |
@saroad2 are you still interested in implementing this? If you do, I can review and merge. |
Hey @mdhaber. After many hours of trying, I could not build Scipy on my Windows environment. It was really frustrating. Nevertheless, I would love to see it get implemented! It should be quite easy once you get to build Scipy... |
Yeah that's the trick. I develop on Windows, too, and it was really difficult to get it working the first time. Have you followed these instructions, including building OpenBLAS? In the meantime, you could consider using Gitpod so you don't need to worry about building locally. Also, building is supposed to become easier soon with this new Meson build system, but I'm not sure if it's working on Windows yet. (Perhaps others will chime in?) I'll let you know when I've been successful with that. |
@saroad2 I am using the Windows Subsystem for Linux on Windows 11. Works like a charm there with the instructions for conda given here . I admit, I have not even tried to build using windows directly. Anything related to complex compiled code on Windows has always been very painful for me too (Fortran ... ). In case you cannot find the time, I would be able to take this over. |
Hey @dschmitz89 , thanks for the feedback! In the following weeks I will not be able to work on this. A little bit preoccupied with other things. I would love if you could take over so I won't block it. Thanks! |
Is your feature request related to a problem? Please describe.
Proposed new feature or change:
Description
Often you want to randomize a "direction" that doesn't have "size". This can be useful when:
This random direction should be an
n
th dimensional unit vector which is randomized uniformly from the unit sphere. That means that sections of the unit sphere with equal areas should have the same chance of getting a vector from.Implementation
If one wants to randomize
n
th dimensional direction uniformly, one must do the following:n
components of the vector using the standard normal distributionIn simple Python code:
This implementation suggestion is based on this blog post. I'm not sure if there's a faster way of implementing it using C++ and Cython, but I'm confident that someone here will know.
Why Implement It in Scipy?
Originally, I suggested implementing it on Numpy (numpy/numpy#21523), but they stated that they are not interested in expending the
numpy.random
library and suggested adding this new feature as part ofscipy.stats
.I believe that random unit vectors are common enough to be a part of Numpy or Scipy, which are two of the most popular python libraries (if not the most popular ones). Scientists, gamers and engineers use random directions all the time, and I believe there is no reason why this ability shouldn't be provided out-of-the-box.
The text was updated successfully, but these errors were encountered: