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 EQUATION_SYSTEM_H
21 #define EQUATION_SYSTEM_H
22 
23 typedef enum {
24     SYS_METHOD_SUR = 0,
25     SYS_METHOD_3SLS,
26     SYS_METHOD_FIML,
27     SYS_METHOD_LIML,
28     SYS_METHOD_OLS,
29     SYS_METHOD_TSLS,
30     SYS_METHOD_WLS,
31     SYS_METHOD_MAX
32 } GretlSystemMethods;
33 
34 enum {
35     /* note: allow for obsoleted flags in saved sessions --
36        treat this enumeration as append-only
37     */
38     SYSTEM_DFCORR      = 1 << 2,
39     SYSTEM_VCV_GEOMEAN = 1 << 3,
40     SYSTEM_RESTRICT    = 1 << 4,
41     SYSTEM_ITERATE     = 1 << 5,
42     SYSTEM_SAVEIT      = 1 << 6,
43     SYSTEM_LIML1       = 1 << 7,
44     SYSTEM_QUIET       = 1 << 8,
45     SYSTEM_ROBUST      = 1 << 9
46 };
47 
48 typedef struct id_atom_ id_atom;
49 typedef struct identity_ identity;
50 typedef struct predet_ predet;
51 typedef struct liml_data_ liml_data;
52 
53 struct equation_system_ {
54     char *name;                 /* user-specified name for system, or NULL */
55     int refcount;               /* for saving/deleting */
56     int fd;                     /* function execution depth */
57     int t1;                     /* starting observation number */
58     int t2;                     /* ending observation number */
59     int smpl_t1;                /* first obs in sample range */
60     int smpl_t2;                /* last obs in sample range */
61     int T;                      /* number of observations per equation */
62     int df;                     /* T - average coeffs per equation */
63     int method;                 /* estimation method */
64     int neqns;                  /* number of stochastic equations */
65     int nidents;                /* number of identities */
66     int order;                  /* max lag of endogenous variable */
67     int iters;                  /* number of iterations taken */
68     int flags;                  /* to record options */
69     double ll;                  /* log-likelihood (restricted) */
70     double llu;                 /* unrestricted log-likelihood */
71     double X2;                  /* chi-square test value */
72     double ess;                 /* total error sum of squares */
73     double diag_test;           /* test stat for diagonal covariance matrix */
74     double bdiff;               /* summary stat for change in coefficients */
75     double ldet;                /* log-determinant of covariance matrix */
76     int **lists;                /* regression lists for stochastic equations */
77     int **tslists;              /* back-up of TSLS-style lists */
78     int *ylist;                 /* list of endogenous variables */
79     int *ilist;                 /* list of instruments */
80     int *xlist;                 /* list of truly exogenous variables */
81     int *plist;                 /* list of predetermined variables */
82     int *biglist;               /* list of all variables, for data checking */
83     predet *pre_vars;           /* array of info on predetermined regressors */
84     identity **idents;          /* set of identities */
85     gretl_matrix *b;            /* coefficient estimates */
86     gretl_matrix *vcv;          /* covariance matrix of coefficients */
87     gretl_matrix *S;            /* cross-equation covariance matrix */
88     gretl_matrix *R;            /* LHS of any linear restrictions */
89     gretl_matrix *q;            /* RHS of any linear restrictions */
90     gretl_matrix *E;            /* residuals, all equations */
91     gretl_matrix *yhat;         /* fitted values, all equations */
92     gretl_matrix *Gamma;        /* structural form Gamma matrix (endog + identities)*/
93     gretl_matrix *B;            /* structural form B matrix (exogenous) */
94     gretl_matrix *A;            /* structural form A matrix (lagged endogenous) */
95     gretl_matrix *F;            /* forecast matrix */
96     gretl_matrix *Sr;           /* reduced-form error covariance matrix */
97     MODEL **models;             /* set of pointers to per-equation models */
98     liml_data *ldata;           /* extra info from LIML estimation */
99 };
100 
101 equation_system *equation_system_start (const char *param,
102 					const char *name,
103 					gretlopt opt,
104 					int *err);
105 
106 char *get_system_name_from_line (const char *s);
107 
108 equation_system *get_anonymous_equation_system (void);
109 
110 int equation_system_append (equation_system *sys, const int *list);
111 
112 int equation_system_append_multi (equation_system *sys,
113 				  const char *parm1,
114 				  const char *parm2,
115 				  const DATASET *dset);
116 
117 int system_parse_line (equation_system *sys,
118 		       const char *line,
119 		       DATASET *dset);
120 
121 int equation_system_finalize (equation_system *sys,
122 			      DATASET *dset,
123 			      gretlopt opt, PRN *prn);
124 
125 int
126 equation_system_estimate (equation_system *sys,
127 			  DATASET *dset,
128 			  gretlopt opt, PRN *prn);
129 
130 int estimate_named_system (const char *sysname,
131 			   const char *param,
132 			   DATASET *dset,
133 			   gretlopt opt, PRN *prn);
134 
135 void equation_system_destroy (equation_system *sys);
136 
137 void delete_anonymous_equation_system (int level);
138 
139 int system_want_df_corr (const equation_system *sys);
140 
141 int system_n_restrictions (const equation_system *sys);
142 
143 int system_max_indep_vars (const equation_system *sys);
144 
145 int system_n_indep_vars (const equation_system *sys);
146 
147 int *system_get_list (const equation_system *sys, int i);
148 
149 int system_get_list_length (const equation_system *sys, int i);
150 
151 int *compose_ivreg_list (const equation_system *sys, int i);
152 
153 int system_get_depvar (const equation_system *sys, int i);
154 
155 const char *system_short_string (const MODEL *pmod);
156 
157 void equation_system_set_name (equation_system *sys, const char *name);
158 
159 int system_method_from_string (const char *s);
160 const char *system_method_full_string (int method);
161 const char *system_method_short_string (int method);
162 
163 int *system_get_endog_vars (const equation_system *sys);
164 int *system_get_instr_vars (const equation_system *sys);
165 
166 void system_attach_uhat (equation_system *sys, gretl_matrix *E);
167 
168 void system_attach_sigma (equation_system *sys, gretl_matrix *S);
169 
170 void system_attach_coeffs (equation_system *sys, gretl_matrix *b);
171 void system_attach_vcv (equation_system *sys, gretl_matrix *vcv);
172 
173 MODEL *system_get_model (const equation_system *sys, int i);
174 
175 int system_get_overid_df (const equation_system *sys);
176 
177 int system_vcv_geomean (const equation_system *sys);
178 double system_vcv_denom (const equation_system *sys,
179 			 int i, int j);
180 
181 int rhs_var_in_identity (const equation_system *sys,
182 			 int lhsvar, int rhsvar);
183 
184 void
185 print_equation_system_info (const equation_system *sys,
186 			    const DATASET *dset,
187 			    gretlopt opt, PRN *prn);
188 
189 void
190 system_set_restriction_matrices (equation_system *sys,
191 				 gretl_matrix *R,
192 				 gretl_matrix *q);
193 
194 int
195 system_normality_test (const equation_system *sys,
196 		       gretlopt opt, PRN *prn);
197 
198 int multi_eqn_wald_test (const gretl_matrix *b,
199 			 const gretl_matrix *V,
200 			 const gretl_matrix *R,
201 			 const gretl_matrix *q,
202 			 int dfu, gretlopt opt,
203 			 PRN *prn);
204 
205 int system_wald_test (const equation_system *sys,
206 		      const gretl_matrix *R,
207 		      const gretl_matrix *q,
208 		      gretlopt opt,
209 		      PRN *prn);
210 
211 int system_diag_test (const equation_system *sys,
212 		      double *test, double *pval);
213 
214 double *system_get_resid_series (equation_system *sys, int eqnum,
215 				 DATASET *dset, int *err);
216 
217 double *
218 equation_system_get_series (const equation_system *sys,
219 			    const DATASET *dset,
220 			    int idx, const char *key,
221 			    int *err);
222 
223 gretl_matrix *
224 equation_system_get_matrix (const equation_system *sys, int idx,
225 			    int *err);
226 
227 int highest_numbered_var_in_system (const equation_system *sys,
228 				    const DATASET *dset);
229 
230 int equation_system_serialize (equation_system *sys,
231 			       SavedObjectFlags flags,
232 			       PRN *prn);
233 
234 int equation_system_bundlize (equation_system *sys,
235 			      gretl_bundle *b);
236 
237 int gretl_system_print (equation_system *sys,
238 			const DATASET *dset,
239 			gretlopt opt, PRN *prn);
240 
241 int system_print_sigma (const equation_system *sys, PRN *prn);
242 
243 const gretl_matrix *
244 system_get_forecast_matrix (equation_system *sys, int t1, int t2,
245 			    DATASET *dset, gretlopt opt,
246 			    int *err);
247 
248 gretl_matrix *sys_get_fitted_values (equation_system *sys,
249 				     int v, int t1, int t2,
250 				     const DATASET *dset,
251 				     int *err);
252 
253 int system_adjust_t1t2 (equation_system *sys,
254 			const DATASET *dset);
255 
256 int system_supports_method (equation_system *sys, int method);
257 
258 #ifdef FULL_XML_HEADERS
259 
260 equation_system *equation_system_from_XML (xmlNodePtr node,
261 					   xmlDocPtr doc,
262 					   int *err);
263 
264 #endif
265 
266 int
267 system_save_and_print_results (equation_system *sys,
268 			       DATASET *dset, gretlopt opt,
269 			       PRN *prn);
270 
271 int system_autocorrelation_test (equation_system *sys, int order,
272 				 gretlopt opt, PRN *prn);
273 
274 int system_arch_test (equation_system *sys, int order,
275 		      gretlopt opt, PRN *prn);
276 
277 MODEL single_equation_liml (const int *list, DATASET *dset,
278 			    gretlopt opt);
279 
280 int gretl_system_get_sample (const equation_system *sys,
281 			     int *t1, int *t2);
282 
283 #endif /* EQUATION_SYSTEM_H */
284