annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_distortion.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [adm,admu,tdmu] = som_distortion(sM, D, arg1, arg2)
wolffd@0 2
wolffd@0 3 %SOM_DISTORTION Calculate distortion measure for the map.
wolffd@0 4 %
wolffd@0 5 % [adm,admu,tdmu] = som_distortion(sMap, D, [radius], ['prob'])
wolffd@0 6 %
wolffd@0 7 % adm = som_distortion(sMap,D);
wolffd@0 8 % [adm,admu] = som_distortion(sMap,D);
wolffd@0 9 % som_show(sMap,'color',admu);
wolffd@0 10 %
wolffd@0 11 % Input and output arguments:
wolffd@0 12 % sMap (struct) a map struct
wolffd@0 13 % D (struct) a data struct
wolffd@0 14 % (matrix) size dlen x dim, a data matrix
wolffd@0 15 % [radius] (scalar) neighborhood function radius to be used.
wolffd@0 16 % Defaults to the last radius_fin in the
wolffd@0 17 % trainhist field of the map struct, or 1 if
wolffd@0 18 % that is missing.
wolffd@0 19 % ['prob'] (string) If given, this argument forces the
wolffd@0 20 % neigborhood function values for each map
wolffd@0 21 % unit to be normalized so that they sum to 1.
wolffd@0 22 %
wolffd@0 23 % adm (scalar) average distortion measure (sum(dm)/dlen)
wolffd@0 24 % admu (vector) size munits x 1, average distortion in each unit
wolffd@0 25 % tdmu (vector) size munits x 1, total distortion for each unit
wolffd@0 26 %
wolffd@0 27 % The distortion measure is defined as:
wolffd@0 28 % 2
wolffd@0 29 % E = sum sum h(bmu(i),j) ||m(j) - x(i)||
wolffd@0 30 % i j
wolffd@0 31 %
wolffd@0 32 % where m(i) is the ith prototype vector of SOM, x(j) is the jth data
wolffd@0 33 % vector, and h(.,.) is the neighborhood function. In case of fixed
wolffd@0 34 % neighborhood and discreet data, the distortion measure can be
wolffd@0 35 % interpreted as the energy function of the SOM. Note, though, that
wolffd@0 36 % the learning rule that follows from the distortion measure is
wolffd@0 37 % different from the SOM training rule, so SOM only minimizes the
wolffd@0 38 % distortion measure approximately.
wolffd@0 39 %
wolffd@0 40 % If the 'prob' argument is given, the distortion measure can be
wolffd@0 41 % interpreted as an expected quantization error when the neighborhood
wolffd@0 42 % function values give the likelyhoods of accidentally assigning
wolffd@0 43 % vector j to unit i. The normal quantization error is a special case
wolffd@0 44 % of this with zero incorrect assignement likelihood.
wolffd@0 45 %
wolffd@0 46 % NOTE: when calculating BMUs and distances, the mask of the given
wolffd@0 47 % map is used.
wolffd@0 48 %
wolffd@0 49 % See also SOM_QUALITY, SOM_BMUS, SOM_HITS.
wolffd@0 50
wolffd@0 51 % Reference: Kohonen, T., "Self-Organizing Map", 2nd ed.,
wolffd@0 52 % Springer-Verlag, Berlin, 1995, pp. 120-121.
wolffd@0 53 %
wolffd@0 54 % Graepel, T., Burger, M. and Obermayer, K.,
wolffd@0 55 % "Phase Transitions in Stochastic Self-Organizing Maps",
wolffd@0 56 % Physical Review E, Vol 56, No 4, pp. 3876-3890 (1997).
wolffd@0 57
wolffd@0 58 % Contributed to SOM Toolbox vs2, Feb 3rd, 2000 by Juha Vesanto
wolffd@0 59 % Copyright (c) by Juha Vesanto
wolffd@0 60 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 61
wolffd@0 62 % Version 2.0beta juuso 030200
wolffd@0 63
wolffd@0 64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 65 %% check arguments
wolffd@0 66
wolffd@0 67 % input arguments
wolffd@0 68 if nargin < 2, error('Not enough input arguments.'); end
wolffd@0 69
wolffd@0 70 % map
wolffd@0 71 M = sM.codebook;
wolffd@0 72 munits = prod(sM.topol.msize);
wolffd@0 73
wolffd@0 74 % data
wolffd@0 75 if isstruct(D), D = D.data; end
wolffd@0 76 [dlen dim] = size(D);
wolffd@0 77
wolffd@0 78 % arg1, arg2
wolffd@0 79 rad = NaN;
wolffd@0 80 normalize = 0;
wolffd@0 81 if nargin>2,
wolffd@0 82 if isnumeric(arg1), rad = arg1;
wolffd@0 83 elseif ischar(arg1) & strcmp(arg1,'prob'), normalize = 0;
wolffd@0 84 end
wolffd@0 85 end
wolffd@0 86 if nargin>3,
wolffd@0 87 if isnumeric(arg2), rad = arg2;
wolffd@0 88 elseif ischar(arg2) & strcmp(arg2,'prob'), normalize = 0;
wolffd@0 89 end
wolffd@0 90 end
wolffd@0 91
wolffd@0 92 % neighborhood radius
wolffd@0 93 if isempty(rad) | isnan(rad),
wolffd@0 94 if ~isempty(sM.trainhist), rad = sM.trainhist(end).radius_fin;
wolffd@0 95 else rad = 1;
wolffd@0 96 end
wolffd@0 97 end
wolffd@0 98 if rad<eps, rad = eps; end
wolffd@0 99
wolffd@0 100 % neighborhood
wolffd@0 101 Ud = som_unit_dists(sM.topol);
wolffd@0 102 switch sM.neigh,
wolffd@0 103 case 'bubble', H = (Ud <= rad);
wolffd@0 104 case 'gaussian', H = exp(-(Ud.^2)/(2*rad*rad));
wolffd@0 105 case 'cutgauss', H = exp(-(Ud.^2)/(2*rad*rad)) .* (Ud <= rad);
wolffd@0 106 case 'ep', H = (1 - (Ud.^2)/rad) .* (Ud <= rad);
wolffd@0 107 end
wolffd@0 108 if normalize,
wolffd@0 109 for i=1:munits, H(:,i) = H(:,i)/sum(H(:,i)); end
wolffd@0 110 end
wolffd@0 111
wolffd@0 112 % total distortion measure
wolffd@0 113 mu_x_1 = ones(munits,1);
wolffd@0 114 tdmu = zeros(munits,1);
wolffd@0 115 hits = zeros(munits,1);
wolffd@0 116 for i=1:dlen,
wolffd@0 117 x = D(i,:); % data sample
wolffd@0 118 known = ~isnan(x); % its known components
wolffd@0 119 Dx = M(:,known) - x(mu_x_1,known); % each map unit minus the vector
wolffd@0 120 dist2 = (Dx.^2)*sM.mask(known); % squared distances
wolffd@0 121 [qerr bmu] = min(dist2); % find BMU
wolffd@0 122 tdmu = tdmu + dist2.*H(:,bmu); % add to distortion measure
wolffd@0 123 hits(bmu) = hits(bmu)+1; % add to hits
wolffd@0 124 end
wolffd@0 125
wolffd@0 126 % average distortion per unit
wolffd@0 127 admu = tdmu;
wolffd@0 128 ind = find(hits>0);
wolffd@0 129 admu(ind) = admu(ind) ./ hits(ind);
wolffd@0 130
wolffd@0 131 % average distortion measure
wolffd@0 132 adm = sum(tdmu)/dlen;
wolffd@0 133
wolffd@0 134 return;
wolffd@0 135
wolffd@0 136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 137
wolffd@0 138