📄 mogp.m
字号:
function p = mogP(x,means,covs,priors)%MOGP Compute the probability density of a Mixture of Gaussians%% P = MOGP(X,MEANS,COVS,PRIORS)%% Calculate the probability density for all objects X for all of the% clusters of a mixture of Gaussians, characterized with the MEANS,% COVS and PRIORS.% Note that P is not normalized!%% See also: mogEM, mog_dd% Copyright: D.M.J. Tax, R.P.W. Duin, davidt@ph.tn.tudelft.nl% Faculty of Applied Physics, Delft University of Technology% P.O. Box 5046, 2600 GA Delft, The Netherlands% Get the useful parameters[N,d] = size(x);k = length(priors);p = zeros(N,k);% First detect which type of covariance matrix we have:covtype = ndims(covs);if ((covtype==2)&(size(covs,2)==1)) covtype = 1;end% Depending on the covariance matrix, the p is computed differently:switch covtypecase 1 % Diagonal cov.matrix with equal variances D = distm(x,means); sig = 2.*repmat(covs',N,1); Z = (pi*sig).^(d/2); p = exp(-(D./sig))./Z;case 2 % Diagonal cov.matrix with unequal variances Z = (2*pi).^(d/2); sig = prod(sqrt(covs),2); for i=1:k dif = x - repmat(means(i,:),N,1); p(:,i) = exp(-sum((dif.*dif)./(repmat(covs(i,:),N,1)) ,2)/2) ./ ... (Z*sig(i)); endcase 3 % Complete covariance matrix Z = (2*pi).^(d/2); for i=1:k dif = x - repmat(means(i,:),N,1); c = chol(squeeze(covs(i,:,:))); Dmah = dif/c; % here is the obfuscated inverse... p(:,i) = exp(-sum(Dmah.*Dmah,2)/2) ./ (Z*prod(diag(c))); endotherwise error('The covariance matrix parameter is not well-defined');end% include the priors:p = p.*repmat(priors,N,1);return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -