annotate toolboxes/FullBNT-1.0.7/KPMstats/parzen.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 [B,B2,dist] = parzen(data, mu, Sigma, N)
wolffd@0 2 % EVAL_PDF_COND_PARZEN Evaluate the pdf of a conditional Parzen window
wolffd@0 3 % function B = eval_pdf_cond_parzen(data, mu, Sigma, N)
wolffd@0 4 %
wolffd@0 5 % B(q,t) = Pr(data(:,t) | Q=q) = sum_{m=1}^{N(q)} w(m,q)*K(data(:,t) - mu(:,m,q); sigma)
wolffd@0 6 % where K() is a Gaussian kernel with spherical variance sigma,
wolffd@0 7 % and w(m,q) = 1/N(q) if m<=N(q) and = 0 otherwise
wolffd@0 8 % where N(q) is the number of mxiture components for q
wolffd@0 9 %
wolffd@0 10 % B2(m,q,t) = K(data(:,t) - mu(:,m,q); sigma) for m=1:max(N)
wolffd@0 11
wolffd@0 12 % This is like eval_pdf_cond_parzen, except mu is mu(:,m,q) instead of mu(:,q,m)
wolffd@0 13 % and we use 1/N(q) instead of mixmat(q,m)
wolffd@0 14
wolffd@0 15 if nargout >= 2
wolffd@0 16 keep_B2 = 1;
wolffd@0 17 else
wolffd@0 18 keep_B2 = 0;
wolffd@0 19 end
wolffd@0 20
wolffd@0 21 if nargout >= 3
wolffd@0 22 keep_dist = 1;
wolffd@0 23 else
wolffd@0 24 keep_dist = 0;
wolffd@0 25 end
wolffd@0 26
wolffd@0 27 [d M Q] = size(mu);
wolffd@0 28 [d T] = size(data);
wolffd@0 29
wolffd@0 30 M = max(N(:));
wolffd@0 31
wolffd@0 32 B = zeros(Q,T);
wolffd@0 33 const1 = (2*pi*Sigma)^(-d/2);
wolffd@0 34 const2 = -(1/(2*Sigma));
wolffd@0 35 if T*Q*M>20000000 % not enough memory to call sqdist
wolffd@0 36 disp('eval parzen for loop')
wolffd@0 37 if keep_dist,
wolffd@0 38 dist = zeros(M,Q,T);
wolffd@0 39 end
wolffd@0 40 if keep_B2
wolffd@0 41 B2 = zeros(M,Q,T);
wolffd@0 42 end
wolffd@0 43 for q=1:Q
wolffd@0 44 D = sqdist(mu(:,1:N(q),q), data); % D(m,t)
wolffd@0 45 if keep_dist
wolffd@0 46 dist(:,q,:) = D;
wolffd@0 47 end
wolffd@0 48 tmp = const1 * exp(const2*D);
wolffd@0 49 if keep_B2,
wolffd@0 50 B2(:,q,:) = tmp;
wolffd@0 51 end
wolffd@0 52 if N(q) > 0
wolffd@0 53 %B(q,:) = (1/N(q)) * const1 * sum(exp(const2*D), 2);
wolffd@0 54 B(q,:) = (1/N(q)) * sum(tmp,1);
wolffd@0 55 end
wolffd@0 56 end
wolffd@0 57 else
wolffd@0 58 %disp('eval parzen vectorized')
wolffd@0 59 dist = sqdist(reshape(mu(:,1:M,:), [d M*Q]), data); % D(mq,t)
wolffd@0 60 dist = reshape(dist, [M Q T]);
wolffd@0 61 B2 = const1 * exp(const2*dist); % B2(m,q,t)
wolffd@0 62 if ~keep_dist
wolffd@0 63 clear dist
wolffd@0 64 end
wolffd@0 65
wolffd@0 66 % weights(m,q) is the weight of mixture component m for q
wolffd@0 67 % = 1/N(q) if m<=N(q) and = 0 otherwise
wolffd@0 68 % e.g., N = [2 3 1], M = 3,
wolffd@0 69 % weights = [1/2 1/3 1 = 1/2 1/3 1/1 2 3 1 1 1 1
wolffd@0 70 % 1/2 1/3 0 1/2 1/3 1/1 .* 2 3 1 <= 2 2 2
wolffd@0 71 % 0 1/3 0] 1/2 1/3 1/1 2 3 1 3 3 3
wolffd@0 72
wolffd@0 73 Ns = repmat(N(:)', [M 1]);
wolffd@0 74 ramp = 1:M;
wolffd@0 75 ramp = repmat(ramp(:), [1 Q]);
wolffd@0 76 n = N + (N==0); % avoid 1/0 by replacing with 0* 1/1m where 0 comes from mask
wolffd@0 77 N1 = repmat(1 ./ n(:)', [M 1]);
wolffd@0 78 mask = (ramp <= Ns);
wolffd@0 79 weights = N1 .* mask;
wolffd@0 80 B2 = B2 .* repmat(mask, [1 1 T]);
wolffd@0 81
wolffd@0 82 % B(q,t) = sum_m B2(m,q,t) * P(m|q) = sum_m B2(m,q,t) * weights(m,q)
wolffd@0 83 B = squeeze(sum(B2 .* repmat(weights, [1 1 T]), 1));
wolffd@0 84 B = reshape(B, [Q T]); % undo effect of squeeze in case Q = 1
wolffd@0 85 end
wolffd@0 86
wolffd@0 87
wolffd@0 88