Search code examples
rnormal-distributionuniform-distribution

Points uniformly distributed on unit disk (2D)


I am trying to generate 10,000 points from the uniform distribution on the unit disk and plot these points.

The method I am using has three steps. The first step is generating the magnitude of the point x. This point has cdf F(x) = x^2 min(x) = 0 and max(x) = 1. The second step involves generating a 2 dimensional vector (which I will call y) from the multivariate normal distribution with mu being the zero vector and sigma being the 2x2 identity matrix - MVN(0,I). Last I normalize the vector y to have length x. I have tried to code the solution in R but I do not think my answer is correct. I would really appreciate if I could be pointed in the right direction.

u = runif(10000)
x = u^2
y = mvrnorm(10000, mu=rep(0,2), Sigma=diag(2))
y_norm = (x*y)/sqrt(sum(y^2))
plot(y_norm, asp = 1)

I used the MASS package for mvrnorm. Also I have included the plot that I ended up with:enter image description here


Solution

  • You need to compute the length of each of the rows in your y matrix, you are getting the square root of the sum of all the numbers in y, which is just scaling your multinomial by a constant. Also, you need x to be sqrt(u) rather than u^2 - this code normalises each row by its length and users sqrt(u) scaling and it looks nice and uniform:

    plot(sqrt(u)*y/sqrt(y[,1]^2+y[,2]^2))
    

    There are better ways of making uniform points on a disc, unless this is just an exercise to do it this way...