1
# Copyright (C) 2003,2004, 2005 Michael Creel <michael.creel@uab.es>
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.
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.
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/>.
16
# GMM example file, shows initial consistent estimator,
17
# estimation of efficient weight, and second round
24
x = [ones(n,1) randn(n,k-1)];
26
theta_true = ones(k,1);
27
lambda = exp(x*theta_true);
28
y = poisson_rnd(lambda);
29
[xs, scalecoef] = scale_data(x);
31
# The arguments for gmm_estimate
34
weight = eye(columns(w));
35
moments = "poisson_moments";
36
momentargs = {k}; # needed to know where x ends and w starts
38
# additional args for gmm_results
39
names = str2mat("theta1", "theta2", "theta3", "theta4", "theta5");
40
gmmtitle = "Poisson GMM trial";
41
control = {100,0,1,1};
44
# initial consistent estimate: only used to get efficient weight matrix, no screen output
45
[theta, obj_value, convergence] = gmm_estimate(theta, data, weight, moments, momentargs, control);
47
# efficient weight matrix
48
# this method is valid when moments are not autocorrelated
49
# the user is reponsible to properly estimate the efficient weight
50
m = feval(moments, theta, data, momentargs);
51
weight = inverse(cov(m));
53
# second round efficient estimator
54
gmm_results(theta, data, weight, moments, momentargs, names, gmmtitle, scalecoef, control);
55
printf("\nThe true parameter values used to generate the data:\n");
56
prettyprint(theta_true, names, "value");
58
# Example doing estimation in parallel on a cluster (requires MPITB)
59
# uncomment the following if you have MPITB installed
63
# title = "GMM estimation done in parallel";
64
# gmm_results(theta, data, weight, moments, momentargs, names, gmmtitle, scalecoef, control, nslaves);