annotate toolboxes/FullBNT-1.0.7/netlab3.3/demhmc1.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 %DEMHMC1 Demonstrate Hybrid Monte Carlo sampling on mixture of two Gaussians.
Daniel@0 2 %
Daniel@0 3 % Description
Daniel@0 4 % The problem consists of generating data from a mixture of two
Daniel@0 5 % Gaussians in two dimensions using a hybrid Monte Carlo algorithm with
Daniel@0 6 % persistence. A mixture model is then fitted to the sample to compare
Daniel@0 7 % it with the true underlying generator.
Daniel@0 8 %
Daniel@0 9 % See also
Daniel@0 10 % DEMHMC3, HMC, DEMPOT, DEMGPOT
Daniel@0 11 %
Daniel@0 12
Daniel@0 13 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 14
Daniel@0 15
Daniel@0 16 dim = 2; % Data dimension
Daniel@0 17 ncentres = 2; % Number of centres in mixture model
Daniel@0 18
Daniel@0 19 seed = 42; % Seed for random weight initialization.
Daniel@0 20 randn('state', seed);
Daniel@0 21 rand('state', seed);
Daniel@0 22
Daniel@0 23 clc
Daniel@0 24 disp('This demonstration illustrates the use of the hybrid Monte Carlo')
Daniel@0 25 disp('algorithm to sample from a mixture of two Gaussians.')
Daniel@0 26 disp('The means of the two components are [0 0] and [2 2].')
Daniel@0 27 disp(' ')
Daniel@0 28 disp('First we set up the parameters of the mixture model we are sampling')
Daniel@0 29 disp('from.')
Daniel@0 30 disp(' ')
Daniel@0 31 disp('Press any key to continue.')
Daniel@0 32 pause
Daniel@0 33
Daniel@0 34 % Set up mixture model to sample from
Daniel@0 35 mix = gmm(dim, ncentres, 'spherical');
Daniel@0 36 mix.centres(1, :) = [0 0];
Daniel@0 37 mix.centres(2, :) = [2 2];
Daniel@0 38 x = [0 1]; % Start vector
Daniel@0 39
Daniel@0 40 % Set up vector of options for hybrid Monte Carlo.
Daniel@0 41
Daniel@0 42 nsamples = 160; % Number of retained samples.
Daniel@0 43
Daniel@0 44 options = foptions; % Default options vector.
Daniel@0 45 options(1) = 1; % Switch on diagnostics.
Daniel@0 46 options(5) = 1; % Use persistence
Daniel@0 47 options(7) = 50; % Number of steps in trajectory.
Daniel@0 48 options(14) = nsamples; % Number of Monte Carlo samples returned.
Daniel@0 49 options(15) = 30; % Number of samples omitted at start of chain.
Daniel@0 50 options(18) = 0.02;
Daniel@0 51
Daniel@0 52 clc
Daniel@0 53 disp(['Next we take ', num2str(nsamples),' samples from the distribution.'...
Daniel@0 54 , 'The first ', num2str(options(15))])
Daniel@0 55 disp('samples at the start of the chain are omitted. As persistence')
Daniel@0 56 disp('is used, the momentum has a small random component added at each step.')
Daniel@0 57 disp([num2str(options(7)), ...
Daniel@0 58 ' iterations are used at each step and the step size is ',...
Daniel@0 59 num2str(options(18))])
Daniel@0 60 disp('Sampling starts at the point [0 1].')
Daniel@0 61 disp('The new state is accepted if the threshold value is greater than')
Daniel@0 62 disp('a random number between 0 and 1.')
Daniel@0 63 disp(' ')
Daniel@0 64 disp('Negative step numbers indicate samples discarded from the start of the')
Daniel@0 65 disp('chain.')
Daniel@0 66 disp(' ')
Daniel@0 67 disp('Press any key to continue.')
Daniel@0 68 pause
Daniel@0 69
Daniel@0 70 [samples, energies] = hmc('dempot', x, options, 'demgpot', mix);
Daniel@0 71
Daniel@0 72 disp(' ')
Daniel@0 73 disp('Press any key to continue.')
Daniel@0 74 pause
Daniel@0 75 clc
Daniel@0 76 disp('The plot shows the samples generated by the HMC function.')
Daniel@0 77 disp('The different colours are used to show how the samples move from')
Daniel@0 78 disp('one component to the other over time.')
Daniel@0 79 disp(' ')
Daniel@0 80 disp('Press any key to continue.')
Daniel@0 81 pause
Daniel@0 82 probs = exp(-energies);
Daniel@0 83 fh1 = figure;
Daniel@0 84 % Plot data in 4 groups
Daniel@0 85 ngroups = 4;
Daniel@0 86 g1end = floor(nsamples/ngroups);
Daniel@0 87 g2end = floor(2*nsamples/ngroups);
Daniel@0 88 g3end = floor(3*nsamples/ngroups);
Daniel@0 89 p1 = plot(samples(1:g1end,1), samples(1:g1end,2), 'k.', 'MarkerSize', 12);
Daniel@0 90 hold on
Daniel@0 91 lstrings = char(['Samples 1-' int2str(g1end)], ...
Daniel@0 92 ['Samples ' int2str(g1end+1) '-' int2str(g2end)], ...
Daniel@0 93 ['Samples ' int2str(g2end+1) '-' int2str(g3end)], ...
Daniel@0 94 ['Samples ' int2str(g3end+1) '-' int2str(nsamples)]);
Daniel@0 95 p2 = plot(samples(g1end+1:g2end,1), samples(g1end+1:g2end,2), ...
Daniel@0 96 'r.', 'MarkerSize', 12);
Daniel@0 97 p3 = plot(samples(g2end+1:g3end,1), samples(g2end+1:g3end,2), ...
Daniel@0 98 'g.', 'MarkerSize', 12);
Daniel@0 99 p4 = plot(samples(g3end+1:nsamples,1), samples(g3end+1:nsamples,2), ...
Daniel@0 100 'b.', 'MarkerSize', 12);
Daniel@0 101 legend([p1 p2 p3 p4], lstrings, 2);
Daniel@0 102
Daniel@0 103 clc
Daniel@0 104 disp('We now fit a Gaussian mixture model to the sampled data.')
Daniel@0 105 disp('The model has spherical covariance structure and the correct')
Daniel@0 106 disp('number of components.')
Daniel@0 107 disp(' ')
Daniel@0 108 disp('Press any key to continue.')
Daniel@0 109 pause
Daniel@0 110 % Fit a mixture model to the sample
Daniel@0 111 newmix = gmm(dim, ncentres, 'spherical');
Daniel@0 112 options = foptions;
Daniel@0 113 options(1) = -1; % Switch off all diagnostics
Daniel@0 114 options(14) = 5; % Just use 5 iterations of k-means in initialisation
Daniel@0 115 % Initialise the model parameters from the samples
Daniel@0 116 newmix = gmminit(newmix, samples, options);
Daniel@0 117
Daniel@0 118 % Set up vector of options for EM trainer
Daniel@0 119 options = zeros(1, 18);
Daniel@0 120 options(1) = 1; % Prints out error values.
Daniel@0 121 options(14) = 15; % Max. Number of iterations.
Daniel@0 122
Daniel@0 123 disp('We now train the model using the EM algorithm for 15 iterations')
Daniel@0 124 disp(' ')
Daniel@0 125 disp('Press any key to continue')
Daniel@0 126 pause
Daniel@0 127 [newmix, options, errlog] = gmmem(newmix, samples, options);
Daniel@0 128
Daniel@0 129 % Print out model
Daniel@0 130 disp(' ')
Daniel@0 131 disp('The trained model has parameters ')
Daniel@0 132 disp(' Priors Centres Variances')
Daniel@0 133 disp([newmix.priors' newmix.centres newmix.covars'])
Daniel@0 134 disp('Note the close correspondence between these parameters and those')
Daniel@0 135 disp('of the distribution used to generate the data')
Daniel@0 136 disp(' ')
Daniel@0 137 disp(' Priors Centres Variances')
Daniel@0 138 disp([mix.priors' mix.centres mix.covars'])
Daniel@0 139 disp(' ')
Daniel@0 140 disp('Press any key to exit')
Daniel@0 141 pause
Daniel@0 142
Daniel@0 143 close(fh1);
Daniel@0 144 clear all;
Daniel@0 145