1 /*
2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef ARMA_PRIV_H
21 #define ARMA_PRIV_H
22 
23 typedef enum {
24     ARMA_SEAS   = 1 << 0, /* includes seasonal component */
25     ARMA_DSPEC  = 1 << 1, /* input list includes differences */
26     ARMA_XDIFF  = 1 << 2, /* ARIMA: exogenous regressors are differenced */
27     ARMA_LBFGS  = 1 << 3, /* using L-BFGS-B with native exact ML */
28     ARMA_VECH   = 1 << 4, /* using vech representation when computing
29 			     variance matrix of state for Kalman filter */
30     ARMA_NAOK   = 1 << 5, /* allow missing observations */
31     ARMA_NAS    = 1 << 6, /* sample contains NAs */
32     ARMA_LEV    = 1 << 7, /* doing ARIMA via levels formulation */
33     ARMA_YDIFF  = 1 << 8, /* ainfo->y contains differenced y */
34     ARMA_AVGLL  = 1 << 9, /* passing average likelihood option to Kalman */
35     ARMA_STDX   = 1 << 10 /* exogenous regressors standardized */
36 } PrivFlags;
37 
38 typedef enum {
39     INI_USER = 1,
40     INI_HR,
41     INI_SMALL,
42     INI_NLS,
43     INI_OLS
44 } IniMethod;
45 
46 typedef struct arma_info_ arma_info;
47 
48 struct arma_info_ {
49     int yno;            /* ID of dependent variable */
50     ArmaFlags flags;    /* specification flags */
51     PrivFlags pflags;   /* "private" flags for estimation */
52     int *alist;         /* copy of incoming list */
53     const int *pqspec;  /* auxiliary list with specific AR, MA lags */
54     char *pmask;        /* specific AR lags included */
55     char *qmask;        /* specific MA lags included */
56     double ll;          /* log-likelihood */
57     IniMethod init;     /* initialization method */
58     int ifc;            /* specification includes a constant? */
59     int p;              /* max non-seasonal AR order */
60     int d;              /* non-seasonal difference */
61     int q;              /* max non-seasonal MA order */
62     int P;              /* seasonal AR order */
63     int D;              /* seasonal difference */
64     int Q;              /* seasonal MA order */
65     int np;             /* total non-seasonal AR lags */
66     int nq;             /* total non-seasonal MA lags */
67     int maxlag;         /* longest lag in model */
68     int nexo;           /* number of other regressors (ARMAX) */
69     int nc;             /* total number of coefficients */
70     int t1;             /* starting observation */
71     int t2;             /* ending observation */
72     int pd;             /* periodicity of data */
73     int T;              /* number of valid observations in sample */
74     int fullT;          /* total obs (possibly including interior NAs) */
75     int r0;             /* size of ARMA state vector */
76     int fncount;        /* count of function evaluations */
77     int grcount;        /* count of gradient evaluations */
78     double *y;          /* dependent variable (possibly differenced) */
79     double *e;          /* forecast errors */
80     const double **Z;   /* virtual dataset */
81     double yscale;      /* scale factor for y */
82     double yshift;      /* shift factor for y */
83     int *xlist;         /* list of regressors (ARMAX) */
84     int *misslist;      /* list of missing observations */
85     gretl_matrix *xstats; /* mean and std dev of regressors */
86     gretl_matrix *dX;   /* differenced regressors (ARIMAX) */
87     gretl_matrix *G;    /* score matrix */
88     gretl_matrix *V;    /* covariance matrix */
89     int n_aux;          /* number of auxiliary arrays */
90     double **aux;       /* auxiliary arrays */
91     PRN *prn;           /* verbose printer */
92 };
93 
94 #define arma_by_x12a(a)        ((a)->flags & ARMA_X12A)
95 #define arma_exact_ml(a)       ((a)->flags & ARMA_EXACT)
96 #define arma_least_squares(a)  ((a)->flags & ARMA_LS)
97 
98 #define set_arma_least_squares(a) ((a)->flags |= ARMA_LS)
99 
100 #define arma_has_seasonal(a)   ((a)->pflags & ARMA_SEAS)
101 #define arma_is_arima(a)       ((a)->pflags & ARMA_DSPEC)
102 #define arma_xdiff(a)          ((a)->pflags & ARMA_XDIFF)
103 #define arma_lbfgs(a)          ((a)->pflags & ARMA_LBFGS)
104 #define arma_using_vech(a)     ((a)->pflags & ARMA_VECH)
105 #define arma_na_ok(a)          ((a)->pflags & ARMA_NAOK)
106 #define arma_missvals(a)       ((a)->pflags & ARMA_NAS)
107 #define arima_levels(a)        ((a)->pflags & ARMA_LEV)
108 #define arima_ydiff(a)         ((a)->pflags & ARMA_YDIFF)
109 #define arma_avg_ll(a)         ((a)->pflags & ARMA_AVGLL)
110 #define arma_stdx(a)           ((a)->pflags & ARMA_STDX)
111 
112 #define set_arma_has_seasonal(a)  ((a)->pflags |= ARMA_SEAS)
113 #define set_arma_is_arima(a)      ((a)->pflags |= ARMA_DSPEC)
114 #define unset_arma_is_arima(a)    ((a)->pflags &= ~ARMA_DSPEC)
115 #define set_arma_use_vech(a)      ((a)->pflags |= ARMA_VECH)
116 #define set_arma_na_ok(a)         ((a)->pflags |= ARMA_NAOK)
117 #define set_arma_missvals(a)      ((a)->pflags |= ARMA_NAS)
118 #define set_arima_levels(a)       ((a)->pflags |= ARMA_LEV)
119 #define set_arma_avg_ll(a)        ((a)->pflags |= ARMA_AVGLL)
120 #define set_arima_ydiff(a)        ((a)->pflags |= ARMA_YDIFF)
121 #define unset_arima_ydiff(a)      ((a)->pflags &= ~ARMA_YDIFF)
122 #define set_arma_stdx(a)          ((a)->pflags |= ARMA_STDX)
123 
124 #define AR_included(a,i) (a->pmask == NULL || a->pmask[i] == '1')
125 #define MA_included(a,i) (a->qmask == NULL || a->qmask[i] == '1')
126 
127 int flip_poly (double *theta, arma_info *ainfo,
128 	       int ar, int seasonal);
129 
130 int maybe_correct_MA (arma_info *ainfo,
131 		      double *theta,
132 		      double *Theta);
133 
134 void maybe_set_yscale (arma_info *ainfo);
135 
136 int hr_arma_init (double *coeff, const DATASET *dset,
137 		  arma_info *ainfo);
138 
139 int ar_arma_init (double *coeff, const DATASET *dset,
140 		  arma_info *ainfo, MODEL *pmod,
141 		  gretlopt opt);
142 
143 int arma_by_ls (const double *coeff, const DATASET *dset,
144 		arma_info *ainfo, MODEL *pmod);
145 
146 int bhhh_arma (double *theta, const DATASET *dset,
147 	       arma_info *ainfo, MODEL *pmod,
148 	       gretlopt opt);
149 
150 void transform_arma_const (double *b, arma_info *ainfo);
151 
152 int arma_list_y_position (arma_info *ainfo);
153 
154 int arma_model_add_roots (MODEL *pmod, arma_info *ainfo,
155 			  const double *coeff);
156 
157 void write_arma_model_stats (MODEL *pmod, arma_info *ainfo,
158 			     const DATASET *dset);
159 
160 int arima_difference (arma_info *ainfo, const DATASET *dset,
161 		      int fullX);
162 
163 void arima_difference_undo (arma_info *ainfo, const DATASET *dset);
164 
165 #endif /* ARMA_PRIV_H */
166