From dc8304dc0562f0fd1fdbf85447ad62c6e68d16cb Mon Sep 17 00:00:00 2001 From: madratman Date: Thu, 2 Jul 2015 03:18:21 -0400 Subject: [PATCH 1/6] fixed deprecated bingham_fit_mlesac.m --- matlab/deprecated/bingham_fit_mlesac.m | 148 +++++++++++++------------ 1 file changed, 77 insertions(+), 71 deletions(-) diff --git a/matlab/deprecated/bingham_fit_mlesac.m b/matlab/deprecated/bingham_fit_mlesac.m index 3e813da..08306cc 100644 --- a/matlab/deprecated/bingham_fit_mlesac.m +++ b/matlab/deprecated/bingham_fit_mlesac.m @@ -1,71 +1,77 @@ -function [B outliers] = bingham_fit_mlesac(X) -% [B outliers] = bingham_fit_mlesac(X) -- where B = B.{V, Z, F} - -d = size(X,1); -n = size(X,2); - -iter = 100; -p0 = 1 / surface_area_hypersphere(d-1); % uniform density for outliers -logp0 = log(p0); - -pmax = -inf; - -for i=1:iter - - % pick d points at random from X - r = randperm(n); - r = r(1:d); - Xi = X(:,r); - - % fit a Bingham to the d points - [V Z F] = bingham_fit(Xi); - %[V Z F] = bingham_fit_scatter(Xi*Xi') - - % compute data log likelihood - logp = 0; - for j=1:n - p = bingham_pdf(X(:,j), V, Z, F); - if p > p0 - logp = logp + log(p); - else - logp = logp + logp0; - end - end - - if logp > pmax - pmax = logp; - B.V = V; - B.Z = Z; - B.F = F; - - %fprintf('*** found new best with log likelihood %f ***\n', logp); - - %figure(20); - %plot_bingham_3d(V,Z,F,X'); - %figure(21); - %plot_bingham_3d_projections(V,Z,F); - - end -end - -% find inliers/outliers and fit the Bingham to all the inliers - -L = zeros(1,n); -for j=1:n - p = bingham_pdf(X(:,j), B.V, B.Z, B.F); - if p > p0 - L(j) = 1; - else - L(j) = 0; - end -end - -inliers = find(L); -outliers = find(~L); - -[B.V B.Z B.F] = bingham_fit(X(:,inliers)); - - - - - +function [B outliers] = bingham_fit_mlesac(X) +% [B outliers] = bingham_fit_mlesac(X) -- where B = B.{V, Z, F} +X = X'; +d = size(X,1) +n = size(X,2) + +iter = 100; +p0 = 1 / surface_area_hypersphere(d-1); % uniform density for outliers +logp0 = log(p0); + +pmax = -inf; + +for i=1:iter + + % pick d points at random from X + r = randperm(n); + r = r(1:d) + for j=1:d + eval(['X_' num2str(j) '= X(:,r(j));']) +% Xi = X(:,r); + end + % fit a Bingham to the d points + X_combined = [X_1 X_2 X_3 X_4]; + X_combined = X_combined'; + bing_X_combined = bingham_fit(X_combined); + %[V Z F] = bingham_fit_scatter(Xi*Xi') + V = bing_X_combined.V; + Z = bing_X_combined.Z; + F = bing_X_combined.F; + % compute data log likelihood + logp = 0; + + for j=1:n + p = bingham_pdf(X(:,j)', bing_X_combined); + if p > p0 + logp = logp + log(p); + else + logp = logp + logp0; + end + end + + if logp > pmax + pmax = logp; + bing_X_combined.V = V; + bing_X_combined.Z = Z; + bing_X_combined.F = F; + + %fprintf('*** found new best with log likelihood %f ***\n', logp); + + %figure(20); + %plot_bingham_3d(V,Z,F,X'); + %figure(21); + %plot_bingham_3d_projections(V,Z,F); + + end +end + +% find inliers/outliers and fit the Bingham to all the inliers + +L = zeros(1,n); +for j=1:n + p = bingham_pdf(X(:,j), bing_X_combined); + if p > p0 + L(j) = 1; + else + L(j) = 0; + end +end + +inliers = find(L); +outliers = find(~L); + +bing_return = bingham_fit(X(:,inliers)'); +B = bing_return; + + + From 2214522fdc979e3a3d10607993af2d3d7e4590d6 Mon Sep 17 00:00:00 2001 From: madratman Date: Thu, 2 Jul 2015 03:18:55 -0400 Subject: [PATCH 2/6] fixed deprecated bingham_cluster.m --- matlab/deprecated/bingham_cluster.m | 46 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/matlab/deprecated/bingham_cluster.m b/matlab/deprecated/bingham_cluster.m index b71b759..45f3100 100644 --- a/matlab/deprecated/bingham_cluster.m +++ b/matlab/deprecated/bingham_cluster.m @@ -1,18 +1,28 @@ -function [B cnts] = bingham_cluster(X, min_points) -% [B cnts] = bingham_cluster(X, min_points) -- where each B(i) contains fields V, Z, F - -if nargin < 2 - min_points = 20; -end - -for i=1:100 - - [B(i) outliers] = bingham_fit_mlesac(X); - cnts(i) = size(X,2) - length(outliers); - - if length(outliers) < min_points - break; - end - - X = X(:, outliers); -end +function [B cnts] = bingham_cluster(X, min_points) +% [B cnts] = bingham_cluster(X, min_points) -- where each B(i) contains fields V, Z, F + +if nargin < 2 + min_points = 20; +end + +for i=1:6 + + [B(i) outliers] = bingham_fit_mlesac(X); +% outliers +% ouliers is a row vector +% size(X,2) +% i + cnts(i) = size(X,1) - length(outliers); + + if length(outliers) < min_points + break; + end + X_updater = zeros(length(outliers), size(X,2)); +% size(X,1) +% size(X,2) + for j = 1:length(outliers) + X_updater(j,:) = X(outliers(j),:); + end + X = X_updater; + size(X) +end From 29938ddaa10c357eb45c94e3ffdc23d21761c674 Mon Sep 17 00:00:00 2001 From: madratman Date: Thu, 2 Jul 2015 04:15:44 -0400 Subject: [PATCH 3/6] adds comments, fixes upper bound back to 100 --- matlab/deprecated/bingham_cluster.m | 30 +++++++++++++---------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/matlab/deprecated/bingham_cluster.m b/matlab/deprecated/bingham_cluster.m index 45f3100..afe169c 100644 --- a/matlab/deprecated/bingham_cluster.m +++ b/matlab/deprecated/bingham_cluster.m @@ -1,28 +1,24 @@ -function [B cnts] = bingham_cluster(X, min_points) -% [B cnts] = bingham_cluster(X, min_points) -- where each B(i) contains fields V, Z, F +function [B weights] = bingham_cluster(X, min_points) +% [B weights] = bingham_cluster(X, min_points) -- where each B(i) contains fields V, Z, F if nargin < 2 min_points = 20; end -for i=1:6 - - [B(i) outliers] = bingham_fit_mlesac(X); -% outliers -% ouliers is a row vector -% size(X,2) -% i - cnts(i) = size(X,1) - length(outliers); - - if length(outliers) < min_points - break; - end +for i=1:100 %max no of clusters + [B(i) outliers] = bingham_fit_mlesac(X); + % ouliers is a row vector + weights(i) = size(X,1) - length(outliers); + % weight of a particular bingham is no of points left - no of ouliers + if length(outliers) < min_points + break; + end X_updater = zeros(length(outliers), size(X,2)); -% size(X,1) -% size(X,2) + % initialize the outlier data for next iteration to zeros for j = 1:length(outliers) X_updater(j,:) = X(outliers(j),:); end + % get them outliers X = X_updater; - size(X) + % update the outliers for the next iteration end From 5b0ed1d1b2b8bac69a3998c42d6eca95a3d6ee4d Mon Sep 17 00:00:00 2001 From: madratman Date: Thu, 2 Jul 2015 04:24:37 -0400 Subject: [PATCH 4/6] comments and cleanup for mlesac.m --- matlab/deprecated/bingham_fit_mlesac.m | 34 ++++++++------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/matlab/deprecated/bingham_fit_mlesac.m b/matlab/deprecated/bingham_fit_mlesac.m index 08306cc..2a08bb0 100644 --- a/matlab/deprecated/bingham_fit_mlesac.m +++ b/matlab/deprecated/bingham_fit_mlesac.m @@ -1,8 +1,8 @@ function [B outliers] = bingham_fit_mlesac(X) % [B outliers] = bingham_fit_mlesac(X) -- where B = B.{V, Z, F} X = X'; -d = size(X,1) -n = size(X,2) +d = size(X,1); +n = size(X,2); iter = 100; p0 = 1 / surface_area_hypersphere(d-1); % uniform density for outliers @@ -11,25 +11,20 @@ pmax = -inf; for i=1:iter - - % pick d points at random from X + % pick d points at random from X -> put them into X_1, X_2, X_3, X_4 r = randperm(n); r = r(1:d) for j=1:d eval(['X_' num2str(j) '= X(:,r(j));']) -% Xi = X(:,r); end - % fit a Bingham to the d points X_combined = [X_1 X_2 X_3 X_4]; X_combined = X_combined'; + + % fit a Bingham to the d points bing_X_combined = bingham_fit(X_combined); - %[V Z F] = bingham_fit_scatter(Xi*Xi') - V = bing_X_combined.V; - Z = bing_X_combined.Z; - F = bing_X_combined.F; + % compute data log likelihood logp = 0; - for j=1:n p = bingham_pdf(X(:,j)', bing_X_combined); if p > p0 @@ -39,24 +34,14 @@ end end + % update the threshold if logp > pmax pmax = logp; - bing_X_combined.V = V; - bing_X_combined.Z = Z; - bing_X_combined.F = F; - - %fprintf('*** found new best with log likelihood %f ***\n', logp); - - %figure(20); - %plot_bingham_3d(V,Z,F,X'); - %figure(21); - %plot_bingham_3d_projections(V,Z,F); - end -end -% find inliers/outliers and fit the Bingham to all the inliers +end +% find inliers/outliers L = zeros(1,n); for j=1:n p = bingham_pdf(X(:,j), bing_X_combined); @@ -70,6 +55,7 @@ inliers = find(L); outliers = find(~L); +% fit a Bingham to all the inliers bing_return = bingham_fit(X(:,inliers)'); B = bing_return; From 3d5eff6558385fbfc82eedeea42c3ad030530d9b Mon Sep 17 00:00:00 2001 From: madratman Date: Thu, 2 Jul 2015 04:29:37 -0400 Subject: [PATCH 5/6] changes directory to undeprecate --- matlab/{deprecated => }/bingham_cluster.m | 0 matlab/{deprecated => }/bingham_fit_mlesac.m | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename matlab/{deprecated => }/bingham_cluster.m (100%) rename matlab/{deprecated => }/bingham_fit_mlesac.m (100%) diff --git a/matlab/deprecated/bingham_cluster.m b/matlab/bingham_cluster.m similarity index 100% rename from matlab/deprecated/bingham_cluster.m rename to matlab/bingham_cluster.m diff --git a/matlab/deprecated/bingham_fit_mlesac.m b/matlab/bingham_fit_mlesac.m similarity index 100% rename from matlab/deprecated/bingham_fit_mlesac.m rename to matlab/bingham_fit_mlesac.m From b29e56641eae63858543c50e055aced53239ba9d Mon Sep 17 00:00:00 2001 From: madratman Date: Thu, 2 Jul 2015 04:52:25 -0400 Subject: [PATCH 6/6] accomodates previous PR, minor changes --- matlab/bingham_cluster.m | 5 ++++- matlab/bingham_fit_mlesac.m | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/matlab/bingham_cluster.m b/matlab/bingham_cluster.m index afe169c..8f077d3 100644 --- a/matlab/bingham_cluster.m +++ b/matlab/bingham_cluster.m @@ -6,9 +6,12 @@ end for i=1:100 %max no of clusters + fprintf('Bingham distrubution #: %d', i); [B(i) outliers] = bingham_fit_mlesac(X); - % ouliers is a row vector + % outliers is a row vector weights(i) = size(X,1) - length(outliers); + + fprintf('no of points left are', weights(i)); % weight of a particular bingham is no of points left - no of ouliers if length(outliers) < min_points break; diff --git a/matlab/bingham_fit_mlesac.m b/matlab/bingham_fit_mlesac.m index 2a08bb0..05f1aa5 100644 --- a/matlab/bingham_fit_mlesac.m +++ b/matlab/bingham_fit_mlesac.m @@ -11,9 +11,11 @@ pmax = -inf; for i=1:iter + fprintf('mlesac iteration: %d', i); + % pick d points at random from X -> put them into X_1, X_2, X_3, X_4 r = randperm(n); - r = r(1:d) + r = r(1:d); for j=1:d eval(['X_' num2str(j) '= X(:,r(j));']) end @@ -44,7 +46,7 @@ % find inliers/outliers L = zeros(1,n); for j=1:n - p = bingham_pdf(X(:,j), bing_X_combined); + p = bingham_pdf(X(:,j)', bing_X_combined); if p > p0 L(j) = 1; else @@ -54,6 +56,7 @@ inliers = find(L); outliers = find(~L); +fprintf('no of outliers were %d', outliers); % fit a Bingham to all the inliers bing_return = bingham_fit(X(:,inliers)');