]> Creatis software - CreaPhase.git/blob - octave_packages/econometrics-1.0.8/mle_estimate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / econometrics-1.0.8 / mle_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:
17 # [theta, obj_value, conv, iters] = mle_estimate(theta, data, model, modelargs, control, nslaves)
18 #
19 # inputs:
20 # theta: column vector of model parameters
21 # data: data matrix
22 # model: name of function that computes log-likelihood
23 # modelargs: (cell) additional inputs needed by model. May be empty ("")
24 # control: (optional) BFGS or SA controls (see bfgsmin and samin). May be empty ("").
25 # nslaves: (optional) number of slaves if executed in parallel (requires MPITB)
26 #
27 # outputs:
28 # theta: ML estimated value of parameters
29 # obj_value: the value of the log likelihood function at ML estimate
30 # conv: return code from bfgsmin (1 means success, see bfgsmin for details)
31 # iters: number of BFGS iteration used
32 #
33 # please see mle_example.m for examples of how to use this
34 function [theta, obj_value, convergence, iters] = mle_estimate(theta, data, model, modelargs, control, nslaves)
35
36
37         if nargin < 3
38                 error("mle_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 = {-1,0,1,1}; endif # default controls and method
44         if !iscell(control) control = {-1,0,1,1}; endif # default controls if receive placeholder
45         if nargin < 6 nslaves = 0; endif
46         if nslaves > 0
47                 global NSLAVES PARALLEL NEWORLD TAG;
48                 LAM_Init(nslaves);
49                 # Send the data to all nodes
50                 NumCmds_Send({"data", "model", "modelargs"}, {data, model, modelargs});
51         endif
52
53         # bfgs or sa?
54         if (size(control,1)*size(control,2) == 0) # use default bfgs if no control
55                 control = {Inf,0,1,1};
56                 method = "bfgs";
57         elseif (size(control,1)*size(control,2) < 11)
58                 method = "bfgs";
59         else method = "sa";
60         endif
61
62         # do estimation using either bfgsmin or samin
63         if strcmp(method, "bfgs")
64           [theta, obj_value, convergence, iters] = bfgsmin("mle_obj", {theta, data, model, modelargs, nslaves}, control);
65         elseif strcmp(method, "sa")
66           [theta, obj_value, convergence] = samin("mle_obj", {theta, data, model, modelargs, nslaves}, control);
67         endif
68
69         if nslaves > 0
70                 LAM_Finalize;
71         endif # cleanup
72         obj_value = - obj_value; # recover from minimization rather than maximization
73 endfunction