|
a |
|
b/featurebased-approach/subfunctions/lib/Minimum Volume Enclosing Ellipsoid/MinVolEllipse.m |
|
|
1 |
function [A , c] = MinVolEllipse(P, tolerance) |
|
|
2 |
% [A , c] = MinVolEllipse(P, tolerance) |
|
|
3 |
% Finds the minimum volume enclsing ellipsoid (MVEE) of a set of data |
|
|
4 |
% points stored in matrix P. The following optimization problem is solved: |
|
|
5 |
% |
|
|
6 |
% minimize log(det(A)) |
|
|
7 |
% subject to (P_i - c)' * A * (P_i - c) <= 1 |
|
|
8 |
% |
|
|
9 |
% in variables A and c, where P_i is the i-th column of the matrix P. |
|
|
10 |
% The solver is based on Khachiyan Algorithm, and the final solution |
|
|
11 |
% is different from the optimal value by the pre-spesified amount of 'tolerance'. |
|
|
12 |
% |
|
|
13 |
% inputs: |
|
|
14 |
%--------- |
|
|
15 |
% P : (d x N) dimnesional matrix containing N points in R^d. |
|
|
16 |
% tolerance : error in the solution with respect to the optimal value. |
|
|
17 |
% |
|
|
18 |
% outputs: |
|
|
19 |
%--------- |
|
|
20 |
% A : (d x d) matrix of the ellipse equation in the 'center form': |
|
|
21 |
% (x-c)' * A * (x-c) = 1 |
|
|
22 |
% c : 'd' dimensional vector as the center of the ellipse. |
|
|
23 |
% |
|
|
24 |
% example: |
|
|
25 |
% -------- |
|
|
26 |
% P = rand(5,100); |
|
|
27 |
% [A, c] = MinVolEllipse(P, .01) |
|
|
28 |
% |
|
|
29 |
% To reduce the computation time, work with the boundary points only: |
|
|
30 |
% |
|
|
31 |
% K = convhulln(P'); |
|
|
32 |
% K = unique(K(:)); |
|
|
33 |
% Q = P(:,K); |
|
|
34 |
% [A, c] = MinVolEllipse(Q, .01) |
|
|
35 |
% |
|
|
36 |
% |
|
|
37 |
% Nima Moshtagh (nima@seas.upenn.edu) |
|
|
38 |
% University of Pennsylvania |
|
|
39 |
% |
|
|
40 |
% December 2005 |
|
|
41 |
% UPDATE: Jan 2009 |
|
|
42 |
% |
|
|
43 |
% Copyright (c) 2009, Nima Moshtagh |
|
|
44 |
%All rights reserved. |
|
|
45 |
% |
|
|
46 |
%Redistribution and use in source and binary forms, with or without |
|
|
47 |
%modification, are permitted provided that the following conditions are |
|
|
48 |
%met: |
|
|
49 |
% |
|
|
50 |
% * Redistributions of source code must retain the above copyright |
|
|
51 |
% notice, this list of conditions and the following disclaimer. |
|
|
52 |
% * Redistributions in binary form must reproduce the above copyright |
|
|
53 |
% notice, this list of conditions and the following disclaimer in |
|
|
54 |
% the documentation and/or other materials provided with the distribution |
|
|
55 |
% |
|
|
56 |
%THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
|
57 |
%AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
|
58 |
%IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
59 |
%ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|
|
60 |
%LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
|
61 |
%CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
|
62 |
%SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
|
63 |
%INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
|
64 |
%CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
|
65 |
%ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
|
66 |
%POSSIBILITY OF SUCH DAMAGE. |
|
|
67 |
% |
|
|
68 |
|
|
|
69 |
%%%%%%%%%%%%%%%%%%%%% Solving the Dual problem%%%%%%%%%%%%%%%%%%%%%%%%%%%5 |
|
|
70 |
% --------------------------------- |
|
|
71 |
% data points |
|
|
72 |
% ----------------------------------- |
|
|
73 |
[d N] = size(P); |
|
|
74 |
|
|
|
75 |
Q = zeros(d+1,N); |
|
|
76 |
Q(1:d,:) = P(1:d,1:N); |
|
|
77 |
Q(d+1,:) = ones(1,N); |
|
|
78 |
|
|
|
79 |
|
|
|
80 |
% initializations |
|
|
81 |
% ----------------------------------- |
|
|
82 |
count = 1; |
|
|
83 |
err = 1; |
|
|
84 |
u = (1/N) * ones(N,1); % 1st iteration |
|
|
85 |
|
|
|
86 |
|
|
|
87 |
% Khachiyan Algorithm |
|
|
88 |
% ----------------------------------- |
|
|
89 |
while err > tolerance, |
|
|
90 |
X = Q * diag(u) * Q'; % X = \sum_i ( u_i * q_i * q_i') is a (d+1)x(d+1) matrix |
|
|
91 |
M = diag(Q' * inv(X) * Q); % M the diagonal vector of an NxN matrix |
|
|
92 |
[maximum j] = max(M); |
|
|
93 |
step_size = (maximum - d -1)/((d+1)*(maximum-1)); |
|
|
94 |
new_u = (1 - step_size)*u ; |
|
|
95 |
new_u(j) = new_u(j) + step_size; |
|
|
96 |
count = count + 1; |
|
|
97 |
err = norm(new_u - u); |
|
|
98 |
u = new_u; |
|
|
99 |
end |
|
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
%%%%%%%%%%%%%%%%%%% Computing the Ellipse parameters%%%%%%%%%%%%%%%%%%%%%% |
|
|
104 |
% Finds the ellipse equation in the 'center form': |
|
|
105 |
% (x-c)' * A * (x-c) = 1 |
|
|
106 |
% It computes a dxd matrix 'A' and a d dimensional vector 'c' as the center |
|
|
107 |
% of the ellipse. |
|
|
108 |
|
|
|
109 |
U = diag(u); |
|
|
110 |
|
|
|
111 |
% the A matrix for the ellipse |
|
|
112 |
% -------------------------------------------- |
|
|
113 |
A = (1/d) * inv(P * U * P' - (P * u)*(P*u)' ); |
|
|
114 |
|
|
|
115 |
|
|
|
116 |
% center of the ellipse |
|
|
117 |
% -------------------------------------------- |
|
|
118 |
c = P * u; |