~ubuntu-branches/ubuntu/lucid/octave-econometrics/lucid

« back to all changes in this revision

Viewing changes to inst/gmm_example.m

  • Committer: Bazaar Package Importer
  • Author(s): Ólafur Jens Sigurðsson
  • Date: 2008-02-06 23:30:04 UTC
  • Revision ID: james.westby@ubuntu.com-20080206233004-bfnf0vmewhwuj8a8
Tags: upstream-1.0.5
Import upstream version 1.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
# GMM example file, shows initial consistent estimator,
 
17
# estimation of efficient weight, and second round
 
18
# efficient estimator
 
19
 
 
20
 
 
21
n = 1000;
 
22
k = 5;
 
23
 
 
24
x = [ones(n,1) randn(n,k-1)];
 
25
w = [x, rand(n,1)];
 
26
theta_true = ones(k,1);
 
27
lambda = exp(x*theta_true);
 
28
y = poisson_rnd(lambda);
 
29
[xs, scalecoef] = scale_data(x);
 
30
 
 
31
# The arguments for gmm_estimate
 
32
theta = zeros(k,1);
 
33
data = [y xs w];
 
34
weight = eye(columns(w));
 
35
moments = "poisson_moments";
 
36
momentargs = {k}; # needed to know where x ends and w starts
 
37
 
 
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};
 
42
 
 
43
 
 
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);
 
46
 
 
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));
 
52
 
 
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");
 
57
 
 
58
# Example doing estimation in parallel on a cluster (requires MPITB)
 
59
# uncomment the following if you have MPITB installed
 
60
# nslaves = 1;
 
61
# theta = zeros(k,1);
 
62
# nslaves = 1;
 
63
# title = "GMM estimation done in parallel";
 
64
# gmm_results(theta, data, weight, moments, momentargs, names, gmmtitle, scalecoef, control, nslaves);