Advertisement
x89codered89x

statset.m

Oct 18th, 2012
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 21.60 KB | None | 0 0
  1. function options = statset(varargin)
  2. %STATSET Create/alter STATS options structure.
  3. %   OPTIONS = STATSET('PARAM1',VALUE1,'PARAM2',VALUE2,...) creates a
  4. %   statistics options structure OPTIONS in which the named parameters have
  5. %   the specified values.  Any unspecified parameters are set to [].  When
  6. %   you pass OPTIONS to a statistics function, a parameter set to []
  7. %   indicates that the function uses its default value for that parameter.
  8. %   Case is ignored for parameter names, and unique partial matches are
  9. %   allowed.  NOTE: For parameters that are string-valued, the complete
  10. %   string is required for the value; if an invalid string is provided, the
  11. %   default is used.
  12. %
  13. %   OPTIONS = STATSET(OLDOPTS,'PARAM1',VALUE1,...) creates a copy of
  14. %   OLDOPTS with the named parameters altered with the specified values.
  15. %
  16. %   OPTIONS = STATSET(OLDOPTS,NEWOPTS) combines an existing options
  17. %   structure OLDOPTS with a new options structure NEWOPTS.  Any parameters
  18. %   in NEWOPTS with non-empty values overwrite the corresponding old
  19. %   parameters in OLDOPTS.
  20. %
  21. %   STATSET with no input arguments and no output arguments displays all
  22. %   parameter names and their possible values, with defaults shown in {}
  23. %   when the default is the same for all functions that use that option.
  24. %   Use STATSET(STATSFUNCTION) (see below) to see function-specific
  25. %   defaults for a specific function.
  26. %
  27. %   OPTIONS = STATSET (with no input arguments) creates an options
  28. %   structure OPTIONS where all the fields are set to [].
  29. %
  30. %   OPTIONS = STATSET(STATSFUNCTION) creates an options structure with all
  31. %   the parameter names and default values relevant to the statistics
  32. %   function named in STATSFUNCTION.  STATSET sets parameters in OPTIONS to
  33. %   [] for parameters that are not valid for STATSFUNCTION.  For example,
  34. %   statset('factoran') or statset(@factoran) returns an options structure
  35. %   containing all the parameter names and default values relevant to the
  36. %   function 'factoran'.
  37. %
  38. %   STATSET parameters:
  39. %      Display     - Level of display.  'off', 'iter', or 'final'.
  40. %      MaxFunEvals - Maximum number of objective function evaluations
  41. %                    allowed.  A positive integer.
  42. %      MaxIter     - Maximum number of iterations allowed.  A positive integer.
  43. %      TolBnd      - Parameter bound tolerance.  A positive scalar.
  44. %      TolFun      - Termination tolerance for the objective function
  45. %                    value.  A positive scalar.
  46. %      TolTypeFun  - Flag to indicate how to use 'TolFun'. 'abs' indicates
  47. %                    using 'TolFun' as an absolute tolerance; 'rel'
  48. %                    indicates using it as a relative tolerance.
  49. %      TolX        - Termination tolerance for the parameters.  A positive scalar.
  50. %      TolTypeX    - Flag to indicate how to use 'TolX'. 'abs' indicates
  51. %                    using 'TolX' as an absolute tolerance; 'rel'
  52. %                    indicates using it as a relative tolerance.
  53. %      GradObj     - Flag to indicate whether the objective function can return
  54. %                    a gradient vector as a second output.  'off' or 'on'.
  55. %      Jacobian    - Flag to indicate whether the model function can return a
  56. %                    Jacobian as a second output.  'off' or 'on'.
  57. %      DerivStep   - Relative difference used in finite difference derivative
  58. %                    calculations.  A positive scalar, or a vector of
  59. %                    positive scalars the same size as the vector of model
  60. %                    parameters being estimated.
  61. %      FunValCheck - Check for invalid values, such as NaN or Inf, from
  62. %                    the objective function.  'off' or 'on'.
  63. %      Robust      - Flag to invoke the robust fitting option.  'off' (the default)
  64. %                    or 'on'.
  65. %      WgtFun      - A weight function for robust fitting.  Valid only when Robust
  66. %                    is 'on'.  'bisquare' (the default), 'andrews', 'cauchy',
  67. %                    'fair', 'huber', 'logistic', 'talwar', or 'welsch'.  Can
  68. %                    also be a function handle that accepts a normalized residual
  69. %                    as input and returns the robust weights as output.
  70. %      Tune        - The tuning constant used in robust fitting to normalize the
  71. %                    residuals before applying the weight function.  A positive
  72. %                    scalar.  The default value depends upon the weight function.
  73. %                    This parameter is required if the weight function is
  74. %                    specified as a function handle.
  75. %      UseParallel - Flag to indicate whether eligible functions should use
  76. %                    capabilities of the Parallel Computing Toolbox (PCT),
  77. %                    if the capabilities are available. Valid values are
  78. %                    'never' (the default), to indicate serial computation,
  79. %                    and 'always', to request parallel computation.
  80. %                    See PARALLELSTATS for more complete information about
  81. %                    this parameter.
  82. %      UseSubstreams-Flag to indicate whether the random number generator
  83. %                    in eligible functions should use the substream feature
  84. %                    of the random number stream. 'never' (default) or
  85. %                    'always'. If 'always', the function will use the
  86. %                    substream feature during iterative loops in such a way
  87. %                    as to generate reproducible random number streams in
  88. %                    parallel and/or serial mode computation.
  89. %                    See PARALLELSTATS for more complete information about
  90. %                    this parameter.
  91. %      Streams     - A single random number stream, or a cell array of random
  92. %                    number streams.  The Streams option is accepted by some
  93. %                    Statistics Toolbox functions to govern what stream(s) to
  94. %                    use when generating random numbers within the function.
  95. %                    Acceptable values for the Streams parameter vary depending
  96. %                    on the Statistics Toolbox function and on other factors.
  97. %                    See PARALLELSTATS for more complete information about
  98. %                    this parameter.
  99. %      OutputFcn   - Function handle specified using @, a cell array with
  100. %                    function handles or an empty array (default).  The
  101. %                    solver calls all output functions after each
  102. %                    iteration.
  103. %
  104. %   See also STATGET.
  105.  
  106. %   Copyright 1993-2010 The MathWorks, Inc.
  107. %   $Revision: 1.1.8.7 $  $Date: 2010/10/08 17:26:42 $
  108.  
  109. % Print out possible values of properties.
  110. if (nargin == 0) && (nargout == 0)
  111.     fprintf('                Display: [ {off} | final | iter ]\n');
  112.     fprintf('            MaxFunEvals: [ positive integer ]\n');
  113.     fprintf('                MaxIter: [ positive integer ]\n');
  114.     fprintf('                 TolBnd: [ positive scalar ]\n');
  115.     fprintf('                 TolFun: [ positive scalar ]\n');
  116.     fprintf('             TolTypeFun: [''abs'' |''rel'']\n')
  117.     fprintf('                   TolX: [ positive scalar ]\n')
  118.     fprintf('               TolTypeX: [''abs'' |''rel'']\n')
  119.     fprintf('                GradObj: [ {off} | on ]\n')
  120.     fprintf('               Jacobian: [ {off} | on ]\n')
  121.     fprintf('              DerivStep: [ positive scalar or vector ]\n')
  122.     fprintf('            FunValCheck: [ off | {on} ]\n')
  123.     fprintf('                 Robust: [ {off} | on ]\n')
  124.     fprintf('                 WgtFun: [ {bisquare} | andrews | cauchy | fair | huber | logistic | talwar | welsch | function handle ]\n')
  125.     fprintf('                   Tune: [ positive scalar ]\n')
  126.     fprintf('            UseParallel: [ {never} | always ]\n')
  127.     fprintf('          UseSubstreams: [ {never} | always ]\n')
  128.     fprintf('                Streams: [ {} | RandStream or cell array ]\n')
  129.     fprintf('              OutputFcn: [ {[]} | function handle or cell array ]\n')
  130.     fprintf('\n');
  131.     return;
  132. end
  133.  
  134. options = struct('Display', [], 'MaxFunEvals', [], 'MaxIter', [], ...
  135.     'TolBnd', [], 'TolFun', [], 'TolTypeFun',[],'TolX', [], 'TolTypeX',[], ...
  136.     'GradObj', [], 'Jacobian', [], 'DerivStep', [], 'FunValCheck', [], ...
  137.     'Robust',[], 'WgtFun',[], 'Tune',[], ...
  138.     'UseParallel',[], 'UseSubstreams',[], 'Streams', [], 'OutputFcn', []);
  139.  
  140. % If a function name/handle was passed in, then return the defaults.
  141. if nargin == 1
  142.     arg = varargin{1};
  143.     if (ischar(arg) || isa(arg,'function_handle'))
  144.         if isa(arg,'function_handle')
  145.             arg = func2str(arg);
  146.         end
  147.         % Display is off by default.  The individual fitters have their own
  148.         % warning/error messages that can be controlled via IDs.  The
  149.         % optimizers print out text when display is on, but do not generate
  150.         % warnings or errors per se.
  151.         options.Display = 'off';
  152.         switch lower(arg)
  153.             case 'factoran' % this uses statsfminbx
  154.                 options.MaxFunEvals = 400;
  155.                 options.MaxIter = 100;
  156.                 options.TolFun = 1e-8;
  157.                 options.TolX = 1e-8;
  158.             case {'normfit' 'lognfit' 'gamfit' 'bisafit' 'invgfit' 'logifit'...
  159.                   'loglfit' 'nakafit' 'coxphfit'} % these use statsfminbx
  160.                 options.MaxFunEvals = 200;
  161.                 options.MaxIter = 100;
  162.                 options.TolBnd = 1e-6;
  163.                 options.TolFun = 1e-8;
  164.                 options.TolX = 1e-8;
  165.             case {'evfit' 'wblfit'} % these use fzero (gamfit sometimes does too)
  166.                 options.TolX = 1e-6;
  167.             case 'copulafit' % this uses fminbnd
  168.                 options.MaxFunEvals = 200;
  169.                 options.MaxIter = 100;
  170.                 options.TolX = 1e-6;
  171.                 options.TolBnd = 1e-6;
  172.             case {'gpfit' 'gevfit' 'nbinfit' 'ricefit' 'tlsfit'} % these use fminsearch
  173.                 options.MaxFunEvals = 400;
  174.                 options.MaxIter = 200;
  175.                 options.TolBnd = 1e-6;
  176.                 options.TolFun = 1e-6;
  177.                 options.TolX = 1e-6;
  178.             case 'kmeans'
  179.                 options.MaxIter = 100;
  180.             case 'svmtrain'
  181.                 % do nothing because QP and SMO have different MaxIter values.                
  182.             case 'nlinfit'
  183.                 options.MaxIter = 200;
  184.                 options.TolFun = 1e-8;
  185.                 options.TolX = 1e-8;
  186.                 options.DerivStep = eps^(1/3);
  187.                 options.FunValCheck = 'on';
  188.                 options.Robust = 'off';
  189.                 options.WgtFun = 'bisquare';
  190.                 options.Tune = []; % default varies by WgtFun, must be supplied for user-defined WgtFun
  191.             case 'nlmefit'
  192.                 options.MaxIter = 200;
  193.                 options.TolFun = 1e-4;
  194.                 options.TolX = 1e-4;
  195.                 options.DerivStep = eps^(1/3);
  196.                 options.Jacobian = 'off';
  197.                 options.FunValCheck = 'on';
  198.                 options.OutputFcn = '';
  199.             case 'nlmefitsa'
  200.                 options.DerivStep = eps^(1/3);
  201.                 options.FunValCheck = 'on';
  202.                 options.Streams = {};
  203.                 options.OutputFcn = {@nlmefitoutputfcn};
  204.             case 'mlecustom' % this uses fminsearch, or maybe fmincon
  205.                 options.MaxFunEvals = 400;
  206.                 options.MaxIter = 200;
  207.                 options.TolBnd = 1e-6;
  208.                 options.TolFun = 1e-6;
  209.                 options.TolX = 1e-6;
  210.                 options.GradObj = 'off';
  211.                 options.DerivStep = eps^(1/3);
  212.                 options.FunValCheck = 'on';
  213.             case 'mlecov'
  214.                 options.GradObj = 'off';
  215.                 options.DerivStep = eps^(1/4);
  216.             case 'mdscale'
  217.                 options.MaxIter = 200;
  218.                 options.TolFun = 1e-6;
  219.                 options.TolX = 1e-6;
  220.             case {'mvncdf' 'mvtcdf'}
  221.                 options.MaxFunEvals = 1e7;
  222.                 options.TolFun = []; % 1e-8 for dim < 4, 1e-4 otherwise
  223.             case {'gmdistribution'}
  224.                 options.MaxIter = 100;
  225.                 options.TolFun = 1e-6;
  226.             case {'nnmf'}
  227.                 options.MaxIter = 100;
  228.                 options.TolFun = 1e-4;
  229.                 options.TolX = 1e-4;
  230.                 options.UseParallel = 'never';
  231.                 options.UseSubstreams = 'never';
  232.                 options.Streams = {};
  233.             case {'sequentialfs'}
  234.                 options.MaxIter = Inf;
  235.                 options.TolTypeFun = 'rel';
  236.                 options.UseParallel = 'never';
  237.                 options.UseSubstreams = 'never';
  238.                 options.Streams = {};
  239.             case {'bootci' 'bootstrp' 'candexch' 'cordexch' 'crossval' 'daugment' 'dcovary' 'plsregress' 'rowexch' 'treebagger'}
  240.                 options.UseParallel = 'never';
  241.                 options.UseSubstreams = 'never';
  242.                 options.Streams = {};
  243.             case {'jackknife'}
  244.                 options.UseParallel = 'never';
  245.             otherwise
  246.                 error(message('stats:statset:BadFunctionName', arg));
  247.         end
  248.         return
  249.     end
  250. end
  251.  
  252. names = fieldnames(options);
  253. lowNames = lower(names);
  254. numNames = numel(names);
  255.  
  256. % Process OLDOPTS and NEWOPTS, if it's there.
  257. i = 1;
  258. while i <= nargin
  259.     arg = varargin{i};
  260.     % Check if we're into the param name/value pairs yet.
  261.     if ischar(arg), break; end
  262.  
  263.     if ~isempty(arg) % [] is a valid options argument
  264.         if ~isa(arg,'struct')
  265.             error('stats:statset:BadInput',...
  266.                 ['Expected argument %d to be a parameter name string or ' ...
  267.                 'an options structure\ncreated with STATSET.'], i);
  268.         end
  269.         argNames = fieldnames(arg);
  270.         for j = 1:numNames
  271.             name = names{j};
  272.             if any(strcmp(name,argNames))
  273.                 val = arg.(name);
  274.                 if ~isempty(val)
  275.                     if ischar(val)
  276.                         val = lower(deblank(val));
  277.                     end
  278.                     [valid, errid, errmsg] = checkparam(name,val);
  279.                     if valid
  280.                         options.(name) = val;
  281.                     elseif ~isempty(errmsg)
  282.                         error(errid,errmsg);
  283.                     end
  284.                 end
  285.             end
  286.         end
  287.     end
  288.     i = i + 1;
  289. end
  290.  
  291. % Done with OLDOPTS and NEWOPTS, now parse parameter name/value pairs.
  292. if rem(nargin-i+1,2) ~= 0
  293.     error(message('stats:statset:BadInput'));
  294. end
  295. expectval = false; % start expecting a name, not a value
  296. while i <= nargin
  297.     arg = varargin{i};
  298.  
  299.     % Process a parameter name.
  300.     if ~expectval
  301.         if ~ischar(arg)
  302.             error('stats:statset:BadParameter',...
  303.                 'Expected argument %d to be a parameter name string.', i);
  304.         end
  305.         lowArg = lower(arg);
  306.         j = strmatch(lowArg,lowNames);
  307.         if numel(j) == 1 % one match
  308.             name = names{j};
  309.         elseif length(j) > 1 % more than one match
  310.             % Check for any exact matches (in case any names are subsets of others)
  311.             k = strmatch(lowArg,lowNames,'exact');
  312.             if numel(k) == 1
  313.                 name = names{k};
  314.             else
  315.                 matches = [names{j(1)}, sprintf(', %s',names{j(2:end)})];
  316.                 error('stats:statset:BadParameter',...
  317.                     'Ambiguous parameter name ''%s'' (%s)', arg, matches);
  318.             end
  319.         else %if isempty(j) % no matches
  320.             error('stats:statset:BadParameter',...
  321.                 'Unrecognized parameter name ''%s''.', arg);
  322.         end
  323.         expectval = true; % expect a value next
  324.  
  325.         % Process a parameter value.
  326.     else
  327.         if ischar(arg)
  328.             arg = lower(deblank(arg));
  329.         end
  330.         [valid, errid, errmsg] = checkparam(name,arg);
  331.         if valid
  332.             options.(name) = arg;
  333.         elseif ~isempty(errmsg)
  334.             error(errid,errmsg);
  335.         end
  336.         expectval = false; % expect a name next
  337.     end
  338.     i = i + 1;
  339. end
  340.  
  341. % The default wgt function for robust fit is bisquare.
  342. if (strcmp(options.Robust, 'on') && isempty(options.WgtFun))
  343.     options.WgtFun = 'bisquare';
  344. end
  345. if strcmp(options.Robust, 'on')
  346.     if ischar(options.WgtFun)
  347.         [~,~,~,options.Tune] = statrobustwfun(options.WgtFun,[]);
  348.     end
  349.     if isempty(options.Tune) || ~isnumeric(options.Tune)
  350.         error('stats:statset:BadParameter', 'Tune must be provided for user-defined WgtFun');
  351.     end
  352. end
  353.  
  354. %-------------------------------------------------
  355. function [valid, errid, errmsg] = checkparam(name,value)
  356. %CHECKPARAM Validate a STATSET parameter value.
  357. %   [VALID,ID,MSG] = CHECKPARAM('name',VALUE) checks that the specified
  358. %   value VALUE is valid for the parameter 'name'.
  359. valid = true;
  360. errmsg = '';
  361. errid = '';
  362.  
  363. % Empty is always a valid parameter value.
  364. if isempty(value)
  365.     return
  366. end
  367.  
  368. switch name
  369.     case {'TolBnd','TolX'} % positive real scalar
  370.         if ~isfloat(value) || ~isreal(value) || ~isscalar(value) || value <= 0
  371.             valid = false;
  372.             errid = 'stats:statset:BadTolerance';
  373.             if ischar(value)
  374.                 errmsg = sprintf('STATSET parameter ''%s'' must be a real positive scalar (not a string).',name);
  375.             else
  376.                 errmsg = sprintf('STATSET parameter ''%s'' must be a real positive scalar.',name);
  377.             end
  378.         end
  379.     case  {'TolFun'}
  380.         if ~isfloat(value) || ~isreal(value) || ~isscalar(value) || value < 0
  381.             valid = false;
  382.             errid = 'stats:statset:BadTolerance';
  383.             if ischar(value)
  384.                 errmsg = sprintf('STATSET parameter ''%s'' must be a real positive scalar (not a string).',name);
  385.             else
  386.                 errmsg = sprintf('STATSET parameter ''%s'' must be a real positive scalar.',name);
  387.             end
  388.         end
  389.     case {'TolTypeFun', 'TolTypeX'}
  390.         values = ['abs'; 'rel'];
  391.         if ~ischar(value) || isempty(strmatch(value,values,'exact'))
  392.             valid = false;
  393.             errid = 'stats:statset:BadTolType';
  394.             errmsg = sprintf('STATSET parameter ''%s'' must be ''abs'' or ''rel''.',name);
  395.         end
  396.     case {'Display'} % off,final,iter; we accept notify, but don't advertise it
  397.         values = ['off   '; 'notify'; 'final '; 'iter  '];
  398.         if ~ischar(value) || isempty(strmatch(value,values,'exact'))
  399.             valid = false;
  400.             errid = 'stats:statset:BadDisplay';
  401.             errmsg = sprintf('STATSET parameter ''%s'' must be ''off'', ''final'', or ''iter''.',name);
  402.         end
  403.     case {'MaxIter' 'MaxFunEvals'} % non-negative integer, possibly inf
  404.         if ~isfloat(value) || ~isreal(value) || ~isscalar(value) || any(value < 0)
  405.             valid = false;
  406.             errid = 'stats:statset:BadMaxValue';
  407.             if ischar(value)
  408.                 errmsg = sprintf('STATSET parameter ''%s'' must be a real positive scalar (not a string).',name);
  409.             else
  410.                 errmsg = sprintf('STATSET parameter ''%s'' must be a real positive scalar.',name);
  411.             end
  412.         end
  413.     case {'GradObj' 'Jacobian' 'FunValCheck' 'Robust'}
  414.         values = ['off'; 'on '];
  415.         if ~ischar(value) || isempty(strmatch(value,values,'exact'))
  416.             valid = false;
  417.             errid = 'stats:statset:BadFlagValue';
  418.             errmsg = sprintf('STATSET parameter ''%s'' must be ''off'' or ''on''.',name);
  419.         end
  420.     case {'WgtFun'}
  421.         values = ['andrews ';'bisquare';'cauchy  ';'fair    '; 'huber   '; 'logistic'; 'talwar  ';  'welsch  '];
  422.         % allow custom wgt function
  423.         if ~strcmp(class(value), 'function_handle')&&(~ischar(value) || isempty(strmatch(value,values,'exact')))
  424.             valid = false;
  425.             errid = 'stats:statset:BadWeightFunction';
  426.             errmsg = sprintf('STATSET parameter ''%s'' is not valid.',name);
  427.         end
  428.     case 'DerivStep'
  429.         if ~isfloat(value) || ~isreal(value) || any(value <= 0) || ~isvector(value)
  430.             valid = false;
  431.             errid = 'stats:statset:BadDifference';
  432.             errmsg = sprintf('STATSET parameter ''%s'' must be a scalar or vector containing real positive values.',name);
  433.         end
  434.     case 'Tune'
  435.         if ~isfloat(value) || ~isreal(value) || any(value <= 0)
  436.             valid = false;
  437.             errid = 'stats:statset:BadTune';
  438.             errmsg = sprintf('STATSET parameter ''%s'' must contain real positive values.',name);
  439.         end
  440.     case {'UseParallel' 'UseSubstreams'}
  441.         values = ['never '; 'always'];
  442.         if ~ischar(value) || isempty(strmatch(value,values,'exact'))
  443.             valid = false;
  444.             errid = 'stats:statset:BadParameterValue';
  445.             errmsg = sprintf('STATSET parameter ''%s'' must be ''never'' or ''always''.',name);
  446.         end
  447.     case {'Streams'}
  448.         if ~( isempty(value) || isa(value,'RandStream') || (iscell(value) && all(cellfun(@(x)isa(x,'RandStream'),value))) )
  449.             valid = false;
  450.             errid = 'stats:statset:BadParameterValue';
  451.             errmsg = sprintf('STATSET parameter ''%s'' must be a RandStream or cell array of RandStreams',name);
  452.         end
  453.     case {'OutputFcn'}
  454.         if ~((iscell(value) && all(cellfun(@(x) isa(x,'function_handle'),value))) || isa(value,'function_handle'))
  455.             valid = false;
  456.             errid = 'stats:statset:BadParameterValue';
  457.             errmsg = sprintf('STATSET parameter ''%s'' must be a function handle class or a cell array with function handles.',name);
  458.         end
  459.    
  460.     otherwise
  461.         valid = false;
  462.         errid = 'stats:statset:BadParameter';
  463.         errmsg = sprintf('Invalid STATSET parameter name: ''%s''.',name);
  464. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement