annotate toolboxes/FullBNT-1.0.7/netlab3.3/demgpard.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 %DEMGPARD Demonstrate ARD using a Gaussian Process.
Daniel@0 2 %
Daniel@0 3 % Description
Daniel@0 4 % The data consists of three input variables X1, X2 and X3, and one
Daniel@0 5 % target variable T. The target data is generated by computing
Daniel@0 6 % SIN(2*PI*X1) and adding Gaussian noise, x2 is a copy of x1 with a
Daniel@0 7 % higher level of added noise, and x3 is sampled randomly from a
Daniel@0 8 % Gaussian distribution. A Gaussian Process, is trained by optimising
Daniel@0 9 % the hyperparameters using the scaled conjugate gradient algorithm.
Daniel@0 10 % The final values of the hyperparameters show that the model
Daniel@0 11 % successfully identifies the importance of each input.
Daniel@0 12 %
Daniel@0 13 % See also
Daniel@0 14 % DEMGP, GP, GPERR, GPFWD, GPGRAD, GPINIT, SCG
Daniel@0 15 %
Daniel@0 16
Daniel@0 17 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 18
Daniel@0 19 clc;
Daniel@0 20 randn('state', 1729);
Daniel@0 21 rand('state', 1729);
Daniel@0 22 disp('This demonstration illustrates the technique of automatic relevance')
Daniel@0 23 disp('determination (ARD) using a Gaussian Process.')
Daniel@0 24 disp(' ');
Daniel@0 25 disp('First, we set up a synthetic data set involving three input variables:')
Daniel@0 26 disp('x1 is sampled uniformly from the range (0,1) and has a low level of')
Daniel@0 27 disp('added Gaussian noise, x2 is a copy of x1 with a higher level of added')
Daniel@0 28 disp('noise, and x3 is sampled randomly from a Gaussian distribution. The')
Daniel@0 29 disp('single target variable is given by t = sin(2*pi*x1) with additive')
Daniel@0 30 disp('Gaussian noise. Thus x1 is very relevant for determining the target')
Daniel@0 31 disp('value, x2 is of some relevance, while x3 should in principle be')
Daniel@0 32 disp('irrelevant.')
Daniel@0 33 disp(' ');
Daniel@0 34 disp('Press any key to see a plot of t against x1.')
Daniel@0 35 pause;
Daniel@0 36
Daniel@0 37 ndata = 100;
Daniel@0 38 x1 = rand(ndata, 1);
Daniel@0 39 x2 = x1 + 0.05*randn(ndata, 1);
Daniel@0 40 x3 = 0.5 + 0.5*randn(ndata, 1);
Daniel@0 41 x = [x1, x2, x3];
Daniel@0 42 t = sin(2*pi*x1) + 0.1*randn(ndata, 1);
Daniel@0 43
Daniel@0 44 % Plot the data and the original function.
Daniel@0 45 h = figure;
Daniel@0 46 plotvals = linspace(0, 1, 200)';
Daniel@0 47 plot(x1, t, 'ob')
Daniel@0 48 hold on
Daniel@0 49 xlabel('Input x1')
Daniel@0 50 ylabel('Target')
Daniel@0 51 axis([0 1 -1.5 1.5])
Daniel@0 52 [fx, fy] = fplot('sin(2*pi*x)', [0 1]);
Daniel@0 53 plot(fx, fy, '-g', 'LineWidth', 2);
Daniel@0 54 legend('data', 'function');
Daniel@0 55
Daniel@0 56 disp(' ');
Daniel@0 57 disp('Press any key to continue')
Daniel@0 58 pause; clc;
Daniel@0 59
Daniel@0 60 disp('The Gaussian Process has a separate hyperparameter for each input.')
Daniel@0 61 disp('The hyperparameters are trained by error minimisation using the scaled.')
Daniel@0 62 disp('conjugate gradient optimiser.')
Daniel@0 63 disp(' ');
Daniel@0 64 disp('Press any key to create and train the model.')
Daniel@0 65 disp(' ');
Daniel@0 66 pause;
Daniel@0 67
Daniel@0 68 net = gp(3, 'sqexp');
Daniel@0 69 % Initialise the parameters.
Daniel@0 70 prior.pr_mean = 0;
Daniel@0 71 prior.pr_var = 0.1;
Daniel@0 72 net = gpinit(net, x, t, prior);
Daniel@0 73
Daniel@0 74 % Now train to find the hyperparameters.
Daniel@0 75 options = foptions;
Daniel@0 76 options(1) = 1;
Daniel@0 77 options(14) = 30;
Daniel@0 78
Daniel@0 79 [net, options] = netopt(net, options, x, t, 'scg');
Daniel@0 80
Daniel@0 81 rel = exp(net.inweights);
Daniel@0 82
Daniel@0 83 fprintf(1, ...
Daniel@0 84 '\nFinal hyperparameters:\n\n bias:\t\t%10.6f\n noise:\t%10.6f\n', ...
Daniel@0 85 exp(net.bias), exp(net.noise));
Daniel@0 86 fprintf(1, ' Vertical scale: %8.6f\n', exp(net.fpar(1)));
Daniel@0 87 fprintf(1, ' Input 1:\t%10.6f\n Input 2:\t%10.6f\n', ...
Daniel@0 88 rel(1), rel(2));
Daniel@0 89 fprintf(1, ' Input 3:\t%10.6f\n\n', rel(3));
Daniel@0 90 disp(' ');
Daniel@0 91 disp('We see that the inverse lengthscale associated with')
Daniel@0 92 disp('input x1 is large, that of x2 has an intermediate value and the variance')
Daniel@0 93 disp('of weights associated with x3 is small.')
Daniel@0 94 disp(' ');
Daniel@0 95 disp('This implies that the Gaussian Process is giving greatest emphasis')
Daniel@0 96 disp('to x1 and least emphasis to x3, with intermediate emphasis on')
Daniel@0 97 disp('x2 in the covariance function.')
Daniel@0 98 disp(' ')
Daniel@0 99 disp('Since the target t is statistically independent of x3 we might')
Daniel@0 100 disp('expect the weights associated with this input would go to')
Daniel@0 101 disp('zero. However, for any finite data set there may be some chance')
Daniel@0 102 disp('correlation between x3 and t, and so the corresponding hyperparameter remains')
Daniel@0 103 disp('finite.')
Daniel@0 104 disp('Press any key to continue.')
Daniel@0 105 pause
Daniel@0 106
Daniel@0 107 disp('Finally, we plot the output of the Gaussian Process along the line')
Daniel@0 108 disp('x1 = x2 = x3, together with the true underlying function.')
Daniel@0 109 xt = linspace(0, 1, 50);
Daniel@0 110 xtest = [xt', xt', xt'];
Daniel@0 111
Daniel@0 112 cn = gpcovar(net, x);
Daniel@0 113 cninv = inv(cn);
Daniel@0 114 [ytest, sigsq] = gpfwd(net, xtest, cninv);
Daniel@0 115 sig = sqrt(sigsq);
Daniel@0 116
Daniel@0 117 figure(h); hold on;
Daniel@0 118 plot(xt, ytest, '-k');
Daniel@0 119 plot(xt, ytest+(2*sig), '-b', xt, ytest-(2*sig), '-b');
Daniel@0 120 axis([0 1 -1.5 1.5]);
Daniel@0 121 fplot('sin(2*pi*x)', [0 1], '--m');
Daniel@0 122
Daniel@0 123 disp(' ');
Daniel@0 124 disp('Press any key to end.')
Daniel@0 125 pause; clc; close(h); clear all
Daniel@0 126