I am getting familiar with 3D representations. I want to check if a set of trivariate data points falls inside an specific region. For gaussian data this may be easily done using something like:
data = cbind(rnorm(1000),rnorm(1000),rnorm(1000))
inside = data[,1]^2+data[,2]^2 + data[,3]^2 <= qchisq(0.95,3)
which(inside==T)
But, how can I do this manually? There is an R package called library(ptinpoly) to do so, but it requires a Triangular mesh, and I do not know how to obtain a triangular mesh these trivariate grid points.
b = 30
x = seq(0,1,length.out = b)
grid = expand.grid(x,x)
u = grid[,1]
v = grid[,2]
theta <- 2 * pi * u
phi <- acos(2 * v - 1)
tau = 0.95
polygon =(t(c(0,0,0) + sqrt(qchisq(tau,3))*t(chol(diag(1,3)))%*%
rbind(sin(phi) * cos(theta) ,sin(theta) * sin(phi),cos(phi))))
require(rgl)
plot3d(polygon,col="grey")
Any clue?
Related
I'm needing to derive xy values and calculate arc length between each xy value, so a length value for every value in i as generated by the attached code below (excluding the origin). The points follow an Archimedean spiral path. I don't have MATLAB and am using R, but the closest I've found that I can interpret was a MATLAB example found here with credit to Jos. Below is a modified version of the MATLAB script to generate the xy data:
r = 938; %outer radius
a = 0; %inner radius
b = 7; %increment per rev
n = (r - a)./(b); %number of revolutions
th = 2*n*pi; %angle
i = linspace(0,n,n*1000);
x = (a+b*i).* cos(2*pi*i);
y = (a+b*i).* sin(2*pi*i);
and the R equivalent:
r <- 938 # outer radius
a <- 0 # inner radius
b <- 7 # increment per revolution
n <- (r - a)/b # number of revolutions
th <- 2*n*pi # angle
i <- seq(0, n, length.out = n*1000) # number of points per revolution
x <- (a+b*i) * cos(2*pi*i)
y <- (a+b*i) * sin(2*pi*i)
My assumption is that the easiest way to derive arc length between every point is to coerce i, x, and y into a MATLAB table (dataframe in R). The closest I've found for calculating arc length is this formula for calculating the total length. I'm unable to interpret math notation, so am not sure how to implement it or how to modify it to calculate arc length between every point. Using the example of the first spiral in the link above for calculating total length I tried:
sqrt((5 + 0.1289155 * 47.12389)^2 + (0.1289155)^2) * 47.12389
The link above says the result should be 378.8 but my attempt returns 521.9324. So in sum, how is the arc length between points derived in MATLAB or R?
The exact formula for the length, with your notations for a (start radius), r (end radius) and b (increment per revolutions) reduces to
(note that in order to preserve the OP notation, there are two different meanings of the same r symbol, that might be frown upon by some)
That formula can be implemented this way
r <- 938 # outer radius
a <- 0 # inner radius
b <- 7 # increment per revolution
A <- 2 * pi / b
fa <- sqrt(1 + A^2 * a^2)
fr <- sqrt(1 + A^2 * r^2)
int_r <- (A*r*fr - log(-(A*r)+fr))/(2*A)
int_a <- (A*a*fa - log(-(A*a)+fa))/(2*A)
spiralLen <- int_r - int_a #exact formula
394877.5
you can also use numerical (approximative) integration in R stats integrate to evaluate the integral
integrate(function(r){sqrt(4*pi^2*r^2/b^2+1)}, a, r)
394877.3 with absolute error < 5.8
Another method, that gives a rather rough approximation, but is a very good verification because it doesn't use any theoretical considerations, but just takes the data you generated - and sums up the length of the segments of all consecutive points in the data:
dx <- x[2:length(x)] - x[1:length(x)-1]
dy <- y[2:length(x)] - y[1:length(x)-1]
len_approx = sum(sqrt(dx^2 + dy^2))
394876.8
As for plotting, in R, since you already have a set of points, it seems the very basic application of plot function does the job
plot(x, y, type="l")
I need to calculate the x and y points of the points A, B, C and D.
For that i need a formula. alpha and r are given.
And i need the 0 point on the top left corner. It cant get placed in the middle of the circle. Because i want to place elements on those points.
I would be realy thankfull for a formular that i can use ;D.
ax = 0
ay = r
bx = r - r * cos(alpha)
by = r + r * sin(alpha)
cx = r - r * cos(2*alpha)
and so on (3*alpha for D)
I am trying to produce a distribution for points within a circle where there are more towards the center, but for a donut shape
I can produce a uniformly distributed donut shape (1), or a circle where there are more points towards the center (2) but not a donut shape that has so many points towards its inside boundary.
(1)
r = sqrt(runif(250, min = 0.25, max =1))
p = runif(250)
theta = p *2*pi
a = r * cos(theta) #coordinates for circle
b = r * sin(theta)
plot(a,b)
(2)
r = runif(250)
p = runif(250)
theta = p *2*pi
a = r * cos(theta)
b = r * sin(theta)
plot(a,b)
My closest attempt is modifying program (2) where r is bound between 0.5 and 1 but this removes most of the points closest to the center and does not have as many around its inside.
As #RobertDodier said, try to use some advanced distribution for radius. What I would like to propose is Beta distribution.
First, it is naturally in the [0...1] range, no need to truncate, accept/reject etc.
Second, it has two parameters (a,b) which could be used to get single peak, zero at 0, and slope to 1. Look at graphs in the wiki page. And last, it is implemented in R. a being smaller than b means peak is on the lef of 0.5, a being larger than b means peak is on the right of 0.5.
Along the lines
N = 10000
r = rbeta(N, 7.0, 5.0)
theta = 2.0*pi*runif(N)
a = r * cos(theta)
b = r * sin(theta)
plot(a,b)
will produce plot like that
Does it look like at donut?
UPDATE
This one is with clear hole at the center and shape proposed by #RobertDodier
N = 10000
hole = 0.25
r = hole + (1.0-hole)*rbeta(N, 1.0, 3.0)
theta = 2.0*pi*runif(N)
a = r * cos(theta)
b = r * sin(theta)
plot(a,b)
Another one with clear hole in the center and symmetric shape, like a true donut
r = hole + (1.0-hole)*rbeta(N, 2.0, 2.0)
Given 2D uniform variable we can generate a uniform distribution in a unit-disk as discussed here.
My problem is similar in that i wish to uniformly sample the intersection area of two intersecting disks where one disk is always the unit-disk and the other can be freely moved and resized like here
I was trying to split the area into two regions (as depicted above) and sample each region individual based on the respected disk. My approach is based on uniform disk algorithm cited above. To sample the first region right of the center line I would restrict theta to be within the two intersection points. Next r would need to be projected based on that theta
such that the points are pushed in the area between our mid line and the radius of the disk. The python sample code can be found here.
u = unifrom2D()
A;B; // Intersection points
for p in allPoints
theta = u.x * (getTheta(A) - getTheta(B)) + getTheta(B)
r = sqrt(u.y + (1- u.y)*length2(lineIntersection(theta)))
p = (r * cos(theta), r * sin(theta))
However this approach is rather expensive and further fails to preserve uniformity. Just to clarify i do not want to use rejection sampling.
I am not sure if this is better than rejection sampling, but here is a solution for uniform sampling of a circle segment (with center angle <= pi) involving the numerical computation of an inverse function. (The uniform sampling of the intersection of two circles can then be composed of the sampling of segments, sectors and triangles - depending on how the intersection can be split into simpler figures.)
First we need to know how to generate a random value Z with given distribution F, i.e. we want
P(Z < x) = F(x) <=> (x = F^-1(y))
P(Z < F^-1(y)) = F(F^-1(y)) = y <=> (F is monotonous)
P(F(Z) < y) = y
This means: if Z has the requested distribution F, then F(Z) is distributed uniformly. The other way round:
Z = F^-1(Y),
where Y is distributed uniformly in [0,1], has the requested distribution.
If F is of the form
/ 0, x < a
F(x) = | (F0(x)-F0(a)) / (F0(b)-F0(a)), a <= x <= b
\ 1, b < x
then we can choose a Y0 uniformly in [F(a),F(b)] and set Z = F0^-1(Y0).
We choose to parametrize the segment by (theta,r), where the center angle theta is measured from one segment side. When the segment's center angle is alpha, the area of the segment intersected with a sector of angle theta starting where the segment starts is (for the unit circle, theta in [0,alpha/2])
F0_theta(theta) = 0.5*(theta - d*(s - d*tan(alpha/2-theta)))
where s = AB/2 = sin(alpha/2) and d = dist(M,AB) = cos(alpha/2) (the distance of the circle center to the segment). (The case alpha/2 <= theta <= alpha is symmetric and not considered here.)
We need a random theta with P(theta < x) = F_theta(x). The inverse of F_theta cannot be computed symbolically - it must be determined by some optimization algorithm (e.g. Newton-Raphson).
Once theta is fixed we need a random radius r in the range
[r_min, 1], r_min = d/cos(alpha/2-theta).
For x in [0, 1-r_min] the distribution must be
F0_r(x) = (x+r_min)^2 - r_min^2 = x^2 + 2*x*r_min.
Here the inverse can be computed symbolically:
F0_r^-1(y) = -r_min + sqrt(r_min^2+y)
Here is an implementation in Python for proof of concept:
from math import sin,cos,tan,sqrt
from scipy.optimize import newton
# area of segment of unit circle
# alpha: center angle of segment (0 <= alpha <= pi)
def segmentArea(alpha):
return 0.5*(alpha - sin(alpha))
# generate a function that gives the area of a segment of a unit circle
# intersected with a sector of given angle, where the sector starts at one end of the segment.
# The returned function is valid for [0,alpha/2].
# For theta=alpha/2 the returned function gives half of the segment area.
# alpha: center angle of segment (0 <= alpha <= pi)
def segmentAreaByAngle_gen(alpha):
alpha_2 = 0.5*alpha
s,d = sin(alpha_2),cos(alpha_2)
return lambda theta: 0.5*(theta - d*(s - d*tan(alpha_2-theta)))
# generate derivative function generated by segmentAreaByAngle_gen
def segmentAreaByAngleDeriv_gen(alpha):
alpha_2 = 0.5*alpha
d = cos(alpha_2)
return lambda theta: (lambda dr = d/cos(alpha_2-theta): 0.5*(1 - dr*dr))()
# generate inverse of function generated by segmentAreaByAngle_gen
def segmentAreaByAngleInv_gen(alpha):
x0 = sqrt(0.5*segmentArea(alpha)) # initial guess by approximating half of segment with right-angled triangle
return lambda area: newton(lambda theta: segmentAreaByAngle_gen(alpha)(theta) - area, x0, segmentAreaByAngleDeriv_gen(alpha))
# for a segment of the unit circle in canonical position
# (i.e. symmetric to x-axis, on positive side of x-axis)
# generate uniformly distributed random point in upper half
def randomPointInSegmentHalf(alpha):
FInv = segmentAreaByAngleInv_gen(alpha)
areaRandom = random.uniform(0,0.5*segmentArea(alpha))
thetaRandom = FInv(areaRandom)
alpha_2 = 0.5*alpha
d = cos(alpha_2)
rMin = d/cos(alpha_2-thetaRandom)
secAreaRandom = random.uniform(0, 1-rMin*rMin)
rRandom = sqrt(rMin*rMin + secAreaRandom)
return rRandom*cos(alpha_2-thetaRandom), rRandom*sin(alpha_2-thetaRandom)
The visualisation seems to verify uniform distribution (of the upper half of a segment with center angle pi/2):
import matplotlib.pyplot as plot
segmentPoints = [randomPointInSegmentHalf(pi/2) for _ in range(500)]
plot.scatter(*zip(*segmentPoints))
plot.show()
I have a normalized direction vector (from a 3d position to a light position) and I would like this vector to be rotated by some angle so I can create a "cone".
Id like to simulate cone tracing by using the direction vector as the center of the cone and create an X number of samples to create more rays to sample from.
What I would like to know is basically the math behind:
https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Random/RandomUnitVectorinCone/index.html
Which seems to do exactly what Im looking for.
1) Make arbitrary vector P, perpendicular to your direction vector D.
You can choose component with max magnitude, exchange it with middle-magnitude component, negate it, and make min magnitude component zero.
For example, if z- component is maximal and y-component is minimal, you may make such P:
D = (dx, dy, dz)
p = (-dz, 0, dx)
P = Normalize(p) //unit vector
2) Make vector Q perpendicular both D and P through vector product:
Q = D x P //unit vector
3) Generate random point in the PQ plane disk
RMax = Tan(Phi) //where Phi is cone angle
Theta = Random(0..2*Pi)
r = RMax * Sqrt(Random(0..1))
V = r * (P * Cos(Theta) + Q * Sin(Theta))
4) Normalize vector V
Note that distribution of vectors is slightly non-uniform on the sphere segment.(it is uniform on the plane disk). There are methods to generate uniform distribution on the sphere but some work needed to apply them to segment (my first attempt before edit was wrong).
Edit: Modification to make sphere-uniform distribution (not checked thoroughly)
RMax = Tan(Phi) //where Phi is cone angle
Theta = Random(0..2*Pi)
u = Random(Cos(Phi)..1)
r = RMax * Sqrt(1 - u^2)
V = r * (P * Cos(Theta) + Q * Sin(Theta))