static void calc_max_lag (arma_info *ainfo) { if (arma_exact_ml(ainfo)) { ainfo->maxlag = ainfo->d + ainfo->D * ainfo->pd; } else { /* conditional ML */ int pmax = ainfo->p + ainfo->P * ainfo->pd; int dmax = ainfo->d + ainfo->D * ainfo->pd; ainfo->maxlag = pmax + dmax; } #if ARMA_DEBUG fprintf(stderr, "calc_max_lag: ainfo->maxlag = %d\n", ainfo->maxlag); #endif }
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; }
int ar_arma_init (double *coeff, const DATASET *dset, arma_info *ainfo, MODEL *pmod) { PRN *prn = ainfo->prn; int *list = ainfo->alist; int nmixed = ainfo->np * ainfo->P; int ptotal = ainfo->np + ainfo->P + nmixed; int av = ptotal + ainfo->nexo + 2; DATASET *aset = NULL; int *arlist = NULL; MODEL armod; int narmax, nonlin = 0; int i, err = 0; #if AINIT_DEBUG fprintf(stderr, "ar_arma_init: dset->t1=%d, dset->t2=%d (dset->n=%d);\n" " ainfo->t1=%d, ainfo->t2=%d, ", dset->t1, dset->t2, dset->n, ainfo->t1, ainfo->t2); fprintf(stderr, "nmixed = %d, ptotal = %d, ifc = %d, nexo = %d\n", nmixed, ptotal, ainfo->ifc, ainfo->nexo); #endif if (ptotal == 0 && ainfo->nexo == 0 && !ainfo->ifc) { /* special case of pure MA model */ for (i=0; i<ainfo->nq + ainfo->Q; i++) { coeff[i] = 0.0001; } pprintf(ainfo->prn, "\n%s: %s\n\n", _("ARMA initialization"), _("small MA values")); return 0; } gretl_model_init(&armod, dset); narmax = arma_exact_ml(ainfo) ? ainfo->nexo : 0; if (narmax > 0 && ptotal > 0) { /* ARMAX-induced lags of exog vars */ av += ainfo->nexo * ptotal; } if (arma_exact_ml(ainfo) && ainfo->ifc) { maybe_set_yscale(ainfo); } aset = create_auxiliary_dataset(av, ainfo->fullT, 0); if (aset == NULL) { return E_ALLOC; } if (ptotal > 0 && (narmax > 0 || nmixed > 0)) { /* we'll have to use NLS */ nonlin = 1; } else { /* OLS: need regression list */ arlist = make_ar_ols_list(ainfo, av); } /* build temporary dataset, dset -> aset */ arma_init_build_dataset(ainfo, ptotal, narmax, list, dset, aset, nonlin); if (nonlin) { PRN *dprn = NULL; #if AINIT_DEBUG fprintf(stderr, "arma:_init_by_ls: doing NLS\n"); dprn = prn; #endif err = arma_get_nls_model(&armod, ainfo, narmax, NULL, aset, dprn); } else { #if AINIT_DEBUG printlist(arlist, "'arlist' in ar_arma_init (OLS)"); #endif armod = lsq(arlist, aset, OLS, OPT_A | OPT_Z); err = armod.errcode; } #if AINIT_DEBUG if (!err) { pputs(prn, "\n*** armod, in ar_arma_init\n"); printmodel(&armod, aset, OPT_NONE, prn); } else { fprintf(stderr, "LS init: armod.errcode = %d\n", err); } #endif if (!err) { arma_init_transcribe_coeffs(ainfo, &armod, coeff); } /* handle the case where we need to translate from an estimate of the regression constant to the unconditional mean of y_t */ if (!err && arma_exact_ml(ainfo) && ainfo->ifc && (!nonlin || ainfo->nexo == 0)) { transform_arma_const(coeff, ainfo); } if (!err && prn != NULL) { if (nonlin) { pprintf(prn, "\n%s: %s\n\n", _("ARMA initialization"), _("using nonlinear AR model")); } else { pprintf(prn, "\n%s: %s\n\n", _("ARMA initialization"), _("using linear AR model")); } } /* clean up */ clear_model(&armod); free(arlist); destroy_dataset(aset); return err; }
static void x12a_maybe_allow_missvals (arma_info *ainfo) { if (arma_exact_ml(ainfo)) { ainfo->pflags |= ARMA_NAOK; } }