Skip to content

Commit

Permalink
feat: allow the use of different channels weights to the removal effe…
Browse files Browse the repository at this point in the history
…ct results before normalization
  • Loading branch information
Nathan Nunes committed Sep 17, 2024
1 parent 72933d9 commit 2959943
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions marketing_attribution_models/MAM.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)"])
Expand All @@ -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:])
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2959943

Please sign in to comment.