X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Foptim-1.2.0%2Ftest_d2_min_2.m;fp=octave_packages%2Foptim-1.2.0%2Ftest_d2_min_2.m;h=50890f922096e90dc34132f54ed80a2df268ba5f;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/optim-1.2.0/test_d2_min_2.m b/octave_packages/optim-1.2.0/test_d2_min_2.m new file mode 100644 index 0000000..50890f9 --- /dev/null +++ b/octave_packages/optim-1.2.0/test_d2_min_2.m @@ -0,0 +1,115 @@ +## Copyright (C) 2002 Etienne Grossmann +## +## 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. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. 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 . + +## Test whether d2_min() functions correctly, with two args +## +## Gives a simple quadratic programming problem (function ff below). +## +## Sets a ok variable to 1 in case of success, 0 in case of failure +## +## If a variables "verbose" is set, then some comments are output. + +1 ; + +ok = 0; + +if ! exist ("verbose"), verbose = 0; end + +if verbose + printf ("\n Testing d2_min () on a quadratic programming problem\n\n"); +end + +P = 10+floor(30*rand(1)) ; # Nparams +R = P+floor(30*rand(1)) ; # Nobses +noise = 0 ; +obsmat = randn(R,P) ; +truep = randn(P,1) ; +xinit = randn(P,1) ; + +obses = obsmat*truep ; +if noise, obses = adnois(obses,noise); end + +y.obses = obses; +y.obsmat = obsmat; + +function v = ff (x, y) + v = msq( y.obses - y.obsmat*x ) ; +endfunction + + +function [v,dv,d2v] = d2ff (x, y) + er = -y.obses + y.obsmat*x ; + dv = er'*y.obsmat ; + v = msq( er ) ; + d2v = pinv( y.obsmat'*y.obsmat ) ; +endfunction + +## dt = mytic() +## +## Returns the cputime since last call to 'mytic'. + +function dt = mytic() + persistent last_mytic = 0 ; + [t,u,s] = cputime() ; + dt = t - last_mytic ; + last_mytic = t ; +endfunction + +## s = msq(x) - Mean squared value, ignoring nans +## +## s == mean(x(:).^2) , but ignores NaN's + + +function s = msq(x) +try + s = mean(x(find(!isnan(x))).^2); +catch + s = nan; +end +endfunction + + +ctl = nan*zeros(1,5); ctl(5) = 1; + +if verbose, printf ( "Going to call d2_min()\n"); end +mytic() ; +[xlev,vlev,nev] = d2_min ("ff", "d2ff", {xinit,y}, ctl) ; +tlev = mytic (); + +if verbose, + printf("d2_min should find in one iteration + one more to check\n"); + printf(["d2_min : niter=%-4d nev=%-4d nobs=%-4d nparams=%-4d\n",\ + " time=%-8.3g errx=%-8.3g minv=%-8.3g\n"],... + nev([2,1]), R, P, tlev, max (abs (xlev-truep)), vlev); +end + +ok = 1; +if nev(2) != 2, + if verbose + printf ( "Too many iterations for this function\n"); + end + ok = 0; +end + +if max (abs(xlev-truep )) > sqrt (eps), + if verbose + printf ( "Error is too big : %-8.3g\n", max (abs (xlev-truep))); + end + ok = 0; +end + +if verbose && ok + printf ( "All tests ok\n"); +end