On Tue, 8 Jan 2013, Allin Cottrell wrote:
On Tue, 8 Jan 2013, Marcin Błażejowski wrote:
> This is maybe a little OT, but we've talked about it at the 1st and later
> at the 2nd Gretl Conference: documentation the C code. We agreeded
> (togeather with Jack) that I would start from very early pieces of C and...
> Allin I asked you last year: where to start?
I'd say a good starting point is the big "switch" statement in
the function gretl_cmd_exec(), in lib/src/interact.c. That's the jumping-off
point for the code that implements most gretl commands.
Roughly, here's how the C source is organised: the most important
subdirectory is "lib/src", which contains the source to the dynamic
library. Then you have "plugin", where you find all the stuff that is not
contained in the library, but is used by the clients: mainly advanced
statistical methods (like eg vecm or dpanel), but also other things, like
the importers from foreign file formats. Then, in the "cli" and "gui2"
directories, you have the source for the CLI and GUI clients,
respectively.
Inside the lib/src/ subdirectory, as Allin said, the first thing to look
at is probably interact.c and the function gretl_cmd_exec(): the main idea
of both the CLI and GUI clients is that they run a huge endless loop which
examines user input and translates it into gretl "actions" as appropriate.
So for example the command "nulldata", given at the CLI or GUI console, or
the action "File > New data set" in the GUI client translate into the
NULLDATA internal gretl command. You'll find a complete list of such
commands in the file lib/src/gretl_commands.h.
Of course, the most important command of all is "genr", so there's a good
deal of C code which deals exclusively with it. If you want to have a look
at how things are done in that department, be aware that it's one of the
most complicated parts of the gretl source, and it's not useless to have
an idea of how the main techniques for lexing/parsing/evaluating
expressions work in compilers. Basically, however, the "lexer" (genlex.c)
translates a string like, say, "scalar a = exp(2+1)" into a "parse
tree",
which is then "evaluated" by the eval() function (geneval.c), which is
typically called recursively and calls the number-crunching functions
(which may be in geneval.c itself, or, in other cases, in dedicated files
like genfuncs.c).
As for estimation techniques, something that is probably useful to get you
started is that practically all of them work by filling up a container
called a MODEL. The description of what a MODEL is can be found in
lib/src/libgretl.h at about line 300, but suffice it to say that it's not
unlike a hansl bundle, that you fill up with numbers as you compute them.
So for example, when you estimate a probit model, you first set up an
empty MODEL, then you fill up the "coeff" array with the estimated
coefficients, etcetera. Once the container is properly filled with all the
good stuff, it gets printed and is put somewhere (the "stack") where
the client program can find it if needed.
Actually, probit/logit models are a nice place to start to get the hang of
how things are typically done: you'll find those in the function
binary_model() in lib/src/discrete.c (don't start from OLS: the code in
there is decidedly harder to follow; but if you have to, look at ar1_lsq()
in lib/src/estimate.c).
Sorry for the long message, but you asked for it! :)
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------