comparison toolboxes/FullBNT-1.0.7/KPMstats/mixgauss_prob.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] = mixgauss_prob(data, mu, Sigma, mixmat, unit_norm)
2 % EVAL_PDF_COND_MOG Evaluate the pdf of a conditional mixture of Gaussians
3 % function [B, B2] = eval_pdf_cond_mog(data, mu, Sigma, mixmat, unit_norm)
4 %
5 % Notation: Y is observation, M is mixture component, and both may be conditioned on Q.
6 % If Q does not exist, ignore references to Q=j below.
7 % Alternatively, you may ignore M if this is a conditional Gaussian.
8 %
9 % INPUTS:
10 % data(:,t) = t'th observation vector
11 %
12 % mu(:,k) = E[Y(t) | M(t)=k]
13 % or mu(:,j,k) = E[Y(t) | Q(t)=j, M(t)=k]
14 %
15 % Sigma(:,:,j,k) = Cov[Y(t) | Q(t)=j, M(t)=k]
16 % or there are various faster, special cases:
17 % Sigma() - scalar, spherical covariance independent of M,Q.
18 % Sigma(:,:) diag or full, tied params independent of M,Q.
19 % Sigma(:,:,j) tied params independent of M.
20 %
21 % mixmat(k) = Pr(M(t)=k) = prior
22 % or mixmat(j,k) = Pr(M(t)=k | Q(t)=j)
23 % Not needed if M is not defined.
24 %
25 % unit_norm - optional; if 1, means data(:,i) AND mu(:,i) each have unit norm (slightly faster)
26 %
27 % OUTPUT:
28 % B(t) = Pr(y(t))
29 % or
30 % B(i,t) = Pr(y(t) | Q(t)=i)
31 % B2(i,k,t) = Pr(y(t) | Q(t)=i, M(t)=k)
32 %
33 % If the number of mixture components differs depending on Q, just set the trailing
34 % entries of mixmat to 0, e.g., 2 components if Q=1, 3 components if Q=2,
35 % then set mixmat(1,3)=0. In this case, B2(1,3,:)=1.0.
36
37
38
39
40 if isvectorBNT(mu) & size(mu,2)==1
41 d = length(mu);
42 Q = 1; M = 1;
43 elseif ndims(mu)==2
44 [d Q] = size(mu);
45 M = 1;
46 else
47 [d Q M] = size(mu);
48 end
49 [d T] = size(data);
50
51 if nargin < 4, mixmat = ones(Q,1); end
52 if nargin < 5, unit_norm = 0; end
53
54 %B2 = zeros(Q,M,T); % ATB: not needed allways
55 %B = zeros(Q,T);
56
57 if isscalarBNT(Sigma)
58 mu = reshape(mu, [d Q*M]);
59 if unit_norm % (p-q)'(p-q) = p'p + q'q - 2p'q = n+m -2p'q since p(:,i)'p(:,i)=1
60 %avoid an expensive repmat
61 disp('unit norm')
62 %tic; D = 2 -2*(data'*mu)'; toc
63 D = 2 - 2*(mu'*data);
64 tic; D2 = sqdist(data, mu)'; toc
65 assert(approxeq(D,D2))
66 else
67 D = sqdist(data, mu)';
68 end
69 clear mu data % ATB: clear big old data
70 % D(qm,t) = sq dist between data(:,t) and mu(:,qm)
71 logB2 = -(d/2)*log(2*pi*Sigma) - (1/(2*Sigma))*D; % det(sigma*I) = sigma^d
72 B2 = reshape(exp(logB2), [Q M T]);
73 clear logB2 % ATB: clear big old data
74
75 elseif ndims(Sigma)==2 % tied full
76 mu = reshape(mu, [d Q*M]);
77 D = sqdist(data, mu, inv(Sigma))';
78 % D(qm,t) = sq dist between data(:,t) and mu(:,qm)
79 logB2 = -(d/2)*log(2*pi) - 0.5*logdet(Sigma) - 0.5*D;
80 %denom = sqrt(det(2*pi*Sigma));
81 %numer = exp(-0.5 * D);
82 %B2 = numer/denom;
83 B2 = reshape(exp(logB2), [Q M T]);
84
85 elseif ndims(Sigma)==3 % tied across M
86 B2 = zeros(Q,M,T);
87 for j=1:Q
88 % D(m,t) = sq dist between data(:,t) and mu(:,j,m)
89 if isposdef(Sigma(:,:,j))
90 D = sqdist(data, permute(mu(:,j,:), [1 3 2]), inv(Sigma(:,:,j)))';
91 logB2 = -(d/2)*log(2*pi) - 0.5*logdet(Sigma(:,:,j)) - 0.5*D;
92 B2(j,:,:) = exp(logB2);
93 else
94 error(sprintf('mixgauss_prob: Sigma(:,:,q=%d) not psd\n', j));
95 end
96 end
97
98 else % general case
99 B2 = zeros(Q,M,T);
100 for j=1:Q
101 for k=1:M
102 %if mixmat(j,k) > 0
103 B2(j,k,:) = gaussian_prob(data, mu(:,j,k), Sigma(:,:,j,k));
104 %end
105 end
106 end
107 end
108
109 % B(j,t) = sum_k B2(j,k,t) * Pr(M(t)=k | Q(t)=j)
110
111 % The repmat is actually slower than the for-loop, because it uses too much memory
112 % (this is true even for small T).
113
114 %B = squeeze(sum(B2 .* repmat(mixmat, [1 1 T]), 2));
115 %B = reshape(B, [Q T]); % undo effect of squeeze in case Q = 1
116
117 B = zeros(Q,T);
118 if Q < T
119 for q=1:Q
120 %B(q,:) = mixmat(q,:) * squeeze(B2(q,:,:)); % squeeze chnages order if M=1
121 B(q,:) = mixmat(q,:) * permute(B2(q,:,:), [2 3 1]); % vector * matrix sums over m
122 end
123 else
124 for t=1:T
125 B(:,t) = sum(mixmat .* B2(:,:,t), 2); % sum over m
126 end
127 end
128 %t=toc;fprintf('%5.3f\n', t)
129
130 %tic
131 %A = squeeze(sum(B2 .* repmat(mixmat, [1 1 T]), 2));
132 %t=toc;fprintf('%5.3f\n', t)
133 %assert(approxeq(A,B)) % may be false because of round off error