This BreitungCandelonTest package for gretl performs frequency-wise Granger
(non-) causality tests as described in
Breitung and Candelon (2006): "Testing for short- and long-run causality:
A frequency-domain approach", Journal of Econometrics, 132, pp. 363–378.
From version 2.0 on it also incorporates some extensions from the paper
Breitung, Jörg, and Schreiber, Sven (2018): "Assessing Causality and Delay
within a Frequency Band", in Econometrics & Statistics, vol. 6, April 2018,
pp. 57-73, see https://doi.org/10.1016/j.ecosta.2017.04.005.
Please cite if you use it in research.

Note that for the plain Breitung-Candelon variant the test results have
the nominal significance level only for a fixed frequency point, not for
a frequency band or for an endogenously chosen frequency.

The code started as an adaptation from the original code by Jörg Breitung
written in Gauss (with permission). The license is the same as the one of
gretl itself (GPL). Please report problems on the gretl mailing list.


# GUI or scripting toplevel function "BreitungCandelonTest"

## Parameters:

- *target*:	series that is (potentially) caused by *cause*.
- *cause*:	series that is (potentially) causal; of course, under the null hypothesis it is non-causal.
- *conditioning*: list of other relevant variables. (As in the original Gauss code, Geweke's approach of conditioning is used; see the JoE article for details.)
- *lagorder*: 	number of lags of all variables in the underlying VAR (refers to levels)
- *freqpoints*:	number of frequencies (in the interval [0.01;3.14]) to test. The points will be more or less evenly spaced.
- *siglevel*:	significance level of the test; only determines the critical value that is returned as part of the results for convenience.
- *fromfreq*:	lower bound of the frequency range (only needed for the Breitung-Schreiber variant)
- *tofreq*:	upper bound of the frequency range (ditto)
- *determ*:	choose between 1 -- const only, or 2 -- const & seasonals

## Output:

- a matrix with three columns: 

	1. column holds the tested frequencies,
	2. column the corresponding test statistics, 
	3. column the constant critical value.

- A graph of the result matrix will be produced with gnuplot. The underlying gnuplot plotting command file will
 be stored in gretl's current working directory under the name "BreitungCandelonTest_plotsource.plt".


# Function with bundle-based interface: "FreqGranger"

Choose this for scripting if you want to use a bundle as container, and/or
if you want to suppress the creation of gnuplot files.

Basically you first have to set up a bundle (say, b, perhaps using gretl's
defbundle function) with the mandatory matrix members:

- *mtarg* and
- *mcause*

(each a vector of time series) and further optional members corresponding
to the function arguments above:

- *mcvars*:	data matrix of conditioning variables, default empty
- *lagorder*:	integer, default 3
- *freqpoints*:	integer, default 100
- *siglevel*:	scalar, default 0.05
- *fromfreq* and *tofreq*: scalars, defaults 0.01 and 3.13
- *determ*:		integer, see above for codes, default 1
- *verbosity*:	integer, control the amount of printing, default 1
- *doplot*:		boolean, set to 0 to avoid gnuplot call, default 1
- *pdfpath*:	string, specify file name to get a pdf format plot, default none, can also be "display"
- *pltsource*:	boolean, write gnuplot source even if pdfpath is there, default 0

So the shortest setup call would be something like:

`bundle b = _(mtarg=mymat1, mcause=mymat2)` 

Then call the function passing the bundle in pointer form:

`FreqGranger(&b)`

Afterwards the results would be in the matrix member *b.BCoutput*.


# More low-level function: "BCtest_fromto"

Choose this for scripting if you need to include some exogenous variables
"as is", without any automatic lagging. This function only calculates the
sequence of Breitung-Candelon statistics and doesn't look for the minimum.
The *fromfreq* and *tofreq* arguments only serve to avoid unnecessary
computations (from the point of view of the caller).

## Parameters:

- *mY*:		matrix, 1st col target, 2nd col cause, then covariates
- *lagorder*:	integer
- *fromfreq* and *tofreq*: scalars
- *numoffreqs*:	integer
- *mAsis*:		matrix of time series that will be added without lagging (such as further deterministic terms)

## Output:

- matrix with two columns: 1st frequency, 2nd B-C test stat


# Version history:

- 2.8, September 2026: fix duplicated plotting when invoked in the GUI; switch this help text to markdown; require gretl 2022b for internal syntax updates ("end" keyword)
- 2.7, July 2023: fix potential problem with plotting when Windows user name contains spaces; modernize syntax, require gretl 2021a; enable menu attachment
- 2.6, January 2021: fix some plotting details; require gretl 2018a due to an internal syntax update
- 2.5, May 2018: explicitly require gretl version 2016a or later (using older versions gave an uncaught error); produce an on-screen plot by default now for scripting
- 2.4, January 2018: easy addition of seasonal dummies, and in the low-level functions enable the addition of variables that shouldn't be lagged
- 2.3, February 2017: fixed a bug (probably introduced in the v2.0 rewrite) that used wrong lags of the non-causal polynomial
- 2.2, February 2017: fixed a small bug preventing use of more than one conditioning variable (thanks to Artur for the report)
- 2.1, June 2016: refactoring of the functions (e.g. separate plotting)
- 2.0, June 2016 (no public release): add Breitung-Schreiber test in a range; add tests of frequencies 0 and pi; avoid copying data too much; rewrite the core function to be more gretl-idiomatic
- 1.5, June 2015: replace textplot with nicer gnuplot graphs (and 1.5.1: enforce common axis)
- 1.4, June 2014: update to 'textplot' (and therefore require gretl 1.9.0), handle missing values, print warnings
- 1.3, 23 Nov 2012: fix argument type bug and loop syntax
