comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_probability_gmm.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 [pd,Pdm,pmd] = som_probability_gmm(D, sM, K, P)
2
3 %SOM_PROBABILITY_GMM Probabilities based on a gaussian mixture model.
4 %
5 % [pd,Pdm,pmd] = som_probability_gmm(D, sM, K, P)
6 %
7 % [K,P] = som_estimate_gmm(sM,D);
8 % [pd,Pdm,pmd] = som_probability_gmm(D,sM,K,P);
9 % som_show(sM,'color',pmd(:,1),'color',Pdm(:,1))
10 %
11 % Input and output arguments:
12 % D (matrix) size dlen x dim, the data for which the
13 % (struct) data struct, probabilities are calculated
14 % sM (struct) map struct
15 % (matrix) size munits x dim, the kernel centers
16 % K (matrix) size munits x dim, kernel width parameters
17 % computed by SOM_ESTIMATE_GMM
18 % P (matrix) size 1 x munits, a priori probabilities for each
19 % kernel computed by SOM_ESTIMATE_GMM
20 %
21 % pd (vector) size dlen x 1, probability of each data vector in
22 % terms of the whole gaussian mixture model
23 % Pdm (matrix) size munits x dlen, probability of each vector in
24 % terms of each kernel
25 % pmd (matrix) size munits x dlen, probability of each vector to
26 % have been generated by each kernel
27 %
28 % See also SOM_ESTIMATE_GMM.
29
30 % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Esa Alhoniemi
31 % Copyright (c) by Esa Alhoniemi
32 % http://www.cis.hut.fi/projects/somtoolbox/
33
34 % ecco 180298 juuso 050100
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37
38 % input arguments
39 if isstruct(sM), M = sM.codebook; else M = sM; end
40 [c dim] = size(M);
41
42 if isstruct(D), D = D.data; end
43 dlen = size(D,1);
44
45 % reserve space for output variables
46 pd = zeros(dlen,1);
47 if nargout>=2, Pdm = zeros(c,dlen); end
48 if nargout==3, pmd = zeros(c,dlen); end
49
50 % the parameters of each kernel
51 cCoeff = cell(c,1);
52 cCoinv = cell(c,1);
53 for m=1:c,
54 co = diag(K(m,:));
55 cCoinv{m} = inv(co);
56 cCoeff{m} = 1 / ((2*pi)^(dim/2)*det(co)^.5);
57 end
58
59 % go through the vectors one by one
60 for i=1:dlen,
61
62 x = D(i,:);
63
64 % compute p(x|m)
65 pxm = zeros(c,1);
66 for m = 1:c,
67 dx = M(m,:) - x;
68 pxm(m) = cCoeff{m} * exp(-.5 * dx * cCoinv{m} * dx');
69 %pxm(m) = normal(dx, zeros(1,dim), diag(K(m,:)));
70 end
71 pxm(isnan(pxm(:))) = 0;
72
73 % p(x|m)
74 if nargin>=2, Pdm(:,i) = pxm; end
75
76 % P(x) = P(x|M) = sum( P(m) * p(x|m) )
77 pd(i) = P*pxm;
78
79 % p(m|x) = p(x|m) * P(m) / P(x)
80 if nargout==3, pmd(:,i) = (P' .* pxm) / pd(i); end
81
82 end
83
84
85 return;
86
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 %
89 % subfunction normal
90 %
91 % computes probability of x when mean and covariance matrix
92 % of a distribution are known
93
94 function result = normal(x, mu, co)
95
96 [l dim] = size(x);
97 coinv = inv(co);
98 coeff = 1 / ((2*pi)^(dim/2)*det(co)^.5);
99 diff = x - mu;
100 result = coeff * exp(-.5 * diff * coinv * diff');