This package includes six public functions: arima_sim(), marima_sim() seas_cum(), seas_mcum(), adj_armacoeff(), and root2poly(). The package can be used for several purposes including: (i) pedagogical (in arima modeling topic) (ii) developing (power analysis of [seasonal] unit root tests; performance analysis of different arima estimation algorithms) # Contents 1. Description 1.1 Introduction 1.2 Standard arima notation and six left-most arguments to arima_sim() 1.2.1 Non-seasonal arma 1.2.2 Seasonal arma 1.2.3 The general arima(p,d,q; P,D,Q) 2. Functions descriptions 2.1 arima_sim() 2.2 marima_sim() 2.3 seas_cum() 2.4 seas_mcum() 2.5 adj_armacoeff() 2.6 root2poly() 3 Cheatsheet 3.1 Several trivial situations 3.2 Stationarity and invertibility issues 3.2.1 Adjusting the coefficient matrices 3.2.2 Using roots instead of coefficients of polynomials 4. References 5. News --------------------------------------------------------------------- # 1. DESCRIPTION ## 1.1 Introduction The main package function is arima_sim(), which simulates a trajectory of a general arima(p,d,q; P,D,Q) process as a series. The function seas_cum() fills the gap that while gretl has both diff() and its inverse cum(), it has no counterpart to sdiff(); seas_cum(normal()) is used for quick generation of an arima(0,0,0; 0,1,0) trajectory; cum(seas_cum(normal())) is used for quick generation of an arima(0,1,0; 0,1,0) trajectory: these models are useful in the investigation of seasonal unit root tests. There are also matrix analogues to the package's functions mentioned above: marima_sim() and seas_mcum(). The functions adj_armacoeff() and root2poly() are helpers for arima_sim(): they are used for constructing matrix arguments to arima_sim() to ensure that the model coefficients fall into stationarity and invertibility areas. ## 1.2 Standard arima notation and six left-most arguments to arima_sim() It is instructive to look through the top six lines of the help entry for arima_sim() (subsection 2.1) before further reading. 1.2.1 Non-seasonal arma We start with arma(p, q) ~ arima(p, 0, q; 0, 0, 0). This model could be written as ``` P(L)y_t = mu + Q(L)epsilon_t, (1.1) ``` where epsilon_t is white noise, P(L) and Q(L) are lag polynomials; in the notation of gretl's arima command output we have: ``` P(L) = 1 - phi_1*L - ... - phi_p*L^p; Q(L) = 1 + theta_1*L + ... + theta_q*L^p; ``` Let z belong to C, then ``` P(z) = 1 - phi_1*z - ... - phi_p*z^p (1.2) ``` is the characteristic polynomial of the ar-part of model in (1.1), and ``` Q(z) = 1 + theta_1*z + ... + theta_q*z^p (1.3) ``` is the characteristic polynomial of the ma-part of model in (1.1). The model in (1.1) is stationary (the condition for consistent estimation) if all roots of the polynomial in (1.2) are outside of the unit circle. The model in (1.1) is invertible (the condition for the likelihood function to have a unique maximum) if all roots of the polynomial in (1.3) are outside of the unit circle. Example 1.1 Let’s consider an arma(3,2) model, then in (1.1) we have ``` P(L) = 1 - phi_1*L - phi_2*L^2 – phi_3*L^3 Q(L) = 1 + theta_1*L + theta_2*L^2 ``` To simulate a trajectory of this process we need ``` matrix arco = {phi_1, phi_2, phi_3} matrix maco = {theta_1, theta_2} ``` where phi_i, and theta_j are scalars. Note that nelem(arco) = 3 is exactly the p parameter of arma(p,q) process, and nelem(maco) is exactly its q parameter. To ensure that all roots of P(z) and Q(z) are outside of the unit circle, adj_armacoeff() or root2poly() can be used. Then: `series y = arma_sim(arco, smaco)` will do the job. Use `series y = arma_sim(arco)` to create trajectories of pure ar-processes, and use `series y = arma_sim(null, maco)` to create trajectories of pure ma-processes. A hint: ``` include arima_sim.gfn nulldata 500 set seed 17 # arco = {phi_1, phi_2, phi_3} matrix arco = {1.373773448, -0.721404521, 0.166666667} # maco = {theta_1, theta_2} matrix maco = {0.4, 0.2} series y1 = arima_sim(arco, maco) eval getinfo(y1).description # output is 'arma(3,2) simulated data' # a model to estimate (or arima 3 2; y1): arima 3 2; y1 --nc ``` The output of this estimation is as follows: ``` coefficient std. error z p-value ------------------------------------------------------- phi_1 1.37376 0.180222 7.623 2.49e-14 *** phi_2 −0.718543 0.248556 −2.891 0.0038 *** phi_3 0.199164 0.104851 1.899 0.0575 * theta_1 0.480110 0.178657 2.687 0.0072 *** theta_2 0.191894 0.0997709 1.923 0.0544 * ``` ``` series y2 = arima_sim(arco) eval getinfo(y2).description # output is 'arma(3,0) simulated data' series y3 = arima_sim(null,0.7) eval getinfo(y3).description # output is 'arma(0,1) simulated data' ``` Just copy-paste the chunk of hansl code above into a new script editor window to reproduce. Below we’ll consider Example 1.2 and Example 1.3. Simply add code chunks therein to the window you have created following the instruction just given. 1.2.2 Seasonal arma The seasonal arma(p, q; P, Q) ~ arima(p, 0, q; P, 0, Q) can be written as ``` P(L)P*(L^$pd)y_t = mu + Q(L)Q*(L^$pd)epsilon_t, (1.4) ``` where epsilon_t, P(L), and Q(L) are the same as in (1.1), while (again using arima output notation): ``` P*(L) = 1 - Phi_1*L - ... - Phi_P*L^P; Q*(L) = 1 + Theta_1*L + ... + Theta_Q*L^Q; ``` In the same fashion as in the previous subsection we define ``` P*(z) = 1 - Phi_1*z - ... - Phi_P*z^P Q*(z) = 1 + Theta_1*z + ... + Theta_Q*z^Q ``` for z in C. Again, the roots of the both polynomials should lie outside of the unit circle. As in subsection 1.1.2, ``` matrix arco = {phi_1, … , phi_p} matrix maco = {theta_1, …, theta_q} ``` By the same logic, ``` matrix sarco = {Phi_1, … , Phi_P} matrix smaco = {Theta_1, … , Theta_Q} ``` Example 1.2 In the settings of Example 2.1 we have ``` setobs 4 1894:1 --time-series # seasonal models are sensible only # if $pd > 1 series y4 = arima_sim(0.7,null,null,0.6) eval getinfo(y4).description #output is #'seasonal arma(1,0; 0,1) simulated data' # hence arima 1 0; 0 1; y4 --nc /* coefficient std. error z p-value ------------------------------------------------------- phi_1 0.709988 0.0317620 22.35 1.12e-110 *** Theta_1 0.618002 0.0332861 18.57 6.01e-77 *** */ ``` Note the huge difference in standard errors in comparison with Example 1.1. Despite the same sample size, sample errors (hence, estimated standard errors under the fixed sample size) depends on: 1) how close to the unit circle are the roots of polynomials; 2) different roots combinations in different polynomials are also important. 1.2.3 The general arima(p,d,q; P,D,Q) The general arima(p,d,q; P,D,Q) is generated by the following equation ``` P(L)(1-L)^d P*(L^$pd) (1-L^$pd)^D y_t = = mu + Q(L)Q*(L^$pd)epsilon_t, (1.5) ``` Here d is the integration order and D is the seasonal integration order. Happily, in practice only d = 0, 1, or 2, and D = 0, 1 are considered. With d = 0, and D = 0 model in (1.5) turns into model in (1.4). d in (1.5) is exactly ‘d’ argument to arima_sim(), while D in (1.5) is exactly ‘D’ argument to arima_sim(). Example 1.3 Prolonging Example 1.2, we execute ``` series y5 = arima_sim(0.7,null,null,0.6,1,1) eval getinfo(y5).description # output is #'seasonal arima(1,1,0; 0,1,1) simulated data' # hence arima 1 1 0; 0 1 1; y5 --nc ``` which gives: ``` coefficient std. error z p-value ------------------------------------------------------ phi_1 0.610875 0.0356552 17.13 8.44e-66 *** Theta_1 0.590229 0.0359834 16.40 1.83e-60 *** ``` Note: we haven’t reset the pseudo random number generator, so we have obtained different sample estimates. # 2. LIST OF PUBLIC FUNCTIONS ## 2.1 arima_sim() The function signature is given by: ``` function series arima_sim (matrix arco[null] "ar coefficients", matrix maco[null] "ma coefficients", matrix sarco[null] "seasonal ar coefficients", matrix smaco[null] "seasonal ma coefficients", int d[0] "arima d parameter", int D[0] "arima D parameter", bool adjust[0] "see help file", scalar min_abs[1.01] "min. abs. val for roots", scalar tol[1e-06] "if |Im(z)| Re(z)", bool nice_description[1] "whether to make readable descr.") ``` This simulates a trajectory of a general (seasonal, [seasonally] integrated) arima process as a series. While the function is based on R::stats::arima.sim[2], in contrast to its role model arima_sim() can simulate general seasonal and seasonally integrated arima(p,d,q;P,D,Q) processes. The resulting output is a gretl series. Arguments details arco, maco, sarco, smaco, d, and D: Denote our model as ``` arima(p, d, q; P, D, Q) (1) ``` Then p = nelem(arco), q = nelem(maco), P = nelem(sarco), Q = nelem(smaco) if the relevant arguments exist, else 0. Naming logics: 'arco' if for AutoRegression part COefficients 'maco' is for Moving Average part COefficients 's' is for Seasonal If $pd == 1, then sarco, smaco, and D arguments are ignored. adjust : whether to adjust arco, maco, sarco, and smaco, to guarantee absolute values of polynomials roots to lie outside of the unit circle; if adjust = 1, adj_armacoeff() is called inside. min_abs: minimal absolute value for polynomials roots; ignored if adjust = 0 tol: due to numerical errors polroot() could output something like {1, 10^-7} instead of 1; tol cuts small imaginary parts to zero; ignored if adjust = 0 nice_description: if it is set to 1, a readable descriptive label will be created for the output series; this can be suppressed for the sake of time performance, e.g. on using arima_sim() inside loops. ## 2.2 marima_sim () ``` function matrix marima_sim (int n[0] "length", matrix arco[null] "ar coeff", matrix maco[null] "ma coeff", matrix sarco[null] "seasonal ar coeff", matrix smaco[null] "seasonal ma coeff", int d[0] "arima d parameter", int D[0] "arima D parameter", int pede[0] "frequency", bool adjust[0] "see help file", scalar min_abs[1.01] "min. abs. val for roots", scalar tol[1e-06] "if |Im(z)| Re(z)") ``` The same as arima_sim(), but outputs a column vector Arguments details The function has two additional arguments, compared to arima_sim(): ``` int n[0] "length": length of created trajectory; if no dataset is present n is set to 20; if a data set is in place the default length is set as number of observations in the current sample int pede[0] "frequency": if no dataset is present the default value is set to 1; otherwise it is set as the dataset frequency ($pd); if pede == 1, then sarco, smaco, and D arguments are ignored; ``` Output is a column vector. Example: (AR(1) with phi = 0.8) The following code: ``` set seed 13 matrix a_matrix = arima_sim(3,0.8) a_matrix ``` will output: ``` a_matrix (3 x 1) 0.50738 -0.59220 -0.096111 ``` ## 2.3 seas_cum() ``` function series seas_cum (series y "series to seasonally cumulate", int pede[0] "frequency, 0->$pd") ``` Does the inverse operation (up to NA's) of sdiff(). ## 2.4 seas_mcum() ``` function matrix seas_mcum (matrix *y "vector to seasonally cumulate", int pede[0] "frequency, 0->1|$pd : nodata|data") ``` The same as seas_cum() but for vector input. ## 2.5 adj_armacoeff() ``` function matrix adj_armacoeff (matrix theta, scalar min_abs[1.01] "min. abs. val for roots", scalar tol[1e-06] "if |Im(z)| Re(z)", string type[null] "'ar' or 'ma'") ``` Adjusts a matrix of coefficients so that the corresponding polynomial has roots with absolute values no less than min_abs; tol is for conversion roots with small imaginary parts to real roots; use tol = $huge to get only real roots. string `type`: use null, or “ar” to adjust ‘arco’ and ‘sarco’ arguments to arima_sim() use “ma” to adjust ‘maco’ and ‘smaco’ arguments to arima_sim() See also the Cheatsheet section. ## 2.6 root2poly() ``` function matrix root2poly (matrix roots "must have either 1, or 2 columns", string pol_type[null] "one of null,'ar', or 'ma'", string r_type[null] "one of null, 'T', or 'F'") ``` In a sense, the function is an inverse function to polroots(). By default, root2poly(roots) outputs a column vector such that polroots(root2poly(roots)) = roots up to rounding errors. For example, `eval polroots(root2poly(seq(1,3)'))` would output ``` 1 2 3 ``` The output vector is normed so that the first coefficient is always 1: `root2poly(seq(1,3)')' -> {1.0000, -1.8333, 1.0000, -0.16667}` The output is changed if pol_type = “ar”, or pol_type = “ma” is used, see below. Arguments details matrix roots: must have either 1, or two columns; if a user wants all roots to be real, ‘roots’ must be a column vector. If some roots would be complex, two columns for ‘roots’ should be used. Real roots in this case are inputted as {x, 0}. As is well known, if z = a + i b is a root, than z = a - i b is a root, too. That’s why only one from the pair should be inputted. For example, `root2poly({2,0;1,1})’ -> {1, -1.5, 1,-0.25}`, at the other side, `polroots({1, -1.5, 1,-0.25})` is ``` 2 0 1 -1 1 1 ``` Also, there is a possibility to input complex roots in trigonometric form, see entry for ‘r_type’. string pol_type: determines the type of output matrix; the output behavior under pol_type = null is explained in the previous entry. If pol_type = “ar”, only coefficients for (s)ar part of arima model are outputted, i.e. -full_output[,2:] If pol_type = “ma”, only coefficients for (s)ma part of arima model are outputted, full_output[,2:]. string r_type: determines, how complex roots are interpreted. If r_type = null, a row of ‘roots’ with (a,b) is interpreted as a = Re( z), b = Im(z). If r_type = “T”, a = |z|, b = angle(z). Since only one root from the each pair of conjugate roots should be inputted, angles, i.e. numbers in the second column of 'roots' argument should belong to [0;pi] closed interval. The r_type = “F” , a = |z|, b = frequency(z). Since only one root from the each pair of conjugate roots should be inputted, angles, i.e. numbers in the second column of 'roots' argument should belong to [0;1/2] closed interval. See Cheatsheet section to see how to use root2poly() as a prelude to arima_sim(). # 3. CHEATSHEET ## 3.1 Several trivial situations For the sake of easily reproducing the examples below let ``` nulldata 600 setobs 4 1869:01 --time-series # white noise set verbose off set seed 13 # I have always used 13 simply because I like using 13 series z1 = normal() set seed 13 series z2 = arima_sim() eval max(abs(z1 - z2)) # # should be 0; use normal(), of course # # # random walk set seed 13 series z3 = cum(normal()) set seed 13 series z4 = arima_sim(null,null,null,null,1) eval max(abs(z3 - z4)) # # should be 0; use cum(normal()), of course # # the simplest seasonally integrated series set seed 13 series z5 = seas_cum(normal()) set seed 13 series z6 = arima_sim(null,null,null,null,0,1) eval max(abs(z5 - z6)) # # should be 0; use seas_cum(normal()), of course # # yet another example set seed 13 series z7 = cum(seas_cum(normal())) set seed 13 series z8 = arima_sim(null,null,null,null,1,1) eval max(abs(z7 - z8)) # # should be approx. 0 # ``` ## 3.2 Stationarity and invertibility issues For estimation of general arima(p,d,q; P,D,Q) we first take d differences and D seasonal differences of the original series and then estimate arma(p,q; P,Q) for the transformed data. Simulation does just the opposite direction: arma(p,q; P,Q) trajectory is simulated first, then cumulative and/or seasonal cumulative sums are computed. That’s why it is enough to restrict ourselves to the arma(p,q; P,Q) case. First, note that we can describe coefficients of polynomials in lag operator in (2) in terms of arima_sim() arguments: P(L) coefficients are 1|-arco coordinates, Q(L) coefficients are 1|maco coordinates The same is true for P*(L), Q*(L) and sarco and smaco. This means that all roots of polynomials with coefficients given in 1|*co should lie outside the unit circle. Even for the second order polynomials one should do some work to ensure the desirable behavior of correspondent roots. To this end the package have 2 functions: adj_armacoeff(), and root2poly(). 3.2.1 Adjusting the coefficient matrices Example: ``` ? matrix maco = {1.3,-1}' Replaced matrix maco # check ? eval polroots(1|maco) -0.54269 1.8427 # adjusting the matrix to enforce all roots to have # absolute values no less than 1.2 ? matrix maco = adj_armacoeff({1.3,-1}, 1.2, null, "ma") Replaced matrix maco ? eval maco 0.29065 -0.45224 # check ? eval polroots(1|maco) -1.2000 1.8427 ``` Now maco is ready to be inserted into arma_sim() as ‘maco’, or ‘smaco’ arguments. For (seasonal) autoregressive coefficients use adj_armacoeff({1.3,-1},1.2). 3.2.2 Using roots instead of coefficients of polynomials Sometimes it may be more convenient to think in terms of roots of polynomials instead of their coefficients. The following example illustrates the idea: ``` ? arco = root2poly({1.5,0;2,$pi/4},"ar","T") Replaced matrix arco ? eval arco 1.3738 -0.72140 0.16667 # check ? plr = polroots(1|-arco) Replaced matrix plr ? eval plr 1.50000 + 0.00000i 1.41421 - 1.41421i 1.41421 + 1.41421i ? eval abs(plr) 1.5 2 2 ``` Now arco is ready to be inserted as ‘arco’ or ‘sarco’ argument to arima_sim(). # 4. REFERENCES 1. Cottrell, A., Lucchetti, R. (2018): Gretl user’s guide. 2. R Core Team (2018). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/. (For the "stats" package.) # 5. NEWS - v. 0.31 gretl team: Minor fixes to adapt to stricter gretl behavior; edit this help text a little - v. 0.3 Allin Cottrell: Update Oleh's code to avoid use of deprecated functions - v. 0.2 Three new functions added by splitting up the old arima_sim(): new marima_sim() is for matrices; new arima_sim() is for series root2poly() and adj_armacoeff() are prefixes to arima_sim(); their aim is to guarantee stationarity and invertibility Some minor bugs are corrected (they affected the way to get integrated output series). Some additions to have readable labels for output series. The help file is revised accordingly.