Generate random sample of points distributed on the n-dimensional sphere of radius R and centred at the origin

13 views (last 30 days)
How to generate random sample of points distributed on the n-dimensional sphere of radius R and centred at the origin?

Accepted Answer

John D'Errico
John D'Errico on 10 Jan 2019
Edited: John D'Errico on 10 Jan 2019
What is a "complex random n-dimensional vector"? Does this imply you want to see complex numbers? If so, then what does that mean?
Does the word complex only means too complicated for you to solve?
What do you mean by a normal distribution inside a bounded region? Sorry, but that has no meaning, since a normal distribution is unbounded. Did you intend to say a uniform distribution over that volume? That is easy.
Just generate a random and uniformly distributed point on the surface of the n-sphere. Then generate a random number (carefully, with the proper distribution) from the interval [0,1]. Combine the two variables, properly.
Npoints = 10000;
Ndim = 2; % points in a circle. ergo, easy to plot.
X = randn(Npoints,Ndim); % A normal dist is symmetrical
X = X./sqrt(sum(X.^2,2)); % project to the surface of the Ndim-sphere
% That last line requires MATLAB R2016b or later. Earlier versions will use bsxfun or even repmat.
% radial scale factor
R = nthroot(rand(Npoints,1),Ndim);
% Combine
X = X.*R;
% and plot
plot(X(:,1),X(:,2),'.')
Or, is your question to generate a truncated normal distribution inside the unit n-Sphere?
Simplest in that case is to just use rejection sampling. Generate the samples, then throw away any that fall outside of the N-sphere. That means you need to over-sample.
X = randn(Npoints,Ndim);
X(sqrt(sum(X.^2,2)) > 1,:) = [];
plot(X(:,1),X(:,2),'.')
size(X)
ans =
3971 2
This is actually more densely populated mear the center of the unit circle. The difference is not immense though. Note that we lost about 65% of the points in the rejection step. In higher dimensions, we would need to over-sample more heavily, so in a seriously high number of dimensions, rejection would be a problem. But I'm not going to answer a question that has not actually been said to be your real problem. If that is the case, you need to tell me you really want to do a truncated Normal in a high number of dimensions. What is high?
Hint: If you really want to do the truncated Normal inside a unit sphere, then you could simply use the same scheme that I did for the uniform case, but the radial computation would use a truncated chi distribution to get R. (As distinct from a chi-square distribution.)
  7 Comments
HyoSeung Kang
HyoSeung Kang on 24 Jul 2023
Edited: HyoSeung Kang on 24 Jul 2023
Could you teach me mathematically how the first code generates a uniform distribution inside a ball?
Also, what is the analytic formula for this distribution's standard deviation?
This is what I am looking for, but I can not understand how it works.
Thanks a lot.

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!