Diff of /inst/nls_estimate.m [000000] .. [afe5a1] Maximize Restore

  Switch to unified view

a b/inst/nls_estimate.m
1
## Copyright (C) 2005  Michael Creel <michael.creel@uab.es>
2
##
3
## This program is free software; you can redistribute it and/or modify
4
## it under the terms of the GNU General Public License as published by
5
## the Free Software Foundation; either version 2 of the License, or
6
## (at your option) any later version.
7
##
8
## This program is distributed in the hope that it will be useful,
9
## but WITHOUT ANY WARRANTY; without even the implied warranty of
10
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
## GNU General Public License for more details.
12
##
13
## You should have received a copy of the GNU General Public License
14
## along with this program; if not, write to the Free Software
15
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
# usage:
18
# [theta, obj_value, conv, iters] = nls_estimate(theta, data, model, modelargs, control, nslaves)
19
#
20
# inputs:
21
# theta: column vector of model parameters
22
# data: data matrix
23
# model: name of function that computes the vector of sums of squared errors
24
# modelargs: (cell) additional inputs needed by model. May be empty ("")
25
# control: (optional) BFGS or SA controls (see bfgsmin and samin). May be empty ("").
26
# nslaves: (optional) number of slaves if executed in parallel (requires MPITB)
27
#
28
# outputs:
29
# theta: NLS estimated value of parameters
30
# obj_value: the value of the sum of squared errors at NLS estimate
31
# conv: return code from bfgsmin (1 means success, see bfgsmin for details)
32
# iters: number of BFGS iteration used
33
#
34
# please see nls_example.m for examples of how to use this
35
function [theta, obj_value, convergence, iters] = nls_estimate(theta, data, model, modelargs, control, nslaves)
36
37
  if nargin < 3
38
      error("nls_estimate: 3 arguments required");
39
  endif
40
41
  if nargin < 4 modelargs = {}; endif # create placeholder if not used
42
  if !iscell(modelargs) modelargs = {}; endif # default controls if receive placeholder
43
  if nargin < 5 control = {Inf,0,1,1}; endif # default controls and method
44
  if !iscell(control) control = {Inf,0,1,1}; endif # default controls if receive placeholder
45
  if nargin < 6 nslaves = 0; endif
46
47
  if nslaves > 0
48
      global NSLAVES PARALLEL NEWORLD TAG
49
      LAM_Init(nslaves);
50
      # Send the data to all nodes
51
      NumCmds_Send({"data", "model", "modelargs"}, {data, model, modelargs});
52
  endif
53
54
  # bfgs or sa?
55
  if (size(control,1)*size(control,2) == 0) # use default bfgs if no control
56
      control = {Inf,0,1,1};
57
    method = "bfgs";
58
  elseif (size(control,1)*size(control,2) < 11)
59
      method = "bfgs";
60
  else method = "sa";
61
  endif
62
63
  if strcmp(method, "bfgs")
64
    [theta, obj_value, convergence, iters] = bfgsmin("nls_obj", {theta, data, model, modelargs, nslaves}, control);
65
  elseif strcmp(method, "sa")
66
    [theta, obj_value, convergence] = samin("nls_obj", {theta, data, model, modelargs, nslaves}, control);
67
  endif
68
69
  # cleanup
70
  if nslaves > 0 LAM_Finalize; endif
71
endfunction