1 # Copyright (C) 2003,2004,2005 Michael Creel <michael.creel@uab.es>
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.
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.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; If not, see <http://www.gnu.org/licenses/>.
16 # usage: [theta, obj_value, convergence, iters] =
17 # gmm_estimate(theta, data, weight, moments, momentargs, control, nslaves)
20 # theta: column vector initial parameters
22 # weight: the GMM weight matrix
23 # moments: name of function computes the moments
24 # (should return nXg matrix of contributions)
25 # momentargs: (cell) additional inputs needed to compute moments.
27 # control: (optional) BFGS or SA controls (see bfgsmin and samin).
29 # nslaves: (optional) number of slaves if executed in parallel
33 # theta: GMM estimate of parameters
34 # obj_value: the value of the gmm obj. function
35 # convergence: return code from bfgsmin
36 # (1 means success, see bfgsmin for details)
37 # iters: number of BFGS iteration used
39 # please type "gmm_example" while in octave to see an example
41 # call the minimizing routine
42 function [theta, obj_value, convergence, iters] = gmm_estimate(theta, data, weight, moments, momentargs, control, nslaves)
44 if nargin < 5 error("gmm_estimate: 5 arguments required"); endif
45 if nargin < 6 control = {-1}; endif # default controls
46 if !iscell(control) control = {-1}; endif # default controls if receive placeholder
47 if nargin < 7 nslaves = 0; endif
50 global NSLAVES PARALLEL NEWORLD NSLAVES TAG;
52 # Send the data to all nodes
53 NumCmds_Send({"data", "weight", "moments", "momentargs"}, {data, weight, moments, momentargs});
57 if (size(control,1)*size(control,2) == 0) # use default bfgs if no control
58 control = {Inf,0,1,1};
60 elseif (size(control,1)*size(control,2) < 11)
66 if strcmp(method, "bfgs")
67 [theta, obj_value, convergence, iters] = bfgsmin("gmm_obj", {theta, data, weight, moments, momentargs}, control);
68 elseif strcmp(method, "sa")
69 [theta, obj_value, convergence] = samin("gmm_obj", {theta, data, weight, moments, momentargs}, control);
72 if nslaves > 0 LAM_Finalize; endif # clean up