Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Confidence ellipse in bivariate scatterplots

378 views
Skip to first unread message

Urs Utzinger

unread,
Jun 11, 1998, 3:00:00 AM6/11/98
to

It is a quite common tool in scatter plots to draw confidence ellipses.
They represent a normal distribution fit on bivariate data.

Has anyone ever programmed this as a function for Matlab?

Thanks for tips.

Urs Utzinger, utzi...@mail.utexas.edu

Paige Miller

unread,
Jun 12, 1998, 3:00:00 AM6/12/98
to Urs Utzinger

Urs Utzinger wrote:
>
> It is a quite common tool in scatter plots to draw confidence
> ellipses.
> They represent a normal distribution fit on bivariate data.
>
> Has anyone ever programmed this as a function for Matlab?
>
> Thanks for tips.

I haven't done exactly this in MATLAB, but the calculations are
relatively trivial, see for example, section 15.1 of Jackson, J. E.
(1991), "A User's Guide to Principal Components", John Wiley and Sons,
New York.

--
+---------------------------------+------------------------------------+
| Paige Miller | "It's nothing until I call it" |
| Eastman Kodak Company | Bill Klem |
| Pai...@kodak.com | NL Umpire |
+---------------------------------+------------------------------------+
| The opinions expressed herein do not necessarily reflect the |
| views of the author nor the views of the Eastman Kodak Company. |
+----------------------------------------------------------------------+

Doug Schwarz

unread,
Jun 12, 1998, 3:00:00 AM6/12/98
to

In article <35813BC9...@kodak.com>, Paige Miller <pai...@kodak.com> wrote:

>Urs Utzinger wrote:
>>
>> It is a quite common tool in scatter plots to draw confidence
>> ellipses.
>> They represent a normal distribution fit on bivariate data.
>>
>> Has anyone ever programmed this as a function for Matlab?
>>
>> Thanks for tips.
>
>I haven't done exactly this in MATLAB, but the calculations are
>relatively trivial, see for example, section 15.1 of Jackson, J. E.
>(1991), "A User's Guide to Principal Components", John Wiley and Sons,
>New York.


Gee, thanks a lot Paige! I thought I was pretty clever when I figured it
out. :-)

Anyway, here's one that uses the Statistics Toolbox. If you don't have
that then you can just as easily use functions from Peter Shaw's ftest
toolbox available from

<ftp://ftp.mathworks.com/pub/contrib/v4/stats/ftest>

The changes are explained in the comments.

------------------------- convellipse2.m ----------------------------
function hh = confellipse2(xy,conf)
%CONFELLIPSE2 Draws a confidence ellipse.
% CONFELLIPSE2(XY,CONF) draws a confidence ellipse on the current axes
% which is calculated from the n-by-2 matrix XY and encloses the
% fraction CONF (e.g., 0.95 for a 95% confidence ellipse).
% H = CONFELLIPSE2(...) returns a handle to the line.

% written by Douglas M. Schwarz
% sch...@kodak.com
% last modified: 12 June 1998

n = size(xy,1);
mxy = mean(xy);

numPts = 181; % The number of points in the ellipse.
th = linspace(0,2*pi,numPts)';


p = 2; % Dimensionality of the data, 2-D in this case.

k = finv(conf,p,n-p)*p*(n-1)/(n-p);
% Comment out line above and uncomment line below to use ftest toolbox.
% k = fdistinv(p,n-p,1-conf)*p*(n-1)/(n-p);

[pc,score,lat] = princomp(xy);
% Comment out line above and uncomment 3 lines below to use ftest toolbox.
% xyp = (xy - repmat(mxy,n,1))/sqrt(n - 1);
% [u,lat,pc] = svd(xyp,0);
% lat = diag(lat).^2;

ab = diag(sqrt(k*lat));
exy = [cos(th),sin(th)]*ab*pc' + repmat(mxy,numPts,1);

% Add ellipse to current plot
h = line(exy(:,1),exy(:,2),'Clipping','off');
if nargout > 0
hh = h;
end
-----------------------------------------------------------------------

--
Doug Schwarz
Eastman Kodak Company
sch...@kodak.com

Nathan Orloff

unread,
May 30, 2013, 3:52:09 PM5/30/13
to
Its not pretty. But here is my solution.

function hh = errorellipse(ex,ey,xx,yy,varargin)
%errorellipse(ex,ey,xx,yy,varargin) creates a colored ellipses
% This creates tilted ellipses at each centroid position and then shades
% them based on some weight.
%% condition the input
hh = zeros(size(xx));
th = zeros(size(xx));
cmaps = ones(size(xx))*[.5,.5,.5];
if nargin == 5
th = varargin{1};
elseif nargin == 6
th = varargin{1};
cmaps = varargin{2};
elseif nargin == 7
th = varargin{1};
wts = varargin{2};
mapstr = varargin{3};
cm = flipud(colormap(mapstr));
cm_o = linspace(min(wts),max(wts),size(cm,1))';
cmaps(:,1) = interp1(cm_o,cm(:,1),wts,'linear');
cmaps(:,2) = interp1(cm_o,cm(:,2),wts,'linear');
cmaps(:,3) = interp1(cm_o,cm(:,3),wts,'linear');
end
%% add them to an open plot
hold on
t = linspace(0,2*pi);
for ii = 1:numel(xx)
x = xx(ii) + ex(ii).*cos(t).*cosd(th(ii)) - ey(ii).*sin(t).*sind(th(ii));
y = yy(ii) + ex(ii).*cos(t).*sind(th(ii)) + ey(ii).*sin(t).*cosd(th(ii));
hh(ii,1) = patch(x,y,'k','line','none');
set(hh(ii,1),'facecolor',cmaps(ii,:))
clear x y
end
end
0 new messages