I have problem with determining if certain points lie inside multi-dimensional ellipsoid. The real problem is caused by potential transformations (rotations, translations) and I don't know how to apply them to my computations.
In my work I will have set of points for which I need to find minimum volume enclosing ellipsoid. Then I will have to check other set of points and determine which of them belong to the found ellipsoid.
I have decided to use code from here:
function [] = Checkup()
points = [[ 0.53135758, -0.25818091, -0.32382715]
[ 0.58368177, -0.3286576, -0.23854156,]
[ 0.18741533, 0.03066228, -0.94294771]
[ 0.65685862, -0.09220681, -0.60347573]
[ 0.63137604, -0.22978685, -0.27479238]
[ 0.59683195, -0.15111101, -0.40536606]
[ 0.68646128, 0.0046802, -0.68407367]
[ 0.62311759, 0.0101013, -0.75863324]];
P = points\'; % <- remove the \ symbol here
dimen = length(P(:,1));
% c - vector with ellipse centers
[A, c] = MinVolEllipse(P, 0.01);
[~, Q, V] = svd(A);
radiuses = 1:dimen;
% Calculate radiuses
for i = 1:dimen
radiuses(1, i) = 1 / sqrt(Q(i,i));
end
% Check if points lie within ellipse and print Ok for every point inside ellipsoid
for i = 1:length(P(1,:)) % length(P(1,:)) is number of points
value = 0;
for j = 1:dimen
%adding ((p_i - c_i) / (r_i))^2 value
value = value + ( ( (P(j,i) - c(j, 1) )^2 ) / (radiuses(1, j)^2));
end
if value <= 1
disp('Ok')
end
end
Right now this code does not print Ok text (but it should print it 8 times).
According to this: "V is the rotation matrix that gives you the orientation of the ellipsoid" I think this is the last element I need to use for my code to work.
My approach
Ok so I made some changes to my code. Right now I try to do something like this:
Assuming I have center of ellipsoid and its radius.
For every point:
Update position as follows: point = point - center -> in other words translate it as if ellipsoid had it center in point (0, 0, ... 0)
Rotate it as if ellipsoid was parallel to all axes: point = invert(V) * point
Check if point lies inside ellipsoid using equation: (point.x / radius.x)^2 + ... + (point.i / radius.i)^2 where i is number of dimensions
If equation gave result <= 1 then point lies within ellipse.
Is this approach good? I mean - I don't know if I'm allowed to invert V matrix and use it as if it inverted previous rotation...
Proper solution
Looks like my proposed approach was good but there is one even better:
function [result] = Ellipse()
% Returns vector consisting of 10 entries
% that represent error ratio for every test set
result = 0:9;
% Run tests for all point sets
for pointSet = 0:9
[Ptraining, Ptest] = LoadPointSet(pointSet);
count = 0;
% c - vector with ellipse centers
[A, c] = MinVolEllipse(Ptraining, 0.00001);
% Check if points lie within ellipse
for i = 1:length(Ptest(1,:)) % length(P(1,:)) is number of points
pp = Ptest(:, i) - c;
value = pp' * A * pp;
if value > 1
count = count + 1;
end
end
% Get the error ratio
index = pointSet + 1;
result(index) = count / length(Ptest(1,:));
end
end