annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_distortion3.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 [Err,sPropTotal,sPropMunits,sPropComps] = som_distortion3(sM,D,rad)
Daniel@0 2
Daniel@0 3 %SOM_DISTORTION3 Map distortion measures.
Daniel@0 4 %
Daniel@0 5 % [sE,Err] = som_distortion3(sM,[D],[rad]);
Daniel@0 6 %
Daniel@0 7 % sE = som_distortion3(sM);
Daniel@0 8 %
Daniel@0 9 % Input and output arguments ([]'s are optional):
Daniel@0 10 % sM (struct) map struct
Daniel@0 11 % [D] (matrix) a matrix, size dlen x dim
Daniel@0 12 % (struct) data or map struct
Daniel@0 13 % by default the map struct is used
Daniel@0 14 % [rad] (scalar) neighborhood radius, looked from sM.trainhist
Daniel@0 15 % by default, or = 1 if that has no valid values
Daniel@0 16 %
Daniel@0 17 % Err (matrix) size munits x dim x 3
Daniel@0 18 % distortion error elements (quantization error,
Daniel@0 19 % neighborhood bias, and neighborhood variance)
Daniel@0 20 % for each map unit and component
Daniel@0 21 % sPropTotal (struct) .n = length of data
Daniel@0 22 % .h = mean neighborhood function value
Daniel@0 23 % .err = errors
Daniel@0 24 % sPropMunits (struct) .Ni = hits per map unit
Daniel@0 25 % .Hi = sum of neighborhood values for each map unit
Daniel@0 26 % .Err = errors per map unit
Daniel@0 27 % sPropComps (struct) .e1 = total squared distance to centroid
Daniel@0 28 % .eq = total squared distance to BMU
Daniel@0 29 % .Err = errors per component
Daniel@0 30 %
Daniel@0 31 % See also SOM_QUALITY.
Daniel@0 32
Daniel@0 33 % Contributed to SOM Toolbox 2.0, January 3rd, 2002 by Juha Vesanto
Daniel@0 34 % Copyright (c) by Juha Vesanto
Daniel@0 35 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 36
Daniel@0 37 % Version 2.0beta juuso 030102
Daniel@0 38
Daniel@0 39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 40 %% arguments
Daniel@0 41
Daniel@0 42 % map
Daniel@0 43 [munits dim] = size(sM.codebook);
Daniel@0 44
Daniel@0 45 % neighborhood radius
Daniel@0 46 if nargin<3,
Daniel@0 47 if ~isempty(sM.trainhist),
Daniel@0 48 rad = sM.trainhist(end).radius_fin;
Daniel@0 49 else
Daniel@0 50 rad = 1;
Daniel@0 51 end
Daniel@0 52 end
Daniel@0 53 if rad<eps, rad = eps; end
Daniel@0 54 if isempty(rad) | isnan(rad), rad = 1; end
Daniel@0 55
Daniel@0 56 % neighborhood function
Daniel@0 57 Ud = som_unit_dists(sM.topol);
Daniel@0 58 switch sM.neigh,
Daniel@0 59 case 'bubble', H = (Ud <= rad);
Daniel@0 60 case 'gaussian', H = exp(-(Ud.^2)/(2*rad*rad));
Daniel@0 61 case 'cutgauss', H = exp(-(Ud.^2)/(2*rad*rad)) .* (Ud <= rad);
Daniel@0 62 case 'ep', H = (1 - (Ud.^2)/rad) .* (Ud <= rad);
Daniel@0 63 end
Daniel@0 64 Hi = sum(H,2);
Daniel@0 65
Daniel@0 66 % data
Daniel@0 67 if nargin<2, D = sM.codebook; end
Daniel@0 68 if isstruct(D), D = D.data; end
Daniel@0 69 [dlen dim] = size(D);
Daniel@0 70
Daniel@0 71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 72 %% quality measures
Daniel@0 73
Daniel@0 74 % find Voronoi sets, and calculate their properties
Daniel@0 75
Daniel@0 76 [bmus,qerr] = som_bmus(sM,D);
Daniel@0 77 M = sM.codebook;
Daniel@0 78 Vn = M;
Daniel@0 79 Vm = M;
Daniel@0 80 Ni = zeros(munits,dim);
Daniel@0 81 for i=1:munits,
Daniel@0 82 inds = find(bmus==i);
Daniel@0 83 Ni(i,:) = sum(isfinite(D(inds,:)),1); % size of Voronoi set
Daniel@0 84 if any(Ni(i,:)), Vn(i,:) = centroid(D(inds,:),M(i,:)); end % centroid of Voronoi set
Daniel@0 85 Vm(i,:) = centroid(M,M(i,:),H(i,:)'); % centroid of neighborhood
Daniel@0 86 end
Daniel@0 87
Daniel@0 88 HN = repmat(Hi,1,dim).*Ni;
Daniel@0 89
Daniel@0 90 %% distortion
Daniel@0 91
Daniel@0 92 % quantization error (in each Voronoi set and for each component)
Daniel@0 93
Daniel@0 94 Eqx = zeros(munits,dim);
Daniel@0 95 Dx = (Vn(bmus,:) - D).^2;
Daniel@0 96 Dx(isnan(Dx)) = 0;
Daniel@0 97 for i = 1:dim,
Daniel@0 98 Eqx(:,i) = full(sum(sparse(bmus,1:dlen,Dx(:,i),munits,dlen),2));
Daniel@0 99 end
Daniel@0 100 Eqx = repmat(Hi,1,dim).*Eqx;
Daniel@0 101
Daniel@0 102 % bias in neighborhood (in each Voronoi set / component)
Daniel@0 103
Daniel@0 104 Enb = (Vn-Vm).^2;
Daniel@0 105 Enb = HN.*Enb;
Daniel@0 106
Daniel@0 107 % variance in neighborhood (in each Voronoi set / component)
Daniel@0 108
Daniel@0 109 Env = zeros(munits,dim);
Daniel@0 110 for i=1:munits, Env(i,:) = H(i,:)*(M-Vm(i*ones(munits,1),:)).^2; end
Daniel@0 111 Env = Ni.*Env;
Daniel@0 112
Daniel@0 113 % total distortion (in each Voronoi set / component)
Daniel@0 114
Daniel@0 115 Ed = Eqx + Enb + Env;
Daniel@0 116
Daniel@0 117 %% other error measures
Daniel@0 118
Daniel@0 119 % squared quantization error (to data centroid)
Daniel@0 120
Daniel@0 121 me = centroid(D,mean(M));
Daniel@0 122 Dx = D - me(ones(dlen,1),:);
Daniel@0 123 Dx(isnan(Dx)) = 0;
Daniel@0 124 e1 = sum(Dx.^2,1);
Daniel@0 125
Daniel@0 126 % squared quantization error (to map units)
Daniel@0 127
Daniel@0 128 Dx = D - M(bmus,:);
Daniel@0 129 Dx(isnan(Dx)) = 0;
Daniel@0 130 eq = sum(Dx.^2,1);
Daniel@0 131
Daniel@0 132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 133 %% output
Daniel@0 134
Daniel@0 135 % distortion error matrix
Daniel@0 136
Daniel@0 137 Err = zeros(munits,dim,5);
Daniel@0 138 Err(:,:,1) = Eqx;
Daniel@0 139 Err(:,:,2) = Enb;
Daniel@0 140 Err(:,:,3) = Env;
Daniel@0 141
Daniel@0 142 % total errors
Daniel@0 143
Daniel@0 144 sPropTotal = struct('n',sum(Ni),'h',mean(Hi),'e1',sum(e1),'err',sum(sum(Err,2),1));
Daniel@0 145
Daniel@0 146 % properties of map units
Daniel@0 147
Daniel@0 148 sPropMunits = struct('Ni',[],'Hi',[],'Err',[]);
Daniel@0 149 sPropMunits.Ni = Ni;
Daniel@0 150 sPropMunits.Hi = Hi;
Daniel@0 151 sPropMunits.Err = squeeze(sum(Err,2));
Daniel@0 152
Daniel@0 153 % properties of components
Daniel@0 154
Daniel@0 155 sPropComps = struct('Err',[],'e1',[],'eq',[]);
Daniel@0 156 sPropComps.Err = squeeze(sum(Err,1));
Daniel@0 157 sPropComps.e1 = e1;
Daniel@0 158 sPropComps.eq = eq;
Daniel@0 159
Daniel@0 160
Daniel@0 161 return;
Daniel@0 162
Daniel@0 163
Daniel@0 164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
Daniel@0 165 %% subfunctions
Daniel@0 166
Daniel@0 167 function v = centroid(D,default,weights)
Daniel@0 168
Daniel@0 169 [n dim] = size(D);
Daniel@0 170 I = sparse(isnan(D));
Daniel@0 171 D(I) = 0;
Daniel@0 172
Daniel@0 173 if nargin==3,
Daniel@0 174 W = weights(:,ones(1,dim));
Daniel@0 175 W(I) = 0;
Daniel@0 176 D = D.*W;
Daniel@0 177 nn = sum(W,1);
Daniel@0 178 else
Daniel@0 179 nn = n-sum(I,1);
Daniel@0 180 end
Daniel@0 181
Daniel@0 182 c = sum(D,1);
Daniel@0 183 v = default;
Daniel@0 184 i = find(nn>0);
Daniel@0 185 v(i) = c(i)./nn(i);
Daniel@0 186
Daniel@0 187 return;
Daniel@0 188
Daniel@0 189
Daniel@0 190 function vis
Daniel@0 191
Daniel@0 192 figure
Daniel@0 193 som_show(sM,'color',{Hi,'Hi'},'color',{Ni,'hits'},...
Daniel@0 194 'color',{Ed,'distortion'},'color',{Eqx,'qxerror'},...
Daniel@0 195 'color',{Enb,'N-bias'},'color',{Env,'N-Var'});
Daniel@0 196
Daniel@0 197 ed = Eqx + Enb + Env;
Daniel@0 198 i = find(ed>0);
Daniel@0 199 eqx = 0*ed; eqx(i) = Eqx(i)./ed(i);
Daniel@0 200 enb = 0*ed; enb(i) = Enb(i)./ed(i);
Daniel@0 201 env = 0*ed; env(i) = Env(i)./ed(i);
Daniel@0 202
Daniel@0 203 figure
Daniel@0 204 som_show(sM,'color',Hi,'color',Ni,'color',Ed,...
Daniel@0 205 'color',eqx,'color',enb,'color',env);
Daniel@0 206
Daniel@0 207