?? gaussmix.m
字號:
function [m,v,w,g,f,pp,gg]=gaussmix(x,c,l,m0,v0,w0)
%GAUSSMIX fits a gaussian mixture pdf to a set of data observations [m,v,w,g,f]=(x,c,l,m0,v0,w0)
%
% Inputs: n data values, k mixtures, p parameters, l loops
%
% X(n,p) Input data vectors, one per row.
% c(1,p) Minimum variance (can be a scalar if all components are identical or 0 if no minimum).
% Use [] to take default value of var(x)/n^2
% L The integer portion of l gives a maximum loop count. The fractional portion gives
% an optional stopping threshold. Iteration will cease if the increase in
% log likelihood density per data point is less than this value. Thus l=10.001 will
% stop after 10 iterations or when the increase in log likelihood falls below
% 0.001.
% As a special case, if L=0, then the first three outputs are omitted.
% Use [] to take default value of 100.0001
% M0(k,p) Initial mixture means, one row per mixture.
% V0(k,p) Initial mixture variances, one row per mixture.
% or V0(p,p,k) one full-covariance matrix per mixture
% W0(k,1) Initial mixture weights, one per mixture. The weights should sum to unity.
%
% Alternatively, if initial values for M0, V0 and W0 are not given explicitly:
%
% M0 Number of mixtures required
% V0 Initialization mode:
% 'f' Initialize with K randomly selected data points [default]
% 'p' Initialize with centroids and variances of random partitions
% 'k' k-means algorithm ('kf' and 'kp' determine initialization of kmeans)
% 'h' k-harmonic means algorithm ('hf' and 'hp' determine initialization of kmeans)
% 's' do not scale data during initialization to have equal variances
% 'v' full covariance matrices
% Mode 'hf' generally gives the best results but 'f' [the default] is faster
%
% Outputs: (Note that M, V and W are omitted if L==0)
%
% M(k,p) Mixture means, one row per mixture. (omitted if L==0)
% V(k,p) Mixture variances, one row per mixture. (omitted if L==0)
% or V(p,p,k) if full covariance matrices in use
% W(k,1) Mixture weights, one per mixture. The weights will sum to unity. (omitted if L==0)
% G Average log probability of the input data points.
% F Fisher's Discriminant measures how well the data divides into classes.
% It is the ratio of the between-mixture variance to the average mixture variance: a
% high value means the classes (mixtures) are well separated.
% PP(n,1) Log probability of each data point
% GG(l+1,1) Average log probabilities at the beginning of each iteration and at the end
% Bugs/Suggestions
% (2) Allow processing in chunks by outputting/reinputting an array of sufficient statistics
% (6) Other initialization options:
% 'l' LBG algorithm
% 'm' Move-means (dog-rabbit) algorithm
% Copyright (C) Mike Brookes 2000-2006
% Version: $Id: gaussmix.m,v 1.14 2009/03/03 08:10:30 dmb Exp $
%
% VOICEBOX is a MATLAB toolbox for speech processing.
% Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You can obtain a copy of the GNU General Public License from
% http://www.gnu.org/copyleft/gpl.html or by writing to
% Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[n,p]=size(x);
x2=x.^2; % need x^2 for variance calculation
if isempty(c)
c=var(x,1)/n^2;
end
if isempty(l)
l=100+1e-4;
end
if nargin<6 % no initial values specified for m0, v0, w0
k=m0;
if n<=k % each data point can have its own mixture
m=x(mod((1:k)-1,n)+1,:); % just include all points several times
v=zeros(k,p); % will be set to floor later
w=zeros(k,1);
w(1:n)=1/n;
if l>0
l=0.1; % no point in iterating
end
else
if nargin<5
v0='f'; % default initialization mode
end
if any(v0=='s')
sx=ones(1,p);
xs=x;
else
sx=sqrt(var(x,1));
sx(sx==0)=1; % do not divide by zero
xs=x./sx(ones(n,1),:);
end
m=zeros(k,p);
w=repmat(1/k,k,1); % all mixtures equally likely
if any(v0=='k') % k-means initialization
if any(v0=='p')
[m,e,j]=kmeans(xs,k,'p');
else
[m,e,j]=kmeans(xs,k,'f');
end
elseif any(v0=='h') % k-harmonic means initialization
if any(v0=='p')
[m,e,j]=kmeanhar(xs,k,'p');
else
[m,e,j]=kmeanhar(xs,k,'f');
end
elseif any(v0=='p') % Initialize using a random partition
j=ceil(rand(1,n)*k); % allocate to random clusters
j(rnsubset(k,n))=1:k; % but force at least one point per cluster
for i=1:k
m(i,:)=mean(xs(j==i,:),1);
end
else % Forgy initialization: choose k random points [default]
m=xs(rnsubset(k,n),:); % sample k centres without replacement
[e,j]=kmeans(xs,k,m,0);
end
m=m.*sx(ones(k,1),:); % undo mean scaling
if any(v0=='v')
v=zeros(p,p,k); % diagonal covariance
for i=1:k
ni=sum(j==i); % number assigned to this centre
x0=x(j==i,:)-repmat(m(i,:),ni,1);
v(:,:,i)=(x0'*x0)/ni;
end
else
v=zeros(k,p); % diagonal covariance
for i=1:k
ni=sum(j==i); % number assigned to this centre
v(i,:)=sum((x(j==i,:)-repmat(m(i,:),ni,1)).^2,1)/ni;
end
end
end
else
k=size(m0,1);
m=m0;
v=v0;
w=w0;
end
fv=(k>1 && (length(size(v))>2)) || (size(v,1))>k; % full covariance matrix
if length(c)>1 % if c is a row vector, turn it into a full matrix so it works with max()
c=c(ones(k,1),:);
end
% Different sections for full and diagonal covariance matrices
memsize=voicebox('memsize');
if fv
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Full Covariance matrices %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pl=p*(p+1)/2;
lix=1:p^2;
cix=repmat(1:p,p,1);
rix=cix';
lix(cix>rix)=[]; % index of lower triangular elements
cix=cix(lix); % index of lower triangular columns
rix=rix(lix); % index of lower triangular rows
dix=find(rix==cix);
lixi=zeros(p,p);
lixi(lix)=1:pl;
lixi=lixi';
lixi(lix)=1:pl; % reverse index to build full matrices
v=reshape(v,p^2,k);
v=v(lix,:)'; % lower triangular in rows
v(:,dix)=max(v(:,dix),c); % force diagonal elements to be >= c
% If data size is large then do calculations in chunks
nb=min(n,max(1,floor(memsize/(24*p*k)))); % chunk size for testing data points
nl=ceil(n/nb); % number of chunks
jx0=n-(nl-1)*nb; % size of first chunk
%
th=(l-floor(l))*n;
sd=(nargout > 3*(l~=0)); % = 1 if we are outputting log likelihood values
l=floor(l)+sd; % extra loop needed to calculate final G value
%
lpx=zeros(1,n); % log probability of each data point
wk=ones(k,1);
wp=ones(1,p);
wpl=ones(1,pl); % 1 index for lower triangular matrix
wnb=ones(1,nb);
wnj=ones(1,jx0);
% EM loop
g=0; % dummy initial value for comparison
gg=zeros(l+1,1);
ss=sd; % initialize stopping count (0 or 1)
vi=zeros(p*k,p); % stack of k inverse cov matrices each size p*p
vim=zeros(p*k,1); % stack of k vectors of the form inv(v)*m
mtk=vim; % stack of k vectors of the form m
vm=zeros(k,1);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -