diff --git a/linalg/unittest_subspace_angles.m b/linalg/unittest_subspace_angles.m new file mode 100644 index 00000000..ea15ea8b --- /dev/null +++ b/linalg/unittest_subspace_angles.m @@ -0,0 +1,46 @@ +function unittest_subspace_angles +% UNITTEST_SUBSPACE_ANGLES Test the SUBSPACE_ANGLES function. +% +% Example (run) +% 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 . + +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)) ); diff --git a/linalg/unittest_subspace_distance.m b/linalg/unittest_subspace_distance.m new file mode 100644 index 00000000..c01b2d74 --- /dev/null +++ b/linalg/unittest_subspace_distance.m @@ -0,0 +1,49 @@ +function unittest_subspace_distance +% UNITTEST_SUBSPACE_DISTANCE Test the SUBSPACE_DISTANCE function. +% +% Example (run) +% 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 . + +munit_set_function( 'subspace_distance' ); + + +rand('seed', 74765); %#ok + +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'); + + + diff --git a/gendist_fix_moments.m b/statistics/gendist_fix_moments.m similarity index 56% rename from gendist_fix_moments.m rename to statistics/gendist_fix_moments.m index 99b83772..effa44e6 100644 --- a/gendist_fix_moments.m +++ b/statistics/gendist_fix_moments.m @@ -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 (run) +% 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 diff --git a/statistics/private/scaled_distance.m b/statistics/private/scaled_distance.m index f096c117..37b44d10 100644 --- a/statistics/private/scaled_distance.m +++ b/statistics/private/scaled_distance.m @@ -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 (run) +% 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 (run) +% 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 @@ -18,13 +43,13 @@ % received a copy of the GNU General Public License along with this % program. If not, see . -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 diff --git a/unittest_gendist_fix_moments.m b/statistics/unittest_gendist_fix_moments.m similarity index 75% rename from unittest_gendist_fix_moments.m rename to statistics/unittest_gendist_fix_moments.m index a25807b7..0ac2e768 100644 --- a/unittest_gendist_fix_moments.m +++ b/statistics/unittest_gendist_fix_moments.m @@ -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' ); - diff --git a/compute_kl_pce_operator_smolyak.m b/testing/wanted/compute_kl_pce_operator_smolyak.m similarity index 100% rename from compute_kl_pce_operator_smolyak.m rename to testing/wanted/compute_kl_pce_operator_smolyak.m