comparison toolboxes/FullBNT-1.0.7/KPMtools/plotgauss2d.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 function h=plotgauss2d(mu, Sigma)
2 % PLOTGAUSS2D Plot a 2D Gaussian as an ellipse with optional cross hairs
3 % h=plotgauss2(mu, Sigma)
4 %
5
6 h = plotcov2(mu, Sigma);
7 return;
8
9 %%%%%%%%%%%%%%%%%%%%%%%%
10
11 % PLOTCOV2 - Plots a covariance ellipse with major and minor axes
12 % for a bivariate Gaussian distribution.
13 %
14 % Usage:
15 % h = plotcov2(mu, Sigma[, OPTIONS]);
16 %
17 % Inputs:
18 % mu - a 2 x 1 vector giving the mean of the distribution.
19 % Sigma - a 2 x 2 symmetric positive semi-definite matrix giving
20 % the covariance of the distribution (or the zero matrix).
21 %
22 % Options:
23 % 'conf' - a scalar between 0 and 1 giving the confidence
24 % interval (i.e., the fraction of probability mass to
25 % be enclosed by the ellipse); default is 0.9.
26 % 'num-pts' - the number of points to be used to plot the
27 % ellipse; default is 100.
28 %
29 % This function also accepts options for PLOT.
30 %
31 % Outputs:
32 % h - a vector of figure handles to the ellipse boundary and
33 % its major and minor axes
34 %
35 % See also: PLOTCOV3
36
37 % Copyright (C) 2002 Mark A. Paskin
38
39 function h = plotcov2(mu, Sigma, varargin)
40
41 if size(Sigma) ~= [2 2], error('Sigma must be a 2 by 2 matrix'); end
42 if length(mu) ~= 2, error('mu must be a 2 by 1 vector'); end
43
44 [p, ...
45 n, ...
46 plot_opts] = process_options(varargin, 'conf', 0.9, ...
47 'num-pts', 100);
48 h = [];
49 holding = ishold;
50 if (Sigma == zeros(2, 2))
51 z = mu;
52 else
53 % Compute the Mahalanobis radius of the ellipsoid that encloses
54 % the desired probability mass.
55 k = conf2mahal(p, 2);
56 % The major and minor axes of the covariance ellipse are given by
57 % the eigenvectors of the covariance matrix. Their lengths (for
58 % the ellipse with unit Mahalanobis radius) are given by the
59 % square roots of the corresponding eigenvalues.
60 if (issparse(Sigma))
61 [V, D] = eigs(Sigma);
62 else
63 [V, D] = eig(Sigma);
64 end
65 % Compute the points on the surface of the ellipse.
66 t = linspace(0, 2*pi, n);
67 u = [cos(t); sin(t)];
68 w = (k * V * sqrt(D)) * u;
69 z = repmat(mu, [1 n]) + w;
70 % Plot the major and minor axes.
71 L = k * sqrt(diag(D));
72 h = plot([mu(1); mu(1) + L(1) * V(1, 1)], ...
73 [mu(2); mu(2) + L(1) * V(2, 1)], plot_opts{:});
74 hold on;
75 h = [h; plot([mu(1); mu(1) + L(2) * V(1, 2)], ...
76 [mu(2); mu(2) + L(2) * V(2, 2)], plot_opts{:})];
77 end
78
79 h = [h; plot(z(1, :), z(2, :), plot_opts{:})];
80 if (~holding) hold off; end
81
82 %%%%%%%%%%%%
83
84 % CONF2MAHAL - Translates a confidence interval to a Mahalanobis
85 % distance. Consider a multivariate Gaussian
86 % distribution of the form
87 %
88 % p(x) = 1/sqrt((2 * pi)^d * det(C)) * exp((-1/2) * MD(x, m, inv(C)))
89 %
90 % where MD(x, m, P) is the Mahalanobis distance from x
91 % to m under P:
92 %
93 % MD(x, m, P) = (x - m) * P * (x - m)'
94 %
95 % A particular Mahalanobis distance k identifies an
96 % ellipsoid centered at the mean of the distribution.
97 % The confidence interval associated with this ellipsoid
98 % is the probability mass enclosed by it. Similarly,
99 % a particular confidence interval uniquely determines
100 % an ellipsoid with a fixed Mahalanobis distance.
101 %
102 % If X is an d dimensional Gaussian-distributed vector,
103 % then the Mahalanobis distance of X is distributed
104 % according to the Chi-squared distribution with d
105 % degrees of freedom. Thus, the Mahalanobis distance is
106 % determined by evaluating the inverse cumulative
107 % distribution function of the chi squared distribution
108 % up to the confidence value.
109 %
110 % Usage:
111 %
112 % m = conf2mahal(c, d);
113 %
114 % Inputs:
115 %
116 % c - the confidence interval
117 % d - the number of dimensions of the Gaussian distribution
118 %
119 % Outputs:
120 %
121 % m - the Mahalanobis radius of the ellipsoid enclosing the
122 % fraction c of the distribution's probability mass
123 %
124 % See also: MAHAL2CONF
125
126 % Copyright (C) 2002 Mark A. Paskin
127
128 function m = conf2mahal(c, d)
129
130 m = chi2inv(c, d); % matlab stats toolbox