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