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

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