Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random points on 10-dimensional unit sphere

Tags:

python

random

I need to generate a vector sampled uniformly with 10 directions (a collection of 10 random numbers) which lies over a unit sphere. So, the sum of the squares of the 10 values should be 1.

This is the exact question for which I need to generate those points:

Implement the Perceptron algorithm and run it on the following synthetic data sets in ℝ10: pick 𝑤∗ = [1,0,0,…,0]; generate 1000 points 𝑥 by sampling uniformly at random over the unit sphere and then removing those that have margin 𝛾 smaller than 0.1; generate label 𝑦 = sign((𝑤∗)T𝑥).

like image 347
Milin Patel Avatar asked Feb 28 '26 08:02

Milin Patel


2 Answers

As @Andrex suggested, here is the right solution:

import numpy as np
import math

s = np.random.normal(0, 1, 10)

norm=math.sqrt(sum(s*s))
result=s/norm

where result is the answer. You can evaluate the result:

sum([x*x for x in result])
1.0
like image 140
alift Avatar answered Mar 01 '26 23:03

alift


There is a math theorem saying that if X = (X1,...,XN) is a vector with Xi the standard normal distribution, then X/NORM(X) is uniform in the unit sphere, where NORM is the euclidean norm. So you have to sample 10 points from a standard normal distribution (using numpy?) and then normalize the result.

like image 40
Andrex Avatar answered Mar 01 '26 22:03

Andrex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!