macrosynergy.management.utils.math#

Contains mathematical utility functions used across the package.

expanding_mean_with_nan(dfw, absolute=False)[source]#

Calculate the expanding mean of a DataFrame’s values across rows, handling NaN values.

This function computes the expanding (cumulative) mean of all elements in the DataFrame dfw, row-by-row. NaN values are ignored in the summation, ensuring they do not affect the calculation. If absolute is set to True, it uses the absolute values of elements for the expanding mean calculation. The function returns a list of expanding mean values, with each element corresponding to the expanding mean up to that row.

Parameters:
  • dfw (pd.DataFrame) – A DataFrame with a datetime index (or convertible to datetime) and numeric data across its columns. The index is expected to represent timestamps.

  • absolute (bool, optional) – If True, computes the expanding mean using the absolute values of the DataFrame’s elements, by default False.

Returns:

A list containing the expanding mean for each row of the DataFrame.

Return type:

List[np.float64]

Raises:

TypeError – If dfw is not a DataFrame, if its index cannot be converted to timestamps, or if absolute is not a boolean.

ewm_sum(df, halflife)[source]#

Compute the exponentially weighted moving sum of a DataFrame.

Parameters:
  • df (pd.DataFrame) – DataFrame in the wide format for which to calculate weights.

  • halflife (Number) – The halflife of the exponential decay.

calculate_cumulative_weights(df, halflife)[source]#

Calculate the cumulative moving exponential weights for a DataFrame.

Parameters:
  • df (pd.DataFrame) – DataFrame in the wide format for which to calculate weights.

  • halflife (Number) – The halflife of the exponential decay.