Hi, I would like to draw 10 uniformly distributed sample points from a circle with redius one and centered at (0,0). Is there any R function to do that? Thanks, [[alternative HTML version deleted]]
On 18-Jun-10 08:04:36, Ron Michael wrote:> Hi, I would like to draw 10 uniformly distributed sample points from a > circle with redius one and centered at (0,0). Is there any R function > to do that? > _ > Thanks,You can quite easily write one. [A] Sampling uniformly on the circumference of the circle: csamp <- function(n,rad=1,centre=c(0,0)){ x0 <- centre[1] ; y0 <- centre[2] u <- 2*pi*runif(n) rad*cbind(x=cos(u)+x0, y=sin(u)+y0) } # Returns an nx2 matrix whose two columns are the x and y coordinates [B] Sampling uniformaly within the circle Csamp <- function(n,rad=1,centre=c(0,0)){ x0 <- centre[1] ; y0 <- centre[2] u <- 2*pi*runif(n) r <- sqrt(runif(n)) rad*cbind(x=r*cos(u)+x0, y=r*sin(u)+y0) } # Returns an nx2 matrix whose two columns are the x and y coordinates [C] Examples: plot(csamp(100),asp=1) plot(Csamp(1000),asp=1) Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 18-Jun-10 Time: 10:33:00 ------------------------------ XFMail ------------------------------
OOPS: AN error on the code below! See in-line. Ted. On 18-Jun-10 09:33:04, Ted Harding wrote:> On 18-Jun-10 08:04:36, Ron Michael wrote: >> Hi, I would like to draw 10 uniformly distributed sample points from a >> circle with redius one and centered at (0,0). Is there any R function >> to do that? >> _ >> Thanks, > > You can quite easily write one. > > [A] > Sampling uniformly on the circumference of the circle: > > csamp <- function(n,rad=1,centre=c(0,0)){ > x0 <- centre[1] ; y0 <- centre[2] > u <- 2*pi*runif(n) > rad*cbind(x=cos(u)+x0, y=sin(u)+y0) > } > ># Returns an nx2 matrix whose two columns are the x and y coordinatesCORRECTION: csamp <- function(n,rad=1,centre=c(0,0)){ x0 <- centre[1] ; y0 <- centre[2] u <- 2*pi*runif(n) cbind(x=rad*cos(u)+x0, y=rad*sin(u)+y0) } # Returns an nx2 matrix whose two columns are the x and y coordinates> [B] > Sampling uniformaly within the circle > > Csamp <- function(n,rad=1,centre=c(0,0)){ > x0 <- centre[1] ; y0 <- centre[2] > u <- 2*pi*runif(n) > r <- sqrt(runif(n)) > rad*cbind(x=r*cos(u)+x0, y=r*sin(u)+y0) > } > ># Returns an nx2 matrix whose two columns are the x and y coordinatesCORRECTION: Csamp <- function(n,rad=1,centre=c(0,0)){ x0 <- centre[1] ; y0 <- centre[2] u <- 2*pi*runif(n) r <- sqrt(runif(n)) cbind(x=rad*r*cos(u)+x0, y=rad*r*sin(u)+y0) } # Returns an nx2 matrix whose two columns are the x and y coordinates> [C] > Examples: > > plot(csamp(100),asp=1) > > plot(Csamp(1000),asp=1) > > Ted. > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> > Fax-to-email: +44 (0)870 094 0861 > Date: 18-Jun-10 Time: 10:33:00 > ------------------------------ XFMail ------------------------------ > > ______________________________________________ > R-help at r-project.org 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.-------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 18-Jun-10 Time: 10:48:14 ------------------------------ XFMail ------------------------------
Sorry Ted The code for the circle is in error, the radius distribution should be proportional to the circle area not uniform. Better / simple to sample from the (-1,1) square uniformly and reject where x^2+y^2 > 1 Heberto ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Ted Harding [Ted.Harding at manchester.ac.uk] Sent: Friday, June 18, 2010 5:48 AM To: r-help at stat.math.ethz.ch Subject: [R] [OOPS] Re: Drawing sample from a circle OOPS: AN error on the code below! See in-line. Ted. On 18-Jun-10 09:33:04, Ted Harding wrote:> On 18-Jun-10 08:04:36, Ron Michael wrote: >> Hi, I would like to draw 10 uniformly distributed sample points from a >> circle with redius one and centered at (0,0). Is there any R function >> to do that? >> _ >> Thanks, > > You can quite easily write one. > > [A] > Sampling uniformly on the circumference of the circle: > > csamp <- function(n,rad=1,centre=c(0,0)){ > x0 <- centre[1] ; y0 <- centre[2] > u <- 2*pi*runif(n) > rad*cbind(x=cos(u)+x0, y=sin(u)+y0) > } > ># Returns an nx2 matrix whose two columns are the x and y coordinatesCORRECTION: csamp <- function(n,rad=1,centre=c(0,0)){ x0 <- centre[1] ; y0 <- centre[2] u <- 2*pi*runif(n) cbind(x=rad*cos(u)+x0, y=rad*sin(u)+y0) } # Returns an nx2 matrix whose two columns are the x and y coordinates> [B] > Sampling uniformaly within the circle > > Csamp <- function(n,rad=1,centre=c(0,0)){ > x0 <- centre[1] ; y0 <- centre[2] > u <- 2*pi*runif(n) > r <- sqrt(runif(n)) > rad*cbind(x=r*cos(u)+x0, y=r*sin(u)+y0) > } > ># Returns an nx2 matrix whose two columns are the x and y coordinatesCORRECTION: Csamp <- function(n,rad=1,centre=c(0,0)){ x0 <- centre[1] ; y0 <- centre[2] u <- 2*pi*runif(n) r <- sqrt(runif(n)) cbind(x=rad*r*cos(u)+x0, y=rad*r*sin(u)+y0) } # Returns an nx2 matrix whose two columns are the x and y coordinates> [C] > Examples: > > plot(csamp(100),asp=1) > > plot(Csamp(1000),asp=1) > > Ted. > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> > Fax-to-email: +44 (0)870 094 0861 > Date: 18-Jun-10 Time: 10:33:00 > ------------------------------ XFMail ------------------------------ > > ______________________________________________ > R-help at r-project.org 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.-------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 18-Jun-10 Time: 10:48:14 ------------------------------ XFMail ------------------------------ ______________________________________________ R-help at r-project.org 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.
I think you have misunderstood the code! In case [B] below (sampling uniformly over the area within the circle) the assignment r <- sqrt(runif(n)) does just that: The radius distribution (of r) is not uniform; the distribution of r^2 is uniform, and r^2 "is proportional to the circle area". In other words: Given a unit circle (0 < r < 1), the probability that r falls within 0 < r < r0 [ < 1 ] is Prob(r < r0) = prob(r^2 < r0^2) = r0^2 since r0^2 is uniformly distributed on (0,1). And the area of the circle r < r0 is pi*r0^2. Along with the uniform distribution of the angle u on (0,2*pi), this then gives points uniformly distributed over the area within the circle r < 1. Ted. On 21-Jun-10 12:45:42, R Heberto Ghezzo, Dr wrote:> Sorry Ted > The code for the circle is in error, the radius distribution should be > proportional to the circle area not > uniform. Better / simple to sample from the (-1,1) square uniformly and > reject where x^2+y^2 > 1 > Heberto > ________________________________________ > From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On > Behalf Of Ted Harding [Ted.Harding at manchester.ac.uk] > Sent: Friday, June 18, 2010 5:48 AM > To: r-help at stat.math.ethz.ch > Subject: [R] [OOPS] Re: Drawing sample from a circle > > OOPS: AN error on the code below! See in-line. > Ted. > > On 18-Jun-10 09:33:04, Ted Harding wrote: >> On 18-Jun-10 08:04:36, Ron Michael wrote: >>> Hi, I would like to draw 10 uniformly distributed sample points from >>> a >>> circle with redius one and centered at (0,0). Is there any R function >>> to do that? >>> _ >>> Thanks, >> >> You can quite easily write one. >> >> [A] >> Sampling uniformly on the circumference of the circle: >> >> csamp <- function(n,rad=1,centre=c(0,0)){ >> x0 <- centre[1] ; y0 <- centre[2] >> u <- 2*pi*runif(n) >> rad*cbind(x=cos(u)+x0, y=sin(u)+y0) >> } >> >># Returns an nx2 matrix whose two columns are the x and y coordinates > CORRECTION: > > csamp <- function(n,rad=1,centre=c(0,0)){ > x0 <- centre[1] ; y0 <- centre[2] > u <- 2*pi*runif(n) > cbind(x=rad*cos(u)+x0, y=rad*sin(u)+y0) > } > ># Returns an nx2 matrix whose two columns are the x and y coordinates > >> [B] >> Sampling uniformaly within the circle >> >> Csamp <- function(n,rad=1,centre=c(0,0)){ >> x0 <- centre[1] ; y0 <- centre[2] >> u <- 2*pi*runif(n) >> r <- sqrt(runif(n)) >> rad*cbind(x=r*cos(u)+x0, y=r*sin(u)+y0) >> } >> >># Returns an nx2 matrix whose two columns are the x and y coordinates > CORRECTION: > > Csamp <- function(n,rad=1,centre=c(0,0)){ > x0 <- centre[1] ; y0 <- centre[2] > u <- 2*pi*runif(n) > r <- sqrt(runif(n)) > cbind(x=rad*r*cos(u)+x0, y=rad*r*sin(u)+y0) > } > ># Returns an nx2 matrix whose two columns are the x and y coordinates > > >> [C] >> Examples: >> >> plot(csamp(100),asp=1) >> >> plot(Csamp(1000),asp=1) >> >> Ted. >> >> -------------------------------------------------------------------- >> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> >> Fax-to-email: +44 (0)870 094 0861 >> Date: 18-Jun-10 Time: 10:33:00 >> ------------------------------ XFMail ------------------------------ >> >> ______________________________________________ >> R-help at r-project.org 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. > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> > Fax-to-email: +44 (0)870 094 0861 > Date: 18-Jun-10 Time: 10:48:14 > ------------------------------ XFMail ------------------------------ > > ______________________________________________ > R-help at r-project.org 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-help at r-project.org 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.-------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 21-Jun-10 Time: 14:07:23 ------------------------------ XFMail ------------------------------