macrosynergy.panel.granger_causality_test#

Run Granger Causality Test on a standardized quantamental dataframe. NOTE: This module is under development, and is not yet ready for production use.

granger_causality_test(df, tickers=None, cids=None, xcats=None, max_lag=4, add_constant=False, freq='M', agg='mean', start=None, end=None, metric='value')[source]#

Run Granger Causality Test on a standardized quantamental dataframe. Since the Granger Causality Test is a pairwise test, only two tickers are permitted. Tickers may be specified directly using the tickers parameter, or formed by providing one cross-section (cid) and two extended categories (xcats), or two cross-sections (cids) and one extended category (xcat). Tickers are constructed from the specified cids and xcats in the provided order. The order of the tickers is preserved, with the first ticker being tested to determine whether it Granger-causes the second. The test evaluates whether the time series of the first ticker Granger-causes the time series of the second ticker.

Parameters:
  • df (pd.DataFrame) – A standardized quantamental dataframe.

  • tickers (List[str]) – A list of tickers to run the test on. A maximum of two tickers can be specified.

  • cids (Union[str, List[str]]) – One or two cross-sections to run the test on. If two cross-sections are specified, then only one extended category can be specified. If one cross-section is specified, then two categories must be specified.

  • xcats (Union[str, List[str]]) – One or two extended categories to run the test on. If two categories are specified, then only one cross-sections can be specified. If one category is specified, then two cross-sections must be specified.

  • max_lag (Union[int, List[int]]) – If max_lag is an integer, then the function computes the test for all lags up to max_lag. If max_lag is a list of integers, then the function computes the test only for lags specified in the list.

  • add_constant (bool) – Whether to add a constant to the regression.

  • freq (str) – The frequency to downsample the data to. Must be one of “D”, “W”, “M”, “Q”, “A”. Default is “M”.

  • agg (str) – The aggregation method to use when downsampling the data. Must be one of “mean” (default), “median”, “min”, “max”, “first” or “last”.

  • start (str) – The start date of the data. Must be a valid ISO date. If not specified, the earliest date in df is used.

  • end (str) – The end date of the data. Must be a valid ISO date. If not specified, the latest date in df is used.

  • metric (str) – The metric to run the test on. Must be a column in df. Default is “value”.

Raises:
  • TypeError – If any of the inputs are of the wrong type.

  • ValueError – If any of the input values are invalid.

Returns:

A dictionary containing the results of the Granger Causality Test. The keys are the lags and the values are the results of the test.

Return type:

Dict[Any, Any]