Generate data inside ellipse and rectangle

Viewed 152

I need to generate data, which I should use in following calculations. One part of my data should include 15000 points, which have even distribution inside ellipse(runif in R). Here is the the equation of ellipse: Equation of ellipse Another part of data should include 10000 points inside rectangle. Here is the equation of rectangle: Equation of rectangleHow can I do this?

2 Answers

Uniformly Distributed Points

Here is an example for the eclipse data (you could follow similar idea for the rectangle one)

n <- 1.5e4
feclipse <- function(x, y) 2 * (x - 6)^2 + 3 * (y - 4)^2 - 10
res <- c()
repeat {
  if (length(res) / 2 == n) break
  x <- runif(1, 6 - sqrt(5), 6 + sqrt(5))
  y <- runif(1, 4 - sqrt(10 / 3), 4 + sqrt(10 / 3))
  if (feclipse(x, y) <= 0) {
    res <- rbind(res, c(X = x, Y = y))
  }
}

and plot(res) gives

enter image description here


Non-uniformly Distributed Points

Another option is using analytical solution of y in terms of x to produce random tuples

x <- runif(n, 6 - sqrt(5), 6 + sqrt(5))
y <- runif(n, 4 - sqrt((10 - 2 * (x - 6)^2) / 3), 4 + sqrt((10 - 2 * (x - 6)^2) / 3))

plot(x, y)

which gives

enter image description here

Or, you can use polar representation to generate the points

theta <- runif(n, 0, 2 * pi)
rho <- runif(n)
x <- rho * sqrt(5) * sin(theta) + 6
y <- rho * sqrt(10 / 3) * cos(theta) + 4
plot(cbind(x, y))

which gives

enter image description here

These can both be sampled without rejection using transformed uniform variates.

Rectangle:

set.seed(94)
u <- runif(1.5e4, max = 3)
v <- runif(1.5e4, max = 2)
x <- u + v - 10
y <- v - u + 8
# sanity check
range(x + y)
#[1] -1.999774  1.999826
range(x - y + 15)
#[1] -2.999646  2.999692
plot(x, y)

enter image description here

Ellipse (see Algorithm: Calculate pseudo-random point inside an ellipse):

phi <- runif(1e4, max = 2*pi)
rho <- sqrt(runif(1e4))
x <- sqrt(5)*rho*cos(phi) + 6
y <- sqrt(10/3)*rho*sin(phi) + 4
# sanity check
range(2*(x - 6)^2 + 3*(y - 4)^2)
#[1] 0.001536582 9.999425234
plot(x, y)

enter image description here

Update with derivation:

Rectangle:
The rectangle is bounded by the lines:
y = -2 - x
y = 2 - x
y = x + 12
y = x + 18
The left-most point of the rectangle is found by point where the first and last lines intersect (-10, 8).

The dimensions of the rectangle are 3*sqrt(2) and 2*sqrt(2), so sample from a rectangle with these dimensions:
u = 3*sqrt(2)*U1
v = 2*sqrt(2)*U2
where U1 and U2 are standard uniform random variates (i.e., runif).

Since the slopes of the bounding lines are -1 and 1, we can think of the rectangle as being rotated -90° about the origin axes. Rotate u and v by -90° to orient the rectangle containing (u, v) to be in the same direction as the desired rectangle:
x = u*cos(-90°) - v*sin(-90°) = sqrt(2)*(u + v)/2 = 3*U1 - 2*U2
y = v*cos(-90°) + u*sin(-90°) = sqrt(2)*(v - u)/2 = 3*U1 + 2*U2

The left-most point of (x, y) is still at the origin, so translate the points by (-10, 8) to match the desired rectangle:
x = 3*U1 - 2*U2 - 10
y = 3*U1 + 2*U2 + 8

Ellipse:
The x - 6 and y - 4 mean your ellipse is centered at (6, 4).

Center the ellipse at the origin by ignoring the 6 and 4, then write the equation for the ellipse in standard form:
x^2/a^2 + y^2/b^2 = 1
a = sqrt(5)
b = sqrt(10/3)
a and b are the dimensions of your ellipse.

Related