annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/Zoubin/mfademo.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 echo on;
Daniel@0 2
Daniel@0 3 clc;
Daniel@0 4
Daniel@0 5 % This is a very basic demo of the mixture of factor analyzer software
Daniel@0 6 % written in Matlab by Zoubin Ghahramani
Daniel@0 7 % Dept of Computer Science
Daniel@0 8 % University of Toronto
Daniel@0 9
Daniel@0 10 pause; % Hit any key to continue
Daniel@0 11
Daniel@0 12 % To demonstrate the software we generate a sample data set
Daniel@0 13 % from a mixture of two Gaussians
Daniel@0 14
Daniel@0 15 pause; % Hit any key to continue
Daniel@0 16
Daniel@0 17 X1=randn(300,5); % zero mean 5 dim Gaussian data
Daniel@0 18 X2=randn(200,5)+2; % 5 dim Gaussian data with mean [1 1 1 1 1]
Daniel@0 19 X=[X1;X2]; % total 500 data points from mixture
Daniel@0 20
Daniel@0 21 % Fitting the model is very easy. For example to fit a mixture of 2
Daniel@0 22 % factor analyzers with three factors each...
Daniel@0 23
Daniel@0 24 pause; % Hit any key to continue
Daniel@0 25
Daniel@0 26
Daniel@0 27 [Lh,Ph,Mu,Pi,LL]=mfa(X,2,3);
Daniel@0 28
Daniel@0 29 % Lh, Ph, Mu, and Pi are the factor loadings, observervation
Daniel@0 30 % variances, observation means for each mixture, and mixing
Daniel@0 31 % proportions. LL is the vector of log likelihoods (the learning
Daniel@0 32 % curve). For more information type: help mfa
Daniel@0 33
Daniel@0 34 % to plot the learning curve (log likelihood at each step of EM)...
Daniel@0 35
Daniel@0 36 pause; % Hit any key to continue
Daniel@0 37
Daniel@0 38 plot(LL);
Daniel@0 39
Daniel@0 40 % you get a more informative picture of convergence by looking at the
Daniel@0 41 % log of the first difference of the log likelihoods...
Daniel@0 42
Daniel@0 43 pause; % Hit any key to continue
Daniel@0 44
Daniel@0 45 semilogy(diff(LL));
Daniel@0 46
Daniel@0 47 % you can look at some of the parameters of the fitted model...
Daniel@0 48
Daniel@0 49 pause; % Hit any key to continue
Daniel@0 50
Daniel@0 51 Mu
Daniel@0 52
Daniel@0 53 Pi
Daniel@0 54
Daniel@0 55 % ...to see whether they make any sense given that me know how the
Daniel@0 56 % data was generated.
Daniel@0 57
Daniel@0 58 % you can also evaluate the log likelihood of another data set under
Daniel@0 59 % the model we have just fitted using the mfa_cl (for Calculate
Daniel@0 60 % Likelihood) function. For example, here we generate a test from the
Daniel@0 61 % same distribution.
Daniel@0 62
Daniel@0 63
Daniel@0 64 X1=randn(300,5);
Daniel@0 65 X2=randn(200,5)+2;
Daniel@0 66 Xtest=[X1; X2];
Daniel@0 67
Daniel@0 68 pause; % Hit any key to continue
Daniel@0 69
Daniel@0 70 mfa_cl(Xtest,Lh,Ph,Mu,Pi)
Daniel@0 71
Daniel@0 72 % we should expect the log likelihood of the test set to be lower than
Daniel@0 73 % that of the training set.
Daniel@0 74
Daniel@0 75 % finally, we can also fit a regular factor analyzer using the ffa
Daniel@0 76 % function (Fast Factor Analysis)...
Daniel@0 77
Daniel@0 78 pause; % Hit any key to continue
Daniel@0 79
Daniel@0 80 [L,Ph,LL]=ffa(X,3);
Daniel@0 81