File: kalman_arma.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 (834 lines) | stat: -rw-r--r-- 21,857 bytes parent folder | download | duplicates (2)
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
/*
  Exact ML for ARMA using gretl-native Kalman filter.

  As of January 2022 we need this for one case that is not
  handled by the faster dedicated ARMA codes AS 154 and
  AS 197, namely ARIMA (with non-zero integration order)
  plus within-sample missing values.
*/

#include "kalman.h"

#define KALMAN_ALL 999

typedef struct kalman_helper_ khelper;

struct kalman_helper_ {
    gretl_matrix_block *Bk;
    gretl_matrix *a;
    gretl_matrix *P;
    gretl_matrix *T;
    gretl_matrix *B;
    gretl_matrix *Z;
    gretl_matrix *Q;
    gretl_matrix *V;
    gretl_matrix *avar;

    gretl_matrix *avar2;
    gretl_matrix *vQ;

    gretl_matrix *T_; /* used only for ARIMA via levels */
    gretl_matrix *Q_; /* ditto */
    gretl_matrix *P_; /* ditto */

    arma_info *kainfo;
};

static int kalman_do_ma_check = 1;

static void kalman_helper_free (khelper *kh)
{
    if (kh != NULL) {
        gretl_matrix_block_destroy(kh->Bk);
        gretl_matrix_free(kh->avar2);
        gretl_matrix_free(kh->vQ);
        gretl_matrix_free(kh->T_);
        gretl_matrix_free(kh->Q_);
        gretl_matrix_free(kh->P_);
        free(kh);
    }
}

static khelper *kalman_helper_new (arma_info *ainfo,
                                   int r, int k)
{
    khelper *kh;
    int r0, r2;
    int err = 0;

    kh = malloc(sizeof *kh);
    if (kh == NULL) {
        return NULL;
    }

    r0 = ainfo->r0;
    r2 = r0 * r0;

    kh->avar2 = kh->vQ = NULL;
    kh->T_ = kh->Q_ = kh->P_ = NULL;

    kh->Bk = gretl_matrix_block_new(&kh->a, r, 1,
				    &kh->P, r, r,
				    &kh->T, r, r,
				    &kh->B, k, 1,
				    &kh->Z, r, 1,
				    &kh->Q, r, r,
				    &kh->V, ainfo->fullT, 1,
				    &kh->avar, r2, r2,
				    NULL);

    if (kh->Bk == NULL) {
        err = E_ALLOC;
    } else if (arma_using_vech(ainfo)) {
        int m = r0 * (r0 + 1) / 2;

        kh->avar2 = gretl_matrix_alloc(m, m);
        kh->vQ = gretl_column_vector_alloc(m);
        if (kh->avar2 == NULL || kh->vQ == NULL) {
            err = E_ALLOC;
        }
    } else {
        kh->vQ = gretl_column_vector_alloc(r2);
        if (kh->vQ == NULL) {
            err = E_ALLOC;
        }
    }

    if (!err && arima_levels(ainfo)) {
        kh->T_ = gretl_matrix_alloc(r0, r0);
        kh->Q_ = gretl_matrix_alloc(r0, r0);
        kh->P_ = gretl_matrix_alloc(r0, r0);
        if (kh->T_ == NULL || kh->Q_ == NULL || kh->P_ == NULL) {
            err = E_ALLOC;
        }
    }

    if (err) {
        kalman_helper_free(kh);
        kh = NULL;
    } else {
        kh->kainfo = ainfo;
    }

    return kh;
}

/* Get the dimension of the state-space representation: note
   that this is augmented if we're estimating an ARIMA
   model using the levels formulation in order to handle
   missing values -- see Harvey and Pierse, "Estimating
   Missing Observations in Economic Time Series", JASA 1984.
*/

static int ainfo_get_state_size (arma_info *ainfo)
{
    int plen = ainfo->p + ainfo->pd * ainfo->P;
    int qlen = ainfo->q + ainfo->pd * ainfo->Q;
    int r = (plen > qlen + 1)? plen : qlen + 1;

    ainfo->r0 = r;

    if (arima_levels(ainfo)) {
        r += ainfo->d + ainfo->pd * ainfo->D;
    }

    return r;
}

