Skip to content

Commit

Permalink
Added some unittests and cleanup (in statistics and linalg)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezander committed Jun 7, 2013
1 parent 6839aec commit 7b783e7
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 10 deletions.
46 changes: 46 additions & 0 deletions linalg/unittest_subspace_angles.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function unittest_subspace_angles
% UNITTEST_SUBSPACE_ANGLES Test the SUBSPACE_ANGLES function.
%
% Example (<a href="matlab:run_example unittest_subspace_angles">run</a>)
% unittest_subspace_angle
%
% See also SUBSPACE_ANGLES, TESTSUITE

% Elmar Zander
% Copyright 2010, Inst. of Scientific Computing, TU Braunschweig
% $Id$
%
% This program is free software: you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation, either version 3 of the License, or (at your
% option) any later version.
% See the GNU General Public License for more details. You should have
% received a copy of the GNU General Public License along with this
% program. If not, see <http://www.gnu.org/licenses/>.

munit_set_function( 'subspace_angles' );

e1=unitvector(1,4);
e2=unitvector(2,4);
e3=unitvector(3,4);
e4=unitvector(4,4);

test([e1, e2], [e1, e2]);
test([e1, e2], [e1, e3]);
test([e1, e2], [e3, e4]);
test([e1, e2], [e1, -e2+e4]);

A=rand(7,5); B=rand(7,5);
assert_equals( asin(subspace_distance(A,B)), subspace(A,B) )
A=[A, A]; B=[B, B];
assert_equals( asin(subspace_distance(A,B)), subspace(A,B) )
A=rand(7,5); B=rand(7,3);
assert_equals( asin(subspace_distance(A,B)), subspace(A,B) )
A=rand(7,3); B=rand(7,5);
assert_equals( asin(subspace_distance(A,B)), subspace(A,B) )


function test(A1,A2)
theta=subspace_angles(A1,A2);
dist=subspace_distance(A1,A2);
assert_equals( dist, sin(theta(2)) );
49 changes: 49 additions & 0 deletions linalg/unittest_subspace_distance.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function unittest_subspace_distance
% UNITTEST_SUBSPACE_DISTANCE Test the SUBSPACE_DISTANCE function.
%
% Example (<a href="matlab:run_example unittest_subspace_distance">run</a>)
% unittest_subspace_distance
%
% See also SUBSPACE_DISTANCE, TESTSUITE

% Elmar Zander
% Copyright 2013, Inst. of Scientific Computing, TU Braunschweig
%
% This program is free software: you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation, either version 3 of the License, or (at your
% option) any later version.
% See the GNU General Public License for more details. You should have
% received a copy of the GNU General Public License along with this
% program. If not, see <http://www.gnu.org/licenses/>.

munit_set_function( 'subspace_distance' );


rand('seed', 74765); %#ok<RAND>

N=10;
A=rand(N,3);
B=rand(N,5);

assert_equals(subspace_distance(A,B), subspace_distance(B,A), 'symm');
assert_equals(subspace_distance(A,B), sin(subspace(A,B)), 'mlab');
assert_equals(subspace_distance(A,[B, A]), 0, 'zero');


opts.type = 'gv';
A=rand(N,3);
B=rand(N,3);
assert_equals(subspace_distance(A,B,opts), subspace_distance(B,A,opts), 'symm_gv');
assert_equals(subspace_distance(A,B,opts), subspace_distance(A,B), 'same_as_mlab_if_same_size');
assert_equals(subspace_distance(A,[B, A],opts), 1, 'one_gv');


opts.type = 'wwf';
A=rand(N,3);
B=rand(N,3);
assert_equals(subspace_distance(A,B,opts), subspace_distance(B,A,opts), 'symm_wwf');
assert_equals(subspace_distance(A, A, opts), 0, 'zero_wwf');



