Skip to content

Commit

Permalink
matlab version with variable lengths and init with medoid
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Petitjean committed Aug 8, 2014
1 parent 2366998 commit d506b21
Showing 1 changed file with 77 additions and 25 deletions.
102 changes: 77 additions & 25 deletions DBA.m
Original file line number Diff line number Diff line change
@@ -1,27 +1,71 @@
/*******************************************************************************
* Copyright (C) 2013 Francois PETITJEAN, Ioannis PAPARRIZOS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.well
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
%*******************************************************************************
% Copyright (C) 2013 Francois PETITJEAN, Ioannis PAPARRIZOS
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, version 3 of the License.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.well
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%*****************************************************************************/

% function average = DBA(sequences)
% index=randi(length(sequences),1);
% average=repmat(sequences{index},1);
% for i=1:15
% average=DBA_one_iteration(average,sequences);
% end
% end

function average = DBA(sequences)
index=randi(size(sequences,1),1);
average=sequences(index,:);
average = repmat(sequences{medoidIndex(sequences)},1);
for i=1:15
average=DBA_one_iteration(average,sequences);
end
end

function sos = sumOfSquares(s,sequences)
sos = 0.0;
for i=1:length(sequences)
dist = dtw(s,sequences{i});
sos = sos + dist * dist;
end
end

function score = dtw(S,T)
costM = zeros(length(S),length(T));

costM(1,1) = (S(1)-T(1))^2;
for i=2:length(S)
costM(i,1)= costM(i-1,1)+ (S(i)-T(1))^2;
end
for i=2:length(T)
costM(1,i)= costM(1,i-1)+ (S(1)-T(i))^2;
end
for i=2:length(S)
for j=2:length(T)
costM(i,j)=min(min(costM(i-1,j-1),costM(i,j-1)),costM(i-1,j))+(S(i)-T(j))^2;
end
end
score = sqrt(costM(length(S),length(T)));
end

function index = medoidIndex(sequences)
index = -1;
lowestInertia = Inf;
for i=1:length(sequences)
tmpInertia = sumOfSquares(sequences{i},sequences);
if (tmpInertia < lowestInertia)
index = i;
lowestInertia = tmpInertia;
end
end
end


function average = DBA_one_iteration(averageS,sequences)

Expand All @@ -30,11 +74,11 @@
tupleAssociation{t}=[];
end

costMatrix = [];
pathMatrix = [];
costMatrix = zeros(1000,1000);
pathMatrix = zeros(1000,1000);

for k=1:size(sequences,1)
sequence = sequences(k,:);
for k=1:length(sequences)
sequence = sequences{k};
costMatrix(1,1) = distanceTo(averageS(1),sequence(1));
pathMatrix(1,1) = -1;
for i=2:size(averageS,2)
Expand Down Expand Up @@ -118,8 +162,16 @@
function dist = distanceTo(a,b)
dist=(a-b)*(a-b);
end

sequences=rand(100,20);
mean=DBA(sequences)
plot(mean);

function ex = test()
sequences = {};
sequences{100}=[];
for i=1:100
length = randi(100);
sequences{i}=rand(1,length);
end
mean=DBA(sequences);
end



0 comments on commit d506b21

Please sign in to comment.