From: Michael Creel <mcreel@us...>  20041020 10:31:47

Update of /cvsroot/octave/octaveforge/main/econometrics In directory sc8prcvs1.sourceforge.net:/tmp/cvsserv15337/main/econometrics Added Files: average_moments.m gmm_estimate.m gmm_example.m gmm_obj.m gmm_results.m gmm_variance_inefficient.m gmm_variance.m Log Message: initial commit of gmm functions  NEW FILE: gmm_results.m  # Copyright (C) 2003,2004 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. function [theta, V, obj_value] = gmm_results(theta, data, weight, moments, momentargs, names, title, unscale, control) if nargin < 9 [theta, obj_value, convergence] = gmm_estimate(theta, data, weight, moments, momentargs); else [theta, obj_value, convergence] = gmm_estimate(theta, data, weight, moments, momentargs, control); endif m = feval(moments, theta, data, momentargs); # find out how many obsns. we have n = rows(m); if convergence == 1 convergence="Normal convergence"; else convergence="No convergence"; endif V = gmm_variance(theta, data, weight, moments, momentargs); # unscale results if argument has been passed # this puts coefficients into scale corresponding to the original data if nargin > 7 if iscell(unscale) [theta, V] = unscale_parameters(theta, V, unscale); endif endif [theta, V] = delta_method("Parameterize", theta, {data, moments, momentargs}, V); n = rows(data); k = rows(theta); se = sqrt(diag(V)); printf("\n\n******************************************************\n"); disp(title); printf("\nGMM Estimation Results\n"); printf("BFGS convergence: %s\n", convergence); printf("\nObjective function value: %f\n", obj_value); printf("Observations: %d\n", n); junk = "X^2 test"; df = rows(weight)  rows(theta); if df > 0 clabels = str2mat("Value","df","pvalue"); a = [n*obj_value, df, 1  chisquare_cdf(n*obj_value, df)]; printf("\n"); prettyprint(a, junk, clabels); else disp("\nExactly identified, no spec. test"); end; # results for parameters a =[theta, se, theta./se, 2  2*normal_cdf(abs(theta ./ se))]; clabels = str2mat("estimate", "st. err", "tstat", "pvalue"); printf("\n"); prettyprint(a, names, clabels); printf("******************************************************\n"); endfunction  NEW FILE: gmm_example.m  # Copyright (C) 2003,2004 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # GMM example file, shows initial consistent estimator, # estimation of efficient weight, and second round # efficient estimator # This also shows how do use data scaling  WHICH YOU SHOULD DO! 1; # the form a userwritten moment function should take function m = mymoments(theta, data, momentargs) k = momentargs{1}; # use this so that data can hold dep, indeps, and instr y = data(:,1); x = data(:,2:k+1); w = data(:, k+2:columns(data)); lambda = exp(x*theta); e = y ./ lambda  1; m = dmult(e, w); endfunction n = 1000; k = 5; x = [ones(n,1) rand(n,k1)]; w = [x, rand(n,1)]; theta = ones(k,1); lambda = exp(x*theta); y = randp(lambda); [xs, scalecoef] = scale_data(x); # The arguments for gmm_estimate theta = zeros(k,1); data = [y xs w]; weight = eye(columns(w)); moments = "mymoments"; momentargs = {k}; # needed to know where x ends and w starts # additional args for gmm_results names = str2mat("theta1", "theta2", "theta3", "theta4", "theta5"); title = "Poisson GMM trial"; control = {100,0,1,1}; # initial consistent estimate: only used to get efficient weight matrix, no screen output [theta, obj_value, convergence] = gmm_estimate(theta, data, weight, moments, momentargs); # efficient weight matrix # this method is valid when moments are not autocorrelated # the user is reponsible to properly estimate the efficient weight m = feval(moments, theta, data, momentargs); weight = inverse(cov(m)); # second round efficient estimator gmm_results(theta, data, weight, moments, momentargs, names, title, scalecoef, control);  NEW FILE: gmm_variance.m  # Copyright (C) 2003,2004 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # GMM variance, which assumes weights are optimal function V = gmm_variance(theta, data, weight, moments, momentargs) D = numgradient("average_moments", {theta, data, moments, momentargs}); D = D'; m = feval(moments, theta, data, momentargs); # find out how many obsns. we have n = rows(m); V = (1/n)*inv(D*weight*D'); endfunction  NEW FILE: gmm_obj.m  # Copyright (C) 2003,2004 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # The GMM objective function # This is scaled so that it converges to a # finite number. To get the chisquare specification # test you need to multiply by n (the sample size) function obj_value = gmm_obj(theta, data, weight, moments, momentargs) m = average_moments(theta, data, moments, momentargs); obj_value = m' * weight *m; if (((abs(obj_value) == Inf))  (isnan(obj_value))) obj_value = realmax; endif endfunction  NEW FILE: gmm_variance_inefficient.m  # Copyright (C) 2003,2004 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # GMM variance, which assumes weights are not optimal function V = gmm_variance_inefficient(theta, data, weight, omega, moments, momentargs) D = NumGradient("average_moments", {theta, data, moments, momentargs}); D = D'; m = feval(moments, theta, data, momentargs); # find out how many obsns. we have n = rows(m); J = D*weight*D'; J = inv(J); I = D*weight*omega*weight*D'; V = (1/n)*J*I*J; endfunction  NEW FILE: gmm_estimate.m  # Copyright (C) 2003,2004 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # performs the minimization function [theta, obj_value, convergence] = gmm_estimate(theta, data, weight, moments, momentargs, control) gmmargs = {theta, data, weight, moments, momentargs}; if nargin < 6 [theta, obj_value, convergence] = bfgsmin("gmm_obj", gmmargs); else [theta, obj_value, convergence] = bfgsmin("gmm_obj", gmmargs, control); endif endfunction  NEW FILE: average_moments.m  # Copyright (C) 2003,2004 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307 USA # Copyright (C) 2003 Michael Creel michael.creel@... # under the terms of the GNU General Public License. # The GPL license is in the file COPYING # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # average moments (separate function so it can be differentiated) function m = average_moments(theta, data, moments, momentargs) m = feval(moments, theta, data, momentargs); m = mean(m)'; # returns Gx1 moment vector endfunction 