From 2959943bc163f0a185abc0bfc0294729f07178d4 Mon Sep 17 00:00:00 2001 From: Nathan Nunes Date: Tue, 17 Sep 2024 17:43:17 -0300 Subject: [PATCH] feat: allow the use of different channels weights to the removal effect results before normalization --- marketing_attribution_models/MAM.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/marketing_attribution_models/MAM.py b/marketing_attribution_models/MAM.py index 9323671..dfd7abc 100644 --- a/marketing_attribution_models/MAM.py +++ b/marketing_attribution_models/MAM.py @@ -1238,6 +1238,7 @@ def attribution_markov( transition_to_same_state=False, group_by_channels_models=True, conversion_value_as_frequency=True, + channel_weights: dict = None ): """Attribution using Markov.""" model_name = "attribution_markov" @@ -1287,6 +1288,7 @@ def path_to_matrix(paths): matrix[-2, -2] = 1 return matrix + #Adds the string (inicio) and (conversion)/(null) into all journeys temp = self.channels.apply( lambda x: ["(inicio)"] + x ) + self.journey_with_conv.apply(lambda x: ["(conversion)" if x else "(null)"]) @@ -1295,6 +1297,7 @@ def path_to_matrix(paths): dest = [] journey_length = [] + #Create arrays with origin and destination of all touchpoints for every journey. def save_orig_dest(arr): orig.extend(arr[:-1]) dest.extend(arr[1:]) @@ -1338,7 +1341,17 @@ def save_orig_dest(arr): temp["orig"] = temp.orig.apply(channels_names.index) temp["dest"] = temp.dest.apply(channels_names.index) matrix = path_to_matrix(temp[["orig", "dest", "count"]].values) + + #Calculate non-normalized removal effect percentage removal_effect_result = removal_effect(matrix)[1:-2] + + #Apply channel weight into non-normalized removal effect percentage + if channel_weights: + for channel, weight in channel_weights.items(): + #Filter [1:-2] to exclude (inicio), (null) and (conversion), as it was done to create the removal_effect_result + channel_index = channels_names[1:-2].index(channel) + removal_effect_result[channel_index] *= weight + results = removal_effect_result / removal_effect_result.sum(axis=0) # Channels weights