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