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 Graner Causality Test is a pairwise test, only two tickers can be specified. Specify either tickers or cids & xcats. When specifying cids & xcats, the user may input one cid and two xcats; or two cids and one xcat to yield two tickers. The function forms the list of tickers from the cids and xcats such that the order of the formed tickers is preserved. The order of the tickers is important as the first ticker is the one that is tested to Granger cause the second ticker. The function tests whether the first ticker Granger causes the second ticker.

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

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

  • cids (Union[str, List[str], None]) – One or two cids to run the test on. If two cids are specified, then only one xcat can be specified. If one cid is specified, then two xcats can be specified.

  • xcats (Union[str, List[str], None]) – One or two xcats to run the test on. If two xcats are specified, then only one cid can be specified. If one xcat is specified, then two cids can 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 (Optional[str]) – The start date of the data. Must be a valid ISO date. If not specified, the earliest date in df is used.

  • end (Optional[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”.

Return <Dict[Any, Any]>:

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

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

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

Return type:

Dict[Any, Any]