Daniel@0: function [P] = cca(D, P, epochs, Mdist, alpha0, lambda0) Daniel@0: Daniel@0: %CCA Projects data vectors using Curvilinear Component Analysis. Daniel@0: % Daniel@0: % P = cca(D, P, epochs, [Dist], [alpha0], [lambda0]) Daniel@0: % Daniel@0: % P = cca(D,2,10); % projects the given data to a plane Daniel@0: % P = cca(D,pcaproj(D,2),5); % same, but with PCA initialization Daniel@0: % P = cca(D, 2, 10, Dist); % same, but the given distance matrix is used Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % D (matrix) the data matrix, size dlen x dim Daniel@0: % (struct) data or map struct Daniel@0: % P (scalar) output dimension Daniel@0: % (matrix) size dlen x odim, the initial projection Daniel@0: % epochs (scalar) training length Daniel@0: % [Dist] (matrix) pairwise distance matrix, size dlen x dlen. Daniel@0: % If the distances in the input space should Daniel@0: % be calculated otherwise than as euclidian Daniel@0: % distances, the distance from each vector Daniel@0: % to each other vector can be given here, Daniel@0: % size dlen x dlen. For example PDIST Daniel@0: % function can be used to calculate the Daniel@0: % distances: Dist = squareform(pdist(D,'mahal')); Daniel@0: % [alpha0] (scalar) initial step size, 0.5 by default Daniel@0: % [lambda0] (scalar) initial radius of influence, 3*max(std(D)) by default Daniel@0: % Daniel@0: % P (matrix) size dlen x odim, the projections Daniel@0: % Daniel@0: % Unknown values (NaN's) in the data: projections of vectors with Daniel@0: % unknown components tend to drift towards the center of the Daniel@0: % projection distribution. Projections of totally unknown vectors are Daniel@0: % set to unknown (NaN). Daniel@0: % Daniel@0: % See also SAMMON, PCAPROJ. Daniel@0: Daniel@0: % Reference: Demartines, P., Herault, J., "Curvilinear Component Daniel@0: % Analysis: a Self-Organizing Neural Network for Nonlinear Daniel@0: % Mapping of Data Sets", IEEE Transactions on Neural Networks, Daniel@0: % vol 8, no 1, 1997, pp. 148-154. Daniel@0: Daniel@0: % Contributed to SOM Toolbox 2.0, February 2nd, 2000 by Juha Vesanto Daniel@0: % Copyright (c) by Juha Vesanto Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % juuso 171297 040100 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% Check arguments Daniel@0: Daniel@0: error(nargchk(3, 6, nargin)); % check the number of input arguments Daniel@0: Daniel@0: % input data Daniel@0: if isstruct(D), Daniel@0: if strcmp(D.type,'som_map'), D = D.codebook; else D = D.data; end Daniel@0: end Daniel@0: [noc dim] = size(D); Daniel@0: noc_x_1 = ones(noc, 1); % used frequently Daniel@0: me = zeros(1,dim); st = zeros(1,dim); Daniel@0: for i=1:dim, Daniel@0: me(i) = mean(D(find(isfinite(D(:,i))),i)); Daniel@0: st(i) = std(D(find(isfinite(D(:,i))),i)); Daniel@0: end Daniel@0: Daniel@0: % initial projection Daniel@0: if prod(size(P))==1, Daniel@0: P = (2*rand(noc,P)-1).*st(noc_x_1,1:P) + me(noc_x_1,1:P); Daniel@0: else Daniel@0: % replace unknown projections with known values Daniel@0: inds = find(isnan(P)); P(inds) = rand(size(inds)); Daniel@0: end Daniel@0: [dummy odim] = size(P); Daniel@0: odim_x_1 = ones(odim, 1); % this is used frequently Daniel@0: Daniel@0: % training length Daniel@0: train_len = epochs*noc; Daniel@0: Daniel@0: % random sample order Daniel@0: rand('state',sum(100*clock)); Daniel@0: sample_inds = ceil(noc*rand(train_len,1)); Daniel@0: Daniel@0: % mutual distances Daniel@0: if nargin<4 | isempty(Mdist) | all(isnan(Mdist(:))), Daniel@0: fprintf(2, 'computing mutual distances\r'); Daniel@0: dim_x_1 = ones(dim,1); Daniel@0: for i = 1:noc, Daniel@0: x = D(i,:); Daniel@0: Diff = D - x(noc_x_1,:); Daniel@0: N = isnan(Diff); Daniel@0: Diff(find(N)) = 0; Daniel@0: Mdist(:,i) = sqrt((Diff.^2)*dim_x_1); Daniel@0: N = find(sum(N')==dim); %mutual distance unknown Daniel@0: if ~isempty(N), Mdist(N,i) = NaN; end Daniel@0: end Daniel@0: else Daniel@0: % if the distance matrix is output from PDIST function Daniel@0: if size(Mdist,1)==1, Mdist = squareform(Mdist); end Daniel@0: if size(Mdist,1)~=noc, Daniel@0: error('Mutual distance matrix size and data set size do not match'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % alpha and lambda Daniel@0: if nargin<5 | isempty(alpha0) | isnan(alpha0), alpha0 = 0.5; end Daniel@0: alpha = potency_curve(alpha0,alpha0/100,train_len); Daniel@0: Daniel@0: if nargin<6 | isempty(lambda0) | isnan(lambda0), lambda0 = max(st)*3; end Daniel@0: lambda = potency_curve(lambda0,0.01,train_len); Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% Action Daniel@0: Daniel@0: k=0; fprintf(2, 'iterating: %d / %d epochs\r',k,epochs); Daniel@0: Daniel@0: for i=1:train_len, Daniel@0: Daniel@0: ind = sample_inds(i); % sample index Daniel@0: dx = Mdist(:,ind); % mutual distances in input space Daniel@0: known = find(~isnan(dx)); % known distances Daniel@0: Daniel@0: if ~isempty(known), Daniel@0: % sample vector's projection Daniel@0: y = P(ind,:); Daniel@0: Daniel@0: % distances in output space Daniel@0: Dy = P(known,:) - y(noc_x_1(known),:); Daniel@0: dy = sqrt((Dy.^2)*odim_x_1); Daniel@0: Daniel@0: % relative effect Daniel@0: dy(find(dy==0)) = 1; % to get rid of div-by-zero's Daniel@0: fy = exp(-dy/lambda(i)) .* (dx(known) ./ dy - 1); Daniel@0: Daniel@0: % Note that the function F here is e^(-dy/lambda)) Daniel@0: % instead of the bubble function 1(lambda-dy) used in the Daniel@0: % paper. Daniel@0: Daniel@0: % Note that here a simplification has been made: the derivatives of the Daniel@0: % F function have been ignored in calculating the gradient of error Daniel@0: % function w.r.t. to changes in dy. Daniel@0: Daniel@0: % update Daniel@0: P(known,:) = P(known,:) + alpha(i)*fy(:,odim_x_1).*Dy; Daniel@0: end Daniel@0: Daniel@0: % track Daniel@0: if rem(i,noc)==0, Daniel@0: k=k+1; fprintf(2, 'iterating: %d / %d epochs\r',k,epochs); Daniel@0: end Daniel@0: Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% clear up Daniel@0: Daniel@0: % calculate error Daniel@0: error = cca_error(P,Mdist,lambda(train_len)); Daniel@0: fprintf(2,'%d iterations, error %f \n', epochs, error); Daniel@0: Daniel@0: % set projections of totally unknown vectors as unknown Daniel@0: unknown = find(sum(isnan(D)')==dim); Daniel@0: P(unknown,:) = NaN; Daniel@0: Daniel@0: return; Daniel@0: Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% tips Daniel@0: Daniel@0: % to plot the results, use the code below Daniel@0: Daniel@0: %subplot(2,1,1), Daniel@0: %switch(odim), Daniel@0: % case 1, plot(P(:,1),ones(dlen,1),'x') Daniel@0: % case 2, plot(P(:,1),P(:,2),'x'); Daniel@0: % otherwise, plot3(P(:,1),P(:,2),P(:,3),'x'); rotate3d on Daniel@0: %end Daniel@0: %subplot(2,1,2), dydxplot(P,Mdist); Daniel@0: Daniel@0: % to a project a new point x in the input space to the output space Daniel@0: % do the following: Daniel@0: Daniel@0: % Diff = D - x(noc_x_1,:); Diff(find(isnan(Diff))) = 0; Daniel@0: % dx = sqrt((Diff.^2)*dim_x_1); Daniel@0: % p = project_point(P,x,dx); % this function can be found from below Daniel@0: % tlen = size(p,1); Daniel@0: % plot(P(:,1),P(:,2),'bx',p(tlen,1),p(tlen,2),'ro',p(:,1),p(:,2),'r-') Daniel@0: Daniel@0: % similar trick can be made to the other direction Daniel@0: Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% subfunctions Daniel@0: Daniel@0: function vals = potency_curve(v0,vn,l) Daniel@0: Daniel@0: % curve that decreases from v0 to vn with a rate that is Daniel@0: % somewhere between linear and 1/t Daniel@0: vals = v0 * (vn/v0).^([0:(l-1)]/(l-1)); Daniel@0: Daniel@0: Daniel@0: function error = cca_error(P,Mdist,lambda) Daniel@0: Daniel@0: [noc odim] = size(P); Daniel@0: noc_x_1 = ones(noc,1); Daniel@0: odim_x_1 = ones(odim,1); Daniel@0: Daniel@0: error = 0; Daniel@0: for i=1:noc, Daniel@0: known = find(~isnan(Mdist(:,i))); Daniel@0: if ~isempty(known), Daniel@0: y = P(i,:); Daniel@0: Dy = P(known,:) - y(noc_x_1(known),:); Daniel@0: dy = sqrt((Dy.^2)*odim_x_1); Daniel@0: fy = exp(-dy/lambda); Daniel@0: error = error + sum(((Mdist(known,i) - dy).^2).*fy); Daniel@0: end Daniel@0: end Daniel@0: error = error/2; Daniel@0: Daniel@0: Daniel@0: function [] = dydxplot(P,Mdist) Daniel@0: Daniel@0: [noc odim] = size(P); Daniel@0: noc_x_1 = ones(noc,1); Daniel@0: odim_x_1 = ones(odim,1); Daniel@0: Pdist = zeros(noc,noc); Daniel@0: Daniel@0: for i=1:noc, Daniel@0: y = P(i,:); Daniel@0: Dy = P - y(noc_x_1,:); Daniel@0: Pdist(:,i) = sqrt((Dy.^2)*odim_x_1); Daniel@0: end Daniel@0: Daniel@0: Pdist = tril(Pdist,-1); Daniel@0: inds = find(Pdist > 0); Daniel@0: n = length(inds); Daniel@0: plot(Pdist(inds),Mdist(inds),'.'); Daniel@0: xlabel('dy'), ylabel('dx') Daniel@0: Daniel@0: Daniel@0: function p = project_point(P,x,dx) Daniel@0: Daniel@0: [noc odim] = size(P); Daniel@0: noc_x_1 = ones(noc,1); Daniel@0: odim_x_1 = ones(odim,1); Daniel@0: Daniel@0: % initial projection Daniel@0: [dummy,i] = min(dx); Daniel@0: y = P(i,:)+rand(1,odim)*norm(P(i,:))/20; Daniel@0: Daniel@0: % lambda Daniel@0: lambda = norm(std(P)); Daniel@0: Daniel@0: % termination Daniel@0: eps = 1e-3; i_max = noc*10; Daniel@0: Daniel@0: i=1; p(i,:) = y; Daniel@0: ready = 0; Daniel@0: while ~ready, Daniel@0: Daniel@0: % mutual distances Daniel@0: Dy = P - y(noc_x_1,:); % differences in output space Daniel@0: dy = sqrt((Dy.^2)*odim_x_1); % distances in output space Daniel@0: f = exp(-dy/lambda); Daniel@0: Daniel@0: fprintf(2,'iteration %d, error %g \r',i,sum(((dx - dy).^2).*f)); Daniel@0: Daniel@0: % all the other vectors push the projected one Daniel@0: fy = f .* (dx ./ dy - 1) / sum(f); Daniel@0: Daniel@0: % update Daniel@0: step = - sum(fy(:,odim_x_1).*Dy); Daniel@0: y = y + step; Daniel@0: Daniel@0: i=i+1; Daniel@0: p(i,:) = y; Daniel@0: ready = (norm(step)/norm(y) < eps | i > i_max); Daniel@0: Daniel@0: end Daniel@0: fprintf(2,'\n'); Daniel@0: Daniel@0: