File: sysest.c

package info (click to toggle)
gretl 2025b-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 64,984 kB
  • sloc: ansic: 426,435; sh: 4,916; makefile: 3,257; cpp: 2,777; xml: 610; perl: 364
file content (1420 lines) | stat: -rw-r--r-- 38,362 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
/*
 *  gretl -- Gnu Regression, Econometrics and Time-series Library
 *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "libgretl.h"
#include "version.h"
#include "gretl_matrix.h"
#include "libset.h"
#include "system.h"
#include "tsls.h"
#include "sysml.h"

#define SDEBUG 0

#define sys_ols_ok(s) (s->method == SYS_METHOD_SUR || \
                       s->method == SYS_METHOD_OLS || \
                       s->method == SYS_METHOD_WLS)

/* Insert the elements of sub-matrix @M, multiplied by @scale, in the
   appropriate position within the big matrix @X. We're building
   a symmetric matrix here so we insert off-diagonal elements both
   above and below the diagonal.
*/

static void
insert_sys_X_block (gretl_matrix *X, const gretl_matrix *M,
                    int startrow, int startcol, double scale)
{
    int r, c, i, j;
    double mij;

    for (i=0; i<M->rows; i++) {
        r = startrow + i;
        for (j=0; j<M->cols; j++) {
            c = startcol + j;
            mij = gretl_matrix_get(M, i, j);
            gretl_matrix_set(X, r, c, mij * scale);
        }
    }

    if (startrow != startcol) {
        for (i=0; i<M->rows; i++) {
            c = startrow + i;
            for (j=0; j<M->cols; j++) {
                r = startcol + j;
                mij = gretl_matrix_get(M, i, j);
                gretl_matrix_set(X, r, c, mij * scale);
            }
        }
    }
}

/* Retrieve the data placed on @pmod in the course of 2sls estimation:
   for endogenous regressors these values are the first-stage fitted
   values, otherwise they're just the original data.  Note that @endog
   is a mask with nonzero values identifying the endogenous
   regressors, and the special array @X contains a column for each
   endogenous variable.
*/

double *model_get_Xi (const MODEL *pmod, DATASET *dset, int i)
{
    gretl_matrix *endog = gretl_model_get_data(pmod, "endog");
    double *xi = NULL;

    if (endog == NULL || endog->val[i] == 0) {
        /* use the original data */
        xi = dset->Z[pmod->list[i+2]];
    } else {
        double **X = gretl_model_get_data(pmod, "tslsX");

        if (X != NULL) {
            /* find and return the correct column of X */
            int j, k = 0;

            for (j=0; j<i; j++) {
                if (endog->val[j] != 0) k++;
            }
            xi = X[k];
        }
    }

    return xi;
}

/* retrieve the special k-class data wanted for LIML estimation: these
   represent a further transformation of the data placed on the model
   via 2sls estimation.
*/

static int make_liml_X_block (gretl_matrix *X, const MODEL *pmod,
                              DATASET *dset, int t1)
{
    const double *Xi;
    int i, t, err = 0;

    X->cols = pmod->ncoeff;

    for (i=0; i<X->cols && !err; i++) {
        Xi = model_get_Xi(pmod, dset, i);
        if (Xi == NULL) {
            err = E_DATA;
        } else {
            for (t=0; t<X->rows; t++) {
                gretl_matrix_set(X, t, i, Xi[t+t1]);
            }
        }
    }

    return err;
}

/* construct the X data block pertaining to a specific equation, using
   either the original data or fitted values from regression on a set
   of instruments
*/

static int
make_sys_X_block (gretl_matrix *X, const MODEL *pmod,
                  DATASET *dset, int t1, int method)
{
    const double *Xi;
    int i, t, err = 0;

    X->cols = pmod->ncoeff;

    for (i=0; i<X->cols && !err; i++) {
        if (method == SYS_METHOD_3SLS ||
            method == SYS_METHOD_FIML ||
            method == SYS_METHOD_TSLS) {
            Xi = model_get_Xi(pmod, dset, i);
        } else {
            Xi = dset->Z[pmod->list[i+2]];
        }
        if (Xi == NULL) {
            err = E_DATA;
        } else {
            for (t=0; t<X->rows; t++) {
                gretl_matrix_set(X, t, i, Xi[t+t1]);
            }
        }
    }

    return err;
}

/* Populate the cross-equation covariance matrix, @S, based on the
   per-equation residuals: if @do_diag is non-zero we also want to compute
   the Breusch-Pagan test for diagonality of the matrix. For the robust
   variant see Halunga, Orme and Yamagata, "A heteroskasticity robust
   Breusch-Pagan test for contemporaneous correlation...", Journal of
   Econometrics, 198 (2017) pp. 209-230.
*/

