david26694 / cluster-experiments

Simulation-based power analysis library
https://david26694.github.io/cluster-experiments/
MIT License
26 stars 3 forks source link

Allow relative perturbator with mixed-sign values #182

Open Gabrielcidral1 opened 2 months ago

Gabrielcidral1 commented 2 months ago

In some cases, you might want to add a relative perturbator for a target that includes negative and positive values, such as profit margin. In this case, with a positive effect of 0.5, and a target of -100, we would the result to be -50.

@david26694 Should we create a new class for it?

Here's a example (suggested by @matheusbuniotto)

class RelativeConstantPerturbator(Perturbator):
    def perturbate(
        self, df: pd.DataFrame, average_effect: Optional[float] = None
    ) -> pd.DataFrame:
        """
        Usage:

        ```python
        from cluster_experiments.perturbator import UniformPerturbator
        import pandas as pd
        df = pd.DataFrame({"target": [1, 2, 3], "treatment": ["A", "B", "A"]})
        perturbator = UniformPerturbator()
        perturbator.perturbate(df, average_effect=1)
    """
    df = df.copy().reset_index(drop=True)
    average_effect = self.get_average_effect(average_effect)
    df = self.apply_multiplicative_effect(df, average_effect)
    return df

def apply_multiplicative_effect(
    self, df: pd.DataFrame, effect: Union[float, np.ndarray]
) -> pd.DataFrame:
    mask = df[self.treatment_col] == self.treatment
    original_values = df.loc[mask, self.target_col]

    # Calculate the new values
    new_values = np.where(
        original_values >= 0,
        original_values * (1 + effect),
        original_values * (1 - effect)
    )

    # Update the dataframe
    df.loc[mask, self.target_col] = new_values

    return df
david26694 commented 2 months ago

Hey gabi, thanks for the issue. Couple questions:

Gabrielcidral1 commented 2 months ago

Our use case is to perturbate effect on profit margin, where it can be either positive or negative. You can expand this other financial metrics, such as profit in monetary values or roi. And yes, your example is what we have in mind :)

david26694 commented 2 months ago

yep, feel free to create a PR!