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