% 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);
% randn('seed',2); % XXX
% rand('seed',2);
fprintf('\n');
if (M==1)
[Lh,Ph,LL]=ffa(X,K,cyc,tol);
Mu=mean(X);
Pi=1;
else
mX=mean(X);
cX=cov(X);
scale=det(cX)^(1/D);
Lh=randn(D*M,K)*sqrt(scale/K);
Ph=diag(cX)+tiny;
Pi=ones(M,1)/M;
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 (lik<oldlik)
fprintf(' violation');
elseif ((lik-likbase)<(1 + tol)*(oldlik-likbase)|~finite(lik))
break;
end;
fprintf('\n');
%%%% M Step %%%%
% means and covariance structure
Ph=zeros(D,1);
for k=1:M
kD=(k-1)*D+1:k*D;
kK=(k-1)*K+1:k*K;
kN=(k-1)*N+1:k*N;
T0=rprod(X,H(:,k));
T1=T0'*[EZ(kN,:) ones(N,1)];
XH=EZ(kN,:)'*H(:,k);
T2=inv([s(k)*EZZ(kK,:) XH; XH' s(k)]);
T3=T1*T2;
Lh(kD,:)=T3(:,1:K);
Mu(k,:)=T3(:,K+1)';
T4=diag(T0'*X-T3*T1')/s2;
Ph=Ph+T4.*(T4>0);
end;
Phmin=exp(-700);
Ph=Ph.*(Ph>Phmin)+(Ph<=Phmin)*Phmin; % to avoid zero variances
% priors
Pi=s'/s2;
end;
fprintf('\n');
end;
阿里matlab建模师
- 粉丝: 4671
- 资源: 2872
最新资源
- 百战程序员-AI算法工程师就业班快速入门.mp4
- 两阶段鲁棒优化模型 多场景 采用matlab编程两阶段鲁棒优化程序,考虑四个场景,模型采用列与约束生成(CCG)算法进行求解,场景分布的概率置信区间由 1-范数和∞-范数约束,程序含拉丁超立方抽样+k
- 奔驰G63生成器app 炫耀你的个性座驾.mp4
- 暴走头像app 超多有趣的应有尽有.mp4
- Steam喜+1《暗黑区域》.mp4
- SSL证书管理系统工具网站源码,自动申请、部署SSL证书,并在证书即将过期时自动续期.mp4
- Sunny截图工具v2.4.0便携版.mp4
- TeamViewer的绝佳替代品出现!RustDesk远程桌面v1.3.3.mp4
- TikTok本土精品小店出海实战营从入门到高阶.mp4
- TB无人直播最新玩法,不违法不封号.mp4
- Tony头像大师app 海量素材和模板.mp4
- Comsol经典小案例 晶格耦合作用结构色,CIE1931计算与绘制
- Via浏览器APP v6.0.1.0 最新Via浏览器谷歌版.mp4
- 基于的RESNET50+CA注意力机制的交通标识识别项目源码+模型-多类别图像分类
- Vidma v2.15.1 海外视频剪辑神器 多种AI功能 完全免费.mp4
- VMware-Workstation-17.6.2精简安装注册版.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