annotate toolboxes/FullBNT-1.0.7/netlab3.3/demmet1.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 samples=demmet1(plot_wait)
Daniel@0 2 %DEMMET1 Demonstrate Markov Chain Monte Carlo sampling on a Gaussian.
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % The problem consists of generating data from a Gaussian in two
Daniel@0 6 % dimensions using a Markov Chain Monte Carlo algorithm. The points are
Daniel@0 7 % plotted one after another to show the path taken by the chain.
Daniel@0 8 %
Daniel@0 9 % DEMMET1(PLOTWAIT) allows the user to set the time (in a whole number
Daniel@0 10 % of seconds) between the plotting of points. This is passed to PAUSE
Daniel@0 11 %
Daniel@0 12 % See also
Daniel@0 13 % DEMHMC1, METROP, GMM, DEMPOT
Daniel@0 14 %
Daniel@0 15
Daniel@0 16 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 17
Daniel@0 18 if nargin == 0 | plot_wait < 0
Daniel@0 19 plot_wait = 0; % No wait if not specified or incorrect
Daniel@0 20 end
Daniel@0 21 dim = 2; % Data dimension
Daniel@0 22 ncentres = 1; % Number of centres in mixture model
Daniel@0 23
Daniel@0 24 seed = 42; % Seed for random weight initialization.
Daniel@0 25 randn('state', seed);
Daniel@0 26 rand('state', seed);
Daniel@0 27
Daniel@0 28 clc
Daniel@0 29 disp('This demonstration illustrates the use of the Markov chain Monte Carlo')
Daniel@0 30 disp('algorithm to sample from a Gaussian distribution.')
Daniel@0 31 disp('The mean is at [0 0].')
Daniel@0 32 disp(' ')
Daniel@0 33 disp('First we set up the parameters of the mixture model we are sampling')
Daniel@0 34 disp('from.')
Daniel@0 35 disp(' ')
Daniel@0 36 disp('Press any key to continue.')
Daniel@0 37 pause
Daniel@0 38
Daniel@0 39 % Set up mixture model to sample from
Daniel@0 40 mix = gmm(dim, ncentres, 'spherical');
Daniel@0 41 mix.centres(1, :) = [0 0];
Daniel@0 42 x = [0 4]; % Start vector
Daniel@0 43
Daniel@0 44 % Set up vector of options for hybrid Monte Carlo.
Daniel@0 45
Daniel@0 46 nsamples = 150; % Number of retained samples.
Daniel@0 47
Daniel@0 48 options = foptions; % Default options vector.
Daniel@0 49 options(1) = 0; % Switch off diagnostics.
Daniel@0 50 options(14) = nsamples; % Number of Monte Carlo samples returned.
Daniel@0 51 options(18) = 0.1;
Daniel@0 52
Daniel@0 53 clc
Daniel@0 54 disp('Next we take 150 samples from the distribution.')
Daniel@0 55 disp('Sampling starts at the point [0 4].')
Daniel@0 56 disp('The new state is accepted if the threshold value is greater than')
Daniel@0 57 disp('a random number between 0 and 1.')
Daniel@0 58 disp(' ')
Daniel@0 59 disp('Press any key to continue.')
Daniel@0 60 pause
Daniel@0 61
Daniel@0 62 [samples, energies] = metrop('dempot', x, options, '', mix);
Daniel@0 63
Daniel@0 64 clc
Daniel@0 65 disp('The plot shows the samples generated by the MCMC function in order')
Daniel@0 66 disp('as an animation to show the path taken by the Markov chain.')
Daniel@0 67 disp('The different colours are used to show that the first few samples')
Daniel@0 68 disp('should be discarded as they lie too far from the mean.')
Daniel@0 69 disp(' ')
Daniel@0 70 disp('Press any key to continue.')
Daniel@0 71 pause
Daniel@0 72 probs = exp(-energies);
Daniel@0 73 fh1 = figure;
Daniel@0 74 g1end = floor(nsamples/4);
Daniel@0 75
Daniel@0 76 for n = 1:nsamples
Daniel@0 77
Daniel@0 78 if n < g1end
Daniel@0 79 Marker = 'k.';
Daniel@0 80 p1 = plot(samples(n,1), samples(n,2), Marker, ...
Daniel@0 81 'EraseMode', 'none', 'MarkerSize', 12);
Daniel@0 82 if n == 1
Daniel@0 83 axis([-3 5 -2 5])
Daniel@0 84 end
Daniel@0 85 else
Daniel@0 86 Marker = 'r.';
Daniel@0 87 p2 = plot(samples(n,1), samples(n,2), Marker, ...
Daniel@0 88 'EraseMode', 'none', 'MarkerSize', 12);
Daniel@0 89 end
Daniel@0 90 hold on
Daniel@0 91 drawnow; % Force drawing immediately
Daniel@0 92 pause(plot_wait);
Daniel@0 93 end
Daniel@0 94 lstrings = char(['Samples 1-' int2str(g1end)], ...
Daniel@0 95 ['Samples ' int2str(g1end+1) '-' int2str(nsamples)]);
Daniel@0 96 legend([p1 p2], lstrings, 1);
Daniel@0 97
Daniel@0 98 disp(' ')
Daniel@0 99 disp('Press any key to exit.')
Daniel@0 100 pause
Daniel@0 101 close(fh1);
Daniel@0 102 clear all;
Daniel@0 103