static void arma_depvar_stats (MODEL *pmod, arma_info *ainfo, const DATASET *dset) { if (arma_is_arima(ainfo) && !arima_ydiff(ainfo)) { /* calculate differenced y for stats */ int d = ainfo->d, D = ainfo->D; int T = pmod->t2 - pmod->t1 + 1; double *dy = malloc(T * sizeof *dy); int *delta = arima_delta_coeffs(d, D, ainfo->pd); if (dy != NULL && delta != NULL) { int k = d + ainfo->pd * D; real_arima_difference_series(dy, dset->Z[ainfo->yno], pmod->t1, pmod->t2, delta, k); pmod->ybar = gretl_mean(0, T - 1, dy); pmod->sdy = gretl_stddev(0, T - 1, dy); } free(dy); free(delta); } else { pmod->ybar = gretl_mean(pmod->t1, pmod->t2, ainfo->y); pmod->sdy = gretl_stddev(pmod->t1, pmod->t2, ainfo->y); } }
static void maybe_set_yscale (arma_info *ainfo) { double ybar = gretl_mean(ainfo->t1, ainfo->t2, ainfo->y); if (fabs(ybar) > 250) { if (arima_levels(ainfo)) { set_arma_avg_ll(ainfo); /* is this a good idea? */ } else { ainfo->yscale = 10 / ybar; } } }
static int arma_get_nls_model (MODEL *amod, arma_info *ainfo, int narmax, const double *coeff, DATASET *dset, PRN *prn) { gretlopt nlsopt = OPT_A; char fnstr[MAXLINE]; char term[32]; nlspec *spec; double *parms = NULL; char **pnames = NULL; double *b0 = NULL, *by1 = NULL; int nparam, lag; int i, j, k, err = 0; spec = nlspec_new(NLS, dset); if (spec == NULL) { return E_ALLOC; } if (arma_least_squares(ainfo)) { /* respect verbose option */ if (prn != NULL) { nlsopt |= OPT_V; } } else { #if AINIT_DEBUG nlsopt |= OPT_V; #else /* don't bother with standard errors */ nlsopt |= OPT_C; #endif } nlspec_set_t1_t2(spec, 0, ainfo->T - 1); nparam = ainfo->ifc + ainfo->np + ainfo->P + ainfo->nexo; if (ainfo->misslist != NULL) { nparam += ainfo->misslist[0]; } parms = malloc(nparam * sizeof *parms); if (parms == NULL) { err = E_ALLOC; goto bailout; } pnames = strings_array_new_with_length(nparam, VNAMELEN); if (pnames == NULL) { err = E_ALLOC; goto bailout; } /* make names for the parameters; construct the param list; and do some rudimentary fall-back initialization */ for (i=0; i<nparam; i++) { parms[i] = 0.0; } k = 0; if (ainfo->ifc) { if (coeff != NULL) { parms[k] = coeff[k]; } else { parms[k] = gretl_mean(0, dset->n - 1, dset->Z[1]); } b0 = &parms[k]; strcpy(pnames[k++], "_b0"); } for (i=0; i<ainfo->p; i++) { if (AR_included(ainfo, i)) { if (by1 == NULL) { by1 = &parms[k]; if (coeff == NULL) { parms[k] = 0.1; } } if (coeff != NULL) { parms[k] = coeff[k]; } sprintf(pnames[k++], "_phi%d", i+1); } } for (i=0; i<ainfo->P; i++) { if (by1 == NULL) { by1 = &parms[k]; if (coeff == NULL) { parms[k] = 0.1; } } if (coeff != NULL) { parms[k] = coeff[k]; } sprintf(pnames[k++], "_Phi%d", i+1); } for (i=0; i<ainfo->nexo; i++) { if (coeff != NULL) { parms[k] = coeff[k]; } sprintf(pnames[k++], "_b%d", i+1); } if (ainfo->misslist != NULL) { for (i=1; i<=ainfo->misslist[0]; i++) { j = ainfo->misslist[i]; parms[k] = dset->Z[1][j]; sprintf(pnames[k++], "_c%d", i); } } /* construct NLS specification string */ strcpy(fnstr, "y="); if (ainfo->ifc) { strcat(fnstr, "_b0"); } else { strcat(fnstr, "0"); } for (i=0; i<ainfo->p && !err; i++) { if (AR_included(ainfo, i)) { lag = i + 1; sprintf(term, "+_phi%d*", lag); err = add_to_spec(fnstr, term); if (!err) { err = y_Xb_at_lag(fnstr, ainfo, narmax, lag); } } } for (j=0; j<ainfo->P && !err; j++) { sprintf(term, "+_Phi%d*", j+1); strcat(fnstr, term); lag = (j + 1) * ainfo->pd; y_Xb_at_lag(fnstr, ainfo, narmax, lag); for (i=0; i<ainfo->p; i++) { if (AR_included(ainfo, i)) { sprintf(term, "-_phi%d*_Phi%d*", i+1, j+1); err = add_to_spec(fnstr, term); if (!err) { lag = (j+1) * ainfo->pd + (i+1); y_Xb_at_lag(fnstr, ainfo, narmax, lag); } } } } for (i=0; i<ainfo->nexo && !err; i++) { sprintf(term, "+_b%d*x%d", i+1, i+1); err = add_to_spec(fnstr, term); } if (!err && ainfo->misslist != NULL) { for (i=1; i<=ainfo->misslist[0]; i++) { sprintf(term, "+_c%d*d%d", i, i); err = add_to_spec(fnstr, term); } } if (!err) { if (coeff == NULL) { nls_kickstart(amod, dset, b0, by1); } #if AINIT_DEBUG fprintf(stderr, "initting using NLS spec:\n %s\n", fnstr); for (i=0; i<nparam; i++) { fprintf(stderr, "initial NLS b[%d] = %g (%s)\n", i, parms[i], pnames[i]); } #endif err = nlspec_set_regression_function(spec, fnstr, dset); } if (!err) { set_auxiliary_scalars(); err = aux_nlspec_add_param_list(spec, nparam, parms, pnames); if (!err) { *amod = model_from_nlspec(spec, dset, nlsopt, prn); err = amod->errcode; #if AINIT_DEBUG if (!err) { printmodel(amod, dset, OPT_NONE, prn); } #endif } unset_auxiliary_scalars(); } bailout: nlspec_destroy(spec); free(parms); strings_array_free(pnames, nparam); return err; }