]> Creatis software - CreaPhase.git/blob - octave_packages/econometrics-1.0.8/average_moments.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / econometrics-1.0.8 / average_moments.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 # for internal use by gmm_estimate 
17
18
19 # average moments (separate function so it can be differentiated)
20 function m = average_moments(theta, data, moments, momentargs)
21         
22         global NSLAVES PARALLEL NEWORLD NSLAVES TAG;
23
24         n = rows(data);
25
26         if PARALLEL
27                 nn = floor(n/(NSLAVES + 1)); # save some work for master
28     
29                 #  The command that the slave nodes will execute
30                 cmd=['contrib = sum_moments_nodes(theta, data, moments, momentargs, nn); ',...  
31                         'MPI_Send(contrib,0,TAG,NEWORLD);'];    
32
33                 # send items to slaves
34                 NumCmds_Send({"theta", "nn", "cmd"},{theta, nn, cmd});
35
36                 # evaluate last block on master while slaves are busy
37                 m = feval("sum_moments_nodes", theta, data, moments, momentargs, nn);
38
39                 # collect slaves' results
40                 contrib = zeros(1,columns(m));
41                 for i = 1:NSLAVES
42                         MPI_Recv(contrib,i,TAG,NEWORLD);
43                         m = m + contrib;
44                 endfor
45
46                 m = m'; # we want a column vector, please
47                 m = m/n; # average please, not sum
48
49         else # serial version
50                 m = feval(moments, theta, data, momentargs);
51                 m = mean(m)';  # returns Gx1 moment vector
52         endif
53         
54 endfunction