Marcin Błażejowski, Pawel Kufel, Tadeusz Kufel 1.08 2010-01-20 CongruentSpecification Jak się napisze to będzie. List of all processes (Y must be the first on the list) List of deterministic components Significance level Print script with GUM? # Main function # Y - Y series # X - list of all potential explanatories (time-series only) # Det_components - list of deterministic variables (saturation i.e.) # alpha - significence level used in all tests # print_res - print script with GUM specification or just return string with OLS (default: Yes) # # returns - string with OLS statement of the GUM # and prints GUM (General Unrestricted Model) # Local variables scalar dfmin # minimal degrees of freedom of GUM scalar GUM_df = 0 # degrees of freedom of GUM scalar d = 0 # order of integration series det_e # residuals after subtraction deterministic components list det_signif = null # list of significant deterministic variables scalar i = 0 # for loops string OLS # string holding OLS statement of the GUM # Minimal degrees of freedom of GUM if $nobs >= 200 dfmin = 20 else dfmin = round(0.1*$nobs) endif # Genereting matrix for results of investigating the internal structure of given processes # col 1: order of autoregression # col 2: order of integration matrix internal_str = zeros(nelem(Processes),2) # Genereting matrix with information about relevance (significance) of deterministic components # row 1: time variable (linear trend) # row 2: seasonal dummies (as a hole block) # value 1 means, that given component is significant matrix det_components = zeros(2,1) ############# # Generating time variable and list of dummy seasonal variables genr time list seasonal = null # Checkig periodicity of series and creating dummy seasonal variables if $pd > 1 genr dummy # Building seasonal variables list if $pd = 4 list seasonal += dq* elif $pd = 12 list seasonal += dm* else list seasonal += dummy_* endif endif ############# # We have to parse given list to find any simple miss specification err = CS_ParseList(Processes, Det_components, seasonal) if strlen(err) != 0 funcerr @err !!! endif ############# # Investigating deterministic internal structure of all variables (Y and X'es) loop foreach V Processes --quiet CS_Check_Internal_Structure(&Processes.$V, seasonal, Det_components, &det_components, &alpha) endloop # Investigating orders of AR structure of all variables (Y and X'es) # First we create list od deterministic components # Do we include time? if det_components[1,1] == 1 list det_signif += time endif # Do we include seasonal dummies? if det_components[2,1] == 1 list det_signif += seasonal endif # We attach block of deterministic variables list det_signif += Det_components i = 0 loop foreach V Processes --quiet i += 1 if nelem(det_signif) > 0 ols Processes.$V const det_signif --quiet det_e = $uhat else det_e = Processes.$V endif d = CS_ADF_check(&det_e, $pd) if d == 1 det_e = diff(det_e) internal_str[i,2] = 1 endif internal_str[i,1] = CS_AR_order(&det_e, CS_PACF_order($nobs), $pd) endloop # We compute degrees of freedom of GUM and reduce AR(p) orders if needed GUM_df = CS_GUM_df_count($nobs, $pd, &internal_str, &det_components, nelem(Det_components)) loop while GUM_df < dfmin CS_GUM_reduce(&internal_str) GUM_df = CS_GUM_df_count($nobs, $pd, &internal_str, &det_components, nelem(Det_components)) endloop # We print results and some additional information if print_res == 1 OLS = CS_Print_CongruentSpecification(Processes, seasonal, Det_components, &internal_str, &det_components, &alpha) return OLS else return CS_Print_CongruentSpecification(Processes, seasonal, Det_components, &internal_str, &det_components, &alpha) endif # Function for setting maximal order of autoregression model (AR(p)) for given variable # pacf_order - order for computing correlogram # *Y - pointer to series for wchich we setting AR(p) order # # returs - order "p" of AR(p) model for given variable Y # Local variables scalar pacf_crit = 2.57583/sqrt($nobs) # critical value for PACF 1.95996 scalar p = 0 # order of AR(p) model for Y matrix acf_pacf = corrgm(Y, pacf_order) loop i=1..pacf_order --quiet if abs(acf_pacf[i,2]) >= pacf_crit p = i endif endloop if freqency > 1 if p > (2*freqency) p = 2*freqency endif endif return p # Function tests if given series has a unit root (ADF with constant only) # by ADF-GLS at significance level 0.25. # *Y - pointer to series for which we check the unit root # lag - maximal lag used in ADF test # # returns - 1, if there is a unit root (process is integrated) and 0 otherwise # Local variables scalar is_integrated = 0 adf lag Y --c --gls --quiet if $pvalue > 0.25 is_integrated = 1 endif return is_integrated # This function determines if time and/or seasonal dummies should be included into congruent model # *Y - pointer to variable for which we check relevance of trend and/or seasonality # l_season - list od seasonal dummy variables # det_variables - list of additional deterministic variables # det_components - pointer to 0-1 matrix, which determines if given deterministic component # should be included (if 1: Yes) # *alpha - pointer to significance level # Local variables scalar i = 0 # for loops scalar tmp # any scalar n_det = nelem(det_variables) # number of det variables scalar n_season = nelem(l_season) # number of seasonal variables scalar k_u # number of coeffs in unrestricted model scalar k_r # number of coeffs in restricted model scalar urss # unrestricted sum of squares scalar rrss # restricted sum of squares # We estimate "full" deterministic model and check relevance of: # - variable "time" by t-statistics # - block of seasonal dummies by F statistics ols Y const time det_variables l_season --quiet urss = $ess k_u = $ncoeff # We check time tmp = 2*pvalue(t, $df, abs($coeff[2]/$stderr[2])) if tmp <= alpha if det_components[1,1] == 0 det_components[1,1] = 1 endif endif # We check if seasonal block is significant if n_season > 0 ols Y const time det_variables --quiet rrss = $ess k_r = $ncoeff tmp = pvalue(F, k_u - k_r, $nobs-k_u-1, (((rrss-urss))/(k_u - k_r)) / (urss/($nobs-k_u-1))) if tmp <= alpha if det_components[2,1] == 0 det_components[2,1] = 1 endif endif endif # Function reduces the GUM model # *internal_str - pointer to matrix holding information about internal structure of variables # Local variables scalar i = 1 # for loops scalar max_ar = maxc(internal_str[,1]) loop for (i=1; i<=rows(internal_str); i++) if internal_str[i,1] == max_ar internal_str[i,1] -= 1 endif endloop # Function computes maximum order for correlogram # N - number of observations # # returs - max_lag based on the same alghoritm that Gretl uses # Local variables scalar max_lag = round(10*log10(N)) if max_lag > round(0.2*N) max_lag = round(0.2*N) endif return max_lag # Function prints script, which should be run on dataset to get congruent model. It also # print some "pre-script" to generate time and/or seasonal dummies (if needed). # big_list - list of all variables [Y X'es] in the same order that were passed to the function # seasonal - list of seasonal dummies # Det_components - list of additional deterministic components # *internal_str - pointer to the matrix holding the internal structure of variables in big_list # *det_components - pointer to the 0-1 matrix holding information # of necessity of including deterministic components # *alpha - pointer to value of alpha (significant level) # GUM_str - string which holds the ols statement of the GUM # returs - GUM_str # Local variables scalar i = 1 # for loops scalar d_det_com = nelem(Det_components) # number of deterministic variables string BUF = "" # string buffer string GUM_str = "" # string with GUM ols statement string hash = "######################################################################" # Some intro... printf "%s\n", hash printf "# You CAN always ask, report a bug or just argue your objections to...\n" printf "# Polish Gretl Team:\n" printf "# Prof. Tadeusz Kufel tadusz.kufel@uni.torun.pl\n" printf "# dr Marcin Błażejowski mblazejowski@wsb.torun.pl\n" printf "# mgr Paweł Kufel qfel@mat.uni.torun.pl\n\n" printf "# All test were run at alpha = %f\n\n", alpha printf "<script>\n" # Do we need to generate time and/or seasonals dummies? if det_components[1,1] == 1 printf "genr time" endif if det_components[2,1] == 1 printf "\ngenr dummy" endif # Main piece - OLS construction BUF = "GUM <" printf "\n%s", BUF BUF = "- " printf "%s", BUF loop foreach V big_list --quiet if i == 1 printf "ols $V const $V(-1 to -%d)\\\n", internal_str[i,1]+internal_str[i,2] sprintf GUM_str, "ols $V const $V(-1 to -%d) ", internal_str[i,1]+internal_str[i,2] # Do we include time? if det_components[1,1] == 1 printf "time\\\n" GUM_str = GUM_str ~ "time " endif # Do we include seasonal dummies? if det_components[2,1] == 1 BUF = "" loop foreach W seasonal --quiet BUF += "$W " endloop printf "%s\\\n", BUF GUM_str = GUM_str ~ BUF endif # We attach additional deterministic variables if d_det_com > 0 BUF = "" loop foreach W Det_components --quiet BUF += "$W " endloop if strlen(BUF) > 0 printf "%s\\\n", BUF GUM_str = GUM_str ~ BUF endif endif else if i == nelem(big_list) printf "$V(0 to -%d)\n", internal_str[i,1]+internal_str[i,2] sprintf BUF, "$V(0 to -%d) ", internal_str[i,1]+internal_str[i,2] else printf "$V(0 to -%d)\\\n", internal_str[i,1]+internal_str[i,2] sprintf BUF, "$V(0 to -%d) ", internal_str[i,1]+internal_str[i,2] endif GUM_str = GUM_str ~ BUF endif i+=1 endloop printf "</script>\n" printf "%s\n", hash return GUM_str # Function parses given list and do some checks # # returns - string with error for funcerr # Local variable string result = null # First we have to check if list of processes has at least 2 elements if nelem(processes) < 2 result = "List of processes MUST have at least 2 elements" return result endif # We have to parse given lists and check if lists Processes and Det_components have not common elements list InterSect = processes && det_comp if InterSect != null result = "List of proceses and list of deterministic components have common elements: " ~ varname(InterSect) return result endif # Now we look for deterministic variables (time and seasonal dummies) # in given lists (we search by name) if nelem(seasonal) != 0 loop foreach el seasonal --quiet string tmp = null tmp = strstr(varname(processes), "$el") if strlen(tmp) != 0 result += "$el" endif string tmp = null tmp = strstr(varname(det_comp), "$el") if strlen(tmp) != 0 result += "$el" endif endloop if strlen(result) != 0 result = "List of independent proceses and/or list of deterministic components inlude(s) seasonal dummies" endif endif string tmp = null tmp = strstr(varname(processes), "time") ~ strstr(varname(det_comp), "time") if strlen(tmp) != 0 if strlen(result) != 0 result += " and time" else result = "List of independent proceses and/or list of deterministic components inlude(s) time" endif endif return result # Function computes degrees of freedom of the GUM # n - number of observations # freqency - freguency of the data # *internal_str - pointer to matrix holding information about internal structure of variables # *det_components - pointer to matrix holding information about necessity of including det_components # Det_components_n - number of additional deterministic variables # # returns - degrees of freedom computed for given specification # Local variables scalar n_var = Det_components_n # number of variables # Number of coeffs based on det_components matrix n_var += sumc(det_components) if det_components[2,1] == 1 n_var = n_var + freqency - 2 # this is because of number of seasonal dummies endif # Number of coeffs based on internal_str n_var = n_var + sumc(internal_str[,1]) + sumc(internal_str[,2]) + rows(internal_str) - 1 # We correct df by number of observations we lost due to AR(p) models return n - n_var - 1 - maxc(internal_str[,1] + internal_str[,2]) # sample function call open data9-7 genr time genr dummy list Dependent = QNC list Independent = PRICE INCOME PRIME UNEMP # And we try... str = CS_CongruentSpecification(Dependent, Independent, null, 0.25, 1) # And we can estimate GUM and look at it GUM <- @str --quiet GUM.show