comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/Zoubin/mfa.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 [Lh,Ph,Mu,Pi,LL]=mfa(X,M,K,cyc,tol);
2 %
3 % Maximum Likelihood Mixture of Factor Analysis using EM
4 %
5 % X - data matrix
6 % M - number of mixtures (default 1)
7 % K - number of factors in each mixture (default 2)
8 % cyc - maximum number of cycles of EM (default 100)
9 % tol - termination tolerance (prop change in likelihood) (default 0.0001)
10 %
11 % Lh - factor loadings
12 % Ph - diagonal uniquenesses matrix
13 % Mu - mean vectors
14 % Pi - priors
15 % LL - log likelihood curve
16 %
17 % Iterates until a proportional change < tol in the log likelihood
18 % or cyc steps of EM
19
20 function [Lh, Ph, Mu, Pi, LL] = mfa(X,M,K,cyc,tol)
21
22 if nargin<5 tol=0.0001; end;
23 if nargin<4 cyc=100; end;
24 if nargin<3 K=2; end;
25 if nargin<2 M=1; end;
26
27 N=length(X(:,1));
28 D=length(X(1,:));
29 tiny=exp(-700);
30
31 %rand('state',0);
32
33 fprintf('\n');
34
35 if (M==1)
36 [Lh,Ph,LL]=ffa(X,K,cyc,tol);
37 Mu=mean(X);
38 Pi=1;
39 else
40 if N==1
41 mX = X;
42 else
43 mX=mean(X);
44 end
45 cX=cov(X);
46 scale=det(cX)^(1/D);
47 randn('state',0);
48 Lh=randn(D*M,K)*sqrt(scale/K);
49 Ph=diag(cX)+tiny;
50 Pi=ones(M,1)/M;
51 %randn('state',0);
52 Mu=randn(M,D)*sqrtm(cX)+ones(M,1)*mX;
53 oldMu=Mu;
54 I=eye(K);
55
56 lik=0;
57 LL=[];
58
59 H=zeros(N,M); % E(w|x)
60 EZ=zeros(N*M,K);
61 EZZ=zeros(K*M,K);
62 XX=zeros(D*M,D);
63 s=zeros(M,1);
64 const=(2*pi)^(-D/2);
65 %%%%%%%%%%%%%%%%%%%%
66 for i=1:cyc;
67
68 %%%% E Step %%%%
69
70 Phi=1./Ph;
71 Phid=diag(Phi);
72 for k=1:M
73 Lht=Lh((k-1)*D+1:k*D,:);
74 LP=Phid*Lht;
75 MM=Phid-LP*inv(I+Lht'*LP)*LP';
76 dM=sqrt(det(MM));
77 Xk=(X-ones(N,1)*Mu(k,:));
78 XM=Xk*MM;
79 H(:,k)=const*Pi(k)*dM*exp(-0.5*rsum(XM.*Xk));
80 EZ((k-1)*N+1:k*N,:)=XM*Lht;
81 end;
82
83 Hsum=rsum(H);
84 oldlik=lik;
85 lik=sum(log(Hsum+(Hsum==0)*exp(-744)));
86
87 Hzero=(Hsum==0); Nz=sum(Hzero);
88 H(Hzero,:)=tiny*ones(Nz,M)/M;
89 Hsum(Hzero)=tiny*ones(Nz,1);
90
91 H=rdiv(H,Hsum);
92 s=csum(H);
93 s=s+(s==0)*tiny;
94 s2=sum(s)+tiny;
95
96 for k=1:M
97 kD=(k-1)*D+1:k*D;
98 Lht=Lh(kD,:);
99 LP=Phid*Lht;
100 MM=Phid-LP*inv(I+Lht'*LP)*LP';
101 Xk=(X-ones(N,1)*Mu(k,:));
102 XX(kD,:)=rprod(Xk,H(:,k))'*Xk/s(k);
103 beta=Lht'*MM;
104 EZZ((k-1)*K+1:k*K,:)=I-beta*Lht +beta*XX(kD,:)*beta';
105 end;
106
107 %%%% log likelihood %%%%
108
109 LL=[LL lik];
110 fprintf('cycle %g \tlog likelihood %g ',i,lik);
111
112 if (i<=2)
113 likbase=lik;
114 elseif (lik<oldlik)
115 fprintf(' violation');
116 elseif ((lik-likbase)<(1 + tol)*(oldlik-likbase)||~isfinite(lik))
117 break;
118 end;
119
120 fprintf('\n');
121
122 %%%% M Step %%%%
123
124 % means and covariance structure
125
126 Ph=zeros(D,1);
127 for k=1:M
128 kD=(k-1)*D+1:k*D;
129 kK=(k-1)*K+1:k*K;
130 kN=(k-1)*N+1:k*N;
131
132 T0=rprod(X,H(:,k));
133 T1=T0'*[EZ(kN,:) ones(N,1)];
134 XH=EZ(kN,:)'*H(:,k);
135 T2=inv([s(k)*EZZ(kK,:) XH; XH' s(k)]);
136 T3=T1*T2;
137 Lh(kD,:)=T3(:,1:K);
138 Mu(k,:)=T3(:,K+1)';
139 T4=diag(T0'*X-T3*T1')/s2;
140 Ph=Ph+T4.*(T4>0);
141 end;
142
143 Phmin=exp(-700);
144 Ph=Ph.*(Ph>Phmin)+(Ph<=Phmin)*Phmin; % to avoid zero variances
145
146 % priors
147 Pi=s'/s2;
148
149 end;
150 fprintf('\n');
151 end;
152
153