static int allocate_ac_mc (arma_info *ainfo)
{
    int m = (ainfo->P > 0) + (ainfo->Q > 0);
    int err = 0;

    if (m > 0) {
        double *ac = NULL, *mc = NULL;
        int n, i = 0;

        ainfo->aux = doubles_array_new(m, 0);
        if (ainfo->aux == NULL) {
            return E_ALLOC;
        }

        if (ainfo->P > 0) {
            n = 1 + ainfo->p + ainfo->pd * ainfo->P;
            ac = malloc(n * sizeof *ac);
            if (ac == NULL) {
                err = E_ALLOC;
            } else {
                ainfo->aux[i++] = ac;
            }
        }

        if (!err && ainfo->Q > 0) {
            n = 1 + ainfo->q + ainfo->pd * ainfo->Q;
            mc = malloc(n * sizeof *mc);
            if (mc == NULL) {
                err = E_ALLOC;
            } else {
                ainfo->aux[i++] = mc;
            }
        }

        if (err) {
            doubles_array_free(ainfo->aux, m);
        } else {
            ainfo->n_aux = m;
        }
    }

    return err;
}

static void write_big_phi (const double *phi,
                           const double *Phi,
                           arma_info *ainfo,
                           gretl_matrix *F)
{
    int pmax = ainfo->p + ainfo->pd * ainfo->P;
    double *ac = ainfo->aux[0];
    double x, y;
    int i, j, k, ii;

    for (i=0; i<=pmax; i++) {
        ac[i] = 0.0;
    }

    for (j=-1; j<ainfo->P; j++) {
        x = (j < 0)? -1 : Phi[j];
        k = 0.0;
        for (i=-1; i<ainfo->p; i++) {
            if (i < 0) {
                y = -1;
            } else if (AR_included(ainfo, i)) {
                y = phi[k++];
            } else {
                y = 0.0;
            }
            ii = (j+1) * ainfo->pd + (i+1);
            ac[ii] -= x * y;
        }
    }

    for (i=0; i<pmax; i++) {
        gretl_matrix_set(F, 0, i, ac[i+1]);
    }
}

static void write_big_theta (const double *theta,
                             const double *Theta,
                             arma_info *ainfo,
                             gretl_matrix *H,
                             gretl_matrix *F)
{
    int qmax = ainfo->q + ainfo->pd * ainfo->Q;
    int i = (ainfo->P > 0)? 1 : 0;
    double *mc = ainfo->aux[i];
    double x, y;
    int j, k, ii;

    for (i=0; i<=qmax; i++) {
        mc[i] = 0.0;
    }

    for (j=-1; j<ainfo->Q; j++) {
        x = (j < 0)? 1 : Theta[j];
        k = 0;
        for (i=-1; i<ainfo->q; i++) {
            if (i < 0) {
                y = 1;
            } else if (MA_included(ainfo, i)) {
                y = theta[k++];
            } else {
                y = 0.0;
            }
            ii = (j+1) * ainfo->pd + (i+1);
            mc[ii] += x * y;
        }
    }

    for (i=1; i<=qmax; i++) {
        if (H != NULL) {
            H->val[i] = mc[i];
        } else {
            gretl_matrix_set(F, ainfo->r0, i, mc[i]);
        }
    }
}

static void condense_row (gretl_matrix *targ,
                          const gretl_matrix *src,
                          int targrow, int srcrow,
                          int n)
{
    double x;
    int i, j, k, g;
    int targcol = 0;

    for (j=0; j<n; j++) {
        for (i=j; i<n; i++) {
            k = j * n + i;
            g = (k % n) * n + k / n;
            x = gretl_matrix_get(src, srcrow, k);
            if (g != k) {
                x += gretl_matrix_get(src, srcrow, g);
            }
            gretl_matrix_set(targ, targrow, targcol++, x);
        }
    }
}

static void condense_state_vcv (gretl_matrix *targ,
                                const gretl_matrix *src,
                                int n)
{
    int posr = 0, posc = 0;
    int i, j;

    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
            if (j >= i) {
                condense_row(targ, src, posr++, posc, n);
            }
            posc++;
        }
    }
}