static int
gls_sigma_from_uhat (equation_system *sys, gretl_matrix *S,
                     int do_diag)
{
    int geomean = system_vcv_geomean(sys);
    double *rdenom = NULL;
    double eti, etj, sij, dij;
    int m = sys->neqns;
    int robust = 0;
    int i, j, t, k;

    if (do_diag && (sys->flags & SYSTEM_ROBUST)) {
        /* denominator of robust B-P test */
        rdenom = malloc((m * m - m) / 2 * sizeof *rdenom);
        if (rdenom != NULL) {
            robust = 1;
        }
    }

    k = 0;
    for (i=0; i<m; i++) {
        for (j=i; j<m; j++) {
            sij = dij = 0.0;
            for (t=0; t<sys->T; t++) {
                eti = gretl_matrix_get(sys->E, t, i);
                etj = gretl_matrix_get(sys->E, t, j);
                /* increment sum of cross-products */
                sij += eti * etj;
                if (i != j && robust) {
                    /* also sum of cross-products of squares */
                    dij += eti * etj * eti * etj;
                }
            }
            if (i != j && robust) {
                rdenom[k++] = dij;
            }
            gretl_matrix_set(S, i, j, sij);
            if (j != i) {
                gretl_matrix_set(S, j, i, sij);
            }
        }
    }
    if (do_diag) {
        /* compute B-P test statistic */
        double sii, sjj;

        sys->diag_test = 0.0;

        k = 0;
        for (i=0; i<m-1; i++) {
            sii = gretl_matrix_get(S, i, i);
            for (j=i+1; j<m; j++) {
                sij = gretl_matrix_get(S, i, j);
                sjj = gretl_matrix_get(S, j, j);
                if (robust) {
                    /* cumulate \hat{gamma}_{ij}^2 */
                    sys->diag_test += (sij * sij) / rdenom[k++];
                } else {
                    /* cumulate \hat{rho}_{ij}^2 */
                    sys->diag_test += (sij * sij) / (sii * sjj);
                }
            }
        }
        if (robust) {
            free(rdenom);
        } else {
            /* supply the missing factor of T */
            sys->diag_test *= sys->T;
        }
    }

    /* scale the S matrix */
    if (geomean) {
        for (j=0; j<S->cols; j++) {
            for (i=j; i<S->rows; i++) {
                sij = gretl_matrix_get(S, i, j);
                sij /= system_vcv_denom(sys, i, j);
                gretl_matrix_set(S, i, j, sij);
                if (i != j) {
                    gretl_matrix_set(S, j, i, sij);
                }
            }
        }
    } else {
        gretl_matrix_divide_by_scalar(S, sys->T);
    }

    return 0;
}

static void maybe_correct_model_dfd (equation_system *sys,
                                     MODEL *pmod)
{
    int nr = system_n_restrictions(sys);

    if (nr > 0) {
        /* adjust the df correction for the number of
           restrictions, averaged across the equations
        */
        double avr = nr / (double) sys->neqns;

        pmod->dfd = sys->T - pmod->ncoeff + floor(avr);
    }
}

/* Compute residuals and related quantities, for all cases
   other than FIML
*/

static void
sys_resids (equation_system *sys, int eq, const DATASET *dset)
{
    MODEL *pmod = sys->models[eq];
    int yno = pmod->list[1];
    double yh;
    int i, t;

    pmod->ess = 0.0;

    for (t=pmod->t1; t<=pmod->t2; t++) {
        yh = 0.0;
        for (i=0; i<pmod->ncoeff; i++) {
            yh += pmod->coeff[i] * dset->Z[pmod->list[i+2]][t];
        }
        pmod->yhat[t] = yh;
        pmod->uhat[t] = dset->Z[yno][t] - yh;
        /* for cross-equation vcv */
        gretl_matrix_set(sys->E, t - pmod->t1, pmod->ID, pmod->uhat[t]);
        pmod->ess += pmod->uhat[t] * pmod->uhat[t];
    }

    /* df correction? */
    if (system_want_df_corr(sys)) {
        maybe_correct_model_dfd(sys, pmod);
        pmod->sigma = sqrt(pmod->ess / pmod->dfd);
    } else {
        pmod->sigma = sqrt(pmod->ess / pmod->nobs);
    }

    if (pmod->ifc && pmod->tss > 0) {
        /* R-squared based on actual-fitted correlation */
        double r;

        pmod->rsq = gretl_corr_rsq(pmod->t1, pmod->t2, dset->Z[yno],
                                   pmod->yhat);
        r = 1 - pmod->rsq;
        pmod->adjrsq = 1.0 - (r * (pmod->nobs - 1) / pmod->dfd);
    } else {
        pmod->rsq = pmod->adjrsq = NADBL;
    }
}

/* use the per-equation residual standard deviations for scaling
   the covariance matrix, block by diagonal block
*/

static void
single_eqn_scale_vcv (MODEL *pmod, gretl_matrix *V, int offset)
{
    double vij, s2 = pmod->sigma * pmod->sigma;
    int i, j, ii, jj;

    for (i=0; i<pmod->ncoeff; i++) {
        ii = i + offset;
        for (j=i; j<pmod->ncoeff; j++) {
            jj = j + offset;
            vij = gretl_matrix_get(V, ii, jj);
            vij *= s2;
            gretl_matrix_set(V, ii, jj, vij);
            gretl_matrix_set(V, jj, ii, vij);
        }
    }
}

/* write results from system estimation into the individual
   model structs
*/

