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