static int kalman_matrices_init (arma_info *ainfo,
                                 khelper *kh,
                                 const double *y)
{
    int r0 = ainfo->r0;
    int r = kh->T->rows;

    gretl_matrix_zero(kh->B);
    gretl_matrix_zero(kh->a);
    gretl_matrix_zero(kh->P);
    gretl_matrix_zero(kh->T);
    gretl_matrix_inscribe_I(kh->T, 1, 0, r0 - 1);

    gretl_matrix_zero(kh->Q);
    gretl_matrix_set(kh->Q, 0, 0, 1.0);

    gretl_matrix_zero(kh->Z);
    gretl_vector_set(kh->Z, 0, 1.0);

    if (arima_levels(ainfo)) {
        /* write additional constant elements of T, Z and a */
        int d = ainfo->d, D = ainfo->D;
        int s = ainfo->pd;
        int i, k = d + s * D;
        int *c = arima_delta_coeffs(d, D, s);
        double y0;

        if (c == NULL) {
            return E_ALLOC;
        }
        for (i=0; i<k; i++) {
            gretl_matrix_set(kh->T, r0, r0 + i, c[i]);
        }
        gretl_matrix_set(kh->T, r0, 0, 1.0);
        if (r - r0 > 1) {
            gretl_matrix_inscribe_I(kh->T, r0 + 1, r0, k - 1);
        }
        for (i=0; i<k; i++) {
            gretl_vector_set(kh->Z, r0 + i, c[i]);
            /* lagged data */
            y0 = y[ainfo->t1 - 1 - i];
            if (ainfo->yscale != 1.0 && !na(y0)) {
                y0 -= ainfo->yshift;
                y0 *= ainfo->yscale;
            }
            gretl_vector_set(kh->a, r0 + i, y0);
        }
        free(c);

#if ARMA_DEBUG
        gretl_matrix_print(kh->a, "a0 (arima via levels)");
#endif
        /* initialize the plain-arma "shadow" matrices */
        gretl_matrix_zero(kh->T_);
        gretl_matrix_inscribe_I(kh->T_, 1, 0, r0 - 1);
        gretl_matrix_zero(kh->Q_);
        gretl_matrix_set(kh->Q_, 0, 0, 1.0);
        gretl_matrix_zero(kh->P_);
    } else if (ainfo->np == 0 && ainfo->P == 0) {
        /* initialize P to identity matrix */
        gretl_matrix_inscribe_I(kh->P, 0, 0, kh->P->rows);
    }

    return 0;
}