static void transcribe_sys_results (equation_system *sys,
                                    const DATASET *dset,
                                    int do_iters)
{
    int do_bdiff = (sys->method == SYS_METHOD_3SLS && do_iters);
    double bij, oldb, bnum = 0, bden = 0;
    int offset = 0;
    int i, j, k;

    for (i=0; i<sys->neqns; i++) {
        MODEL *pmod = sys->models[i];

        /* update the coefficients */
        for (j=0; j<pmod->ncoeff; j++) {
            k = j + offset;
            bij = gretl_vector_get(sys->b, k);
            if (do_bdiff) {
                oldb = pmod->coeff[j];
                bnum += (bij - oldb) * (bij - oldb);
                bden += oldb * oldb;
            }
            pmod->coeff[j] = bij;
        }
        /* update residuals (and sigma-hat) */
        sys_resids(sys, i, dset);
        /* for single-equation methods, vcv needs scaling by
           an estimate of the residual variance
        */
        if (sys->method == SYS_METHOD_OLS ||
            sys->method == SYS_METHOD_TSLS ||
            sys->method == SYS_METHOD_LIML) {
            single_eqn_scale_vcv(pmod, sys->vcv, offset);
        }
        /* update standard errors */
        for (j=0; j<pmod->ncoeff; j++) {
            k = j + offset;
            pmod->sderr[j] = sqrt(gretl_matrix_get(sys->vcv, k, k));
        }
        offset += pmod->ncoeff;
    }

    if (do_bdiff) {
        sys->bdiff = sqrt(bnum / bden);
    }
}

/* compute SUR, 3SLS or LIML parameter estimates (or restricted OLS,
   TSLS, WLS)
*/

static int
calculate_sys_coeffs (equation_system *sys, const DATASET *dset,
                      gretl_matrix *X, gretl_matrix *y,
                      int mk, int nr, int do_iters)
{
    gretl_matrix *V;
    int posdef = 0;
    int err = 0;

    V = gretl_matrix_copy(X);
    if (V == NULL) {
        return E_ALLOC;
    }

#if SDEBUG
    gretl_matrix_print(X, "sys X");
    gretl_matrix_print(y, "sys y");
#endif

    /* calculate the coefficients */

    if (sys->R == NULL) {
        /* hopefully X may be positive definite */
        err = gretl_cholesky_decomp_solve(X, y);
        if (!err) {
            posdef = 1;
            err = gretl_cholesky_invert(X);
        } else {
            /* nope: fall back to the LU solver */
            gretl_matrix_copy_values(X, V);
            err = 0;
        }
    }

    if (!posdef) {
        err = gretl_LU_solve_invert(X, y);
    }

    if (!err) {
        /* save the coefficient vector and covariance matrix */
        gretl_matrix *b = gretl_matrix_copy(y);

        if (b == NULL) {
            err = E_ALLOC;
        } else {
            system_attach_coeffs(sys, b);
            gretl_matrix_copy_values(V, X);
            system_attach_vcv(sys, V);
            /* transcribe stuff to the included models */
            transcribe_sys_results(sys, dset, do_iters);
        }
    }

    if (err) {
        gretl_matrix_free(V);
    }

    return err;
}

static int *
system_model_list (equation_system *sys, int i, int *freeit)
{
    int *list = NULL;

    *freeit = 0;

    if (sys_ols_ok(sys)) {
        return system_get_list(sys, i);
    }

    if (sys->method != SYS_METHOD_FIML) {
        list = system_get_list(sys, i);
    }

    if (sys->method == SYS_METHOD_3SLS ||
        sys->method == SYS_METHOD_TSLS ||
        sys->method == SYS_METHOD_LIML) {
        /* Is the list already in ivreg form?
           If not, ignore it. */
        if (list != NULL && !gretl_list_has_separator(list)) {
            list = NULL;
        }
    }

    if ((sys->method == SYS_METHOD_FIML ||
         sys->method == SYS_METHOD_LIML ||
         sys->method == SYS_METHOD_3SLS ||
         sys->method == SYS_METHOD_TSLS)
        && list == NULL) {
        list = compose_ivreg_list(sys, i);
        *freeit = 1;
    }

    return list;
}

/* Hansen-Sargan overidentification test for the system as a whole, as
   in Davidson and MacKinnon, ETM: p. 511 and equation (12.25) for the
   case of SUR; p. 532 and equation (12.61) for the case of 3SLS.  See
   also D & M, Estimation and Inference in Econometrics, equation
   (18.60), for a more computation-friendly statement of the criterion
   function.
*/

