diff options
| author | ziejd2 | 2017-09-28 15:04:40 -0500 |
|---|---|---|
| committer | ziejd2 | 2017-09-28 15:04:40 -0500 |
| commit | 8070dc963753142bb86c4ed698d91fd623ed28e7 (patch) | |
| tree | d0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/netlab3.3/gmmactiv.m | |
| parent | 7cc31810d53176e805532b2789955f4eedbce6bb (diff) | |
| download | BNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz | |
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
Diffstat (limited to 'sourcecodes/bnt-master/netlab3.3/gmmactiv.m')
| -rw-r--r-- | sourcecodes/bnt-master/netlab3.3/gmmactiv.m | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/netlab3.3/gmmactiv.m b/sourcecodes/bnt-master/netlab3.3/gmmactiv.m new file mode 100644 index 00000000..117e1cdc --- /dev/null +++ b/sourcecodes/bnt-master/netlab3.3/gmmactiv.m @@ -0,0 +1,77 @@ +function a = gmmactiv(mix, x) +%GMMACTIV Computes the activations of a Gaussian mixture model. +% +% Description +% This function computes the activations A (i.e. the probability +% P(X|J) of the data conditioned on each component density) for a +% Gaussian mixture model. For the PPCA model, each activation is the +% conditional probability of X given that it is generated by the +% component subspace. The data structure MIX defines the mixture model, +% while the matrix X contains the data vectors. Each row of X +% represents a single vector. +% +% See also +% GMM, GMMPOST, GMMPROB +% + +% Copyright (c) Ian T Nabney (1996-2001) + +% Check that inputs are consistent +errstring = consist(mix, 'gmm', x); +if ~isempty(errstring) + error(errstring); +end + +ndata = size(x, 1); +a = zeros(ndata, mix.ncentres); % Preallocate matrix + +switch mix.covar_type + +case 'spherical' + % Calculate squared norm matrix, of dimension (ndata, ncentres) + n2 = dist2(x, mix.centres); + + % Calculate width factors + wi2 = ones(ndata, 1) * (2 .* mix.covars); + normal = (pi .* wi2) .^ (mix.nin/2); + + % Now compute the activations + a = exp(-(n2./wi2))./ normal; + +case 'diag' + normal = (2*pi)^(mix.nin/2); + s = prod(sqrt(mix.covars), 2); + for j = 1:mix.ncentres + diffs = x - (ones(ndata, 1) * mix.centres(j, :)); + a(:, j) = exp(-0.5*sum((diffs.*diffs)./(ones(ndata, 1) * ... + mix.covars(j, :)), 2)) ./ (normal*s(j)); + end + +case 'full' + normal = (2*pi)^(mix.nin/2); + for j = 1:mix.ncentres + diffs = x - (ones(ndata, 1) * mix.centres(j, :)); + % Use Cholesky decomposition of covariance matrix to speed computation + c = chol(mix.covars(:, :, j)); + temp = diffs/c; + a(:, j) = exp(-0.5*sum(temp.*temp, 2))./(normal*prod(diag(c))); + end +case 'ppca' + log_normal = mix.nin*log(2*pi); + d2 = zeros(ndata, mix.ncentres); + logZ = zeros(1, mix.ncentres); + for i = 1:mix.ncentres + k = 1 - mix.covars(i)./mix.lambda(i, :); + logZ(i) = log_normal + mix.nin*log(mix.covars(i)) - ... + sum(log(1 - k)); + diffs = x - ones(ndata, 1)*mix.centres(i, :); + proj = diffs*mix.U(:, :, i); + d2(:,i) = (sum(diffs.*diffs, 2) - ... + sum((proj.*(ones(ndata, 1)*k)).*proj, 2)) / ... + mix.covars(i); + end + a = exp(-0.5*(d2 + ones(ndata, 1)*logZ)); +otherwise + error(['Unknown covariance type ', mix.covar_type]); +end + |
