wolffd@0
|
1 % PLOTCOV3 - Plots a covariance ellipsoid with axes for a trivariate
|
wolffd@0
|
2 % Gaussian distribution.
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % Usage:
|
wolffd@0
|
5 % [h, s] = plotcov3(mu, Sigma[, OPTIONS]);
|
wolffd@0
|
6 %
|
wolffd@0
|
7 % Inputs:
|
wolffd@0
|
8 % mu - a 3 x 1 vector giving the mean of the distribution.
|
wolffd@0
|
9 % Sigma - a 3 x 3 symmetric positive semi-definite matrix giving
|
wolffd@0
|
10 % the covariance of the distribution (or the zero matrix).
|
wolffd@0
|
11 %
|
wolffd@0
|
12 % Options:
|
wolffd@0
|
13 % 'conf' - a scalar between 0 and 1 giving the confidence
|
wolffd@0
|
14 % interval (i.e., the fraction of probability mass to
|
wolffd@0
|
15 % be enclosed by the ellipse); default is 0.9.
|
wolffd@0
|
16 % 'num-pts' - if the value supplied is n, then (n + 1)^2 points
|
wolffd@0
|
17 % to be used to plot the ellipse; default is 20.
|
wolffd@0
|
18 % 'plot-opts' - a cell vector of arguments to be handed to PLOT3
|
wolffd@0
|
19 % to contol the appearance of the axes, e.g.,
|
wolffd@0
|
20 % {'Color', 'g', 'LineWidth', 1}; the default is {}
|
wolffd@0
|
21 % 'surf-opts' - a cell vector of arguments to be handed to SURF
|
wolffd@0
|
22 % to contol the appearance of the ellipsoid
|
wolffd@0
|
23 % surface; a nice possibility that yields
|
wolffd@0
|
24 % transparency is: {'EdgeAlpha', 0, 'FaceAlpha',
|
wolffd@0
|
25 % 0.1, 'FaceColor', 'g'}; the default is {}
|
wolffd@0
|
26 %
|
wolffd@0
|
27 % Outputs:
|
wolffd@0
|
28 % h - a vector of handles on the axis lines
|
wolffd@0
|
29 % s - a handle on the ellipsoid surface object
|
wolffd@0
|
30 %
|
wolffd@0
|
31 % See also: PLOTCOV2
|
wolffd@0
|
32
|
wolffd@0
|
33 % Copyright (C) 2002 Mark A. Paskin
|
wolffd@0
|
34 %
|
wolffd@0
|
35 % This program is free software; you can redistribute it and/or modify
|
wolffd@0
|
36 % it under the terms of the GNU General Public License as published by
|
wolffd@0
|
37 % the Free Software Foundation; either version 2 of the License, or
|
wolffd@0
|
38 % (at your option) any later version.
|
wolffd@0
|
39 %
|
wolffd@0
|
40 % This program is distributed in the hope that it will be useful, but
|
wolffd@0
|
41 % WITHOUT ANY WARRANTY; without even the implied warranty of
|
wolffd@0
|
42 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
wolffd@0
|
43 % General Public License for more details.
|
wolffd@0
|
44 %
|
wolffd@0
|
45 % You should have received a copy of the GNU General Public License
|
wolffd@0
|
46 % along with this program; if not, write to the Free Software
|
wolffd@0
|
47 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
wolffd@0
|
48 % USA.
|
wolffd@0
|
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
wolffd@0
|
50
|
wolffd@0
|
51 function [h, s] = plotcov3(mu, Sigma, varargin)
|
wolffd@0
|
52
|
wolffd@0
|
53 if size(Sigma) ~= [3 3], error('Sigma must be a 3 by 3 matrix'); end
|
wolffd@0
|
54 if length(mu) ~= 3, error('mu must be a 3 by 1 vector'); end
|
wolffd@0
|
55
|
wolffd@0
|
56 [p, ...
|
wolffd@0
|
57 n, ...
|
wolffd@0
|
58 plot_opts, ...
|
wolffd@0
|
59 surf_opts] = process_options(varargin, 'conf', 0.9, ...
|
wolffd@0
|
60 'num-pts', 20, ...
|
wolffd@0
|
61 'plot-opts', {}, ...
|
wolffd@0
|
62 'surf-opts', {});
|
wolffd@0
|
63 h = [];
|
wolffd@0
|
64 holding = ishold;
|
wolffd@0
|
65 if (Sigma == zeros(3, 3))
|
wolffd@0
|
66 z = mu;
|
wolffd@0
|
67 else
|
wolffd@0
|
68 % Compute the Mahalanobis radius of the ellipsoid that encloses
|
wolffd@0
|
69 % the desired probability mass.
|
wolffd@0
|
70 k = conf2mahal(p, 3);
|
wolffd@0
|
71 % The axes of the covariance ellipse are given by the eigenvectors of
|
wolffd@0
|
72 % the covariance matrix. Their lengths (for the ellipse with unit
|
wolffd@0
|
73 % Mahalanobis radius) are given by the square roots of the
|
wolffd@0
|
74 % corresponding eigenvalues.
|
wolffd@0
|
75 if (issparse(Sigma))
|
wolffd@0
|
76 [V, D] = eigs(Sigma);
|
wolffd@0
|
77 else
|
wolffd@0
|
78 [V, D] = eig(Sigma);
|
wolffd@0
|
79 end
|
wolffd@0
|
80 if (any(diag(D) < 0))
|
wolffd@0
|
81 error('Invalid covariance matrix: not positive semi-definite.');
|
wolffd@0
|
82 end
|
wolffd@0
|
83 % Compute the points on the surface of the ellipsoid.
|
wolffd@0
|
84 t = linspace(0, 2*pi, n);
|
wolffd@0
|
85 [X, Y, Z] = sphere(n);
|
wolffd@0
|
86 u = [X(:)'; Y(:)'; Z(:)'];
|
wolffd@0
|
87 w = (k * V * sqrt(D)) * u;
|
wolffd@0
|
88 z = repmat(mu(:), [1 (n + 1)^2]) + w;
|
wolffd@0
|
89
|
wolffd@0
|
90 % Plot the axes.
|
wolffd@0
|
91 L = k * sqrt(diag(D));
|
wolffd@0
|
92 h = plot3([mu(1); mu(1) + L(1) * V(1, 1)], ...
|
wolffd@0
|
93 [mu(2); mu(2) + L(1) * V(2, 1)], ...
|
wolffd@0
|
94 [mu(3); mu(3) + L(1) * V(3, 1)], plot_opts{:});
|
wolffd@0
|
95 hold on;
|
wolffd@0
|
96 h = [h; plot3([mu(1); mu(1) + L(2) * V(1, 2)], ...
|
wolffd@0
|
97 [mu(2); mu(2) + L(2) * V(2, 2)], ...
|
wolffd@0
|
98 [mu(3); mu(3) + L(2) * V(3, 2)], plot_opts{:})];
|
wolffd@0
|
99 h = [h; plot3([mu(1); mu(1) + L(3) * V(1, 3)], ...
|
wolffd@0
|
100 [mu(2); mu(2) + L(3) * V(2, 3)], ...
|
wolffd@0
|
101 [mu(3); mu(3) + L(3) * V(3, 3)], plot_opts{:})];
|
wolffd@0
|
102 end
|
wolffd@0
|
103
|
wolffd@0
|
104 s = surf(reshape(z(1, :), [(n + 1) (n + 1)]), ...
|
wolffd@0
|
105 reshape(z(2, :), [(n + 1) (n + 1)]), ...
|
wolffd@0
|
106 reshape(z(3, :), [(n + 1) (n + 1)]), ...
|
wolffd@0
|
107 surf_opts{:});
|
wolffd@0
|
108
|
wolffd@0
|
109 if (~holding) hold off; end
|