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