static int hansen_sargan_test (equation_system *sys,
                               const DATASET *dset)
{
    const int *exlist = system_get_instr_vars(sys);
    const double *Wi, *Wj;
    int nx = exlist[0];
    int m = sys->neqns;
    int T = sys->T;
    int df = system_get_overid_df(sys);
    gretl_matrix_block *B;
    gretl_matrix *WTW, *eW, *tmp;
    double x, X2;
    int i, j, t;
    int err = 0;

    if (df <= 0) {
        return 1;
    }

    B = gretl_matrix_block_new(&WTW, nx, nx,
                               &eW, m, nx,
                               &tmp, m, nx,
                               NULL);
    if (B == NULL) {
        return E_ALLOC;
    }

    /* construct W-transpose W in WTW */
    for (i=0; i<nx; i++) {
        Wi = dset->Z[exlist[i+1]] + sys->t1;
        for (j=i; j<nx; j++) {
            Wj = dset->Z[exlist[j+1]] + sys->t1;
            x = 0.0;
            for (t=0; t<sys->T; t++) {
                x += Wi[t] * Wj[t];
            }
            gretl_matrix_set(WTW, i, j, x);
            if (i != j) {
                gretl_matrix_set(WTW, j, i, x);
            }
        }
    }

    err = gretl_invert_symmetric_matrix(WTW);
#if SDEBUG
    fprintf(stderr, "hansen_sargan: on invert, err=%d\n", err);
#endif
    if (err) {
        sys->X2 = NADBL;
        goto bailout;
    }

    /* set up vectors of SUR or 3SLS residuals, transposed,
       times W; these are stacked in the m * nx matrix eW
    */
    for (i=0; i<m; i++) {
        for (j=0; j<nx; j++) {
            Wj = dset->Z[exlist[j+1]] + sys->t1;
            x = 0.0;
            for (t=0; t<T; t++) {
                x += gretl_matrix_get(sys->E, t, i) * Wj[t];
            }
            gretl_matrix_set(eW, i, j, x);
        }
    }

    /* multiply these vectors into (WTW)^{-1} */
    for (i=0; i<m; i++) {
        for (j=0; j<nx; j++) {
            x = 0.0;
            for (t=0; t<nx; t++) {
                x += gretl_matrix_get(eW, i, t) *
                    gretl_matrix_get(WTW, t, j);
            }
            gretl_matrix_set(tmp, i, j, x);
        }
    }

    /* cumulate the Chi-square value */
    X2 = 0.0;
    for (i=0; i<m; i++) {
        for (j=0; j<m; j++) {
            x = 0.0;
            for (t=0; t<nx; t++) {
                x += gretl_matrix_get(tmp, i, t) *
                    gretl_matrix_get(eW, j, t); /* transposed */
            }
            X2 += gretl_matrix_get(sys->S, i, j) * x;
        }
    }

#if SDEBUG
    fprintf(stderr, "Hansen-Sargan: Chi-square(%d) = %g (p-value %g)\n",
            df, X2, chisq_cdf_comp(df, X2));
#endif
    sys->X2 = X2;

 bailout:

    gretl_matrix_block_destroy(B);

    return err;
}

static int basic_system_allocate (equation_system *sys,
                                  int mk, int nr,
                                  gretl_matrix **X,
                                  gretl_matrix **y)
{
    int m = sys->neqns;
    int T = sys->T;
    int ldx = mk + nr;

    /* allocate a model for each stochastic equation */
    sys->models = gretl_model_array_new(m);
    if (sys->models == NULL) {
        return E_ALLOC;
    }

    sys->E = gretl_matrix_alloc(T, m);
    if (sys->E == NULL) {
        return E_ALLOC;
    }

    sys->S = gretl_matrix_alloc(m, m);
    if (sys->S == NULL) {
        return E_ALLOC;
    }

    if (X != NULL) {
        *X = gretl_matrix_alloc(ldx, ldx);
        if (*X == NULL) {
            return E_ALLOC;
        }
    }

    if (y != NULL) {
        *y = gretl_column_vector_alloc(ldx);
        if (*y == NULL) {
            return E_ALLOC;
        }
    }

    return 0;
}

/* compute log-likelihood for iterated SUR estimator */

double sur_loglik (equation_system *sys)
{
    int m = sys->neqns;
    int T = sys->T;
    gretl_matrix *tmp;
    double ldet;
    int err = 0;

    tmp = gretl_matrix_alloc(m, m);
    if (tmp == NULL) {
        return NADBL;
    }

    gls_sigma_from_uhat(sys, tmp, 0);
    ldet = gretl_vcv_log_determinant(tmp, &err);

    if (na(ldet)) {
        sys->ll = NADBL;
    } else {
        sys->ll = -(m * T / 2.0) * (LN_2_PI + 1.0);
        sys->ll -= (T / 2.0) * ldet;
    }

    gretl_matrix_free(tmp);

    return sys->ll;
}

/* When estimating with a specified set of linear restrictions,
   Rb = q, augment the X matrix with R and R-transpose.
*/

static void
augment_X_with_restrictions (gretl_matrix *X, int mk,
                             equation_system *sys)
{
    int i, j, nr = sys->R->rows;

    /* place the R matrix */
    insert_sys_X_block(X, sys->R, mk, 0, 1.0);

    /* zero the bottom right-hand block */
    for (i=mk; i<mk+nr; i++) {
        for (j=mk; j<mk+nr; j++) {
            gretl_matrix_set(X, i, j, 0.0);
        }
    }
}

/* When estimating with a specified set of linear restrictions,
   Rb = q, augment the y matrix with q.
*/

static int
augment_y_with_restrictions (gretl_matrix *y, int mk, int nr,
                             equation_system *sys)
{
    int i;

    if (sys->q == NULL) {
        return 1;
    }

    for (i=0; i<nr; i++) {
        gretl_vector_set(y, mk + i, gretl_vector_get(sys->q, i));
    }

    return 0;
}

