random unit vector in multi-dimensional space

后端 未结 5 1954
栀梦
栀梦 2020-12-05 10:42

I\'m working on a data mining algorithm where i want to pick a random direction from a particular point in the feature space.

If I pick a random number for each of

5条回答
  •  盖世英雄少女心
    2020-12-05 11:04

    You will not get a uniformly distributed ensemble of angles with the algorithm you described. The angles will be biased toward the corners of your n-dimensional hypercube.

    This can be fixed by eliminating any points with distance greater than 1 from the origin. Then you're dealing with a spherical rather than a cubical (n-dimensional) volume, and your set of angles should then be uniformly distributed over the sample space.

    Pseudocode:

    Let n be the number of dimensions, K the desired number of vectors:

    vec_count=0
    while vec_count < K
       generate n uniformly distributed values a[0..n-1] over [-1, 1]
       r_squared = sum over i=0,n-1 of a[i]^2
       if 0 < r_squared <= 1.0
          b[i] = a[i]/sqrt(r_squared)  ; normalize to length of 1
          add vector b[0..n-1] to output list
          vec_count = vec_count + 1
       else
          reject this sample
    end while
    

提交回复
热议问题