static int write_kalman_matrices (khelper *kh,
                                  const double *b,
                                  int idx)
{
    arma_info *ainfo = kh->kainfo;
    const double *phi =       b + ainfo->ifc;
    const double *Phi =     phi + ainfo->np;
    const double *theta =   Phi + ainfo->P;
    const double *Theta = theta + ainfo->nq;
    const double *beta =  Theta + ainfo->Q;
    double mu = (ainfo->ifc)? b[0] : 0.0;
    int rewrite_B = 0;
    int rewrite_T = 0;
    int rewrite_Z = 0;
    int i, k, err = 0;

    if (idx == KALMAN_ALL) {
        rewrite_B = rewrite_T = rewrite_Z = 1;
    } else {
        /* called in context of calculating score, for OPG matrix */
        int pmax = ainfo->ifc + ainfo->np + ainfo->P;
        int tmax = pmax + ainfo->nq + ainfo->Q;

        if (ainfo->ifc && idx == 0) {
            rewrite_B = 1;
        } else if (idx >= ainfo->ifc && idx < pmax) {
            rewrite_T = 1;
        } else if (idx >= ainfo->ifc && idx < tmax) {
            rewrite_Z = 1;
        } else {
            rewrite_B = 1;
        }
    }

    /* revise for pure MA model */
    if (ainfo->np == 0 && ainfo->P == 0 && !arima_levels(ainfo)) {
        rewrite_T = 0;
    }
    /* and for case of no constant or other regressors */
    if (ainfo->ifc == 0 && ainfo->nexo == 0) {
        rewrite_B = 0;
    }

    /* See Hamilton, Time Series Analysis, ch 13, p. 375 */

    if (rewrite_B) {
        /* const and coeffs on exogenous vars */
        gretl_vector_set(kh->B, 0, mu);
        for (i=0; i<ainfo->nexo; i++) {
            gretl_vector_set(kh->B, i + 1, beta[i]);
        }
    }

    if (rewrite_Z) {
        /* form the Z' vector using theta and/or Theta */
        if (ainfo->Q > 0) {
            write_big_theta(theta, Theta, ainfo, kh->Z, NULL);
        } else {
            k = 0;
            for (i=0; i<ainfo->q; i++) {
                if (MA_included(ainfo, i)) {
                    gretl_vector_set(kh->Z, i+1, theta[k++]);
                } else {
                    gretl_vector_set(kh->Z, i+1, 0.0);
                }
            }
        }
    }

    if (rewrite_T) {
        /* form the T matrix using phi and/or Phi */
        gretl_matrix *T = (kh->T_ != NULL)? kh->T_ : kh->T;
        gretl_matrix *Q = (kh->Q_ != NULL)? kh->Q_ : kh->Q;
        gretl_matrix *P = (kh->P_ != NULL)? kh->P_ : kh->P;

        if (ainfo->P > 0) {
            write_big_phi(phi, Phi, ainfo, T);
        } else {
            k = 0;
            for (i=0; i<ainfo->p; i++) {
                if (AR_included(ainfo, i)) {
                    gretl_matrix_set(T, 0, i, phi[k++]);
                } else {
                    gretl_matrix_set(T, 0, i, 0.0);
                }
            }
        }

        if (arima_levels(ainfo)) {
            /* the full T matrix incorporates \theta */
            if (ainfo->Q > 0) {
                write_big_theta(theta, Theta, ainfo, NULL, kh->T);
            } else {
                k = 0;
                for (i=0; i<ainfo->q; i++) {
                    if (MA_included(ainfo, i)) {
                        gretl_matrix_set(kh->T, ainfo->r0, i+1, theta[k++]);
                    } else {
                        gretl_matrix_set(kh->T, ainfo->r0, i+1, 0.0);
                    }
                }
            }
        }

        /* form $P_{1|0}$ (MSE) matrix, as per Hamilton, ch 13, p. 378. */

        gretl_matrix_kronecker_product(T, T, kh->avar);
        gretl_matrix_I_minus(kh->avar);
        if (arma_using_vech(ainfo)) {
            condense_state_vcv(kh->avar2, kh->avar, gretl_matrix_rows(T));
            gretl_matrix_vectorize_h(kh->vQ, Q);
            err = gretl_LU_solve(kh->avar2, kh->vQ);
            if (!err) {
                gretl_matrix_unvectorize_h(P, kh->vQ);
            }
        } else {
            gretl_matrix_vectorize(kh->vQ, Q);
            err = gretl_LU_solve(kh->avar, kh->vQ);
            if (!err) {
                gretl_matrix_unvectorize(P, kh->vQ);
            }
        }
    }

    if (arima_levels(ainfo)) {
        /* complete the job on T, Q, P */
        gretl_matrix_inscribe_matrix(kh->T, kh->T_, 0, 0, GRETL_MOD_NONE);
        gretl_matrix_inscribe_matrix(kh->Q, kh->Q_, 0, 0, GRETL_MOD_NONE);
        gretl_matrix_inscribe_matrix(kh->P, kh->P_, 0, 0, GRETL_MOD_NONE);
    }

    return err;
}

static int rewrite_kalman_matrices (kalman *K, const double *b, int i)
{
    khelper *kh = (khelper *) kalman_get_data(K);
    int err = write_kalman_matrices(kh, b, i);

    if (!err) {
        kalman_set_initial_state_vector(K, kh->a);
        kalman_set_initial_MSE_matrix(K, kh->P);
    }

    return err;
}

/* used only in obtaining the OPG, if wanted */

static const double *kalman_arma_llt_callback (const double *b, int i,
                                               void *data)
{
    kalman *K = (kalman *) data;
    khelper *kh = kalman_get_data(K);
    int err;

    rewrite_kalman_matrices(K, b, i);
    err = kfilter_standard(K, NULL);

    return (err)? NULL : kh->V->val;
}

static double kalman_arma_ll (const double *b, void *data)
{
    kalman *K = (kalman *) data;
    khelper *kh = kalman_get_data(K);
    arma_info *ainfo = kh->kainfo;
    int offset = ainfo->ifc + ainfo->np + ainfo->P;
    double *theta = (double *) b + offset;
    double *Theta = theta + ainfo->nq;
    double ll = NADBL;
    int err = 0;

    if (kalman_do_ma_check && maybe_correct_MA(ainfo, theta, Theta)) {
        pputs(kalman_get_printer(K), _("MA estimate(s) out of bounds\n"));
        return NADBL;
    }

    err = rewrite_kalman_matrices(K, b, KALMAN_ALL);

    if (!err) {
        err = kfilter_standard(K, NULL);
        ll = kalman_get_loglik(K);
    }

    return ll;
}

