1 /*
2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef LIBGRETL_H
21 #define LIBGRETL_H
22 
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 
27 #ifdef HAVE_VASPRINTF
28 # define _GNU_SOURCE
29 # define _ISOC99_SOURCE
30 # include <stdio.h>
31 # undef _GNU_SOURCE
32 #else
33 # include <stdio.h>
34 #endif
35 
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <math.h>
39 #include <limits.h>
40 
41 #include <zlib.h>
42 
43 #define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_28
44 #define GLIB_VERSION_MAX_ALLOWED (GLIB_VERSION_CUR_STABLE)
45 #include <glib.h>
46 
47 #ifdef G_OS_WIN32
48 /* set our non-standard Windows identifier, if
49    it's not already defined by the compiler
50 */
51 # ifndef WIN32
52 #  define WIN32
53 # endif
54 #endif
55 
56 #ifdef FULL_XML_HEADERS
57 # include <libxml/xmlmemory.h>
58 # include <libxml/parser.h>
59 #endif
60 
61 #ifdef  __cplusplus
62 extern "C" {
63 #endif
64 
65 #ifdef WIN32
66 # ifndef isnan
67 #  define isnan(x) ((x) != (x))
68 # endif
69 #endif
70 
71 #define YMD_READ_FMT     "%d-%d-%d"
72 #define YMD_WRITE_FMT    "%d-%02d-%02d"
73 #define YMD_WRITE_Y2_FMT "%02d-%02d-%02d"
74 #define YMD_WRITE_Y4_FMT "%04d-%02d-%02d"
75 
76 #ifdef ENABLE_NLS
77 # include "libintl.h"
78 # include "locale.h"
79 # define gettext_noop(String) String
80 # define _(String) gettext (String)
81 # define N_(String) gettext_noop (String)
82 #else /* no NLS */
83 # define _(String)  ((char *) String)
84 # define N_(String) String
85 #endif /* NLS or not */
86 
87 #define MAXLINE 65536  /* maximum length of command line */
88 #define MAXLABEL  128  /* maximum length of descriptive labels for variables */
89 #define MAXLEN    512  /* max length of regular "long" strings */
90 #define MAXDISP    32  /* max length of "display names" for variables */
91 #define VNAMELEN   32  /* space allocated for var names (including termination) */
92 #define OBSLEN     16  /* space allocated for obs strings (including termination) */
93 
94 #ifndef M_PI
95 # define M_PI 3.1415926535897932384626432
96 #endif
97 
98 #ifndef M_2PI
99 # define M_2PI 6.2831853071795864769252864
100 #endif
101 
102 #define SQRT_2_PI     2.506628274631000502415765284811
103 #define LN_2_PI       1.837877066409345483560659472811
104 #define LN_SQRT_2_PI  0.918938533204672741780329736406
105 
106 #define PMAX_NOT_AVAILABLE 666
107 
108 /* numbers smaller than the given limit will print as zero */
109 #define screen_zero(x)  ((fabs(x) > 1.0e-13)? x : 0.0)
110 
111 typedef enum {
112     GRETL_TYPE_NONE,
113     GRETL_TYPE_BOOL,
114     GRETL_TYPE_INT,
115     GRETL_TYPE_UNSIGNED,
116     GRETL_TYPE_OBS,
117     GRETL_TYPE_LIST,
118     GRETL_TYPE_DOUBLE,
119     GRETL_TYPE_INT_ARRAY,
120     GRETL_TYPE_DOUBLE_ARRAY,
121     GRETL_TYPE_STRING,
122     GRETL_TYPE_CMPLX_ARRAY,
123     GRETL_TYPE_SERIES,
124     GRETL_TYPE_MATRIX,
125     GRETL_TYPE_STRUCT,
126     GRETL_TYPE_SCALAR_REF,
127     GRETL_TYPE_SERIES_REF,
128     GRETL_TYPE_MATRIX_REF,
129     GRETL_TYPE_STRING_REF,
130     GRETL_TYPE_LIST_REF,
131     GRETL_TYPE_USERIES,
132     GRETL_TYPE_DATE,
133     GRETL_TYPE_BUNDLE,
134     GRETL_TYPE_BUNDLE_REF,
135     GRETL_TYPE_ARRAY,
136     GRETL_TYPE_ARRAY_REF,
137     GRETL_TYPE_STRINGS,
138     GRETL_TYPE_MATRICES,
139     GRETL_TYPE_BUNDLES,
140     GRETL_TYPE_LISTS,
141     GRETL_TYPE_ARRAYS,
142     GRETL_TYPE_STRINGS_REF,
143     GRETL_TYPE_MATRICES_REF,
144     GRETL_TYPE_BUNDLES_REF,
145     GRETL_TYPE_LISTS_REF,
146     GRETL_TYPE_ARRAYS_REF,
147     GRETL_TYPE_VOID,
148     GRETL_TYPE_NUMERIC,
149     GRETL_TYPE_ANY
150 } GretlType;
151 
152 #define gretl_scalar_type(t) (t == GRETL_TYPE_BOOL ||	\
153                               t == GRETL_TYPE_INT ||	\
154 			      t == GRETL_TYPE_UNSIGNED || \
155 			      t == GRETL_TYPE_OBS ||	\
156 			      t == GRETL_TYPE_DOUBLE)
157 
158 #define gretl_ref_type(t) (t == GRETL_TYPE_SCALAR_REF ||   \
159 	                   t == GRETL_TYPE_SERIES_REF ||   \
160 	                   t == GRETL_TYPE_MATRIX_REF ||   \
161 	                   t == GRETL_TYPE_BUNDLE_REF ||   \
162 			   t == GRETL_TYPE_STRING_REF ||   \
163 			   t == GRETL_TYPE_STRINGS_REF ||  \
164 			   t == GRETL_TYPE_MATRICES_REF || \
165 			   t == GRETL_TYPE_BUNDLES_REF || \
166 			   t == GRETL_TYPE_LISTS_REF || \
167 			   t == GRETL_TYPE_ARRAYS_REF)
168 
169 #define gretl_array_type(t) (t == GRETL_TYPE_STRINGS ||  \
170 			     t == GRETL_TYPE_MATRICES || \
171 			     t == GRETL_TYPE_BUNDLES || \
172 			     t == GRETL_TYPE_LISTS || \
173 			     t == GRETL_TYPE_ARRAYS)
174 
175 enum ts_codes {
176     CROSS_SECTION,
177     TIME_SERIES,
178     STACKED_TIME_SERIES,
179     STACKED_CROSS_SECTION,
180     PANEL_UNKNOWN,
181     SPECIAL_TIME_SERIES,
182     STRUCTURE_UNKNOWN
183 };
184 
185 enum progress_flags {
186     SP_NONE,
187     SP_TOTAL,
188     SP_LOAD_INIT,
189     SP_SAVE_INIT,
190     SP_FONT_INIT,
191     SP_UPDATER_INIT,
192     SP_FINISH
193 };
194 
195 enum progress_return_flags {
196     SP_RETURN_OK,
197     SP_RETURN_DONE,
198     SP_RETURN_CANCELED
199 };
200 
201 enum test_stats {
202     GRETL_STAT_NONE,
203     GRETL_STAT_NORMAL_CHISQ,
204     GRETL_STAT_LM,
205     GRETL_STAT_F,
206     GRETL_STAT_LMF,
207     GRETL_STAT_HARVEY_COLLIER,
208     GRETL_STAT_RESET,
209     GRETL_STAT_LR,
210     GRETL_STAT_WALD_CHISQ,
211     GRETL_STAT_SUP_WALD,
212     GRETL_STAT_Z,
213     GRETL_STAT_STUDENT,
214     GRETL_STAT_LB_CHISQ,
215     GRETL_STAT_WF
216 };
217 
218 typedef enum {
219     OPT_NONE = 0,
220     OPT_A = 1 <<  0,
221     OPT_B = 1 <<  1,
222     OPT_C = 1 <<  2,
223     OPT_D = 1 <<  3,
224     OPT_E = 1 <<  4,
225     OPT_F = 1 <<  5,
226     OPT_G = 1 <<  6,
227     OPT_H = 1 <<  7,
228     OPT_I = 1 <<  8,
229     OPT_J = 1 <<  9,
230     OPT_K = 1 << 10,
231     OPT_L = 1 << 11,
232     OPT_M = 1 << 12,
233     OPT_N = 1 << 13,
234     OPT_O = 1 << 14,
235     OPT_P = 1 << 15,
236     OPT_Q = 1 << 16,
237     OPT_R = 1 << 17,
238     OPT_S = 1 << 18,
239     OPT_T = 1 << 19,
240     OPT_U = 1 << 20,
241     OPT_V = 1 << 21,
242     OPT_W = 1 << 22,
243     OPT_X = 1 << 23,
244     OPT_Z = 1 << 24,
245     OPT_Y = 1 << 25,
246     OPT_UNSET = 1 << 30
247 } gretlopt;
248 
249 typedef enum {
250     OP_EQ  = '=',
251     OP_GT  = '>',
252     OP_LT  = '<',
253     OP_NEQ = 21,
254     OP_GTE = 22,
255     OP_LTE = 23
256 } GretlOp;
257 
258 typedef enum {
259     C_AIC,
260     C_BIC,
261     C_HQC,
262     C_MAX
263 } ModelSelCriteria;
264 
265 typedef enum {
266     FC_STATIC,
267     FC_DYNAMIC,
268     FC_AUTO,
269     FC_KSTEP
270 } ForecastMethod;
271 
272 #ifndef CMPLX
273 typedef struct _cmplx {
274     double r;
275     double i;
276 } cmplx;
277 #endif
278 
279 typedef struct GRETL_VAR_ GRETL_VAR;
280 typedef struct _FITRESID FITRESID;
281 
282 typedef struct model_data_item_ model_data_item;
283 typedef struct ModelTest_ ModelTest;
284 typedef struct equation_system_ equation_system;
285 
286 typedef struct gretl_bundle_ gretl_bundle;
287 typedef struct gretl_array_ gretl_array;
288 typedef struct gretl_string_table_ gretl_string_table;
289 
290 /**
291  * VARINFO:
292  *
293  * Holds extended private information on an individual data series.
294  */
295 
296 typedef struct VARINFO_ VARINFO;
297 
298 /* information on data set */
299 typedef struct DATASET_ {
300     int v;              /* number of variables */
301     int n;              /* number of observations */
302     int pd;             /* periodicity or frequency of data */
303     int structure;      /* time series, cross section or whatever */
304     double sd0;         /* float representation of stobs */
305     int t1, t2;         /* start and end of current sample */
306     char stobs[OBSLEN];  /* string representation of starting obs (date) */
307     char endobs[OBSLEN]; /* string representation of ending obs */
308     double **Z;         /* data array */
309     char **varname;     /* array of names of variables */
310     VARINFO **varinfo;  /* array of specific info on vars */
311     char markers;       /* NO_MARKERS (0), REGULAR MARKERS or DAILY_DATE_STRINGS */
312     char modflag;       /* binary flag for dataset modified or not */
313     char **S;           /* to hold observation markers */
314     char *descrip;      /* to hold info on data sources etc. */
315     char *submask;      /* subsampling mask */
316     char *restriction;  /* record of sub-sampling restriction */
317     char *padmask;      /* record of padding to re-balance panel data */
318     char *mapfile;      /* name of associated map (polygons) file, if any */
319     unsigned int rseed; /* resampling seed */
320     int auxiliary;      /* = 0 for regular dataset, 1 for aux dataset */
321     char *pangrps;      /* panel-only: name of series holding group names */
322     int panel_pd;       /* panel-only: panel time-series frequency */
323     double panel_sd0;   /* panel-only: time-series start */
324 } DATASET;
325 
326 typedef struct VMatrix_ {
327     int ci;
328     int dim;
329     int t1, t2, n;
330     char **names;
331     double *vec;
332     double *xbar;
333     double *ssx;
334     int *list;
335     int missing;
336 } VMatrix;
337 
338 typedef struct SAMPLE_ {
339     int t1;
340     int t2;
341     unsigned int rseed;
342 } SAMPLE;
343 
344 typedef struct ARINFO_ {
345     int *arlist;          /* list of autoreg lags */
346     double *rho;          /* array of autoreg. coeffs. */
347     double *sderr;        /* and their standard errors */
348 } ARINFO;
349 
350 /* struct to hold model results */
351 typedef struct MODEL_ {
352     int ID;                      /* ID number for model */
353     int refcount;                /* for saving/deleting */
354     int ci;                      /* "command index" -- estimation method */
355     gretlopt opt;                /* record of options */
356     int t1, t2, nobs;            /* starting observation, ending
357                                     observation, and number of obs */
358     char *submask;               /* keep track of sub-sample in force
359                                     when model was estimated */
360     char *missmask;              /* missing observations mask */
361     SAMPLE smpl;                 /* numeric start and end of current sample
362                                     when model was estimated */
363     int full_n;                  /* full length of dataset on estimation */
364     int ncoeff, dfn, dfd;        /* number of coefficents; degrees of
365                                     freedom in numerator and denominator */
366     int *list;                   /* list of variables by ID number */
367     int ifc;                     /* = 1 if the equation includes a constant,
368 				    else = 0 */
369     int nwt;                     /* ID number of the weight variable (WLS) */
370     int aux;                     /* code representing the sort of
371 				    auxiliary regression this is (or not) */
372     double *coeff;               /* array of coefficient estimates */
373     double *sderr;               /* array of estimated std. errors */
374     double *uhat;                /* regression residuals */
375     double *yhat;                /* fitted values from regression */
376     double *xpx;                 /* X'X matrix, in packed form */
377     double *vcv;                 /* VCV matrix for coefficient estimates */
378     double ess, tss;             /* Error and Total Sums of Squares */
379     double sigma;                /* Standard error of regression */
380     double rsq, adjrsq;          /* Unadjusted and adjusted R^2 */
381     double fstt;                 /* overall F-statistic */
382     double chisq;                /* overall chi-square statistic */
383     double lnL;                  /* log-likelihood */
384     double ybar, sdy;            /* mean and std. dev. of dependent var. */
385     double criterion[C_MAX];     /* array of model selection statistics */
386     double dw, rho;              /* Durbin-Watson stat. and estimated 1st
387 				    order autocorrelation coefficient */
388     ARINFO *arinfo;              /* pointer to struct to hold special info for
389 				    autoregressive model */
390     int errcode;                 /* Error code in case of failure */
391     char *name;                  /* for use in GUI */
392     char *depvar;                /* name of dependent var in special cases */
393     int nparams;                 /* number of named model parameters */
394     char **params;               /* for named model parameters */
395     gint64 esttime;              /* time of estimation */
396     int ntests;                  /* number of attached test results */
397     ModelTest *tests;            /* attached hypothesis test results */
398     DATASET *dataset;            /* for handling models estimated on a
399 				    sub-sampled portion of the dataset */
400     int n_data_items;            /* number of extra data items */
401     model_data_item **data_items; /* pointer to additional data */
402 } MODEL;
403 
404 #include "gretl_commands.h"
405 #include "gretl_prn.h"
406 #include "gretl_errors.h"
407 #include "interact.h"
408 #include "dataset.h"
409 #include "estimate.h"
410 #include "genmain.h"
411 #include "genfuncs.h"
412 #include "compare.h"
413 #include "gretl_bundle.h"
414 #include "gretl_array.h"
415 #include "gretl_intl.h"
416 #include "gretl_list.h"
417 #include "gretl_paths.h"
418 #include "gretl_utils.h"
419 #include "gretl_model.h"
420 #include "pvalues.h"
421 #include "dataio.h"
422 #include "gretl_data_io.h"
423 #include "strutils.h"
424 #include "describe.h"
425 #include "printout.h"
426 #include "printscan.h"
427 #include "modelprint.h"
428 #include "graphing.h"
429 #include "random.h"
430 #include "nonparam.h"
431 #include "options.h"
432 #include "discrete.h"
433 #include "adf_kpss.h"
434 #include "subsample.h"
435 #include "calendar.h"
436 #include "plugins.h"
437 #include "nls.h"
438 #include "missing.h"
439 #include "transforms.h"
440 
441 #ifdef  __cplusplus
442 }
443 #endif
444 
445 #endif /* LIBGRETL_H */
446