macrosynergy.panel.adjust_weights#

Implementation of adjust_weights.

check_missing_cids_xcats(weights, adj_zns, cids, r_xcats, r_cids)[source]#

Checks if there are missing cids or xcats in the input DataFrame.

check_types(weights, adj_zns, method, adj_func, params, cids, start=None, end=None)[source]#

Type checking for the input variables of adjust_weights.

lincomb_backend(df_adj_zns_wide, df_weights_wide, coeff_new, min_score=None)[source]#

Linear combination of the parameters.

Parameters:
  • df_adj_zns_wide (pd.DataFrame) – DataFrame with adjustment factors in wide format.

  • df_weights_wide (pd.DataFrame) – DataFrame with weights in wide format.

  • coeff_new (float) – Coefficient (between 0 and 1) for the new weights. 1 means the result consists entirely of the new weights, 0 means the result consists entirely of the old weights.

  • min_score (float, optional) – Minimum score for the adjustment factors. Default is None, where it is set to the minimum score discovered in the panel of df_adj_zns_wide.

Return type:

DataFrame

generic_weights_backend(df_weights_wide, df_adj_zns_wide, adj_func, params={})[source]#

Backend function for adjust_weights. Applies the method function to the weights and multiplies the result by the adjustment factors, and by the parameter param. Expects the input DataFrames to be in wide format, with the same columns AND index (see macrosynergy.panel.adjust_weights.split_weights_adj_zns).

Parameters:
  • df_weights_wide (pd.DataFrame) – DataFrame with weights in wide format.

  • df_adj_zns_wide (pd.DataFrame) – DataFrame with adjustment factors in wide format.

  • method (Callable) – Function that will be applied to the weights to adjust them.

  • params (Dict[str, Any], optional) – Parameters to be passed to the method function. Default is {}.

Returns:

DataFrame with the adjusted weights.

Return type:

pd.DataFrame

split_weights_adj_zns(df, weights, adj_zns)[source]#

Splits the input DataFrame into two DataFrames, one containing the weights and the other containing the adjustment factors.

Parameters:
  • df (QuantamentalDataFrame) – DataFrame containing the weights and adjustment factors.

  • weights (str) – Name of the xcat to be used as weights.

  • adj_zns (str) – Name of the z-n score xcat to be used as adjustment factors.

Returns:

Tuple containing two wide DataFrames (one for weights and one for adjustment factors), with one column per cid.

Return type:

Tuple[pd.DataFrame, pd.DataFrame]

normalize_weights(out_weights, normalize_to_pct=False)[source]#

Output weights are normalized by dividing each row by the sum of the row. Function exists to allow easy modification of normalization method.

Parameters:
  • out_weights (pd.DataFrame) – DataFrame with weights in wide format. (one column per cid)

  • normalize_to_pct (bool, optional) – If True, the resulting weights will be scaled to 100%. Default is False.

Returns:

DataFrame with normalized weights (sum of each row is 1).

Return type:

pd.DataFrame

adjust_weights(df, weights_xcat, adj_zns_xcat, method='generic', adj_func=None, params={}, cids=None, start=None, end=None, blacklist=None, normalize=True, normalize_to_pct=False, adj_name='ADJWGT')[source]#

Adjusts the weights of a given xcat by a given adjustment xcat using a given method. The resulting weights will be scaled to sum to 100% for each date.

Parameters:
  • df (QuantamentalDataFrame) – QuantamentalDataFrame with weights and adjustment categories for all cross-sections.

  • weights_xcat (str) – Name of the category containing the weights.

  • adj_zns_xcat (str) – Name of the category containing the adjustment factors.

  • method (Callable) – One of the available methods for adjusting weights. Default is “generic”. See notes for available methods.

  • adj_func (Callable, optional) – Function to be used for the adjustment when method is “generic”. This function will be applied to the weights and multiplied by the adjustment factors. Default is None.

  • params (Dict[str, Any], optional) – Parameters to be passed to the method function. Default is {}.

  • cids (List[str], optional) – List of cross-sections to adjust. If None, all cross-sections will be adjusted. Default is None.

  • start (str, optional) – Start date for the adjustment as YYYY-MM-DD. Default is None.

  • end (str, optional) – End date for the adjustment as YYYY-MM-DD. Default is None.

  • blacklist (Dict[str, Any], optional) – Blacklist dictionary passed to the reduce_df function. Default is None. See macrosynergy.management.utils.df_utils.reduce_df() for more details.

  • normalize (bool, optional) – If True, the resulting weights will be normalized to sum to one for each date for the entire list of cross-sections. Default is True.

  • normalize_to_pct (bool, optional) – If True, the resulting weights will be scaled to 100%. Default is False. This only applies if normalize is True.

  • adj_name (str, optional) – Name of the resulting xcat. Default is “ADJWGT”.

Returns:

DataFrame with the adjusted weights.

Return type:

QuantamentalDataFrame

Notes

Available methods: - “generic”: Applies the method function to the weights and multiplies the result by the

adjustment factors. The method function’s signature must match: method(weight: float, **params) -> float.

  • “lincomb”: Linear combination of the parameters. The method function must accept a single

    argument (the weight) and return a single value (the adjusted weight). The parameters min_score (minimum score for the adjustment factors) and coeff_new (coefficient for the new weights) must be provided in the params dictionary. See macrosynergy.panel.adjust_weights.lincomb_backend for more details.

Examples

>>> df = make_test_df(xcats=["weights", "adj_zns"], cids=["cid1", "cid2", "cid3"])
>>>