]> Creatis software - CreaPhase.git/blob - octave_packages/econometrics-1.0.8/gmm_estimate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / econometrics-1.0.8 / gmm_estimate.m
1 # Copyright (C) 2003,2004,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, see <http://www.gnu.org/licenses/>.
15
16 # usage: [theta, obj_value, convergence, iters] =
17 #           gmm_estimate(theta, data, weight, moments, momentargs, control, nslaves)
18 #
19 # inputs:
20 #      theta: column vector initial parameters
21 #       data: data matrix
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.
26 #             May be empty ("")
27 #    control: (optional) BFGS or SA controls (see bfgsmin and samin).
28 #             May be empty ("").
29 #    nslaves: (optional) number of slaves if executed in parallel
30 #             (requires MPITB)
31 #
32 # outputs:
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
38 #
39 # please type "gmm_example" while in octave to see an example
40
41 # call the minimizing routine
42 function [theta, obj_value, convergence, iters] = gmm_estimate(theta, data, weight, moments, momentargs, control, nslaves)
43
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
48
49         if nslaves > 0
50                 global NSLAVES PARALLEL NEWORLD NSLAVES TAG;
51                 LAM_Init(nslaves);
52                 # Send the data to all nodes
53                 NumCmds_Send({"data", "weight", "moments", "momentargs"}, {data, weight, moments, momentargs});
54         endif
55
56         # bfgs or sa?
57         if (size(control,1)*size(control,2) == 0) # use default bfgs if no control
58                 control = {Inf,0,1,1};
59                 method = "bfgs";
60         elseif (size(control,1)*size(control,2) < 11)
61                 method = "bfgs";
62         else method = "sa";
63         endif
64
65
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);
70         endif
71
72         if nslaves > 0 LAM_Finalize; endif # clean up
73
74 endfunction