16 changes: 13 additions & 3 deletions gendist_fix_moments.m → statistics/gendist_fix_moments.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
function [shift,scale]=gendist_fix_moments(dist, params, mean, var)
% GENDIST_FIX_MOMENTS Short description of gendist_fix_moments.
% GENDIST_FIX_MOMENTS Long description of gendist_fix_moments.
% GENDIST_FIX_MOMENTS Computes scale and shift to get specific moments.
% [SHIFT,SCALE]=GENDIST_FIX_MOMENTS(DIST, PARAMS, MEAN, VAR) computes
% shift and scale parameters such that the distribution specified by
% DIST, PARAMS, SHIFT and SCALE has mean MEAN and variance VAR.
%
% Example (<a href="matlab:run_example gendist_fix_moments">run</a>)
% dist = 'beta';
% params = {2, 4};
% [m, v] = gendist_moments(dist, params);
% fprintf( 'before: mu=%g, var=%g\n', m, v);
%
% [shift,scale]=gendist_fix_moments(dist, params, pi, exp(1));
% [m, v] = gendist_moments(dist, params, shift, scale);
% fprintf( 'after: mu=%g, var=%g\n', m, v);
%
% See also
% See also GENDIST_MOMENTS

% Elmar Zander
% Copyright 2010, Inst. of Scientific Computing, TU Braunschweig
Expand Down
37 changes: 31 additions & 6 deletions statistics/private/scaled_distance.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
function dist=scaled_distance(x1, x2, l, smooth, sqr)
% SCALED_DISTANCE Short description of scaled_distance.
% SCALED_DISTANCE Long description of scaled_distance.
% SCALED_DISTANCE Computes the scaled distance between points.
% DIST=SCALED_DISTANCE(X1, X2) computes the Euclidean distance between
% points given in X1 and X2.
%
% Example (<a href="matlab:run_example scaled_distance">run</a>)
% DIST=SCALED_DISTANCE(X1, X2, L) computes the Euclidean distance between
% X1 and X2 scaled by L where L can be scalar or a vector of the same
% dimension as the points in X1 and X2.
%
% DIST=SCALED_DISTANCE(X1, X2, L, SMOOTH) computes the smoothed
% Euclidean distance where the smoothing function is given by
% D_SMOOTH=SQRT(D+SMOOTH^2)-SMOOTH.
%
% DIST=SCALED_DISTANCE(X1, X2, L, SMOOTH, SQR) returns the square of the
% Eudclidean smoothed, scaled distance. This saves computing the square
% root if only the square is needed by the calling code anyway.
%
% Note: if an empty array is specified for a parameter the default value
% is used. DIST=SCALED_DISTANCE(X1, X2, [], [], TRUE) computes the square
% of the distance with parameters L and SMOOTH set to their defaults 1
% and 0.
%
% Example (<a href="matlab:run_example scaled_distance">run</a>)
% d=linspace(-1,1);
% plot(d, scaled_distance(d, [], [], 0)); hold all;
% plot(d, scaled_distance(d, [], [], 1));
% plot(d, scaled_distance(d, [], [], 2));
% plot(d, scaled_distance(d, [], 0.3, 0));
% plot(d, scaled_distance(d, [], 0.3, 1));
% plot(d, scaled_distance(d, [], 0.3, 2)); hold off;
% axis equal;
% See also

% Elmar Zander
Expand All @@ -18,13 +43,13 @@
% received a copy of the GNU General Public License along with this
% program. If not, see <http://www.gnu.org/licenses/>.

if nargin<5
if nargin<5 || isempty(sqr)
sqr=false;
end
if nargin<4
if nargin<4 || isempty(smooth)
smooth=0;
end
if nargin<3
if nargin<3 || isempty(l)
l=1;
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@

munit_set_function( 'gendist_fix_moments' );

% can test directly for the normal and uniform distributions
[shift,scale]=gendist_fix_moments( 'normal', {2, 5}, 7, 13 );
assert_equals([shift,scale], [5, sqrt(13/25)], 'normal');

[shift,scale]=gendist_fix_moments( 'uniform', {22, 88}, 50, 3 );
assert_equals([shift,scale], [-5, 1/11], 'uniform');

% can test via the moments for the lognormal distribution
dist='lognormal';
params={0,1};
[shift,scale]=gendist_fix_moments( dist, params, 3.1, 2.4 );
[mean,var]=gendist_moments( dist, params, shift, scale );
assert_equals( mean, 3.1, 'mean' );
assert_equals( var, 2.4, 'var' );

File renamed without changes.

0 comments on commit 7b783e7

Please sign in to comment.