annotate toolboxes/FullBNT-1.0.7/netlab3.3/demgp.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 %DEMGP Demonstrate simple regression using a Gaussian Process.
Daniel@0 2 %
Daniel@0 3 % Description
Daniel@0 4 % The problem consists of one input variable X and one target variable
Daniel@0 5 % T. The values in X are chosen in two separated clusters and the
Daniel@0 6 % target data is generated by computing SIN(2*PI*X) and adding Gaussian
Daniel@0 7 % noise. Two Gaussian Processes, each with different covariance
Daniel@0 8 % functions are trained by optimising the hyperparameters using the
Daniel@0 9 % scaled conjugate gradient algorithm. The final predictions are
Daniel@0 10 % plotted together with 2 standard deviation error bars.
Daniel@0 11 %
Daniel@0 12 % See also
Daniel@0 13 % GP, GPERR, GPFWD, GPGRAD, GPINIT, SCG
Daniel@0 14 %
Daniel@0 15
Daniel@0 16 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 17
Daniel@0 18
Daniel@0 19 % Find out if flops is available (i.e. pre-version 6 Matlab)
Daniel@0 20 v = version;
Daniel@0 21 if (str2num(strtok(v, '.')) >= 6)
Daniel@0 22 flops_works = logical(0);
Daniel@0 23 else
Daniel@0 24 flops_works = logical(1);
Daniel@0 25 end
Daniel@0 26
Daniel@0 27 randn('state', 42);
Daniel@0 28 x = [0.1 0.15 0.2 0.25 0.65 0.7 0.75 0.8 0.85 0.9]';
Daniel@0 29 ndata = length(x);
Daniel@0 30 t = sin(2*pi*x) + 0.05*randn(ndata, 1);
Daniel@0 31
Daniel@0 32 xtest = linspace(0, 1, 50)';
Daniel@0 33
Daniel@0 34 clc
Daniel@0 35 disp('This demonstration illustrates the use of a Gaussian Process')
Daniel@0 36 disp('model for regression problems. The data is generated from a noisy')
Daniel@0 37 disp('sine function.')
Daniel@0 38 disp(' ')
Daniel@0 39 disp('Press any key to continue.')
Daniel@0 40 pause
Daniel@0 41
Daniel@0 42 flops(0);
Daniel@0 43 % Initialise the parameters.
Daniel@0 44 net = gp(1, 'sqexp');
Daniel@0 45 prior.pr_mean = 0;
Daniel@0 46 prior.pr_var = 1;
Daniel@0 47 net = gpinit(net, x, t, prior);
Daniel@0 48
Daniel@0 49 clc
Daniel@0 50 disp('The first GP uses the squared exponential covariance function.')
Daniel@0 51 disp('The hyperparameters are initialised by sampling from a Gaussian with a')
Daniel@0 52 disp(['mean of ', num2str(prior.pr_mean), ' and variance ', ...
Daniel@0 53 num2str(prior.pr_var), '.'])
Daniel@0 54 disp('After initializing the network, we train it using the scaled conjugate')
Daniel@0 55 disp('gradients algorithm for 20 cycles.')
Daniel@0 56 disp(' ')
Daniel@0 57 disp('Press any key to continue')
Daniel@0 58 pause
Daniel@0 59
Daniel@0 60 % Now train to find the hyperparameters.
Daniel@0 61 options = foptions;
Daniel@0 62 options(1) = 1; % Display training error values
Daniel@0 63 options(14) = 20;
Daniel@0 64 flops(0)
Daniel@0 65 [net, options] = netopt(net, options, x, t, 'scg');
Daniel@0 66 if flops_works
Daniel@0 67 sflops = flops;
Daniel@0 68 end
Daniel@0 69
Daniel@0 70 disp('The second GP uses the rational quadratic covariance function.')
Daniel@0 71 disp('The hyperparameters are initialised by sampling from a Gaussian with a')
Daniel@0 72 disp(['mean of ', num2str(prior.pr_mean), ' and variance ', num2str(prior.pr_var)])
Daniel@0 73 disp('After initializing the network, we train it using the scaled conjugate')
Daniel@0 74 disp('gradients algorithm for 20 cycles.')
Daniel@0 75 disp(' ')
Daniel@0 76 disp('Press any key to continue')
Daniel@0 77 pause
Daniel@0 78 flops(0)
Daniel@0 79 net2 = gp(1, 'ratquad');
Daniel@0 80 net2 = gpinit(net2, x, t, prior);
Daniel@0 81 flops(0)
Daniel@0 82 [net2, options] = netopt(net2, options, x, t, 'scg');
Daniel@0 83 if flops_works
Daniel@0 84 rflops = flops;
Daniel@0 85 end
Daniel@0 86
Daniel@0 87 disp(' ')
Daniel@0 88 disp('Press any key to continue')
Daniel@0 89 disp(' ')
Daniel@0 90 pause
Daniel@0 91 clc
Daniel@0 92
Daniel@0 93 fprintf(1, 'For squared exponential covariance function,');
Daniel@0 94 if flops_works
Daniel@0 95 fprintf(1, 'flops = %d', sflops);
Daniel@0 96 end
Daniel@0 97 fprintf(1, '\nfinal hyperparameters:\n')
Daniel@0 98 format_string = strcat(' bias:\t\t\t%10.6f\n noise:\t\t%10.6f\n', ...
Daniel@0 99 ' inverse lengthscale:\t%10.6f\n vertical scale:\t%10.6f\n');
Daniel@0 100 fprintf(1, format_string, ...
Daniel@0 101 exp(net.bias), exp(net.noise), exp(net.inweights(1)), exp(net.fpar(1)));
Daniel@0 102 fprintf(1, '\n\nFor rational quadratic covariance function,');
Daniel@0 103 if flops_works
Daniel@0 104 fprintf(1, 'flops = %d', rflops);
Daniel@0 105 end
Daniel@0 106 fprintf(1, '\nfinal hyperparameters:\n')
Daniel@0 107 format_string = [format_string ' cov decay order:\t%10.6f\n'];
Daniel@0 108 fprintf(1, format_string, ...
Daniel@0 109 exp(net2.bias), exp(net2.noise), exp(net2.inweights(1)), ...
Daniel@0 110 exp(net2.fpar(1)), exp(net2.fpar(2)));
Daniel@0 111 disp(' ')
Daniel@0 112 disp('Press any key to continue')
Daniel@0 113 pause
Daniel@0 114
Daniel@0 115 disp(' ')
Daniel@0 116 disp('Now we plot the data, underlying function, model outputs and two')
Daniel@0 117 disp('standard deviation error bars on a single graph to compare the results.')
Daniel@0 118 disp(' ')
Daniel@0 119 disp('Press any key to continue.')
Daniel@0 120 pause
Daniel@0 121 cn = gpcovar(net, x);
Daniel@0 122 cninv = inv(cn);
Daniel@0 123 [ytest, sigsq] = gpfwd(net, xtest, cninv);
Daniel@0 124 sig = sqrt(sigsq);
Daniel@0 125
Daniel@0 126 fh1 = figure;
Daniel@0 127 hold on
Daniel@0 128 plot(x, t, 'ok');
Daniel@0 129 xlabel('Input')
Daniel@0 130 ylabel('Target')
Daniel@0 131 fplot('sin(2*pi*x)', [0 1], '--m');
Daniel@0 132 plot(xtest, ytest, '-k');
Daniel@0 133 plot(xtest, ytest+(2*sig), '-b', xtest, ytest-(2*sig), '-b');
Daniel@0 134 axis([0 1 -1.5 1.5]);
Daniel@0 135 title('Squared exponential covariance function')
Daniel@0 136 legend('data', 'function', 'GP', 'error bars');
Daniel@0 137 hold off
Daniel@0 138
Daniel@0 139 cninv2 = inv(gpcovar(net2, x));
Daniel@0 140 [ytest2, sigsq2] = gpfwd(net2, xtest, cninv2);
Daniel@0 141 sig2 = sqrt(sigsq2);
Daniel@0 142 fh2 = figure;
Daniel@0 143 hold on
Daniel@0 144 plot(x, t, 'ok');
Daniel@0 145 xlabel('Input')
Daniel@0 146 ylabel('Target')
Daniel@0 147 fplot('sin(2*pi*x)', [0 1], '--m');
Daniel@0 148 plot(xtest, ytest2, '-k');
Daniel@0 149 plot(xtest, ytest2+(2*sig2), '-b', xtest, ytest2-(2*sig2), '-b');
Daniel@0 150 axis([0 1 -1.5 1.5]);
Daniel@0 151 title('Rational quadratic covariance function')
Daniel@0 152 legend('data', 'function', 'GP', 'error bars');
Daniel@0 153 hold off
Daniel@0 154
Daniel@0 155 disp(' ')
Daniel@0 156 disp('Press any key to end.')
Daniel@0 157 pause
Daniel@0 158 close(fh1);
Daniel@0 159 close(fh2);
Daniel@0 160 clear all;