wolffd@0: function [adm,admu,tdmu] = som_distortion(sM, D, arg1, arg2) wolffd@0: wolffd@0: %SOM_DISTORTION Calculate distortion measure for the map. wolffd@0: % wolffd@0: % [adm,admu,tdmu] = som_distortion(sMap, D, [radius], ['prob']) wolffd@0: % wolffd@0: % adm = som_distortion(sMap,D); wolffd@0: % [adm,admu] = som_distortion(sMap,D); wolffd@0: % som_show(sMap,'color',admu); wolffd@0: % wolffd@0: % Input and output arguments: wolffd@0: % sMap (struct) a map struct wolffd@0: % D (struct) a data struct wolffd@0: % (matrix) size dlen x dim, a data matrix wolffd@0: % [radius] (scalar) neighborhood function radius to be used. wolffd@0: % Defaults to the last radius_fin in the wolffd@0: % trainhist field of the map struct, or 1 if wolffd@0: % that is missing. wolffd@0: % ['prob'] (string) If given, this argument forces the wolffd@0: % neigborhood function values for each map wolffd@0: % unit to be normalized so that they sum to 1. wolffd@0: % wolffd@0: % adm (scalar) average distortion measure (sum(dm)/dlen) wolffd@0: % admu (vector) size munits x 1, average distortion in each unit wolffd@0: % tdmu (vector) size munits x 1, total distortion for each unit wolffd@0: % wolffd@0: % The distortion measure is defined as: wolffd@0: % 2 wolffd@0: % E = sum sum h(bmu(i),j) ||m(j) - x(i)|| wolffd@0: % i j wolffd@0: % wolffd@0: % where m(i) is the ith prototype vector of SOM, x(j) is the jth data wolffd@0: % vector, and h(.,.) is the neighborhood function. In case of fixed wolffd@0: % neighborhood and discreet data, the distortion measure can be wolffd@0: % interpreted as the energy function of the SOM. Note, though, that wolffd@0: % the learning rule that follows from the distortion measure is wolffd@0: % different from the SOM training rule, so SOM only minimizes the wolffd@0: % distortion measure approximately. wolffd@0: % wolffd@0: % If the 'prob' argument is given, the distortion measure can be wolffd@0: % interpreted as an expected quantization error when the neighborhood wolffd@0: % function values give the likelyhoods of accidentally assigning wolffd@0: % vector j to unit i. The normal quantization error is a special case wolffd@0: % of this with zero incorrect assignement likelihood. wolffd@0: % wolffd@0: % NOTE: when calculating BMUs and distances, the mask of the given wolffd@0: % map is used. wolffd@0: % wolffd@0: % See also SOM_QUALITY, SOM_BMUS, SOM_HITS. wolffd@0: wolffd@0: % Reference: Kohonen, T., "Self-Organizing Map", 2nd ed., wolffd@0: % Springer-Verlag, Berlin, 1995, pp. 120-121. wolffd@0: % wolffd@0: % Graepel, T., Burger, M. and Obermayer, K., wolffd@0: % "Phase Transitions in Stochastic Self-Organizing Maps", wolffd@0: % Physical Review E, Vol 56, No 4, pp. 3876-3890 (1997). wolffd@0: wolffd@0: % Contributed to SOM Toolbox vs2, Feb 3rd, 2000 by Juha Vesanto wolffd@0: % Copyright (c) by Juha Vesanto wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Version 2.0beta juuso 030200 wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% check arguments wolffd@0: wolffd@0: % input arguments wolffd@0: if nargin < 2, error('Not enough input arguments.'); end wolffd@0: wolffd@0: % map wolffd@0: M = sM.codebook; wolffd@0: munits = prod(sM.topol.msize); wolffd@0: wolffd@0: % data wolffd@0: if isstruct(D), D = D.data; end wolffd@0: [dlen dim] = size(D); wolffd@0: wolffd@0: % arg1, arg2 wolffd@0: rad = NaN; wolffd@0: normalize = 0; wolffd@0: if nargin>2, wolffd@0: if isnumeric(arg1), rad = arg1; wolffd@0: elseif ischar(arg1) & strcmp(arg1,'prob'), normalize = 0; wolffd@0: end wolffd@0: end wolffd@0: if nargin>3, wolffd@0: if isnumeric(arg2), rad = arg2; wolffd@0: elseif ischar(arg2) & strcmp(arg2,'prob'), normalize = 0; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % neighborhood radius wolffd@0: if isempty(rad) | isnan(rad), wolffd@0: if ~isempty(sM.trainhist), rad = sM.trainhist(end).radius_fin; wolffd@0: else rad = 1; wolffd@0: end wolffd@0: end wolffd@0: if rad0); wolffd@0: admu(ind) = admu(ind) ./ hits(ind); wolffd@0: wolffd@0: % average distortion measure wolffd@0: adm = sum(tdmu)/dlen; wolffd@0: wolffd@0: return; wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: