en fr

nyctergatis.com

Sysquake Remote Live

Covariance

A set of points can be characterized by its first- and second-order moments. First-order moments are the average values along each dimension; second-order moments are (co)variance. They can be represented graphically as an ellipse.

In the graphic below, you can add points and observe their influence on the covariance ellipse.

Source code

Here is the code inserted in the file stored on the server. If you look at the source of this page in your browser, you will see only the HTML code produced by Sysquake Remote.

<?sqr
% get points from data submitted to this page as form variable "x"
try
  x = str2obj(httpvars.x);
  if size(x, 1) < 3 || size(x, 2) ~= 2
    % detect errors (which wouldn't compromise security anyway)
    error invalid
  end
catch
  x = -5 + 10 * rand(3, 2);
end

% get click
% (managed automatically in figures 'interactive' or 'forminput')
click = getclick;
if isfield(click, 'x')
  x(end+1, :) = [click.x, click.y];
end

% reduce precision to shorten url
x = 1e-3 * round(1e3 * x);

% computation
M1 = mean(x, 1).';
M2 = cov(x);
phi = 2 * pi * (0:0.01:1);
crcle = [cos(phi); sin(phi)];
ell = sqrtm(M2) * crcle + repmat(M1, 1, size(crcle, 2));
fprintf('<form method="get">');
fprintf('<input type="hidden" name="x" value="%s">', ...
        htmlspecialchars(dumpvar(x)));
beginfigure('kind', 'forminput');
scale('equal', [-10, 10, -10, 10]);
plot(ell(1,:), ell(2,:), 'b');
plot(x(:,1), x(:,2), 'xK');
endfigure;
fprintf('</form>');
?>