static int kalman_arma_finish (MODEL *pmod,
                               arma_info *ainfo,
                               const DATASET *dset,
                               kalman *K, double *b,
                               gretlopt opt, PRN *prn)
{
    khelper *kh = kalman_get_data(K);
    int do_opg = arma_use_opg(opt);
    int i, t, k = ainfo->nc;
    int QML = (opt & OPT_R);
    double s2;
    int err;

    pmod->t1 = ainfo->t1;
    pmod->t2 = ainfo->t2;
    pmod->nobs = ainfo->T;
    pmod->ncoeff = ainfo->nc;
    pmod->full_n = dset->n;

    /* in the Kalman case the basic model struct is empty, so we
       have to allocate for coefficients, residuals and so on
    */
    err = gretl_model_allocate_storage(pmod);
    if (err) {
        return err;
    }

    for (i=0; i<k; i++) {
        pmod->coeff[i] = b[i];
    }

    i = 0;
    for (t=pmod->t1; t<=pmod->t2; t++) {
        pmod->uhat[t] = gretl_vector_get(kh->V, i++);
    }

    s2 = kalman_get_arma_variance(K);
    pmod->sigma = sqrt(s2);
    pmod->lnL = kalman_get_loglik(K);

    if (!do_opg) {
        /* covariance matrix based on Hessian (perhaps QML) */
        gretl_matrix *Hinv;
        double d = 0.0; /* adjust? */

        kalman_do_ma_check = 0;
        Hinv = numerical_hessian_inverse(b, ainfo->nc, kalman_arma_ll,
                                         K, d, &err);
        kalman_do_ma_check = 1;
        if (!err) {
            if (QML) {
                err = arma_QML_vcv(pmod, Hinv, K, 0, b, s2, k, ainfo->T, prn);
            } else {
                err = gretl_model_write_vcv(pmod, Hinv);
                if (!err) {
                    gretl_model_set_vcv_info(pmod, VCV_ML, ML_HESSIAN);
                }
            }
        } else if (!(opt & OPT_H)) {
            /* fallback when Hessian not explicitly requested */
            err = 0;
            do_opg = 1;
            gretl_model_set_int(pmod, "hess-error", 1);
        }
        gretl_matrix_free(Hinv);
    }

    if (do_opg) {
        err = arma_OPG_vcv(pmod, K, 0, b, s2, k, ainfo->T, prn);
        if (!err) {
            gretl_model_set_vcv_info(pmod, VCV_ML, ML_OP);
            pmod->opt |= OPT_G;
        }
    }

    if (!err) {
        write_arma_model_stats(pmod, ainfo, dset);
        arma_model_add_roots(pmod, ainfo, b);
        gretl_model_set_int(pmod, "arma_flags", ARMA_EXACT);
        if (arma_lbfgs(ainfo)) {
            pmod->opt |= OPT_L;
        }
        if (arima_ydiff_only(ainfo)) {
            pmod->opt |= OPT_Y;
        }
    }

    return err;
}

static int kalman_undo_y_scaling (arma_info *ainfo,
                                  gretl_matrix *y, double *b,
                                  kalman *K)
{
    double *beta = b + ainfo->ifc + ainfo->np + ainfo->P +
        ainfo->nq + ainfo->Q;
    int i, t, T = ainfo->t2 - ainfo->t1 + 1;
    int err = 0;

    if (ainfo->ifc) {
        b[0] /= ainfo->yscale;
        b[0] += ainfo->yshift;
    }

    for (i=0; i<ainfo->nexo; i++) {
        beta[i] /= ainfo->yscale;
    }

    i = ainfo->t1;
    for (t=0; t<T; t++) {
        y->val[t] /= ainfo->yscale;
        y->val[t] += ainfo->yshift;
    }

    if (na(kalman_arma_ll(b, K))) {
        err = 1;
    }

    return err;
}

static void free_arma_X_matrix (arma_info *ainfo, gretl_matrix *X)
{
    if (X == ainfo->dX) {
        gretl_matrix_free(ainfo->dX);
        ainfo->dX = NULL;
    } else {
        gretl_matrix_free(X);
    }
}

