Hi, as asked in Feb and March 2023 on the mailing list with no answer, and recently discussed with @atecon, it would be good to have GMM as an option for estimation of a gretl "system". Since internally gretl already has the gmm block, hooking this up shouldn't be too difficult, should it?
Thanks for ticket, Sven. Indeed, that would be a useful feature.
OK, I created a hansl-based sort-of proof of concept (attached).
However, there are some complications here:
BTW, I think that the syntax introduction in section 27.2 is missing the part that you also need to provide a formula (assignment) that updates your target variable as a function of the parameters. There it only says you need orthog, weights, params. Further down the rest also becomes clear (sort of), but it's a bit confusing.
Thanks, Sven
Several points merit a reply but right now I'll just deal with one: using a list in an
orthogstatement. Up till now we've assumed there's only one term on each side of the semicolon in such a statement: a named list would work, but not a "raw" list. However, now in git and snapshots there's a smarter parser which allows for a list in the form of two or more series names -- since in some cases that's more convenient. (We then manufacture an internal named list to keep track of what's wanted.)Thanks, Allin. Actually, using a named list would have been enough (and of course still must be used with current gretl), it just didn't occur to me to try that.
The only real remaining question then is number 3.
Number 3 (constructing N residual series): "It says you can use a matrix as the target quantity there". What's the "it" in question and where does it say something about a residuals target, please?
Hi @allin, the context was always section 27.2 (and/or 27.3) of the guide. Specifically: "...must follow the syntax 'orthog x ; Z' where x may be a series, matrix or list of series..."
Since the first term in the orthog line (the x here) is the stuff that depends on the parameters, I am inferring from that statement that the constructed residuals can also be calculated as a gretl matrix or list.
And indeed I have now successfully specified the list of residuals (list resids = ud us) in a single orthog line: 'orthog resids ; allinst', so that's progress. I'm still struggling a bit with updating the contained series, because I'm not allowed to use a loop within the gmm block. So I guess I'll have to switch to a matrix-based formulation. I'm working on that.
I'm not quite sure what you mean by the "constructed residuals". But the LHS values from an orthog statement are automatically updated in the course of iteration, and on successful completion of a gmm block they hold the final estimates. Here's a series-and-list oriented variant of what I posted on the devel list:
Yes, that's similar to what I have. By "constructed residuals" I mean the u1 and u2 series which are re-calculated (constructed) every time.
The two generalizations that I'm trying to achieve are:
I'm going back to the original idea here, which I take to be providing a "method=gmm" option for gretl's "system" command. I'd suggest that it might be more fruitful to figure out, based initially on the current "system" syntax, how one would best go about expressing the information in a system block to facilitate GMM estimation. I mean, in general terms, in pseudo-code if you like -- not necessarily tied to the current syntax for a "gmm" block.
Yes, that's what I have in mind as well. I'm thinking of user input in terms of what's in section 34.2 of the guide: The wanted input is basically (i) a list of LHS series, then (ii) a lists array of the regressor lists for each equation. Finally, although this is not directly mentioned in 34.2 (it probably should be), but in the previous section 34.1 --albeit without an example--, (iii) a list of system-wide instruments, or (iiiB) alternatively perhaps a list of system-wide endogenous variables.
I think I'm getting there, though. As Jack mentioned elsewhere, writing a helper function that contains a loop circumvents some problems. (I had also tried to build a quasi-block-diagonal matrix with the diagcat function, which was then multiplied with a certain stacked coef vector, but that approach gave numerical issues and non-convergence. So the plain but outsourced loop-based variant is preferred now.)
OK, I'm attaching an updated prototype. Given the insight that a loop can be used if it's encapsulated in a function I went back to a series and list-based formulation. (And avoiding a loop proved basically impossible also when working with matrices.)
The user input in this example script is given by the two list types "sysLHS" and "allinst", plus an N-element lists array "sysRHSs". This should already be very close to the programmatic syntax for the system command. An alternative might be to let the user specify the endogenous variables instead of the instruments and back out the instrument list automatically.
What's missing in this prototype apart from moving everything into functions:
At least this example works for me.
cheers, sven
Nice job getting this working. I notice that the coefficient estimates are identical with equation-by-equation tsls. That's correct, of course, given the setup. But I wonder: how, under the GMM approach, would one go about constructing a true "system" estimator which takes into account covariance of the errors across the equations?
My more or less spontaneous reaction is that it should differ in the overidentified case, shouldn't it? In that sense this just-identified example is probably not optimal. And do I understand correctly that the weights matrix is updated automatically in the gmm block, generally speaking?
Exact identification is also an essential ingredient. Adding a randomly-generated instrument the equality vanishes.
I would have thought that iterated GMM would have done the trick, but I'm getting different results than 3-stage ls.
Hmm. I'm pretty confident about our one- and two-step GMM implementations, but not sure I'd go to the stake claiming our iterated GMM is irreproachable. Maybe something to look into.
Right now I cannot run the system comparison, but why should iterated GMM be exactly equivalent to 3sls?
I'm not sure if that's quite right (Jack?), but at any rate Davidson and MacKinnon are clear that 3sls can be represented as a GMM estimator.
Or did Jack mean to apply iteration for both?
Anyway, yet another incarnation is attached, where I changed the system to be overidentified, and the "system" block formulation is also added for comparison.
I'm never getting exactly identical estimates, but they (3sls, iterated or not, and iterated gmm) are close enough I think.
BTW, I guess the --two-step and --iterate options are mutually exclusive for gmm? But I'm not getting an error.
First of all, for the record, the --two-step and --iterate options are now officially incompatible and their combination is rejected by gretl.
Secondly, as a reminder for the future, the gmm approach to system estimation would especially be useful for nonlinear systems, because the other estimators aren't set up for this (in gretl).
OK, so what do we want to do with this now?
I guess the quickest way is write a user-contributed package. One could postpone a native implementation, I think.
The extension to non-linear systems would be nice, but may be less demanding now.
Your code, @svetosch, is a good starting point for a package. I've started to build a package based on it. The project can be found here:
https://github.com/atecon/gmmsys/tree/main/src
Relevant files are:
I can replicate Sven's example. I've also added many post-estimation statistics to the printout such as R² etc. as you will see. The package is not finished yet but hopefully a good starting point to progress.
If anyone is interested in developing the package jointly, I can "invite" you to the repo. Please let me know.
Best
Artur
Hi @atecon, sorry I didn't react to your message about a year ago -- yes please "invite" me officially to this github repo for gmmsys. thanks
Hi, I just sent you github invitation.
Sorry, I think I didn't react, and it's expired now - can you re-invite me? thanks