annotate toolboxes/FullBNT-1.0.7/netlab3.3/demgauss.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 %DEMGAUSS Demonstrate sampling from Gaussian distributions.
Daniel@0 2 %
Daniel@0 3 % Description
Daniel@0 4 %
Daniel@0 5 % DEMGAUSS provides a simple illustration of the generation of data
Daniel@0 6 % from Gaussian distributions. It first samples from a one-dimensional
Daniel@0 7 % distribution using RANDN, and then plots a normalized histogram
Daniel@0 8 % estimate of the distribution using HISTP together with the true
Daniel@0 9 % density calculated using GAUSS.
Daniel@0 10 %
Daniel@0 11 % DEMGAUSS then demonstrates sampling from a Gaussian distribution in
Daniel@0 12 % two dimensions. It creates a mean vector and a covariance matrix, and
Daniel@0 13 % then plots contours of constant density using the function GAUSS. A
Daniel@0 14 % sample of points drawn from this distribution, obtained using the
Daniel@0 15 % function GSAMP, is then superimposed on the contours.
Daniel@0 16 %
Daniel@0 17 % See also
Daniel@0 18 % GAUSS, GSAMP, HISTP
Daniel@0 19 %
Daniel@0 20
Daniel@0 21 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 22
Daniel@0 23 clc
Daniel@0 24 mean = 2; var = 5; nsamp = 3000;
Daniel@0 25 xmin = -10; xmax = 10; nbins = 30;
Daniel@0 26 disp('Demonstration of sampling from a uni-variate Gaussian with mean')
Daniel@0 27 dstring = [num2str(mean), ' and variance ', num2str(var), '. ', ...
Daniel@0 28 num2str(nsamp), ' samples are taken.'];
Daniel@0 29 disp(dstring);
Daniel@0 30 x = mean + sqrt(var)*randn(nsamp, 1);
Daniel@0 31 fh1 = figure;
Daniel@0 32 histp(x, xmin, xmax, nbins);
Daniel@0 33 hold on;
Daniel@0 34 axis([xmin xmax 0 0.2]);
Daniel@0 35 plotvals = linspace(xmin, xmax, 200)';
Daniel@0 36 probs = gauss(mean, var, plotvals);
Daniel@0 37 plot(plotvals, probs, '-r');
Daniel@0 38 xlabel('X')
Daniel@0 39 ylabel('Density')
Daniel@0 40
Daniel@0 41 disp(' ')
Daniel@0 42 disp('Press any key to continue')
Daniel@0 43 pause;
Daniel@0 44 mu = [3 2];
Daniel@0 45 lam1 = 0.5;
Daniel@0 46 lam2 = 5.0;
Daniel@0 47 Sigma = lam1*[1,1]'*[1,1] + lam2*[1,-1]'*[1,-1];
Daniel@0 48 disp(' ')
Daniel@0 49 disp('Demonstration of sampling from a bi-variate Gaussian. The mean is')
Daniel@0 50 dstring = ['[', num2str(mu(1)), ', ', num2str(mu(2)), ...
Daniel@0 51 '] and the covariance matrix is'];
Daniel@0 52 disp(dstring)
Daniel@0 53 disp(Sigma);
Daniel@0 54 ngrid = 40;
Daniel@0 55 cmin = -5; cmax = 10;
Daniel@0 56 cvals = linspace(cmin, cmax, ngrid);
Daniel@0 57 [X1, X2] = meshgrid(cvals, cvals);
Daniel@0 58 XX = [X1(:), X2(:)];
Daniel@0 59 probs = gauss(mu, Sigma, XX);
Daniel@0 60 probs = reshape(probs, ngrid, ngrid);
Daniel@0 61
Daniel@0 62 fh2 = figure;
Daniel@0 63 contour(X1, X2, probs, 'b');
Daniel@0 64 hold on
Daniel@0 65
Daniel@0 66 nsamp = 300;
Daniel@0 67 dstring = [num2str(nsamp), ' samples are generated.'];
Daniel@0 68 disp('The plot shows the sampled data points with a contour plot of their density.')
Daniel@0 69 samples = gsamp(mu, Sigma, nsamp);
Daniel@0 70 plot(samples(:,1), samples(:,2), 'or');
Daniel@0 71 xlabel('X1')
Daniel@0 72 ylabel('X2')
Daniel@0 73 grid off;
Daniel@0 74
Daniel@0 75 disp(' ')
Daniel@0 76 disp('Press any key to end')
Daniel@0 77 pause;
Daniel@0 78 close(fh1);
Daniel@0 79 close(fh2);
Daniel@0 80 clear all;