annotate toolboxes/FullBNT-1.0.7/netlab3.3/ppca.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [var, U, lambda] = ppca(x, ppca_dim)
Daniel@0 2 %PPCA Probabilistic Principal Components Analysis
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % [VAR, U, LAMBDA] = PPCA(X, PPCA_DIM) computes the principal
Daniel@0 6 % component subspace U of dimension PPCA_DIM using a centred covariance
Daniel@0 7 % matrix X. The variable VAR contains the off-subspace variance (which
Daniel@0 8 % is assumed to be spherical), while the vector LAMBDA contains the
Daniel@0 9 % variances of each of the principal components. This is computed
Daniel@0 10 % using the eigenvalue and eigenvector decomposition of X.
Daniel@0 11 %
Daniel@0 12 % See also
Daniel@0 13 % EIGDEC, PCA
Daniel@0 14 %
Daniel@0 15
Daniel@0 16 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 17
Daniel@0 18
Daniel@0 19 if ppca_dim ~= round(ppca_dim) | ppca_dim < 1 | ppca_dim > size(x, 2)
Daniel@0 20 error('Number of PCs must be integer, >0, < dim');
Daniel@0 21 end
Daniel@0 22
Daniel@0 23 [ndata, data_dim] = size(x);
Daniel@0 24 % Assumes that x is centred and responsibility weighted
Daniel@0 25 % covariance matrix
Daniel@0 26 [l Utemp] = eigdec(x, data_dim);
Daniel@0 27 % Zero any negative eigenvalues (caused by rounding)
Daniel@0 28 l(l<0) = 0;
Daniel@0 29 % Now compute the sigma squared values for all possible values
Daniel@0 30 % of q
Daniel@0 31 s2_temp = cumsum(l(end:-1:1))./[1:data_dim]';
Daniel@0 32 % If necessary, reduce the value of q so that var is at least
Daniel@0 33 % eps * largest eigenvalue
Daniel@0 34 q_temp = min([ppca_dim; data_dim-min(find(s2_temp/l(1) > eps))]);
Daniel@0 35 if q_temp ~= ppca_dim
Daniel@0 36 wstringpart = 'Covariance matrix ill-conditioned: extracted';
Daniel@0 37 wstring = sprintf('%s %d/%d PCs', ...
Daniel@0 38 wstringpart, q_temp, ppca_dim);
Daniel@0 39 warning(wstring);
Daniel@0 40 end
Daniel@0 41 if q_temp == 0
Daniel@0 42 % All the latent dimensions have disappeared, so we are
Daniel@0 43 % just left with the noise model
Daniel@0 44 var = l(1)/data_dim;
Daniel@0 45 lambda = var*ones(1, ppca_dim);
Daniel@0 46 else
Daniel@0 47 var = mean(l(q_temp+1:end));
Daniel@0 48 end
Daniel@0 49 U = Utemp(:, 1:q_temp);
Daniel@0 50 lambda(1:q_temp) = l(1:q_temp);
Daniel@0 51
Daniel@0 52