comparison toolboxes/FullBNT-1.0.7/KPMtools/plotcov2New.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 ellipsoid with axes for a bivariate
2 % Gaussian distribution.
3 %
4 % Usage:
5 % [h, s] = 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' - if the value supplied is n, then (n + 1)^2 points
17 % to be used to plot the ellipse; default is 20.
18 % 'label' - if non-empty, a string that will label the
19 % ellipsoid (default: [])
20 % 'plot-axes' - a 0/1 flag indicating if the ellipsoid's axes
21 % should be plotted (default: 1)
22 % 'plot-opts' - a cell vector of arguments to be handed to PLOT3
23 % to contol the appearance of the axes, e.g.,
24 % {'Color', 'g', 'LineWidth', 1}; the default is {}
25 % 'fill-color' - a color specifier; is this is not [], the
26 % covariance ellipse is filled with this color
27 % (default: [])
28 %
29 % Outputs:
30 % h - a vector of handles on the axis lines
31 %
32 % See also: PLOTCOV3
33
34 % Copyright (C) 2002 Mark A. Paskin
35 %
36 % This program is free software; you can redistribute it and/or modify
37 % it under the terms of the GNU General Public License as published by
38 % the Free Software Foundation; either version 2 of the License, or
39 % (at your option) any later version.
40 %
41 % This program is distributed in the hope that it will be useful, but
42 % WITHOUT ANY WARRANTY; without even the implied warranty of
43 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44 % General Public License for more details.
45 %
46 % You should have received a copy of the GNU General Public License
47 % along with this program; if not, write to the Free Software
48 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
49 % USA.
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51
52 function [h, s] = plotcov2New(mu, Sigma, varargin)
53
54 h = [];
55 s = [];
56
57 if size(Sigma) ~= [2 2], error('Sigma must be a 2 by 2 matrix'); end
58 if length(mu) ~= 2, error('mu must be a 2 by 1 vector'); end
59
60 Sigma = checkpsd(Sigma);
61
62 [p, ...
63 n, ...
64 label, ...
65 plot_axes, ...
66 plot_opts, ...
67 fill_color] = process_options(varargin, 'conf', 0.9, ...
68 'num-pts', 20, ...
69 'label', [], ...
70 'plot-axes', 1, ...
71 'plot-opts', {}, ...
72 'fill-color', []);
73 holding = ishold;
74 % Compute the Mahalanobis radius of the ellipsoid that encloses
75 % the desired probability mass.
76 k = conf2mahal(p, 2);
77 % Scale the covariance matrix so the confidence region has unit
78 % Mahalanobis distance.
79 Sigma = Sigma * k;
80 % The axes of the covariance ellipse are given by the eigenvectors of
81 % the covariance matrix. Their lengths (for the ellipse with unit
82 % Mahalanobis radius) are given by the square roots of the
83 % corresponding eigenvalues.
84 [V, D] = eig(full(Sigma));
85 V = real(V);
86 D = real(D);
87 D = abs(D);
88
89 % Compute the points on the boundary of the ellipsoid.
90 t = linspace(0, 2*pi, n);
91 u = [cos(t(:))'; sin(t(:))'];
92 w = (V * sqrt(D)) * u;
93 z = repmat(mu(:), [1 n]) + w;
94 h = [h; plot(z(1, :), z(2, :), plot_opts{:})];
95 if (~isempty(fill_color))
96 s = patch(z(1, :), z(2, :), fill_color);
97 end
98
99 % Plot the axes.
100 if (plot_axes)
101 hold on;
102 L = sqrt(diag(D));
103 h = plot([mu(1); mu(1) + L(1) * V(1, 1)], ...
104 [mu(2); mu(2) + L(1) * V(2, 1)], plot_opts{:});
105 h = [h; plot([mu(1); mu(1) + L(2) * V(1, 2)], ...
106 [mu(2); mu(2) + L(2) * V(2, 2)], plot_opts{:})];
107 end
108
109
110 if (~isempty(label))
111 th = text(mu(1), mu(2), label);
112 set(th, 'FontSize', 18);
113 set(th, 'FontName', 'Times');
114 set(th, 'FontWeight', 'bold');
115 set(th, 'FontAngle', 'italic');
116 set(th, 'HorizontalAlignment', 'center');
117 end
118
119 if (~holding & plot_axes) hold off; end