#define SYS_MAX_ITER 250
#define SYS_LL_TOL 1.0e-12
#define SYS_BDIFF_TOL 1.0e-9

/* check for convergence of iteration: we use the change in the
   log-likelihood when iterating SUR to the ML solution, or
   a measure of the change in the coefficients when iterating
   3SLS
*/

static int sys_converged (equation_system *sys, double *llbak,
                          gretlopt opt, PRN *prn, int *err)
{
    double crit, tol = 0.0;
    int met = 0;

    if (sys->method == SYS_METHOD_SUR ||
        sys->method == SYS_METHOD_WLS) {
        double ll = sur_loglik(sys);

        tol = SYS_LL_TOL;
        crit = ll - *llbak;

        if (opt & OPT_V) {
            pprintf(prn, _("Iteration %3d, ll = %#.8g\n"), sys->iters, ll);
        }
        if (crit <= tol) {
            met = 1;
        } else if (sys->iters < SYS_MAX_ITER) {
            *llbak = ll;
        }
    } else if (sys->method == SYS_METHOD_3SLS) {
        tol = SYS_BDIFF_TOL;
        crit = sys->bdiff;
        if (opt & OPT_V) {
            pprintf(prn, _("Iteration %3d, criterion = %g\n"), sys->iters, crit);
        }
        if (crit <= tol) {
            met = 1;
        }
    }

    if (met && tol > 0 && (opt & OPT_V)) {
        pprintf(prn, _("Tolerance of %g is met\n"), tol);
    }

    if (!met && sys->iters >= SYS_MAX_ITER) {
        pprintf(prn, _("Reached %d iterations without meeting "
                "tolerance of %g\n"), sys->iters, tol);
        *err = E_NOCONV;
    }

    return met;
}

static void clean_up_models (equation_system *sys)
{
    int i;

    if (sys->models == NULL) {
        /* "can't happen" */
        return;
    }

    sys->ess = 0.0;

    for (i=0; i<sys->neqns; i++) {
        sys->ess += sys->models[i]->ess;
        if (sys->method == SYS_METHOD_3SLS ||
            sys->method == SYS_METHOD_FIML ||
            sys->method == SYS_METHOD_TSLS ||
            sys->method == SYS_METHOD_LIML) {
            tsls_free_data(sys->models[i]);
        }
        if (sys->neqns > 1) {
            gretl_model_free(sys->models[i]);
        }
    }

    if (sys->flags & SYSTEM_LIML1) {
        /* ivreg (single-equation LIML) */
        sys->models[0]->rho = sys->models[0]->dw = NADBL;
    } else {
        free(sys->models);
        sys->models = NULL;
    }
}

#define REJECT_COLLINEAR 1

#if REJECT_COLLINEAR

static int perfect_collinearity_check (MODEL *pmod,
                                       DATASET *dset,
                                       int i)
{
    const int *d1 = gretl_model_get_list(pmod, "droplist");
    const int *d2 = gretl_model_get_list(pmod, "inst_droplist");

    if (d1 != NULL) {
        gretl_errmsg_sprintf(_("Equation %d exhibits perfect collinearity.\n"
                             "The regressor %s cannot be included. Please "
                             "respecify the system."), i, dset->varname[d1[1]]);
        return E_SINGULAR;
    } else if (d2 != NULL) {
        gretl_errmsg_sprintf(_("Equation %d exhibits perfect collinearity.\n"
                             "The instrument %s cannot be included. Please "
                             "respecify the system."), i, dset->varname[d2[1]]);
        return E_SINGULAR;
    } else {
        return 0;
    }
}

#else

/* Given a record of redundant regressors or instruments dropped
   at the initial OLS or TSLS stage, namely @droplist, purge these
   series IDs from both the relevant system lists. The @mk pointer
   argument tells us whether we're looking at regular regressors
   (mk non-NULL) or instruments.
*/

static int drop_redundant_variables (equation_system *sys,
                                     const int *droplist,
                                     int eqn, int *mk)
{
    int i, j, di, pmax, pmin = 0;
    int *eqnlist = sys->lists[eqn];
    int insts = (mk == NULL);
    int err = 0;

    if (insts) {
        for (i=1; i<=droplist[0]; i++) {
            di = droplist[i];
            j = in_gretl_list(sys->ilist, di);
            if (j > 0) {
                gretl_list_delete_at_pos(sys->ilist, j);
            } else {
                err = 1;
            }
        }
    }

    j = gretl_list_separator_position(eqnlist);

    if (insts) {
        /* instruments */
        if (j > 0) {
            pmin = j + 1;
            pmax = eqnlist[0];
        }
    } else {
        pmin = 2;
        pmax = (j > 0) ? j - 1 : eqnlist[0];
    }

    if (pmin > 0) {
        for (i=1; i<=droplist[0]; i++) {
            di = droplist[i];
            for (j=pmax; j>=pmin; j--) {
                if (eqnlist[j] == di) {
                    gretl_list_delete_at_pos(eqnlist, j);
                    if (mk != NULL) {
                        mk -= 1;
                    }
                    break;
                }
            }
        }
    }

    return err;
}

