comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/Zoubin/ffa.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 [L,Ph,LL]=ffa(X,K,cyc,tol);
2 %
3 % Fast Maximum Likelihood Factor Analysis using EM
4 %
5 % X - data matrix
6 % K - number of factors
7 % cyc - maximum number of cycles of EM (default 100)
8 % tol - termination tolerance (prop change in likelihood) (default 0.0001)
9 %
10 % L - factor loadings
11 % Ph - diagonal uniquenesses matrix
12 % LL - log likelihood curve
13 %
14 % Iterates until a proportional change < tol in the log likelihood
15 % or cyc steps of EM
16 %
17
18 function [L,Ph,LL]=ffa(X,K,cyc,tol);
19
20 if nargin<4 tol=0.0001; end;
21 if nargin<3 cyc=100; end;
22
23 N=length(X(:,1));
24 D=length(X(1,:));
25 tiny=exp(-700);
26
27 X=X-ones(N,1)*mean(X);
28 XX=X'*X/N;
29 diagXX=diag(XX);
30
31 randn('seed', 0);
32 cX=cov(X);
33 scale=det(cX)^(1/D);
34 L=randn(D,K)*sqrt(scale/K);
35 Ph=diag(cX);
36
37 I=eye(K);
38
39 lik=0; LL=[];
40
41 const=-D/2*log(2*pi);
42
43
44 for i=1:cyc;
45
46 %%%% E Step %%%%
47 Phd=diag(1./Ph);
48 LP=Phd*L;
49 MM=Phd-LP*inv(I+L'*LP)*LP';
50 dM=sqrt(det(MM));
51 beta=L'*MM;
52 XXbeta=XX*beta';
53 EZZ=I-beta*L +beta*XXbeta;
54
55 %%%% Compute log likelihood %%%%
56
57 oldlik=lik;
58 lik=N*const+N*log(dM)-0.5*N*sum(diag(MM*XX));
59 fprintf('cycle %i lik %g \n',i,lik);
60 LL=[LL lik];
61
62 %%%% M Step %%%%
63
64 L=XXbeta*inv(EZZ);
65 Ph=diagXX-diag(L*XXbeta');
66
67 if (i<=2)
68 likbase=lik;
69 elseif (lik<oldlik)
70 disp('VIOLATION');
71 elseif ((lik-likbase)<(1+tol)*(oldlik-likbase)||~isfinite(lik))
72 break;
73 end;
74
75 end