static int add_smoothed_y (kalman *K, MODEL *pmod,
			   arma_info *ainfo)
{
    gretl_matrix *S;
    int err = 0;

    S = kalman_smooth(K, OPT_NONE, NULL, &err);

#if 0
    fprintf(stderr, "HERE ainfo->r0 = %d\n", ainfo->r0);
    fprintf(stderr, " ainfo->t1 = %d, ainfo->t2 = %d\n", ainfo->t1, ainfo->t2);
#endif

    if (S != NULL) {
	gretl_matrix *m = gretl_matrix_alloc(S->rows, 1);
	int t;

	if (m == NULL) {
	    err = E_ALLOC;
	} else {
	    for (t=0; t<S->rows; t++) {
		m->val[t] = gretl_matrix_get(S, t+1, ainfo->r0);
	    }
	    m->val[S->rows-1] = NADBL;
	    gretl_matrix_set_t1(m, ainfo->t1);
	    gretl_matrix_set_t2(m, ainfo->t2);
	    gretl_model_set_matrix_as_data(pmod, "smstate", m);
	}
	gretl_matrix_free(S);
    }

    return err;
}

static int kalman_arma (const double *coeff,
                        const DATASET *dset,
                        arma_info *ainfo,
                        MODEL *pmod,
                        gretlopt opt)
{
    kalman *K = NULL;
    khelper *kh = NULL;
    gretl_matrix *y = NULL;
    gretl_matrix *X = NULL;
    int r, k = 1 + ainfo->nexo; /* number of exog vars plus space for const */
    double *b;
    int err = 0;

    b = copyvec(coeff, ainfo->nc);
    if (b == NULL) {
        return E_ALLOC;
    }

    y = form_arma_y_vector(ainfo, &err);

    if (!err && ainfo->nexo > 0) {
        if (ainfo->dX != NULL) {
            X = ainfo->dX;
        } else {
            X = form_arma_X_matrix(ainfo, dset, &err);
        }
    }

    if (!err) {
        err = allocate_ac_mc(ainfo);
    }

    if (err) {
        goto bailout;
    }

    r = ainfo_get_state_size(ainfo);

    /* when should we use vech apparatus? */
    if (r > 4) {
        set_arma_use_vech(ainfo);
    }

    kh = kalman_helper_new(ainfo, r, k);
    if (kh == NULL) {
        err = E_ALLOC;
        goto bailout;
    }

    kalman_matrices_init(ainfo, kh, dset->Z[ainfo->yno]);

    K = kalman_new(kh->a, kh->P, kh->T, kh->B, kh->Z, kh->Q,
                   NULL, y, X, NULL, kh->V, &err);

    if (err) {
        fprintf(stderr, "kalman_new(): err = %d\n", err);
    } else {
        int save_lbfgs = libset_get_bool(USE_LBFGS);
        double toler;
        int maxit;

        kalman_attach_printer(K, ainfo->prn);
        kalman_attach_data(K, kh);
	kalman_set_arma_ll(K);

        BFGS_defaults(&maxit, &toler, ARMA);

        if (save_lbfgs) {
            ainfo->pflags |= ARMA_LBFGS;
        } else if (opt & OPT_L) {
            libset_set_bool(USE_LBFGS, 1);
            ainfo->pflags |= ARMA_LBFGS;
        }

        err = BFGS_max(b, ainfo->nc, maxit, toler,
                       &ainfo->fncount, &ainfo->grcount,
                       kalman_arma_ll, C_LOGLIK,
                       NULL, K, NULL, opt | OPT_A,
                       ainfo->prn);

        if (save_lbfgs == 0 && (opt & OPT_L)) {
            libset_set_bool(USE_LBFGS, 0);
        }

        if (err) {
            fprintf(stderr, "kalman_arma: optimizer returned %d\n", err);
        } else if (ainfo->yscale == 1.0 && ainfo->r0 > 0) {
	    /* experimental, could handle non-unit yscale with some work? */
	    add_smoothed_y(K, pmod, ainfo);
	}
    }

    if (!err && ainfo->yscale != 1.0) {
        kalman_undo_y_scaling(ainfo, y, b, K);
    }

    if (!err) {
	gretl_model_set_int(pmod, "fncount", ainfo->fncount);
	gretl_model_set_int(pmod, "grcount", ainfo->grcount);
        err = kalman_arma_finish(pmod, ainfo, dset, K, b,
                                 opt, ainfo->prn);
    }

 bailout:

    if (err) {
        pmod->errcode = err;
    }

    kalman_free(K);
    kalman_helper_free(kh);

    gretl_matrix_free(y);
    free_arma_X_matrix(ainfo, X);
    free(b);

    return err;
}