Re: [R] How to generate the random numbers uniformly distributed on the unit disc?

2006-10-10 Thread Scionforbai
It depends on what you're actually doing, but I normally prefer Van
der Corput pseudo-random sequences to draw uniformly AND regularly
distributed points on a 2d disc or 3d sphere.
VdC sequences need binary and ternary decompositions of the numbers of
simulated points. They are a little bit time consuming, but the
sequences can be

Here a small piece of code (all functions of mine begin with M.):

###

# binary or ternary decomposition. results in a vector.
M.decomp <- function(n,base) {
   l <- trunc(log(n,base)+1)
   dec <- vector(mode = "integer", length = l)

   for (i in c(1:l) ) {
  dec[l-i+1] <- n%%base
  n <- n%/%base
   }
   return(dec)

}

# Calculates the i-th terms of VdC binary and ternary sequences:
M.seq.vdc <- function(i) {
   # binary decomp.
   v2 <- M.decomp(i,2)
   lv <- v2/(2**(length(v2):1))
   # ternary decomp.
   u3 <- M.decomp(i,3)
   lu <- u3/(3**(length(u3):1))

   # actual terms of VdC sequence:
   vdc2=sum(lv)
   vdc3=sum(lu)

   return(c(vdc2,vdc3))
}

# Uniformly distributed points on the unitary disc (2D)
M.vdc.2d <- function(n,plot=F) {

   a <- lapply(1:n,M.seq.vdc)
   a <- matrix(unlist(a),ncol=2,byrow=T)
   u <- 2*pi*a[,1]
   v <- sqrt(a[,2])
   x <- v*cos(u)
   y <- v*sin(u)

   if (plot) plot(x,y,pch=".",asp=1)

   invisible(list(phi = u, rho = v, x = x, y = y))
}

# For comparison, the "uniform" algorithm
M.unif.disc <- function (n = 10^4, R = 1,plot=F) {
   phi <- runif(n) * 2 * pi
   rho <- R * sqrt(runif(n))
   xx <- rho * cos(phi)
   yy <- rho * sin(phi)

   if (plot) plot(xx, yy, pch = ".", asp = 1)

   invisible(list(phi = phi, rho = rho, x = xx, y = yy))
}


# Uniformly distributed points on the unitary sphere (3D)
M.vdc.3d <- function(n) {

   a <- lapply(1:n,M.seq.vdc)
   out   <- matrix(unlist(a),ncol=2,byrow=T)
   u <- out[,1]
   v <- out[,2]
   x <- cos(2*pi*u)*sqrt(1-v^2)
   y <- sin(2*pi*u)*sqrt(1-v^2)
   out <- cbind(x,y,v)
   return(out)
}


"


# test and visualization:
op <- par(no.readonly = TRUE)
par(mfrow = c(1,2))
par("mar" = c(1, 1, 1, 1) + 0.1)

vdc <- M.vdc.2d(n,plot=T)
uni <- M.unif.disc(n,plot=T)
par(op)

Actually, one should randomly rotate the VdC points (and maybe
permutate the realization?) for each simulation (the sequence is
deterministic). Google for "Van der Corput sequence" to find more
details about its nice properties.

Regards,

Marco

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to generate the random numbers uniformly distributed on the unit disc?

2006-10-09 Thread Peter Dalgaard
"S.Q. WEN" <[EMAIL PROTECTED]> writes:

> Hi,
> I want to get  random number which is uniformly distributed  on the unit
> disc.
> How can I do that with R?

Most easily, although perhaps not most efficiently, by using spherical
coordinates:

 N <- 1
 r <- sqrt(runif(N))
 theta <- runif(N,0,2*pi)
 x <- r*cos(theta)
 y <- r*sin(theta)
 plot(x,y,pch=".")




-- 
   O__   Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph:  (+45) 35327918
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to generate the random numbers uniformly distributed on the unit disc?

2006-10-09 Thread Joerg van den Hoff
S.Q. WEN wrote:
> Hi,
> I want to get  random number which is uniformly distributed  on the unit
> disc.
> How can I do that with R?
> 
> 
> Best wishes,
> WAN WAN
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

the following function should put "N" uniformly distributed points into 
a disc of radius "R" (and plot the points in the (phi,rho) and (xx, yy) 
coordinate systems).

disc <- function (N = 10^4, R = 1) {
phi <- runif(N) * 2 * pi
rho <- R * sqrt(runif(N))
xx <- rho * cos(phi)
yy <- rho * sin(phi)
layout(1:2)
plot(phi, rho, pch = ".")
plot(xx, yy, pch = ".", asp = 1)
layout(1)
invisible(list(phi = phi, rho = rho))
}

the trick is to transform a uniform distribution along `rho' in such a 
way that it gets "denser" towards the rim in such a way that the 
'smearing out' of the  points over the circumference is compensated.

regards

joerg

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to generate the random numbers uniformly distributed on the unit disc?

2006-10-09 Thread S.Q. WEN
Hi,
I want to get  random number which is uniformly distributed  on the unit
disc.
How can I do that with R?


Best wishes,
WAN WAN

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.