comparison toolboxes/FullBNT-1.0.7/netlab3.3/demmet1.m @ 0:e9a9cd732c1e tip

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