]> Creatis software - CreaPhase.git/blob - octave_packages/econometrics-1.0.8/mle_obj.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / econometrics-1.0.8 / mle_obj.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: [obj_value, score] = mle_obj(theta, data, model, modelargs, nslaves)
17 ##
18 ## Returns the average log-likelihood for a specified model
19 ## This is for internal use by mle_estimate
20
21
22 function [obj_value, score] = mle_obj(theta, data, model, modelargs, nslaves)
23
24         n = rows(data);   
25         
26         if nargin < 5 nslaves = 0; endif
27         if nslaves > 0
28                 global NSLAVES PARALLEL NEWORLD NSLAVES TAG;
29
30                 nn = floor(n/(NSLAVES + 1)); # number of obsns per slave
31
32                 # The command that the slave nodes will execute
33                 cmd=['contrib = mle_obj_nodes(theta, data, model, modelargs, nn); ',... 
34                         'MPI_Send(contrib,0,TAG,NEWORLD);'];    
35
36                 # send items to slaves
37                 NumCmds_Send({"theta", "nn", "cmd"}, {theta, nn, cmd});
38
39                 # evaluate last block on master while slaves are busy
40                 obj_value = mle_obj_nodes(theta, data, model, modelargs, nn);
41
42                 # collect slaves' results
43                 contrib = 0.0; # must be initialized to use MPI_Recv
44                 for i = 1:NSLAVES
45                         MPI_Recv(contrib,i,TAG,NEWORLD);
46                         obj_value = obj_value + contrib;
47                 endfor
48
49                 # compute the average
50                 obj_value = - obj_value / n;
51                 score = "na"; # fix this later to allow analytic score in parallel
52                 
53         else # serial version
54                 [contribs, score] = feval(model, theta, data, modelargs);
55                 obj_value = - mean(contribs);
56                 if isnumeric(score) score = - mean(score)'; endif # model passes "na" when score not available
57         endif
58
59         # let's bullet-proof this in case the model goes nuts
60         if (((abs(obj_value) == Inf)) || (isnan(obj_value)))
61                 obj_value = realmax/10;
62         endif       
63
64 endfunction