#endif /* REJECT_COLLINEAR or not */

/* options to be passed in running initial 2SLS */

static gretlopt sys_tsls_opt (const equation_system *sys,
                              gretlopt opt)
{
    gretlopt tsls_opt = (OPT_E | OPT_A);

    if (!(sys->flags & SYSTEM_DFCORR)) {
        tsls_opt |= OPT_N; /* suppress df correction */
    }

    if (sys->flags & SYSTEM_LIML1) {
        tsls_opt |= OPT_H; /* add "hatlist" of instrumented vars */
    }

    return tsls_opt;
}

/* options to be passed in running initial OLS; @nr is
   the number of restrictions imposed
*/

static gretlopt sys_ols_opt (equation_system *sys,
                             int nr, gretlopt opt)
{
    gretlopt ols_opt = OPT_S; /* flag as part of system */

    if (sys->method == SYS_METHOD_OLS && !(sys->flags & SYSTEM_DFCORR)) {
        /* suppress df correction */
        ols_opt |= OPT_N;
    } else if (sys->method == SYS_METHOD_WLS) {
        if (sys->R == NULL && !(opt & OPT_N)) {
            /* equivalent to OLS */
            sys->flags |= SYSTEM_DFCORR;
        } else {
            ols_opt |= OPT_N;
        }
    }

    if (sys->method == SYS_METHOD_OLS && nr == 0) {
        /* initial OLS will supply the estimates */
        if (sys->flags & SYSTEM_ROBUST) {
             ols_opt |= OPT_R;
        }
    } else {
        /* treat initial OLS as auxiliary */
         ols_opt |= OPT_A;
    }

    return  ols_opt;
}

static int allocate_Xi_etc (gretl_matrix **Xi,
                            gretl_matrix **Xj,
                            gretl_matrix **M,
                            int T, int k)
{
    *Xi = gretl_matrix_alloc(T, k);
    *Xj = gretl_matrix_alloc(T, k);
    *M = gretl_matrix_alloc(k, k);

    if (*Xi == NULL || *Xj == NULL || *M == NULL) {
        return E_ALLOC;
    } else {
        return 0;
    }
}

static int ols_data_to_sys (equation_system *sys, int mk)
{
    gretl_matrix *B, *V;
    MODEL *pmod;
    double vij;
    int i, j, k;
    int mi, mj;
    int vi, vj;
    int err = 0;

    B = gretl_matrix_alloc(mk, 1);
    V = gretl_zero_matrix_new(mk, mk);
    if (B == NULL || V == NULL) {
        return E_ALLOC;
    }

    j = vi = vj = 0;
    for (i=0; i<sys->neqns; i++) {
        pmod = sys->models[i];
        if (pmod->vcv == NULL) {
            err = makevcv(pmod, pmod->sigma);
            if (err) {
                break;
            }
        }
        k = pmod->ncoeff;
        for (mi=0; mi<k; mi++) {
            B->val[j++] = pmod->coeff[mi];
            for (mj=0; mj<k; mj++) {
                vij = gretl_model_get_vcv_element(pmod, mi, mj, k);
                gretl_matrix_set(V, vi+mi, vj+mj, vij);
            }
        }
        vi += k;
        vj += k;
    }

    if (err) {
        gretl_matrix_free(B);
        gretl_matrix_free(V);
    } else {
        system_attach_coeffs(sys, B);
        system_attach_vcv(sys, V);
    }

    return err;
}

/* general function that forms the basis for all specific system
   estimators */

