Daniel@0: function h=plotgauss2d(mu, Sigma) Daniel@0: % PLOTGAUSS2D Plot a 2D Gaussian as an ellipse with optional cross hairs Daniel@0: % h=plotgauss2(mu, Sigma) Daniel@0: % Daniel@0: Daniel@0: h = plotcov2(mu, Sigma); Daniel@0: return; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: % PLOTCOV2 - Plots a covariance ellipse with major and minor axes Daniel@0: % for a bivariate Gaussian distribution. Daniel@0: % Daniel@0: % Usage: Daniel@0: % h = plotcov2(mu, Sigma[, OPTIONS]); Daniel@0: % Daniel@0: % Inputs: Daniel@0: % mu - a 2 x 1 vector giving the mean of the distribution. Daniel@0: % Sigma - a 2 x 2 symmetric positive semi-definite matrix giving Daniel@0: % the covariance of the distribution (or the zero matrix). Daniel@0: % Daniel@0: % Options: Daniel@0: % 'conf' - a scalar between 0 and 1 giving the confidence Daniel@0: % interval (i.e., the fraction of probability mass to Daniel@0: % be enclosed by the ellipse); default is 0.9. Daniel@0: % 'num-pts' - the number of points to be used to plot the Daniel@0: % ellipse; default is 100. Daniel@0: % Daniel@0: % This function also accepts options for PLOT. Daniel@0: % Daniel@0: % Outputs: Daniel@0: % h - a vector of figure handles to the ellipse boundary and Daniel@0: % its major and minor axes Daniel@0: % Daniel@0: % See also: PLOTCOV3 Daniel@0: Daniel@0: % Copyright (C) 2002 Mark A. Paskin Daniel@0: Daniel@0: function h = plotcov2(mu, Sigma, varargin) Daniel@0: Daniel@0: if size(Sigma) ~= [2 2], error('Sigma must be a 2 by 2 matrix'); end Daniel@0: if length(mu) ~= 2, error('mu must be a 2 by 1 vector'); end Daniel@0: Daniel@0: [p, ... Daniel@0: n, ... Daniel@0: plot_opts] = process_options(varargin, 'conf', 0.9, ... Daniel@0: 'num-pts', 100); Daniel@0: h = []; Daniel@0: holding = ishold; Daniel@0: if (Sigma == zeros(2, 2)) Daniel@0: z = mu; Daniel@0: else Daniel@0: % Compute the Mahalanobis radius of the ellipsoid that encloses Daniel@0: % the desired probability mass. Daniel@0: k = conf2mahal(p, 2); Daniel@0: % The major and minor axes of the covariance ellipse are given by Daniel@0: % the eigenvectors of the covariance matrix. Their lengths (for Daniel@0: % the ellipse with unit Mahalanobis radius) are given by the Daniel@0: % square roots of the corresponding eigenvalues. Daniel@0: if (issparse(Sigma)) Daniel@0: [V, D] = eigs(Sigma); Daniel@0: else Daniel@0: [V, D] = eig(Sigma); Daniel@0: end Daniel@0: % Compute the points on the surface of the ellipse. Daniel@0: t = linspace(0, 2*pi, n); Daniel@0: u = [cos(t); sin(t)]; Daniel@0: w = (k * V * sqrt(D)) * u; Daniel@0: z = repmat(mu, [1 n]) + w; Daniel@0: % Plot the major and minor axes. Daniel@0: L = k * sqrt(diag(D)); Daniel@0: h = plot([mu(1); mu(1) + L(1) * V(1, 1)], ... Daniel@0: [mu(2); mu(2) + L(1) * V(2, 1)], plot_opts{:}); Daniel@0: hold on; Daniel@0: h = [h; plot([mu(1); mu(1) + L(2) * V(1, 2)], ... Daniel@0: [mu(2); mu(2) + L(2) * V(2, 2)], plot_opts{:})]; Daniel@0: end Daniel@0: Daniel@0: h = [h; plot(z(1, :), z(2, :), plot_opts{:})]; Daniel@0: if (~holding) hold off; end Daniel@0: Daniel@0: %%%%%%%%%%%% Daniel@0: Daniel@0: % CONF2MAHAL - Translates a confidence interval to a Mahalanobis Daniel@0: % distance. Consider a multivariate Gaussian Daniel@0: % distribution of the form Daniel@0: % Daniel@0: % p(x) = 1/sqrt((2 * pi)^d * det(C)) * exp((-1/2) * MD(x, m, inv(C))) Daniel@0: % Daniel@0: % where MD(x, m, P) is the Mahalanobis distance from x Daniel@0: % to m under P: Daniel@0: % Daniel@0: % MD(x, m, P) = (x - m) * P * (x - m)' Daniel@0: % Daniel@0: % A particular Mahalanobis distance k identifies an Daniel@0: % ellipsoid centered at the mean of the distribution. Daniel@0: % The confidence interval associated with this ellipsoid Daniel@0: % is the probability mass enclosed by it. Similarly, Daniel@0: % a particular confidence interval uniquely determines Daniel@0: % an ellipsoid with a fixed Mahalanobis distance. Daniel@0: % Daniel@0: % If X is an d dimensional Gaussian-distributed vector, Daniel@0: % then the Mahalanobis distance of X is distributed Daniel@0: % according to the Chi-squared distribution with d Daniel@0: % degrees of freedom. Thus, the Mahalanobis distance is Daniel@0: % determined by evaluating the inverse cumulative Daniel@0: % distribution function of the chi squared distribution Daniel@0: % up to the confidence value. Daniel@0: % Daniel@0: % Usage: Daniel@0: % Daniel@0: % m = conf2mahal(c, d); Daniel@0: % Daniel@0: % Inputs: Daniel@0: % Daniel@0: % c - the confidence interval Daniel@0: % d - the number of dimensions of the Gaussian distribution Daniel@0: % Daniel@0: % Outputs: Daniel@0: % Daniel@0: % m - the Mahalanobis radius of the ellipsoid enclosing the Daniel@0: % fraction c of the distribution's probability mass Daniel@0: % Daniel@0: % See also: MAHAL2CONF Daniel@0: Daniel@0: % Copyright (C) 2002 Mark A. Paskin Daniel@0: Daniel@0: function m = conf2mahal(c, d) Daniel@0: Daniel@0: m = chi2inv(c, d); % matlab stats toolbox