static int hr_transcribe_coeffs (arma_info *ainfo, MODEL *pmod, double *b) { const double *theta = NULL; const double *Theta = NULL; int j = ainfo->nexo + ainfo->ifc; int i, k = 0; int err = 0; if (ainfo->ifc) { b[0] = pmod->coeff[0]; if (arma_xdiff(ainfo)) { b[0] /= ainfo->T; } k = 1; } for (i=0; i<ainfo->p; i++) { if (AR_included(ainfo, i)) { b[k++] = pmod->coeff[j++]; } } for (i=0; i<ainfo->P; i++) { b[k++] = pmod->coeff[j]; j += ainfo->np + 1; /* assumes ainfo->p < pd */ } theta = pmod->coeff + j; for (i=0; i<ainfo->q; i++) { if (MA_included(ainfo, i)) { b[k++] = pmod->coeff[j++]; } } Theta = pmod->coeff + j; for (i=0; i<ainfo->Q; i++) { b[k++] = pmod->coeff[j]; j += ainfo->nq + 1; /* assumes ainfo->q < pd */ } j = ainfo->ifc; for (i=0; i<ainfo->nexo; i++) { b[k++] = pmod->coeff[j++]; } /* check MA values? */ if (ainfo->q > 0 || ainfo->Q > 0) { err = ma_out_of_bounds(ainfo, theta, Theta); bounds_checker_cleanup(); } return err; }
static void x12_pdq_string (arma_info *ainfo, FILE *fp) { fputc('(', fp); if (ainfo->pmask == NULL) { fprintf(fp, "%d", ainfo->p); } else { int i; fputc('[', fp); for (i=0; i<ainfo->p; i++) { if (AR_included(ainfo, i)) { fprintf(fp, "%d", i+1); if (i < ainfo->p - 1) { fputc(' ', fp); } } } fputc(']', fp); } fprintf(fp, " %d ", ainfo->d); if (ainfo->qmask == NULL) { fprintf(fp, "%d", ainfo->q); } else { int i; fputc('[', fp); for (i=0; i<ainfo->q; i++) { if (MA_included(ainfo, i)) { fprintf(fp, "%d", i+1); if (i < ainfo->q - 1) { fputc(' ', fp); } } } fputc(']', fp); } fputc(')', fp); }
static int real_hr_arma_init (double *coeff, const DATASET *dset, arma_info *ainfo, PRN *prn) { const int *list = ainfo->alist; int np = ainfo->p, nq = ainfo->q; int nP = ainfo->P, nQ = ainfo->Q; int ptotal = np + nP + np * nP; int qtotal = nq + nQ + nq * nQ; int nexo = ainfo->nexo; int pass1lags, pass1v; const double *y; DATASET *aset = NULL; int *pass1list = NULL; int *pass2list = NULL; int *arlags = NULL; int *malags = NULL; MODEL armod; int xstart; int m, pos, s; int i, j, t; int err = 0; pass1lags = (ainfo->Q + ainfo->P) * dset->pd; if (pass1lags < HR_MINLAGS) { pass1lags = HR_MINLAGS; } pass1v = pass1lags + nexo + 2; /* dependent variable */ if (arma_xdiff(ainfo)) { /* for initialization, use the level of y */ y = dset->Z[ainfo->yno]; } else { y = ainfo->y; } aset = create_auxiliary_dataset(pass1v + qtotal, ainfo->T, 0); if (aset == NULL) { return E_ALLOC; } #if AINIT_DEBUG fprintf(stderr, "hr_arma_init: dataset allocated: %d vars, %d obs\n", pass1v + qtotal, ainfo->T); #endif /* in case we bomb before estimating a model */ gretl_model_init(&armod, dset); /* Start building stuff for pass 1 */ pass1list = gretl_list_new(pass1v); if (pass1list == NULL) { err = E_ALLOC; goto bailout; } pass1list[1] = 1; pass1list[2] = 0; for (i=2; i<pass1v; i++) { pass1list[i+1] = i; } /* variable names */ strcpy(aset->varname[1], "y"); for (i=0; i<nexo; i++) { /* exogenous vars */ sprintf(aset->varname[i+1], "x%d", i); } for (i=1; i<=pass1lags; i++) { /* lags */ sprintf(aset->varname[i+1+nexo], "y_%d", i); } /* Fill the dataset with the data for pass 1 */ /* starting position for reading exogeneous vars */ if (ainfo->d > 0 || ainfo->D > 0) { xstart = (arma_has_seasonal(ainfo))? 10 : 6; } else { xstart = (arma_has_seasonal(ainfo))? 8 : 5; } for (t=0; t<ainfo->T; t++) { s = t + ainfo->t1; aset->Z[1][t] = y[s]; for (i=0, pos=2; i<nexo; i++) { m = list[xstart + i]; aset->Z[pos++][t] = dset->Z[m][s]; } for (i=1; i<=pass1lags; i++) { s = t + ainfo->t1 - i; aset->Z[pos++][t] = (s >= 0)? y[s] : NADBL; } } /* pass 1 proper */ armod = lsq(pass1list, aset, OLS, OPT_A); if (armod.errcode) { err = armod.errcode; goto bailout; } #if AINIT_DEBUG fprintf(stderr, "pass1 model: t1=%d, t2=%d, nobs=%d, ncoeff=%d, dfd = %d\n", armod.t1, armod.t2, armod.nobs, armod.ncoeff, armod.dfd); #endif /* allocations for pass 2 */ if (qtotal > 0) { malags = malloc(qtotal * sizeof *malags); if (malags == NULL) { err = E_ALLOC; } else { for (i=0, pos=0; i<nq; i++) { malags[pos++] = i+1; } for (i=0; i<ainfo->Q; i++) { for (j=0; j<=nq; j++) { malags[pos++] = (i+1) * dset->pd + j; } } } } if (ptotal > 0 && !err) { arlags = malloc(ptotal * sizeof *arlags); if (arlags == NULL) { err = E_ALLOC; } else { for (i=0, pos=0; i<np; i++) { arlags[pos++] = i+1; } for (i=0; i<ainfo->P; i++) { for (j=0; j<=np; j++) { arlags[pos++] = (i+1) * dset->pd + j; } } } } if (!err) { pass2list = gretl_list_new(2 + nexo + ptotal + qtotal); if (pass2list == NULL) { err = E_ALLOC; } } /* handle error in pass2 allocations */ if (err) { goto bailout; } /* stick lagged residuals into temp dataset */ pos = pass1v; for (i=0; i<qtotal; i++) { sprintf(aset->varname[pos], "e_%d", malags[i]); for (t=0; t<ainfo->T; t++) { s = t - malags[i]; aset->Z[pos][t] = (s >= 0)? armod.uhat[s] : NADBL; } pos++; } /* compose pass 2 regression list */ for (i=1, pos=1; i<=nexo+2; i++) { pass2list[pos++] = pass1list[i]; } for (i=0; i<ptotal; i++) { /* FIXME? */ if (AR_included(ainfo,i)) { pass2list[pos++] = arlags[i] + nexo + 1; } } for (i=0; i<qtotal; i++) { /* FIXME? */ if (MA_included(ainfo,i)) { pass2list[pos++] = pass1v + i; } } /* now do pass2 */ clear_model(&armod); armod = lsq(pass2list, aset, OLS, OPT_A); if (armod.errcode) { err = armod.errcode; } else { #if AINIT_DEBUG PRN *modprn = gretl_print_new(GRETL_PRINT_STDERR, NULL); printmodel(&armod, aset, OPT_S, modprn); gretl_print_destroy(modprn); #endif err = hr_transcribe_coeffs(ainfo, &armod, coeff); if (!err && arma_exact_ml(ainfo) && ainfo->ifc && ainfo->nexo == 0) { transform_arma_const(coeff, ainfo); } } #if AINIT_DEBUG if (!err) { fprintf(stderr, "HR init:\n"); for (i=0; i<ainfo->nc; i++) { fprintf(stderr, "coeff[%d] = %g\n", i, coeff[i]); } } #endif bailout: free(pass1list); free(pass2list); free(arlags); free(malags); destroy_dataset(aset); clear_model(&armod); if (!err && prn != NULL) { pprintf(prn, "\n%s: %s\n\n", _("ARMA initialization"), _("Hannan-Rissanen method")); } return err; }