int system_estimate (equation_system *sys, DATASET *dset,
                     gretlopt opt, PRN *prn)
{
    int i, j, k, T, t;
    int v, l, mk, krow, nr;
    int orig_t1 = dset->t1;
    int orig_t2 = dset->t2;
    gretl_matrix *X = NULL;
    gretl_matrix *y = NULL;
    gretl_matrix *Xi = NULL;
    gretl_matrix *Xj = NULL;
    gretl_matrix *M = NULL;
    gretl_matrix **pX = NULL;
    gretl_matrix **py = NULL;
    MODEL **models = NULL;
    int method = sys->method;
    double llbak = -1.0e9;
    int single_equation = 0;
    int do_iteration = 0;
    int plain_ols = 0;
    int rsingle = 0;
    int do_diag = 0;
    int err = 0;

    sys->iters = 0;

    if (sys->flags & SYSTEM_ITERATE) {
        do_iteration = 1;
    }

    nr = system_n_restrictions(sys);

    if (method == SYS_METHOD_OLS || method == SYS_METHOD_TSLS ||
        method == SYS_METHOD_LIML || method == SYS_METHOD_WLS) {
        single_equation = 1;
    }

    if ((method == SYS_METHOD_OLS || method == SYS_METHOD_WLS) &&
        sys->R == NULL) {
        plain_ols = 1;
    } else {
        pX = &X;
        py = &y;
    }

    if (nr > 0) {
        if (method == SYS_METHOD_3SLS) {
            /* doing 3SLS with restrictions: we need to obtain
               restricted TSLS estimates as a starting point
            */
            rsingle = 1;
        } else if (method == SYS_METHOD_SUR ||
                   method == SYS_METHOD_WLS) {
            /* doing SUR or WLS with restrictions: we need to obtain
               restricted OLS estimates as a starting point
            */
            rsingle = 1;
        }
    }

    /* get uniform sample starting and ending points and check for
       missing data */
    err = system_adjust_t1t2(sys, dset);
    if (err) {
        return err;
    }

    /* set sample for auxiliary regressions */
    dset->t1 = sys->t1;
    dset->t2 = sys->t2;

    /* max indep vars per equation */
    k = system_max_indep_vars(sys);

    /* total indep vars, all equations */
    mk = system_n_indep_vars(sys);

    /* set sample for auxiliary regressions */
    dset->t1 = sys->t1;
    dset->t2 = sys->t2;

    /* number of observations per series */
    T = sys->T;

    /* allocate models etc */
    err = basic_system_allocate(sys, mk, nr, pX, py);
    if (err) {
        goto cleanup;
    }

    /* convenience pointers */
    models = sys->models;

    if ((method == SYS_METHOD_FIML ||
         method == SYS_METHOD_LIML) && !(opt & OPT_Q)) {
        print_equation_system_info(sys, dset, OPT_H, prn);
    }

    /* First estimate the equations separately (either by OLS or
       TSLS), and put the single-equation residuals into the uhat
       matrix.  Note that at this stage we are not in a position to
       impose any cross-equation restrictions, since we're doing
       straight equation-by-equation estimation.
    */

    for (i=0; i<sys->neqns; i++) {
#if !REJECT_COLLINEAR
        const int *droplist = NULL;
#endif
        int freeit = 0;
        int *list = system_model_list(sys, i, &freeit);
        MODEL *pmod = models[i];
        gretlopt eq_opt;

        if (list == NULL) {
            err = 1;
            break;
        }

        if (sys_ols_ok(sys)) {
            eq_opt = sys_ols_opt(sys, nr, opt);
            *pmod = lsq(list, dset, OLS, eq_opt);
        } else {
            eq_opt = sys_tsls_opt(sys, opt);
            *pmod = tsls(list, dset, eq_opt);
        }

        if (freeit) {
            free(list);
        }

        if ((err = pmod->errcode)) {
            fprintf(stderr, "system_estimate: failed to estimate equation %d: "
                    "err = %d\n", i+1, err);
            break;
        }

#if REJECT_COLLINEAR
        err = perfect_collinearity_check(pmod, dset, i+1);
        if (err) break;
#else
        droplist = gretl_model_get_list(pmod, "droplist");
        if (droplist != NULL) {
            drop_redundant_variables(sys, droplist, i, &mk);
        }
        droplist = gretl_model_get_list(pmod, "inst_droplist");
        if (droplist != NULL) {
            drop_redundant_variables(sys, droplist, i, NULL);
        }
#endif

        pmod->ID = i;
        pmod->aux = AUX_SYS;
        gretl_model_set_int(pmod, "method", method);
        if (eq_opt & OPT_N) {
            gretl_model_set_int(pmod, "asy", 1);
        }

        /* save sigma-squared for an LR test for diagonal
           covariance matrix */
        if (method == SYS_METHOD_SUR && do_iteration && nr == 0) {
            gretl_model_set_double(pmod, "ols_sigma_squared",
                                   pmod->ess / pmod->nobs);
        }

        /* do we want this with @rsingle? */
        for (t=0; t<T; t++) {
            gretl_matrix_set(sys->E, t, i, pmod->uhat[t + sys->t1]);
        }
    }

    if (err) {
        fprintf(stderr, "system_estimate: after single-equation "
                "estimation, err = %d\n", err);
        goto cleanup;
    }

    if (method == SYS_METHOD_LIML) {
        /* compute the minimum eigenvalues and generate the
           k-class data matrices */
        err = liml_driver(sys, dset, prn);
        if (err) goto cleanup;
    }

    if (plain_ols) {
        gls_sigma_from_uhat(sys, sys->S, 1);
        err = ols_data_to_sys(sys, mk);
        goto save_etc;
    }

    /* marker for iterated versions of SUR, WLS, or 3SLS; also for
       loopback in case of restricted 3SLS, where we want to compute
       restricted TSLS estimates first
    */
 iteration_start:

    gls_sigma_from_uhat(sys, sys->S, 0);

    if (method == SYS_METHOD_WLS) {
        gretl_matrix_zero(X);
        err = gretl_invert_diagonal_matrix(sys->S);
    } else if (single_equation || rsingle) {
        gretl_matrix_zero(X);
    } else {
        err = gretl_invert_symmetric_matrix(sys->S);
    }

#if SDEBUG
    fprintf(stderr, "system_estimate: on invert, err=%d\n", err);
    gretl_matrix_print(sys->S, "sys->S");
#endif

    if (!err && Xi == NULL) {
        /* the test against NULL here allows for the possibility
           that we're iterating
        */
        err = allocate_Xi_etc(&Xi, &Xj, &M, T, k);
    }

    if (err) goto cleanup;

    /* form the big stacked X matrix: Xi = data matrix for equation i,
       specified in lists[i]
    */

    krow = 0;
    for (i=0; i<sys->neqns && !err; i++) {
        int kcol = 0;

        err = make_sys_X_block(Xi, models[i], dset, sys->t1, method);

        for (j=0; j<=i && !err; j++) {
            const gretl_matrix *Xk;
            double sij;

            if (i != j) {
                if (single_equation || rsingle) {
                    kcol += models[j]->ncoeff;
                    continue;
                }
                err = make_sys_X_block(Xj, models[j], dset, sys->t1, method);
                Xk = Xj;
            } else if (method == SYS_METHOD_LIML) {
                err = make_liml_X_block(Xj, models[i], dset, sys->t1);
                Xk = Xj;
            } else {
                Xk = Xi;
            }

            M->rows = Xi->cols;
            M->cols = Xk->cols;

            err = gretl_matrix_multiply_mod(Xi, GRETL_MOD_TRANSPOSE,
                                            Xk, GRETL_MOD_NONE,
                                            M, GRETL_MOD_NONE);

            if (rsingle || (single_equation && method != SYS_METHOD_WLS)) {
                sij = 1.0;
            } else {
                sij = gretl_matrix_get(sys->S, i, j);
            }

            insert_sys_X_block(X, M, krow, kcol, sij);
            kcol += models[j]->ncoeff;
        }

        krow += models[i]->ncoeff;
    }

    if (err) {
        fprintf(stderr, "after trying to make X matrix: err = %d\n", err);
        goto cleanup;
    }

    if (nr > 0) {
        /* there are restrictions to be imposed */
        augment_X_with_restrictions(X, mk, sys);
    }

    if (!do_iteration && !rsingle) {
        /* we're not coming back this way, so free some storage */
        gretl_matrix_free(Xj);
        Xj = NULL;
        gretl_matrix_free(M);
        M = NULL;
    }

    /* form stacked Y column vector (m x k) */

    v = 0;
    for (i=0; i<sys->neqns; i++) {

        /* loop over the m vertically-arranged blocks */
        make_sys_X_block(Xi, models[i], dset, sys->t1, method);

        for (j=0; j<models[i]->ncoeff; j++) {
            /* loop over the rows within each of the m blocks */
            double yv = 0.0;
            int lmin = 0, lmax = sys->neqns;

            if (single_equation || rsingle) {
                /* no cross terms wanted */
                lmin = i;
                lmax = i + 1;
            }

            for (l=lmin; l<lmax; l++) {
                /* loop over the components that must be
                   added to form each element */
                const double *yl = NULL;
                double xx = 0.0;

                if (method == SYS_METHOD_LIML) {
                    yl = gretl_model_get_data(models[l], "liml_y");
                } else {
                    yl = dset->Z[system_get_depvar(sys, l)];
                }

                /* multiply X'[l] into y */
                for (t=0; t<T; t++) {
                    xx += gretl_matrix_get(Xi, t, j) * yl[t + sys->t1];
                }

                if (rsingle || (single_equation && method != SYS_METHOD_WLS)) {
                    ; /* leave xx unmodified */
                } else if (method == SYS_METHOD_WLS && i == l) {
                    xx *= gretl_matrix_get(sys->S, i, i);
                } else {
                    xx *= gretl_matrix_get(sys->S, i, l);
                }

                yv += xx;
            }
            gretl_vector_set(y, v++, yv);
        }
    }

    if (nr > 0) {
        /* there are restrictions */
        augment_y_with_restrictions(y, mk, nr, sys);
    }

    /* The estimates calculated below will be SUR, 3SLS or LIML,
       depending on how the data matrices above were constructed --
       unless, that is, we're just doing restricted OLS, WLS or TSLS
       estimates.
    */
    err = calculate_sys_coeffs(sys, dset, X, y, mk, nr,
                               do_iteration);

    if (!err && rsingle) {
        /* take one more pass */
        rsingle = 0;
        goto iteration_start;
    }

    if (!err && do_iteration) {
        /* check for convergence */
        if (!sys_converged(sys, &llbak, opt, prn, &err)) {
            if (!err) {
                sys->iters += 1;
                goto iteration_start;
            }
        }
    }

    if (err) goto cleanup;

    if (nr == 0 && (method == SYS_METHOD_3SLS || method == SYS_METHOD_SUR)) {
        /* compute this test while we have sigma-inverse available */
        hansen_sargan_test(sys, dset);
    }

    do_diag = !(method == SYS_METHOD_SUR && do_iteration);

    /* refresh sigma (non-inverted) */
    gls_sigma_from_uhat(sys, sys->S, do_diag);

    if (method == SYS_METHOD_FIML) {
        /* compute FIML estimates */
        err = fiml_driver(sys, dset, opt, prn);
    }

 save_etc:

    if (!err && !(sys->flags & SYSTEM_LIML1)) {
        err = system_save_and_print_results(sys, dset, opt, prn);
    }

 cleanup:

    gretl_matrix_free(Xi);
    gretl_matrix_free(Xj);
    gretl_matrix_free(M);
    gretl_matrix_free(X);
    gretl_matrix_free(y);

    clean_up_models(sys);

    dset->t1 = orig_t1;
    dset->t2 = orig_t2;

    return err;
}