From 8070dc963753142bb86c4ed698d91fd623ed28e7 Mon Sep 17 00:00:00 2001 From: ziejd2 Date: Thu, 28 Sep 2017 15:04:40 -0500 Subject: BNW using Octave instead of Matlab. This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning. I am calling this BNW_1.02. It can be accessed at: compbio.uthsc.edu/BNW_1.02 --- .../bnt-master/BNT/examples/static/Zoubin/mfa.m | 153 +++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa.m (limited to 'sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa.m') diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa.m b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa.m new file mode 100644 index 00000000..2060e331 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa.m @@ -0,0 +1,153 @@ +% function [Lh,Ph,Mu,Pi,LL]=mfa(X,M,K,cyc,tol); +% +% Maximum Likelihood Mixture of Factor Analysis using EM +% +% X - data matrix +% M - number of mixtures (default 1) +% K - number of factors in each mixture (default 2) +% cyc - maximum number of cycles of EM (default 100) +% tol - termination tolerance (prop change in likelihood) (default 0.0001) +% +% Lh - factor loadings +% Ph - diagonal uniquenesses matrix +% Mu - mean vectors +% Pi - priors +% LL - log likelihood curve +% +% Iterates until a proportional change < tol in the log likelihood +% or cyc steps of EM + +function [Lh, Ph, Mu, Pi, LL] = mfa(X,M,K,cyc,tol) + +if nargin<5 tol=0.0001; end; +if nargin<4 cyc=100; end; +if nargin<3 K=2; end; +if nargin<2 M=1; end; + +N=length(X(:,1)); +D=length(X(1,:)); +tiny=exp(-700); + +%rand('state',0); + +fprintf('\n'); + +if (M==1) + [Lh,Ph,LL]=ffa(X,K,cyc,tol); + Mu=mean(X); + Pi=1; +else + if N==1 + mX = X; + else + mX=mean(X); + end + cX=cov(X); + scale=det(cX)^(1/D); + randn('state',0); + Lh=randn(D*M,K)*sqrt(scale/K); + Ph=diag(cX)+tiny; + Pi=ones(M,1)/M; + %randn('state',0); + Mu=randn(M,D)*sqrtm(cX)+ones(M,1)*mX; + oldMu=Mu; + I=eye(K); + + lik=0; + LL=[]; + + H=zeros(N,M); % E(w|x) + EZ=zeros(N*M,K); + EZZ=zeros(K*M,K); + XX=zeros(D*M,D); + s=zeros(M,1); + const=(2*pi)^(-D/2); + %%%%%%%%%%%%%%%%%%%% + for i=1:cyc; + + %%%% E Step %%%% + + Phi=1./Ph; + Phid=diag(Phi); + for k=1:M + Lht=Lh((k-1)*D+1:k*D,:); + LP=Phid*Lht; + MM=Phid-LP*inv(I+Lht'*LP)*LP'; + dM=sqrt(det(MM)); + Xk=(X-ones(N,1)*Mu(k,:)); + XM=Xk*MM; + H(:,k)=const*Pi(k)*dM*exp(-0.5*rsum(XM.*Xk)); + EZ((k-1)*N+1:k*N,:)=XM*Lht; + end; + + Hsum=rsum(H); + oldlik=lik; + lik=sum(log(Hsum+(Hsum==0)*exp(-744))); + + Hzero=(Hsum==0); Nz=sum(Hzero); + H(Hzero,:)=tiny*ones(Nz,M)/M; + Hsum(Hzero)=tiny*ones(Nz,1); + + H=rdiv(H,Hsum); + s=csum(H); + s=s+(s==0)*tiny; + s2=sum(s)+tiny; + + for k=1:M + kD=(k-1)*D+1:k*D; + Lht=Lh(kD,:); + LP=Phid*Lht; + MM=Phid-LP*inv(I+Lht'*LP)*LP'; + Xk=(X-ones(N,1)*Mu(k,:)); + XX(kD,:)=rprod(Xk,H(:,k))'*Xk/s(k); + beta=Lht'*MM; + EZZ((k-1)*K+1:k*K,:)=I-beta*Lht +beta*XX(kD,:)*beta'; + end; + + %%%% log likelihood %%%% + + LL=[LL lik]; + fprintf('cycle %g \tlog likelihood %g ',i,lik); + + if (i<=2) + likbase=lik; + elseif (lik0); + end; + + Phmin=exp(-700); + Ph=Ph.*(Ph>Phmin)+(Ph<=Phmin)*Phmin; % to avoid zero variances + + % priors + Pi=s'/s2; + + end; + fprintf('\n'); +end; + + -- cgit 1.4.1