X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Feconometrics-1.0.8%2Fnls_estimate.m;fp=octave_packages%2Feconometrics-1.0.8%2Fnls_estimate.m;h=70ef350488774a5022cabbe4ea99c877b55bc459;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/econometrics-1.0.8/nls_estimate.m b/octave_packages/econometrics-1.0.8/nls_estimate.m new file mode 100644 index 0000000..70ef350 --- /dev/null +++ b/octave_packages/econometrics-1.0.8/nls_estimate.m @@ -0,0 +1,70 @@ +## Copyright (C) 2005 Michael Creel +## +## 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 2 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 . + +# usage: +# [theta, obj_value, conv, iters] = nls_estimate(theta, data, model, modelargs, control, nslaves) +# +# inputs: +# theta: column vector of model parameters +# data: data matrix +# model: name of function that computes the vector of sums of squared errors +# modelargs: (cell) additional inputs needed by model. May be empty ("") +# control: (optional) BFGS or SA controls (see bfgsmin and samin). May be empty (""). +# nslaves: (optional) number of slaves if executed in parallel (requires MPITB) +# +# outputs: +# theta: NLS estimated value of parameters +# obj_value: the value of the sum of squared errors at NLS estimate +# conv: return code from bfgsmin (1 means success, see bfgsmin for details) +# iters: number of BFGS iteration used +# +# please see nls_example.m for examples of how to use this +function [theta, obj_value, convergence, iters] = nls_estimate(theta, data, model, modelargs, control, nslaves) + + if nargin < 3 + error("nls_estimate: 3 arguments required"); + endif + + if nargin < 4 modelargs = {}; endif # create placeholder if not used + if !iscell(modelargs) modelargs = {}; endif # default controls if receive placeholder + if nargin < 5 control = {Inf,0,1,1}; endif # default controls and method + if !iscell(control) control = {Inf,0,1,1}; endif # default controls if receive placeholder + if nargin < 6 nslaves = 0; endif + + if nslaves > 0 + global NSLAVES PARALLEL NEWORLD TAG + LAM_Init(nslaves); + # Send the data to all nodes + NumCmds_Send({"data", "model", "modelargs"}, {data, model, modelargs}); + endif + + # bfgs or sa? + if (size(control,1)*size(control,2) == 0) # use default bfgs if no control + control = {Inf,0,1,1}; + method = "bfgs"; + elseif (size(control,1)*size(control,2) < 11) + method = "bfgs"; + else method = "sa"; + endif + + if strcmp(method, "bfgs") + [theta, obj_value, convergence, iters] = bfgsmin("nls_obj", {theta, data, model, modelargs, nslaves}, control); + elseif strcmp(method, "sa") + [theta, obj_value, convergence] = samin("nls_obj", {theta, data, model, modelargs, nslaves}, control); + endif + + # cleanup + if nslaves > 0 LAM_Finalize; endif +endfunction