comparison toolboxes/FullBNT-1.0.7/netlab3.3/demprgp.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 demprgp(action);
2 %DEMPRGP Demonstrate sampling from a Gaussian Process prior.
3 %
4 % Description
5 % This function plots the functions represented by a Gaussian Process
6 % model. The hyperparameter values can be adjusted on a linear scale
7 % using the sliders (though the exponential of the parameters is used
8 % in the covariance function), or by typing values into the text boxes
9 % and pressing the return key. Both types of covariance function are
10 % supported. An extra function specific parameter is needed for the
11 % rational quadratic function.
12 %
13 % See also
14 % GP
15 %
16
17 % Copyright (c) Ian T Nabney (1996-2001)
18
19 if nargin<1,
20 action='initialize';
21 end;
22
23 if strcmp(action,'initialize')
24
25 % Bounds on hyperparameter values
26 biasminval = -3.0; biasmaxval = 3.0;
27 noiseminval = -20; noisemaxval = -2;
28 fparminval = 0.0; fparmaxval = 2.0;
29 inwminval = 0; inwmaxval = 8;
30 % Initial hyperparameter values
31 bias = (biasminval+biasmaxval)/2;
32 noise = (noiseminval+noisemaxval)/2;
33 inweights = (inwminval+inwmaxval)/2;
34 fpar = (fparminval+fparmaxval)/2;
35 fpar2 = (fparminval+fparmaxval)/2;
36
37 gptype = 'sqexp';
38
39 % Create FIGURE
40 fig=figure( ...
41 'Name','Sampling from a Gaussian Process prior', ...
42 'Position', [50 50 480 380], ...
43 'NumberTitle','off', ...
44 'Color', [0.8 0.8 0.8], ...
45 'Visible','on');
46
47 % List box for covariance function type
48 nettype_box = uicontrol(fig, ...
49 'Style', 'listbox', ...
50 'Units', 'normalized', ...
51 'HorizontalAlignment', 'center', ...
52 'Position', [0.52 0.77 0.40 0.12], ...
53 'String', 'Squared Exponential|Rational Quadratic', ...
54 'Max', 1, 'Min', 0, ... % Only allow one selection
55 'Value', 1, ... % Initial value is squared exponential
56 'BackgroundColor',[0.60 0.60 0.60],...
57 'CallBack', 'demprgp GPtype');
58
59 % Title for list box
60 uicontrol(fig, ...
61 'Style', 'text', ...
62 'Units', 'normalized', ...
63 'Position', [0.52 0.89 0.40 0.05], ...
64 'String', 'Covariance Function Type', ...
65 'BackgroundColor', get(fig, 'Color'), ...
66 'HorizontalAlignment', 'center');
67
68 % Frames to enclose sliders
69 bottom_row = 0.04;
70 slider_frame_height = 0.15;
71 biasframe = uicontrol(fig, ...
72 'Style', 'frame', ...
73 'Units', 'normalized', ...
74 'BackgroundColor', [0.6 0.6 0.6], ...
75 'String', 'bias', ...
76 'HorizontalAlignment', 'left', ...
77 'Position', [0.05 bottom_row 0.35 slider_frame_height]);
78
79 bpos = get(biasframe, 'Position');
80 noise_frame_bottom = bpos(2) + bpos(4) + 0.02;
81 noiseframe = uicontrol(fig, ...
82 'Style', 'frame', ...
83 'Units', 'normalized', ...
84 'BackgroundColor', [0.6 0.6 0.6], ...
85 'Position', [0.05 noise_frame_bottom 0.35 slider_frame_height]);
86
87 npos = get(noiseframe, 'Position');
88 inw_frame_bottom = npos(2) + npos(4) + 0.02;
89 inwframe = uicontrol(fig, ...
90 'Style', 'frame', ...
91 'Units', 'normalized', ...
92 'BackgroundColor', [0.6 0.6 0.6], ...
93 'Position', [0.05 inw_frame_bottom 0.35 slider_frame_height]);
94
95 inwpos = get(inwframe, 'Position');
96 fpar_frame_bottom = inwpos(2) + inwpos(4) + 0.02;
97 % This frame sometimes has multiple parameters
98 uicontrol(fig, ...
99 'Style', 'frame', ...
100 'Units', 'normalized', ...
101 'BackgroundColor', [0.6 0.6 0.6], ...
102 'Position', [0.05 fpar_frame_bottom 0.35 2*slider_frame_height]);
103
104 % Frame text
105 slider_text_height = 0.05;
106 slider_text_voffset = 0.08;
107 uicontrol(fig, ...
108 'Style', 'text', ...
109 'Units', 'normalized', ...
110 'HorizontalAlignment', 'left', ...
111 'BackgroundColor', [0.6 0.6 0.6], ...
112 'Position', [0.07 bottom_row+slider_text_voffset ...
113 0.06 slider_text_height], ...
114 'String', 'bias');
115
116 % Frame text
117 noiseframe = uicontrol(fig, ...
118 'Style', 'text', ...
119 'Units', 'normalized', ...
120 'HorizontalAlignment', 'left', ...
121 'BackgroundColor', [0.6 0.6 0.6], ...
122 'Position', [0.07 noise_frame_bottom+slider_text_voffset ...
123 0.08 slider_text_height], ...
124 'String', 'noise');
125
126 % Frame text
127 uicontrol(fig, ...
128 'Style', 'text', ...
129 'Units', 'normalized', ...
130 'HorizontalAlignment', 'left', ...
131 'BackgroundColor', [0.6 0.6 0.6], ...
132 'Position', [0.07 inw_frame_bottom+slider_text_voffset ...
133 0.14 slider_text_height], ...
134 'String', 'inweights');
135
136 % Frame text
137 uicontrol(fig, ...
138 'Style', 'text', ...
139 'Units', 'normalized', ...
140 'HorizontalAlignment', 'left', ...
141 'BackgroundColor', [0.6 0.6 0.6], ...
142 'Position', [0.07 fpar_frame_bottom+slider_frame_height+ ...
143 slider_text_voffset 0.06 slider_text_height], ...
144 'String', 'fpar');
145
146 uicontrol(fig, ...
147 'Style', 'text', ...
148 'Units', 'normalized', ...
149 'HorizontalAlignment', 'left', ...
150 'BackgroundColor', [0.6 0.6 0.6], ...
151 'Position', [0.07 fpar_frame_bottom+slider_text_voffset ...
152 0.06 slider_text_height], ...
153 'String', 'fpar2', ...
154 'Tag', 'fpar2text', ...
155 'Enable', 'off');
156
157 % Slider
158 slider_left = 0.07;
159 slider_width = 0.31;
160 slider_frame_voffset = 0.02;
161 biasslide = uicontrol(fig, ...
162 'Style', 'slider', ...
163 'Units', 'normalized', ...
164 'Value', bias, ...
165 'BackgroundColor', [0.8 0.8 0.8], ...
166 'Position', [slider_left bottom_row+slider_frame_voffset ...
167 slider_width 0.05], ...
168 'Min', biasminval, 'Max', biasmaxval, ...
169 'Callback', 'demprgp update');
170
171 % Slider
172 noiseslide = uicontrol(fig, ...
173 'Style', 'slider', ...
174 'Units', 'normalized', ...
175 'Value', noise, ...
176 'BackgroundColor', [0.8 0.8 0.8], ...
177 'Position', [slider_left noise_frame_bottom+slider_frame_voffset ...
178 slider_width 0.05], ...
179 'Min', noiseminval, 'Max', noisemaxval, ...
180 'Callback', 'demprgp update');
181
182 % Slider
183 inweightsslide = uicontrol(fig, ...
184 'Style', 'slider', ...
185 'Units', 'normalized', ...
186 'Value', inweights, ...
187 'BackgroundColor', [0.8 0.8 0.8], ...
188 'Position', [slider_left inw_frame_bottom+slider_frame_voffset ...
189 slider_width 0.05], ...
190 'Min', inwminval, 'Max', inwmaxval, ...
191 'Callback', 'demprgp update');
192
193 % Slider
194 fparslide = uicontrol(fig, ...
195 'Style', 'slider', ...
196 'Units', 'normalized', ...
197 'Value', fpar, ...
198 'BackgroundColor', [0.8 0.8 0.8], ...
199 'Position', [slider_left fpar_frame_bottom+slider_frame_height+ ...
200 slider_frame_voffset slider_width 0.05], ...
201 'Min', fparminval, 'Max', fparmaxval, ...
202 'Callback', 'demprgp update');
203
204 fpar2slide = uicontrol(fig, ...
205 'Style', 'slider', ...
206 'Units', 'normalized', ...
207 'Value', fpar2, ...
208 'BackgroundColor', [0.8 0.8 0.8], ...
209 'Position', [slider_left fpar_frame_bottom+slider_frame_voffset ...
210 slider_width 0.05], ...
211 'Min', fparminval, 'Max', fparmaxval, ...
212 'Callback', 'demprgp update', ...
213 'Tag', 'fpar2slider', ...
214 'Enable', 'off');
215
216 % Text display of hyper-parameter values
217
218 format = '%8f';
219
220 hp_left = 0.20;
221 hp_width = 0.17;
222 biasval = uicontrol(fig, ...
223 'Style', 'edit', ...
224 'Units', 'normalized', ...
225 'Position', [hp_left bottom_row+slider_text_voffset ...
226 hp_width slider_text_height], ...
227 'String', sprintf(format, bias), ...
228 'Callback', 'demprgp newval');
229
230 noiseval = uicontrol(fig, ...
231 'Style', 'edit', ...
232 'Units', 'normalized', ...
233 'Position', [hp_left noise_frame_bottom+slider_text_voffset ...
234 hp_width slider_text_height], ...
235 'String', sprintf(format, noise), ...
236 'Callback', 'demprgp newval');
237
238 inweightsval = uicontrol(fig, ...
239 'Style', 'edit', ...
240 'Units', 'normalized', ...
241 'Position', [hp_left inw_frame_bottom+slider_text_voffset ...
242 hp_width slider_text_height], ...
243 'String', sprintf(format, inweights), ...
244 'Callback', 'demprgp newval');
245
246 fparval = uicontrol(fig, ...
247 'Style', 'edit', ...
248 'Units', 'normalized', ...
249 'Position', [hp_left fpar_frame_bottom+slider_frame_height+ ...
250 slider_text_voffset hp_width slider_text_height], ...
251 'String', sprintf(format, fpar), ...
252 'Callback', 'demprgp newval');
253
254 fpar2val = uicontrol(fig, ...
255 'Style', 'edit', ...
256 'Units', 'normalized', ...
257 'Position', [hp_left fpar_frame_bottom+slider_text_voffset ...
258 hp_width slider_text_height], ...
259 'String', sprintf(format, fpar), ...
260 'Callback', 'demprgp newval', ...
261 'Enable', 'off', ...
262 'Tag', 'fpar2val');
263
264
265 % The graph box
266 haxes = axes('Position', [0.5 0.28 0.45 0.45], ...
267 'Units', 'normalized', ...
268 'Visible', 'on');
269
270 % The SAMPLE button
271 uicontrol(fig, ...
272 'Style','push', ...
273 'Units','normalized', ...
274 'BackgroundColor', [0.6 0.6 0.6], ...
275 'Position',[0.5 bottom_row 0.13 0.1], ...
276 'String','Sample', ...
277 'Callback','demprgp replot');
278
279 % The CLOSE button
280 uicontrol(fig, ...
281 'Style','push', ...
282 'Units','normalized', ...
283 'BackgroundColor', [0.6 0.6 0.6], ...
284 'Position',[0.82 bottom_row 0.13 0.1], ...
285 'String','Close', ...
286 'Callback','close(gcf)');
287
288 % The HELP button
289 uicontrol(fig, ...
290 'Style','push', ...
291 'Units','normalized', ...
292 'BackgroundColor', [0.6 0.6 0.6], ...
293 'Position',[0.66 bottom_row 0.13 0.1], ...
294 'String','Help', ...
295 'Callback','demprgp help');
296
297 % Save handles to objects
298
299 hndlList=[fig biasslide noiseslide inweightsslide fparslide ...
300 biasval noiseval inweightsval ...
301 fparval haxes nettype_box];
302 set(fig, 'UserData', hndlList);
303
304 demprgp('replot')
305
306
307 elseif strcmp(action, 'update'),
308
309 % Update when a slider is moved.
310
311 hndlList = get(gcf, 'UserData');
312 biasslide = hndlList(2);
313 noiseslide = hndlList(3);
314 inweightsslide = hndlList(4);
315 fparslide = hndlList(5);
316 biasval = hndlList(6);
317 noiseval = hndlList(7);
318 inweightsval = hndlList(8);
319 fparval = hndlList(9);
320 haxes = hndlList(10);
321 nettype_box = hndlList(11);
322
323
324 bias = get(biasslide, 'Value');
325 noise = get(noiseslide, 'Value');
326 inweights = get(inweightsslide, 'Value');
327 fpar = get(fparslide, 'Value');
328 fpar2 = get(findobj('Tag', 'fpar2slider'), 'Value');
329
330 format = '%8f';
331 set(biasval, 'String', sprintf(format, bias));
332 set(noiseval, 'String', sprintf(format, noise));
333 set(inweightsval, 'String', sprintf(format, inweights));
334 set(fparval, 'String', sprintf(format, fpar));
335 set(findobj('Tag', 'fpar2val'), 'String', ...
336 sprintf(format, fpar2));
337
338 demprgp('replot');
339
340 elseif strcmp(action, 'newval'),
341
342 % Update when text is changed.
343
344 hndlList = get(gcf, 'UserData');
345 biasslide = hndlList(2);
346 noiseslide = hndlList(3);
347 inweightsslide = hndlList(4);
348 fparslide = hndlList(5);
349 biasval = hndlList(6);
350 noiseval = hndlList(7);
351 inweightsval = hndlList(8);
352 fparval = hndlList(9);
353 haxes = hndlList(10);
354
355 bias = sscanf(get(biasval, 'String'), '%f');
356 noise = sscanf(get(noiseval, 'String'), '%f');
357 inweights = sscanf(get(inweightsval, 'String'), '%f');
358 fpar = sscanf(get(fparval, 'String'), '%f');
359 fpar2 = sscanf(get(findobj('Tag', 'fpar2val'), 'String'), '%f');
360
361 set(biasslide, 'Value', bias);
362 set(noiseslide, 'Value', noise);
363 set(inweightsslide, 'Value', inweights);
364 set(fparslide, 'Value', fpar);
365 set(findobj('Tag', 'fpar2slider'), 'Value', fpar2);
366
367 demprgp('replot');
368
369 elseif strcmp(action, 'GPtype')
370 hndlList = get(gcf, 'UserData');
371 nettype_box = hndlList(11);
372 gptval = get(nettype_box, 'Value');
373 if gptval == 1
374 % Squared exponential, so turn off fpar2
375 set(findobj('Tag', 'fpar2text'), 'Enable', 'off');
376 set(findobj('Tag', 'fpar2slider'), 'Enable', 'off');
377 set(findobj('Tag', 'fpar2val'), 'Enable', 'off');
378 else
379 % Rational quadratic, so turn on fpar2
380 set(findobj('Tag', 'fpar2text'), 'Enable', 'on');
381 set(findobj('Tag', 'fpar2slider'), 'Enable', 'on');
382 set(findobj('Tag', 'fpar2val'), 'Enable', 'on');
383 end
384 demprgp('replot');
385
386 elseif strcmp(action, 'replot'),
387
388 % Re-sample from the prior and plot graphs.
389
390 oldFigNumber=watchon;
391
392 hndlList = get(gcf, 'UserData');
393 biasslide = hndlList(2);
394 noiseslide = hndlList(3);
395 inweightsslide = hndlList(4);
396 fparslide = hndlList(5);
397 haxes = hndlList(10);
398 nettype_box = hndlList(11);
399 gptval = get(nettype_box, 'Value');
400 if gptval == 1
401 gptype = 'sqexp';
402 else
403 gptype = 'ratquad';
404 end
405
406 bias = get(biasslide, 'Value');
407 noise = get(noiseslide, 'Value');
408 inweights = get(inweightsslide, 'Value');
409 fpar = get(fparslide, 'Value');
410
411
412 axes(haxes);
413 cla
414 set(gca, ...
415 'Box', 'on', ...
416 'Color', [0 0 0], ...
417 'XColor', [0 0 0], ...
418 'YColor', [0 0 0], ...
419 'FontSize', 14);
420 ymin = -10;
421 ymax = 10;
422 axis([-1 1 ymin ymax]);
423 set(gca,'DefaultLineLineWidth', 2);
424
425 xvals = (-1:0.01:1)';
426 nsample = 10; % Number of samples from prior.
427 hold on
428 plot([-1 0; 1 0], [0 ymin; 0 ymax], 'b--');
429 net = gp(1, gptype);
430 net.bias = bias;
431 net.noise = noise;
432 net.inweights = inweights;
433 if strcmp(gptype, 'sqexp')
434 net.fpar = fpar;
435 else
436 fpar2 = get(findobj('Tag', 'fpar2slider'), 'Value');
437 net.fpar = [fpar fpar2];
438 end
439 cn = gpcovar(net, xvals);
440 cninv = inv(cn);
441 cnchol = chol(cn);
442 set(gca, 'DefaultLineLineWidth', 1);
443 for n = 1:nsample
444 y = (cnchol') * randn(size(xvals));
445 plot(xvals, y, 'y');
446 end
447
448 watchoff(oldFigNumber);
449
450 elseif strcmp(action, 'help'),
451
452 % Provide help to user.
453
454 oldFigNumber=watchon;
455
456 helpfig = figure('Position', [100 100 480 400], ...
457 'Name', 'Help', ...
458 'NumberTitle', 'off', ...
459 'Color', [0.8 0.8 0.8], ...
460 'Visible','on');
461
462 % The HELP TITLE BAR frame
463 uicontrol(helpfig, ...
464 'Style','frame', ...
465 'Units','normalized', ...
466 'HorizontalAlignment', 'center', ...
467 'Position', [0.05 0.82 0.9 0.1], ...
468 'BackgroundColor',[0.60 0.60 0.60]);
469
470 % The HELP TITLE BAR text
471 uicontrol(helpfig, ...
472 'Style', 'text', ...
473 'Units', 'normalized', ...
474 'BackgroundColor', [0.6 0.6 0.6], ...
475 'Position', [0.26 0.85 0.6 0.05], ...
476 'HorizontalAlignment', 'left', ...
477 'String', 'Help: Sampling from a Gaussian Process Prior');
478
479 helpstr1 = strcat(...
480 'This demonstration shows the effects of sampling from a Gaussian', ...
481 ' process prior. The parameters bias, noise, inweights and fpar', ...
482 ' control the corresponding terms in the covariance function of the',...
483 ' Gaussian process. Their values can be adjusted on a linear scale',...
484 ' using the sliders, or by typing values into the text boxes and',...
485 ' pressing the return key. After setting these values, press the',...
486 ' ''Sample'' button to see a new sample from the prior.');
487
488 helpstr2 = strcat(...
489 'Observe how inweights controls horizontal length-scale of the',...
490 ' variation in the functions, noise controls the roughness of the',...
491 ' functions, and the bias controls the size of the', ...
492 ' vertical offset of the signal.');
493 helpstr3 = strcat(...
494 'There are two types of covariance function supported by', ...
495 ' Netlab which can be selected using the ''Covariance Function', ...
496 ' Type'' menu.');
497 helpstr4 = strcat(...
498 'The squared exponential has a single fpar which', ...
499 ' controls the vertical scale of the process.');
500 helpstr5 = strcat(...
501 'The rational quadratic has two fpar values. The first is', ...
502 ' is a scale parameter inside the rational function like the',...
503 ' first fpar for the squared exponential covariance, while the', ...
504 ' second gives the exponent of the rational function (i.e. the',...
505 ' rate of decay of the covariance function.');
506 % Set up cell array with help strings
507 hstr(1) = {helpstr1};
508 hstr(2) = {''};
509 hstr(3) = {helpstr2};
510 hstr(4) = {''};
511 hstr(5) = {helpstr3};
512 hstr(6) = {''};
513 hstr(7) = {helpstr4};
514 hstr(8) = {''};
515 hstr(9) = {helpstr5};
516
517 % The HELP text
518 helpui = uicontrol(helpfig, ...
519 'Style', 'Text', ...
520 'Units', 'normalized', ...
521 'ForegroundColor', [0 0 0], ...
522 'HorizontalAlignment', 'left', ...
523 'BackgroundColor', [1 1 1], ...
524 'Min', 0, ...
525 'Max', 2, ...
526 'Position', [0.05 0.2 0.9 0.57]);
527 [hstrw, newpos] = textwrap(helpui, hstr);
528 set(helpui, 'String', hstrw, 'Position', [0.05, 0.2, 0.9 newpos(4)]);
529
530 % The CLOSE button
531 uicontrol(helpfig, ...
532 'Style','push', ...
533 'Units','normalized', ...
534 'BackgroundColor', [0.6 0.6 0.6], ...
535 'Position',[0.4 0.05 0.2 0.1], ...
536 'String','Close', ...
537 'Callback','close(gcf)');
538
539 watchoff(oldFigNumber);
540
541 end;
542