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