comparison toolboxes/FullBNT-1.0.7/KPMtools/plotcov2.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 % PLOTCOV2 - Plots a covariance ellipse with major and minor axes
2 % for a bivariate Gaussian distribution.
3 %
4 % Usage:
5 % h = plotcov2(mu, Sigma[, OPTIONS]);
6 %
7 % Inputs:
8 % mu - a 2 x 1 vector giving the mean of the distribution.
9 % Sigma - a 2 x 2 symmetric positive semi-definite matrix giving
10 % the covariance of the distribution (or the zero matrix).
11 %
12 % Options:
13 % 'conf' - a scalar between 0 and 1 giving the confidence
14 % interval (i.e., the fraction of probability mass to
15 % be enclosed by the ellipse); default is 0.9.
16 % 'num-pts' - the number of points to be used to plot the
17 % ellipse; default is 100.
18 %
19 % This function also accepts options for PLOT.
20 %
21 % Outputs:
22 % h - a vector of figure handles to the ellipse boundary and
23 % its major and minor axes
24 %
25 % See also: PLOTCOV3
26
27 % Copyright (C) 2002 Mark A. Paskin
28 %
29 % This program is free software; you can redistribute it and/or modify
30 % it under the terms of the GNU General Public License as published by
31 % the Free Software Foundation; either version 2 of the License, or
32 % (at your option) any later version.
33 %
34 % This program is distributed in the hope that it will be useful, but
35 % WITHOUT ANY WARRANTY; without even the implied warranty of
36 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 % General Public License for more details.
38 %
39 % You should have received a copy of the GNU General Public License
40 % along with this program; if not, write to the Free Software
41 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
42 % USA.
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44
45 function h = plotcov2(mu, Sigma, varargin)
46
47 if size(Sigma) ~= [2 2], error('Sigma must be a 2 by 2 matrix'); end
48 if length(mu) ~= 2, error('mu must be a 2 by 1 vector'); end
49
50 [p, ...
51 n, ...
52 plot_opts] = process_options(varargin, 'conf', 0.9, ...
53 'num-pts', 100);
54 h = [];
55 holding = ishold;
56 if (Sigma == zeros(2, 2))
57 z = mu;
58 else
59 % Compute the Mahalanobis radius of the ellipsoid that encloses
60 % the desired probability mass.
61 k = conf2mahal(p, 2);
62 % The major and minor axes of the covariance ellipse are given by
63 % the eigenvectors of the covariance matrix. Their lengths (for
64 % the ellipse with unit Mahalanobis radius) are given by the
65 % square roots of the corresponding eigenvalues.
66 if (issparse(Sigma))
67 [V, D] = eigs(Sigma);
68 else
69 [V, D] = eig(Sigma);
70 end
71 % Compute the points on the surface of the ellipse.
72 t = linspace(0, 2*pi, n);
73 u = [cos(t); sin(t)];
74 w = (k * V * sqrt(D)) * u;
75 z = repmat(mu, [1 n]) + w;
76 % Plot the major and minor axes.
77 L = k * sqrt(diag(D));
78 h = plot([mu(1); mu(1) + L(1) * V(1, 1)], ...
79 [mu(2); mu(2) + L(1) * V(2, 1)], plot_opts{:});
80 hold on;
81 h = [h; plot([mu(1); mu(1) + L(2) * V(1, 2)], ...
82 [mu(2); mu(2) + L(2) * V(2, 2)], plot_opts{:})];
83 end
84
85 h = [h; plot(z(1, :), z(2, :), plot_opts{:})];
86 if (~holding) hold off; end