@* . %\RCSID $Id: bj.w,v 1.4 1997/10/10 20:59:32 tjchol01 Exp tjchol01 $ @ Standard C file structure: @s bool int @s ml_func_table int @s ml_messages_table int @s error i @c @@; @@; @@; @ @= ml_func_table ml_functions = { @@; {NULL} }; ml_messages_table ml_messages = { @@; NULL }; @ @= #include /* printf */ #include /* free */ #include /* memset, memcpy */ #include #include #include RCSID ("$Id: bj.w,v 1.4 1997/10/10 20:59:32 tjchol01 Exp tjchol01 $") @ @= @@; @@; @@; @@; @@; @@; @@; @@; @@; @@; @@; @@; @ Retrieve a commonly analyzed data set. @= {GDATA, "GDATA[idata_Integer, iprint:(0|1|2):0]", "{idata, iprint}", 2, "Retrieve a commonly analyzed data set."}, @ @= int GDATA (void) { int idata, iprint; int anobs[] = {16, 176, 150, 144, 13, 197, 296, 100, 113}; int anvar[] = {7, 2, 5, 1, 5, 1, 2, 4, 34}; int nobs; int nvar; real *x; if (!MLGetInteger (stdlink, &idata) ||@| !MLGetInteger (stdlink, &iprint) ||@| !MLNewPacket (stdlink)) return MLerror ("GDATA::mlink"); if (idata < 1 || idata > LEN (anobs)) return MLerror ("GDATA::unknown"); nobs = anobs[idata - 1]; nvar = anvar[idata - 1]; x = (real *) malloc ((nobs * nvar) * sizeof (real)); gdata (&idata, &iprint, &nobs, &nvar, x, &nobs, &nvar); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("GDATA::ddiff"); write_real_array (x, nvar, nobs); free (x); return 1; } @ @= "GDATA::unknown=\"Unknown data set\"",@/ "GDATA::ddiff=\"IMSL error\"",@/ @ Difference a time series. @= {DIFF, "DIFF[z_List, iper_List, iord_List, iprint:(0|1):0]", "{z, iper, iord, iprint}", 4, "Difference a time series."}, @ @= int DIFF (void) { real *z; /* input arrays */ int nz; /* input arrays dimensions */ int *iper, *iord; long niper, niord; int iprint; int nobsz; int ndiff; int imiss; int nlost, nobsx; real *x; if (!read_real_list (&z, &nz) ||@| !MLGetIntegerList (stdlink, &iper, &niper) ||@| !MLGetIntegerList (stdlink, &iord, &niord) ||@| !MLGetInteger (stdlink, &iprint) ||@| !MLNewPacket (stdlink)) return MLerror ("DIFF::mlink"); if (niper != niord) return MLerror ("DIFF::shape"); nobsz = nz; ndiff = niper; imiss = 1; /* to avoid NaNs in \ML{} */ x = (real *) malloc (nobsz * sizeof (real)); diff (&nobsz, z, &ndiff, iper, iord, &iprint, &imiss, &nlost, &nobsx, x); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("DIFF::ddiff"); write_real_list (x, nobsx); free (x); free (z); MLDisownIntegerList (stdlink, iper, niper); MLDisownIntegerList (stdlink, iord, niord); return 1; } @ Compute the sample autocorrelation function of a stationary time series. @= {ACF, "ACF[x_List, iseopt:(0|1|2), imean:(0|1), xmean_?NumberQ, \ maxlag_Integer, iprint:(0|1|2|3):0]", "{x, iseopt, imean, xmean, maxlag, iprint}", 6, "Compute the sample autocorrelation function of a stationary time \ series. Returns {xmean, acv_List, ac_List, seac_List}."}, @ @= int ACF (void) { real *x; int nobs; int iprint, iseopt, imean, maxlag; real xmean; real *acv; real *ac; real *seac; if (!read_real_list (&x, &nobs) ||@| !MLGetInteger (stdlink, &iseopt) ||@| !MLGetInteger (stdlink, &imean) ||@| !MLGetReal (stdlink, &xmean) ||@| !MLGetInteger (stdlink, &maxlag) ||@| !MLGetInteger (stdlink, &iprint) ||@| !MLNewPacket (stdlink)) return MLerror ("ACF::mlink"); if (maxlag < 1 || maxlag >= nobs) return MLerror ("ACF::shape"); if (iseopt == 0 && iprint > 2 ) iprint = 2; acv = (real *) malloc ((maxlag + 1) * sizeof (real)); ac = (real *) malloc ((maxlag + 1) * sizeof (real)); seac = (real *) malloc (maxlag * sizeof (real)); acf (&nobs, x, &iprint, &iseopt, &imean, &xmean, &maxlag, acv, ac, seac); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("ACF::dacf"); MLPutFunction (stdlink, "List", 4); { MLPutReal (stdlink, xmean); write_real_list (acv, maxlag + 1); write_real_list (ac, maxlag + 1); write_real_list (seac, iseopt > 0 ? maxlag : 0); /* empty list */ } free (acv); free (ac); free (seac); free (x); return 1; } @ Compute the sample partial autocorrelation function of a stationary time series. @= {PACF, "PACF[maxlag_Integer, ac_List]", "{maxlag, ac}", 2, "Compute the sample partial autocorrelation function of a \ stationary time series. Returns pac_List."}, @ @= int PACF (void) { real *ac; int nobs, maxlag; real *pac; if (!MLGetInteger (stdlink, &maxlag) ||@| !read_real_list (&ac, &nobs) ||@| !MLNewPacket (stdlink)) return MLerror ("PACF::mlink"); if (maxlag < 1 || maxlag >= nobs) return MLerror ("PACF::shape"); pac = (real *) malloc ((maxlag + 1) * sizeof (real)); pacf (&maxlag, ac, pac); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("PACF::pacf"); write_real_list (pac, maxlag); free (pac); free (ac); return 1; } @ Compute method of moments estimates of the autoregressive parameters of an ARMA model. @= {ARMME, "ARMME[acv_List, npar_Integer, npma_Integer, iprint:(0|1):0]", "{acv, npar, npma, iprint}", 4, "Compute method of moments estimates of the autoregressive parameters \ of an ARMA model. Returns par_List."}, @ @= int ARMME (void) { real *acv; int nacv; int npma, npar; int iprint; int maxlag; real *par; if (!read_real_list (&acv, &nacv) ||@| !MLGetInteger (stdlink, &npar) ||@| !MLGetInteger (stdlink, &npma) ||@| !MLGetInteger (stdlink, &iprint) ||@| !MLNewPacket (stdlink)) return MLerror ("DIFF::mlink"); if (nacv - 1 < npma + npar) return MLerror ("DIFF::shape"); maxlag = nacv - 1; par = (real *) malloc (npar * sizeof (real)); armme (&maxlag, acv, &iprint, &npma, &npar, par); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("DIFF::darmme"); write_real_list (par, npar); free (par); free (acv); return 1; } @ Compute method of moments estimates of the moving average parameters of an ARMA model. @= {MAMME, "MAMME[acv_List, par_List, relerr_?NumberQ, maxit_Integer, npma_Integer, iprint:(0|1):0]", "{acv, par, relerr, maxit, npma, iprint}", 6, "Compute method of moments estimates of the moving average parameters \ of an ARMA model. Returns pma_List."}, @ @= int MAMME (void) { real *acv, *par; int nacv, npar; real relerr; int maxit, npma; int iprint; int maxlag; real *pma; if (!read_real_list (&acv, &nacv) ||@| !read_real_list (&par, &npar) ||@| !MLGetReal (stdlink, &relerr) ||@| !MLGetInteger (stdlink, &maxit) ||@| !MLGetInteger (stdlink, &npma) ||@| !MLGetInteger (stdlink, &iprint) ||@| !MLNewPacket (stdlink)) return MLerror ("DIFF::mlink"); if (nacv - 1 < npma + npar) return MLerror ("DIFF::shape"); maxlag = nacv - 1; pma = (real *) malloc (npma * sizeof (real)); if (npma > 0) { /* return empty list if |npma == 0| */ mamme (&maxlag, acv, &iprint, &npar, par, &relerr, &maxit, &npma, pma); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("DIFF::darmme"); } write_real_list (pma, npma); free (pma); free (acv); free (par); return 1; } @ Compute preliminary estimates of the autoregressive and moving average parameters of an ARMA model. @= {NSPE, "NSPE[w_List, imean:(0|1), wmean_?NumberQ, npar_Integer, npma_Integer, \ relerr_?NumberQ, maxit_Integer, iprint:(0|1):0]", "{w, imean, wmean, npar, npma, relerr, maxit, iprint}", 8, "Compute preliminary estimates of the autoregressive and moving average \ parameters of an ARMA model. Returns {wmean, constant, par_List, pma_List, avar}."}, @ @= int NSPE (void) { real *w; int nw; int imean, npar, npma, maxit, iprint; real wmean, relerr; int nobs; real constant; real *par; real *pma; real avar; if (!read_real_list (&w, &nw) ||@| !MLGetInteger (stdlink, &imean) ||@| !MLGetReal (stdlink, &wmean) ||@| !MLGetInteger (stdlink, &npar) ||@| !MLGetInteger (stdlink, &npma) ||@| !MLGetReal (stdlink, &relerr) ||@| !MLGetInteger (stdlink, &maxit) ||@| !MLGetInteger (stdlink, &iprint) ||@| !MLNewPacket (stdlink)) return MLerror ("DIFF::mlink"); if (nw <= npma + npar + 1) return MLerror ("DIFF::shape"); nobs = nw; par = (real *) malloc (npar * sizeof (real)); pma = (real *) malloc (npma * sizeof (real)); nspe (&nobs, w, &iprint, &imean, &wmean, &npar, &npma, &relerr, &maxit, &constant, par, pma, &avar); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("DIFF::darmme"); MLPutFunction (stdlink, "List", 5); { MLPutReal (stdlink, wmean); MLPutReal (stdlink, constant); write_real_list (par, npar); write_real_list (pma, npma); MLPutReal (stdlink, avar); } free (par); free (pma); free (w); return 1; } @ Compute least-squares estimates of parameters for a nonseasonal ARMA model. @= {NSLSE, "NSLSE[w_List, imean:(0|1), wmean_?NumberQ, par_List, lagar_List, \ pma_List, lagma_List, maxbc_Integer, tolbc_?NumberQ, tolss_?NumberQ, \ iprint:(0|1|2):0]", "{w, imean, wmean, par, lagar, pma, lagma, maxbc, tolbc, tolss, iprint}", 11, "Compute least-squares estimates of parameters for a nonseasonal \ ARMA model. Returns {wmean, constant, par, pma, cov_?MatrixQ, a_List, avar}."}, @ @= int NSLSE (void) { real *w, *par, *pma; int nobs, npar, npma; int *lagar, *lagma; long nlagar, nlagma; int imean, maxbc, iprint; real wmean, tolbc, tolss; int iardeg, imadeg; real constant; real *opar; real *opma; int np; real *cov; int ldcov; int na; real *a; real avar; if (!read_real_list (&w, &nobs) ||@| !MLGetInteger (stdlink, &imean) ||@| !MLGetReal (stdlink, &wmean) ||@| !read_real_list (&par, &npar) ||@| !MLGetIntegerList (stdlink, &lagar, &nlagar) ||@| !read_real_list (&pma, &npma) ||@| !MLGetIntegerList (stdlink, &lagma, &nlagma) ||@| !MLGetInteger (stdlink, &maxbc) ||@| !MLGetReal (stdlink, &tolbc) ||@| !MLGetReal (stdlink, &tolss) ||@| !MLGetInteger (stdlink, &iprint) ||@| !MLNewPacket (stdlink)) return MLerror ("DIFF::mlink"); iardeg = npar; imadeg = npma; /* max(lagar) ignored!!! */ if (npar != nlagar || nobs <= iardeg + imadeg + 1) return MLerror ("DIFF::shape"); opar = (real *) malloc (npar * sizeof (real)); memcpy (opar, par, npar * sizeof (real)); opma = (real *) malloc (npma * sizeof (real)); memcpy (opma, pma, npma * sizeof (real)); np = imean + npar + npma; cov = (real *) malloc ((np * np) * sizeof (real)); ldcov = np; na = nobs - iardeg + maxbc; a = (real *) malloc (na * sizeof (real)); nslse (&nobs, w, &iprint, &imean, &wmean, &npar, opar, lagar, &npma, opma, lagma, &maxbc, &tolbc, &tolss, &constant, cov, &ldcov, &na, a, &avar); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("DIFF::darmme"); MLPutFunction (stdlink, "List", 7); { MLPutReal (stdlink, wmean); MLPutReal (stdlink, constant); write_real_list (opar, npar); write_real_list (opma, npma); write_real_array (cov, np, np); write_real_list (a, na); MLPutReal (stdlink, avar); } free (opar); free (opma); free (cov); free (w); free (par); MLDisownIntegerList (stdlink, lagar, nlagar); free (pma); MLDisownIntegerList (stdlink, lagma, nlagma); return 1; } @ Compute Box-Jenkins forecasts and their associated probability limits for a nonseasonal ARMA model. @= {NSBJF, "NSBJF[w_List, par_List, lagar_List, pma_List, lagma_List, \ iconst:(0|1), constant_?NumberQ, avar_?NumberQ, alpha_?NumberQ, \ mxbkor_Integer, mxlead_Integer, iprint:(0|1):0]", "{w, par, lagar, pma, lagma, iconst, constant, avar, alpha, mxbkor, mxlead, iprint}", 12, "Compute Box-Jenkins forecasts and their associated probability limits \ for a nonseasonal ARMA model. Returns fcst_?MatrixQ."}, @ @= int NSBJF (void) { real *w, *par, *pma; int *lagar, *lagma; int nobs, npar, npma; long nlagar, nlagma; int iconst, mxbkor, mxlead, iprint; real constant, avar, alpha; int iardeg, imadeg; real *fcst; int ldfcst; if (!read_real_list (&w, &nobs) ||@| !read_real_list (&par, &npar) ||@| !MLGetIntegerList (stdlink, &lagar, &nlagar) ||@| !read_real_list (&pma, &npma) ||@| !MLGetIntegerList (stdlink, &lagma, &nlagma) ||@| !MLGetInteger (stdlink, &iconst) ||@| !MLGetReal (stdlink, &constant) ||@| !MLGetReal (stdlink, &avar) ||@| !MLGetReal (stdlink, &alpha) ||@| !MLGetInteger (stdlink, &mxbkor) ||@| !MLGetInteger (stdlink, &mxlead) ||@| !MLGetInteger (stdlink, &iprint) ||@| !MLNewPacket (stdlink)) return MLerror ("DIFF::mlink"); iardeg = npar; imadeg = npma; /* max(lagar) ignored!!! */ if (npar != nlagar || nobs <= iardeg + imadeg + 1) return MLerror ("DIFF::shape"); fcst = (real *) malloc (mxlead * (mxbkor + 3) * sizeof (real)); ldfcst = mxlead; nsbjf (&nobs, w, &iprint, &npar, par, lagar, &npma, pma, lagma, &iconst, &constant, &avar, &alpha, &mxbkor, &mxlead, fcst, &ldfcst); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("DIFF::darmme"); write_real_array (fcst, mxbkor + 3, mxlead); free (fcst); free (w); free (par); MLDisownIntegerList (stdlink, lagar, nlagar); free (pma); MLDisownIntegerList (stdlink, lagma, nlagma); return 1; } @ Perform lack-of-fit test for a univariate time series or transfer function given the appropriate correlation function. @= {LOFCF, "LOFCF[nobs_Integer, lagmin_Integer, lagmax_Integer, cf_List, npfree_Integer]", "{nobs, lagmin, lagmax, cf, npfree}", 5, "Perform lack-of-fit test for a univariate time series or transfer \ function given the appropriate correlation function. Returns {Q, pvalue}."}, @ @= int LOFCF (void) { int nobs, lagmin, lagmax, ncf, npfree; real *cf, Q, pvalue; if (!MLGetInteger (stdlink, &nobs) ||@| !MLGetInteger (stdlink, &lagmin) ||@| !MLGetInteger (stdlink, &lagmax) ||@| !read_real_list (&cf, &ncf) ||@| !MLGetInteger (stdlink, &npfree) ||@| !MLNewPacket (stdlink)) return MLerror ("LOFCF::mlink"); if (lagmax < lagmin || lagmax >= nobs || npfree < 0 || npfree >= lagmax || ncf <= lagmax) return MLerror ("LOFCF::shape"); lofcf (&nobs, &lagmin, &lagmax, cf, &npfree, &Q, &pvalue); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("LOFCF::lofcf"); MLPutFunction (stdlink, "List", 2); { MLPutReal (stdlink, Q); MLPutReal (stdlink, pvalue); } free (cf); return 1; } @ Generate a time series from a specified ARMA model. @= {RNARM, "RNARM[nw_Integer, constant_?NumberQ, par_List, lagar_List, \ pma_List, lagma_List, iadist:(0|1), avar_?NumberQ, a_List, wi_List]", "{nw, constant, par, lagar, pma, lagma, iadist, avar, a, wi}", 10, "Generate a time series from a specified ARMA model. Returns {w_List, a_List}. \ Inputs: \n\ NW - A Number of observations of the time series to generate. \ NW must be greater than or equal to one. \n\ CONST - Overall constant. \n" "PAR - Vector of length NPAR containing the autoregressive parameters. \n" "LAGAR - Vector of length NPAR containing the order of the autoregressive \ parameters. The elements of LAGAR must be greater than or equal to one. \n" "PMA - Vector of length NPMA containing the moving average parameters. \n" "LAGMA - Vector of length NPMA containing the order of the moving average \ parameters. The elements of LAGMA must be greater than or equal to one. \n" "IADIST - Option for normally distributed innovations. \n\ 0: Innovations are generated from a normal distribution (white noise) with \ mean 0 and variance AVAR. \n\ 1: Innovations are specifed by the user. \n" "AVAR - Variance of the normal distribution, if used. For IADIST = 0, \ AVAR is input; and for IADIST = 1, AVAR is unused. \n\ A - Vector of length NW + max(LAGMA(.j.2)) containing the innovations. \ For IADIST = 1, A is input; and for IADIST = 0, A is output. \n\ WI - Vector of length max(.LAGAR.(.i.>)) containing the initial values of \ the time series. \n\ \n\ Outputs: \n\ W - Vector of length NW containing the generated time series. \n\ A - Vector of length NW + max(LAGMA(.j.2)) containing the innovations. \n\ "}, @ @= int RNARM (void) { real *wi, *par, *pma, *a; int *lagar, *lagma; int na, nwi, npar, npma; long nlagar, nlagma; int nw, iadist; real constant, avar; int iardeg, imadeg; real *w; real *oa; if (!MLGetInteger (stdlink, &nw) ||@| !MLGetReal (stdlink, &constant) ||@| !read_real_list (&par, &npar) ||@| !MLGetIntegerList (stdlink, &lagar, &nlagar) ||@| !read_real_list (&pma, &npma) ||@| !MLGetIntegerList (stdlink, &lagma, &nlagma) ||@| !MLGetInteger (stdlink, &iadist) ||@| !MLGetReal (stdlink, &avar) ||@| !read_real_list (&a, &na) ||@| !read_real_list (&wi, &nwi) ||@| !MLNewPacket (stdlink)) return MLerror ("DIFF::mlink"); iardeg = npar; imadeg = npma; /* max(lagar) ignored!!! */ if (nwi != iardeg || (iadist && na != nw + imadeg)) return MLerror ("DIFF::shape"); #if DEBUG MLprintf ("iadist = %d, na = %d", iadist, na); #endif w = (real *) malloc (nw * sizeof (real)); if (!iadist) na = nw + max (imadeg, 2); oa = (real *) malloc (na * sizeof (real)); if (iadist) memcpy (oa, a, na * sizeof (real)); rnarm (&nw, &constant, &npar, par, lagar, &npma, pma, lagma, &iadist, &avar, oa, wi, w); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("DIFF::darmme"); MLPutFunction (stdlink, "List", 2); { write_real_list (w, nw); write_real_list (oa, na); } free (w); free (oa); free (par); MLDisownIntegerList (stdlink, lagar, nlagar); free (pma); MLDisownIntegerList (stdlink, lagma, nlagma); free (a); free (wi); return 1; } @ Initialize a random seed for use in the IMSL random number generators. @= {RNSET, "RNSET[iseed_Integer]", "{iseed}", 1, "Initialize a random seed for use in the IMSL random number generators."}, @ @= int RNSET (void) { int iseed; if (!MLGetInteger (stdlink, &iseed) ||@| !MLNewPacket (stdlink)) return MLerror ("DIFF::mlink"); rnset (&iseed); if (IMSL_ERROR > IMSLS_WARNING) return MLerror ("DIFF::darmme"); MLPutSymbol (stdlink, "Null"); return 1; } @ \begin{rcslog} $Log: bj.w,v $ \Revision 1.4 1997/10/10 20:59:32 tjchol01 Runs on mecca. Zurada exam. \Revision 1.3 1996/09/29 15:45:34 tjchol01 Using |ml.w| interface. Moved to ANSI C. \Revision 1.2 1996/03/12 02:49:13 tjchol01 Using old MathLink. \Revision 1.1 1996/03/12 02:34:36 tjchol01 Initial revision \end{rcslog} @