validate

PURPOSE ^

VALIDATE Cross validation of the methods in the toolbox

SYNOPSIS ^

function [validationData, stats] = validate(x, y, z, k, options, methods, verbose)

DESCRIPTION ^

VALIDATE Cross validation of the methods in the toolbox
 
 Input:
   - x, y, z: the known function values of the bivariate function
       f(x,y)=z.
   - k: k-fold cross validation.
   - options: options structure containing the parameters for each method,
       as returned by the hmitDefaultOptions function.
   - methods: cell array of char vectors listing the methods to test. If
       empty or not specified, all the methods in the toolbox will be
       used.
   - verbose: show the progress on screen.
 
 Output:
   - validationData: structure containing the different tests and results

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [validationData, stats] = validate(x, y, z, k, options, methods, verbose)
0002 %VALIDATE Cross validation of the methods in the toolbox
0003 %
0004 % Input:
0005 %   - x, y, z: the known function values of the bivariate function
0006 %       f(x,y)=z.
0007 %   - k: k-fold cross validation.
0008 %   - options: options structure containing the parameters for each method,
0009 %       as returned by the hmitDefaultOptions function.
0010 %   - methods: cell array of char vectors listing the methods to test. If
0011 %       empty or not specified, all the methods in the toolbox will be
0012 %       used.
0013 %   - verbose: show the progress on screen.
0014 %
0015 % Output:
0016 %   - validationData: structure containing the different tests and results
0017 %
0018 
0019 % Parameters' check
0020 if nargin < 6
0021     methods = {'Nearest', 'Delaunay', 'Natural', 'IDW', 'Kriging', 'MLS', ...
0022                'RBF.linear', 'RBF.cubic', 'RBF.quintic', 'RBF.multiquadric', 'RBF.thinplate', 'RBF.green', 'RBF.tensionspline', 'RBF.regularizedspline', 'RBF.gaussian', 'RBF.wendland', ...
0023                'QTPURBF.linear', 'QTPURBF.cubic', 'QTPURBF.quintic', 'QTPURBF.multiquadric', 'QTPURBF.thinplate', 'QTPURBF.green', 'QTPURBF.tensionspline', 'QTPURBF.regularizedspline', 'QTPURBF.gaussian', 'QTPURBF.wendland'};
0024 end
0025 if nargin < 7
0026     verbose = true;
0027 end
0028 
0029 numPts = numel(x);
0030 if k == 1
0031     % Leave One Out Cross Validation
0032     inds = 1:numPts;
0033     k = numPts;
0034     if verbose, fprintf('--- LOOCV ---\n'); end
0035 else
0036     inds = crossvalind('Kfold', 1:numPts, k);
0037     if verbose, fprintf('--- %d-Fold Cross Validation ---\n', k); end
0038 end
0039 
0040 for i = 1:k
0041     if verbose, fprintf('- Step %d/%d\n', i, k); end
0042     % Split into test and train data
0043     validationData(i).test = (inds == i);    
0044     validationData(i).train = ~validationData(i).test;
0045     xx = x(validationData(i).train);
0046     yy = y(validationData(i).train);
0047     zz = z(validationData(i).train);
0048     xi = x(validationData(i).test);
0049     yi = y(validationData(i).test);
0050     ziRef = z(validationData(i).test);
0051     for m = 1:numel(methods)
0052         % Compute the results for the desired method
0053         method = methods{m};
0054         if verbose, fprintf('  - Interpolating with %s...', method); end
0055         tic
0056         zi = interpolate(xx, yy, zz, xi, yi, method, options);
0057         t = toc;
0058         if verbose, fprintf('done, %f sec\n', t); end
0059         absDiff = abs(zi-ziRef);
0060         meanAbsDiff = mean(absDiff);
0061         stdAbsDiff = std(absDiff);
0062         runTime = t;
0063         
0064         % Store this data in the validation structure
0065         splMethod = regexp(method, '\.', 'split');                
0066         str = struct('zi', zi, 'absDiff', absDiff, 'meanAbsDiff', meanAbsDiff, 'stdAbsDiff', stdAbsDiff, 'runTime', runTime);
0067         if numel(splMethod) == 1
0068             validationData(i).(splMethod{1}) = str;
0069         else
0070             validationData(i).(splMethod{1}).(splMethod{2}) = str;
0071         end        
0072     end
0073 end
0074 
0075 % Compute the stats for each method
0076 for m = 1:numel(methods)
0077     % Compute the results for the desired method
0078     method = methods{m};
0079     splMethod = regexp(method, '\.', 'split');                
0080     
0081     % All mean results
0082     allMeans = zeros(1, k);
0083     allRunTimes = zeros(1, k);
0084     for j = 1:k
0085         if numel(splMethod) == 1
0086             allMeans(j) = validationData(i).(splMethod{1}).meanAbsDiff;
0087             allRunTimes(j) = validationData(i).(splMethod{1}).runTime;
0088         else
0089             allMeans(j) = validationData(i).(splMethod{1}).(splMethod{2}).meanAbsDiff;
0090             allRunTimes(j) = validationData(i).(splMethod{1}).(splMethod{2}).runTime;
0091         end
0092     end
0093     
0094     if numel(splMethod) == 1
0095         stats.(splMethod{1}).meanAbsDiff = mean(allMeans(j));
0096         stats.(splMethod{1}).stdAbsDiff = std(allMeans(j));
0097         stats.(splMethod{1}).meanRunTime = mean(allRunTimes(j));
0098     else
0099         stats.(splMethod{1}).(splMethod{2}).meanAbsDiff = mean(allMeans(j));
0100         stats.(splMethod{1}).(splMethod{2}).stdAbsDiff = std(allMeans(j));
0101         stats.(splMethod{1}).(splMethod{2}).meanRunTime = mean(allRunTimes(j));
0102     end
0103 end
0104

Generated on Thu 10-Dec-2020 17:34:27 by m2html © 2005